目前微信小程序在使用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;