赞
踩
场景:实现微信支付的时候需要获取到openid,openid需要在登录的时候用登录返回的code获取
1.登录获取code ,用code获取openid
- uni.login({
- success: function (res) {
- let appid = 'appid';//后台获取
- //小程序secret
- let secret = 'secret';//后台获取
- //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)
- },
- });
- }
- });

解决方法:报错code无效:invalid code:
1.先在微信小程序管理平台找到APPID,这里的https://mp.weixin.qq.com/wxamp/devprofile;
2.检查项目的project.config.json的APPID,manifest.json的微信小程序配置中的APPID对比是否一致
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。