場(chǎng)景:實(shí)現(xiàn)微信支付的時(shí)候需要獲取到openid,openid需要在登錄的時(shí)候用登錄返回的code獲取
1.登錄獲取code ,用code獲取openid
uni.login({
success: function (res) {
let appid = 'appid';//后臺(tái)獲取
//小程序secret
let secret = 'secret';//后臺(tái)獲取
//wx接口路徑
let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
uni.request({
url: url,
method: 'GET',
success: result => {
let a: any = result.data
uni.setStorageSync('openid', a.openid)
},
});
}
});
解決方法:報(bào)錯(cuò)code無(wú)效:invalid code:
1.先在微信小程序管理平臺(tái)找到APPID,這里的https://mp.weixin.qq.com/wxamp/devprofile;文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-503162.html
2.檢查項(xiàng)目的project.config.json的APPID,manifest.json的微信小程序配置中的APPID對(duì)比是否一致文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-503162.html


到了這里,關(guān)于uniapp開(kāi)發(fā)微信小程序登錄獲取openid并解決code無(wú)效問(wèn)題:invalid code的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!