当前位置:网站首页>关于微信小程序体验版获取不到openId的问题
关于微信小程序体验版获取不到openId的问题
2022-08-08 11:24:00 【用户9127725】
我们知道openid是微信用户验证的重要标识,支付功能严重依赖这个东西,之前我们做微信支付的时候是通过在微信客户端直接调用官方接口,通过传code参数来调用,下面这样
getOpenId(){ //获取用户的openid
let _this=this;
wx.login({
success(res) {
if (res.code) {
// 发起网络请求
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid:appId, //开发者appid
secret:secret, //开发者AppSecret(小程序密钥)
grant_type:"authorization_code", //默认authorization_code
js_code: res.code //wx.login登录获取的code值
},
success(res) {
_this.userinfo.openid=res.data.openid;
_this.userinfo.session_key=res.data.session_key;
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
}
这样做理论上也可以拿到openid,但是这里有一个神坑,就是在小程序开发者工具直接测试,使用真机调试,都没有任何问题,但是一旦上传代码,使用小程序的体验版测试的话,就拿不到openid,奇怪的是,如果在体验版开启调试模式,又可以拿到,这是一个非常脑残的问题,直接影响就是开发环境和生产环境在代码相同的情况下,体现的效果不一样,微信官方也没有任何合理解释,经过很长时间的查找资料,获取openid不能直接在微信客户端来获取,应该改用后端来获取openid然后再返回给前端。
所以应该用Django来获取openid
def getopenid(request):
res = {}
appId = request.GET.get('appId')#开发者appid
secret = request.GET.get('secret')#开发者AppSecret(小程序密钥)
grant_type = "authorization_code" #默认authorization_code
js_code = request.GET.get('js_code')#wx.login登录获取的code值
data = {'appId':appId,'secret':secret,"grant_type":grant_type,"js_code":js_code}
url = "https://api.weixin.qq.com/sns/jscode2session"
jscode = requests.get(url,data)
res = jscode.json()
return JsonResponse(res,safe=False,json_dumps_params={'ensure_ascii':False})
而前端获取openid的方法改造成请求本地接口
getOpenId(){ //获取用户的openid
let _this=this;
wx.login({
success(res) {
if (res.code) {
// 发起网络请求,改造成请求本地接口获取openid,规避体验版获取不到的问题
wx.request({
url: 'http://localhost:8000/getopenid',
data: {
appid:appId, //开发者appid
secret:secret, //开发者AppSecret(小程序密钥)
grant_type:"authorization_code", //默认authorization_code
js_code: res.code //wx.login登录获取的code值
},
success(res) {
_this.userinfo.openid=res.data.openid;
_this.userinfo.session_key=res.data.session_key;
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
}
至此才解决了这个问题,这件事情说明一个问题,就是做任何事情都不能想当然,同时开发小程序的时候,真机测试没有问题并不代表没有问题,一定要到生产环境测一下,另外最后一个忠告,微信小程序获取openid一定要在server端获取再返回给前端,千万不要图省事在前端直接获取openid
边栏推荐
猜你喜欢
【力扣】两数相加
关于振弦采集模块及采集仪振弦频率值准确率的问题
目标检测中的Bounding Box Regression Loss
分分钟快速定制您的专属个性化软件应用——BizTool自动化工具简介
Classificition Loss in target detection
Kubernetes资源编排系列之四: CRD+Operator篇
C语言详解系列——指针与结构体
leetcode:761. 特殊的二进制序列【递归 + 转换有效括号】
Combining "xPlus" to discuss the innovation and change of software architecture
四、哈希表
随机推荐
动图图解!既然IP层会分片,为什么TCP层也还要分段?
NLP和CV中的Local和Global建模
刷题《剑指Offer》day12
自学脚手架——《热学》 by 李椿(第一,二,三,四,五章)
微服务负载均衡器LoadBalancer实战
Combining "xPlus" to discuss the innovation and change of software architecture
卫星互联网真能替代 5G?
300万招标!青岛市医疗保障局主机数据库中间件运行维护服务项目
8/7 牛客6+div2D+倍增lca
一起学习集合框架之 TreeSet
Pattern Recognition Study Notes: Chapter 6 Other Classification Methods (Continuously updated...)
Replication监控及自动故障切换
2G 3G 4G 5G 基站覆盖范围
win10安装Solidworks2016安装出错:solidworks\sldfuncfeat.dll“ 已返回 0x3,如何解决.
移动适配vw/vh方法—vw/vh实例—模拟B站手机端首页—获取样式教程视频
基于ftp协议的上传与下载
关于win下面Celery服务报 Process 'Worker' exited with 'exitcode 1' [duplicate]
图数据库一般用于什么时候呢?
ets declarative ui development, how to get the current system time
LeetCode 219. Repeating Elements II (2022.08.07)