12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
建站问答 时间:2024-12-01 19:17:19
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
小程序获取openid的案例:第一步,获取code,在app.js文件添加以下代码://app.jsApp({onLaunch:function(){wx.login({success:function(res){if(res.code){//发起网络请求wx.requ
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
小程序获取openid的案例:
第一步,获取code,在app.js文件添加以下代码:
//app.js
App({
onLaunch:function(){
wx.login({
success:function(res){
if(res.code){
//发起网络请求
wx.request({
url:'https://test.com/onLogin',
data:{
code:res.code
}
})
}else{
console.log('获取用户登录态失败!'+res.errMsg)
}
}
});
}
})
第二步,在登录页面根据code获取openid,代码:
//根据code获取openid等信息
wx.login({
//获取code
success:function(res){
varcode=res.code;//返回code
console.log(code);
varappId='...';
varsecret='...';
wx.request({
url:'https://api.weixin.qq.com/sns/jscode2session?appid='+appId+'&secret='+secret+'&js_code='+code+'&grant_type=authorization_code',
data:{},
header:{
'content-type':'json'
},
success:function(res){
varopenid=res.data.openid//返回openid
console.log('openid为'+openid);
}
})
}
})
//正常返回的JSON数据包
{
"openid":"OPENID",
"session_key":"SESSIONKEY",
"unionid":"UNIONID"
}
//错误时返回JSON数据包(示例为Code无效)
{
"errcode":40029,
"errmsg":"invalidcode"
}
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19