js文件内容:
onLoad: function () {
var that=this
wx.login({
//获取code
success: function (res) {
console.log(res.code)
that.data.code = res.code //返回code
if (res.code) {
wx.request({
url: 'https://www.xxxxx.com/shop/index.php/Home/Goods/wx/js_code/' + res.code,
header: {
'content-type': 'application/json'
},
data: {},
success: function (res) {
console.log(res.data.openid)
}
})
}else{
console.log('登录失败!' + res.errMsg)
}
}
})
}
Thinkphp3.2 后台控制器GoodsController.class.php:
public function wx(){
//配置appid
$appid = "wxba9*********";
//配置appscret
$secret = "59b7****************33c8";
$js_code=I('js_code');
$api = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$js_code."&grant_type=authorization_code";
$a = json_decode(file_get_contents($api),true);
$this->ajaxReturn($a);exit;
}
搞定~