微信小程序多个项目登录时用一个服务器数据库的方法

目前微信小程序在使用wx.login登录时,需要在cAppinfo表中根据appid及secret查询相应的记录。在实际使用过程中,如果多个项目使用同一个服务器地址及数据库,login时只会在cAppinfo表中取第一条记录,而不会根据appid条件选取。可通过以下方法,使多个项目用同一个数据库:

那一个与cAppinfo个相同结构的表cAppinfo_bk,新建小程序项目时,将appid及secret内容插入到表中,cAppinfo_bk表中会有多条记录,在每个小程序项目使用login前,先将cAppinfo记录删除,然后根据appid将cAppinfo_bk表中的内容插入到cAppinfo表中即可。具体代码相关js如下:

setcAuth:function()
{
var appid = config.appid;
var that = this;
wx.request({
url: "https://" + config.service.host + "/func_sdb.php",
data: { event: "setcAuth", appid: appid },
method: 'POST',
header: { 'content-type': 'application/x-www-form-urlencoded' },
success: function (result) {
console.log('submit success')
that.login()
},
fail: function (result) {
console.log('submit fail')
},
complete: function (result) {
console.log('submit complete')
}
})
}

后台php数据库操作内容如下:

        case 'setcAuth':
        $appid=$_POST["appid"];
        require_once('######');
        mysql_query("SET NAMES 'UTF8'");        
        $query = "delete from cAppinfo";    
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());    
        $query = "insert into cAppinfo (appid,secret) select appid,secret from cAppinfo_bk where appid='$appid'";    
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());    
        if ($result)    
        $json_data['rinfo'] = 'success';
        else
        $json_data['rinfo'] = 'error';
        $json = json_encode($json_data);                    
        echo $json;
        break;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值