【度客Creator】cfc使用mysql数据基础操作
易伟 发布于2018-10-28 21:25 浏览:1219 回复:3
0
收藏

收藏
点赞
0
个赞
共3条回复 最后由hehung回复于2018-11-01 13:23
#2易伟回复于2018-10-28
 this.addIntentHandler('sign', () => {
            return new Promise(function (resolve, reject) {

           var mysql = require('mysql');
var connection = mysql.createConnection({
    host: '**',
    user: '**',
    password: '**',
    database: '**'
});

connection.connect();    
                var sql = 'SELECT * FROM number';
                connection.query(sql, function (err, result) {
                    console.log(result);
                    resolve({

                        outputSpeech: '返回结果' + result[0].num
                    });
                });
            
            });
        });
0
#3易伟回复于2018-10-31

新老用户的数据库添加是养成类技能、支付等技能的基础,用户的区别可以根据request.getuserId()来实现,注意var that。数据表中建立openid字段。

启动技能时以id进行查询,数据为空,则是新用户,进行插入操作,建立档案;不为空,则为老用户。

this.addLaunchHandler(() => {
            var that = this
            this.waitAnswer();
            return new Promise(function (resolve, reject) {

                var mysql = require('mysql');
                var connection = mysql.createConnection({
                    host: '*',
                    user: '*',
                    password: '*',
                    database: '*'
                });

                connection.connect();
                let userId = that.request.getUserId();
                
                var sql = 'SELECT * FROM number WHERE openid ="' + userId + '"';

                connection.query(sql, function (err, result) {
                    console.log(result);
                    if (result == "") {
                        resolve({

                            outputSpeech: '新用户' + userId
                        });
                        var sql2 = 'INSERT INTO number (openid) VALUES("' + userId + '")';
                        connection.query(sql2, function (err, result) {
                            console.log(result)
                        });
                    }
                    else {
                        resolve({

                            outputSpeech: '老用户' + userId
                        });
                    }

                });

            });     
         
            
            });
           
0
#4hehung回复于2018-11-01

很棒

0
TOP