1. AppSecret获取: 登录小程序后台->开发->开发设置->开发者id
2. openid是无感获取的,不需要用户授权啥的,页面加载自动获取
// 微信小程序代码
onLaunch: function () {
var _t=this;
wx.login({
success(res) {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
if (res.code) {
wx.request({
url: '你的后端地址',
data: {
js_code: res.code
},
header: {
'content-type': 'application/json'
},
method:'POST',
success(res) {
console.log(res.data.openid)
}
})
}
}
})
}
//PHP后台代码
$code=$_REQUEST["code"];
$api="https://api.weixin.qq.com/sns/jscode2session?appid=填写自己的appid&secret=填写自己的secret&js_code={$code}&grant_type=authorization_code";
$weixin=file_get_contents($api);
$jsondecode=json_decode($weixin);
$array=get_object_vars($jsondecode);
$openid=$array["openid"];
return $openid;//返回用户openid