马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
[JavaScript] 纯文本查看 复制代码
// 登录
wx.login({
success: res => {
console.log(res)
wx.request({
url: this.globalData.class_api_url + '&do=bookapi&op=get_wx_open_info&code='+res.code,
header: {
'content-type': 'application/josn'
},
method: 'GET',
success: function (data) {
data = JSON.parse(data.data)
console.log(data.openid)
that.globalData.openid = data.openid // 将返回的openid 保存到全局的openid
}
})
// 发送 res.code 到后台换取 openId, sessionKey, unionId
},
fail:function(){
console.log('登陆失败'+res.errMsg)
}
})
服务端代码:
[PHP] 纯文本查看 复制代码
#另 appid 、appsecret 需自备
$code = $_GPC['code']; // 接收GET、POST、cookie数据
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appSecret."&js_code=".$code."&grant_type=authorization_code";
$result = file_get_contents($url);// 获取到session_key和openid
exit(json_encode($result, JSON_UNESCAPED_UNICODE));
|