当前位置:网站首页>关于微信小程序体验版获取不到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
边栏推荐
猜你喜欢

NLP和CV中的Local和Global建模

一条SQL在MySQL中是如何执行的

Combining "xPlus" to discuss the innovation and change of software architecture

分分钟快速定制您的专属个性化软件应用——BizTool自动化工具简介

消防安全知识培训讲座

300万招标!青岛市医疗保障局主机数据库中间件运行维护服务项目

刷题《剑指Offer》day12

探究!一个数据包在网络中的心路历程

移动适配vw/vh方法—vw/vh实例—模拟B站手机端首页—获取样式教程视频

论文阅读《Omnidirectional DSO: Direct Sparse Odometry with Fisheye Cameras》
随机推荐
新款“廉价”SUV曝光,安全、舒适一个不落
8/7 牛客6+div2D+倍增lca
【kali-权限提升】(4.2.4)社会工程学工具包:远控木马使用、设置、利用
gaussdb数据库连接报错:psycopg2.OperationalError: server closed the connection unexpectedly
ets声明式ui开发,怎么获取当前系统时间
neural network classification
ets declarative ui development, how to get the current system time
Combining "xPlus" to discuss the innovation and change of software architecture
Kunpeng Developer Creation Day 2022: Kunpeng Full-Stack Innovation and Developers Build Digital Hunan
《STM32MP1 M4裸机CubeIDE开发指南》第二十四章 DAC实验
深度强化学习发展史
内网渗透学习(五)域横向移动——PTH&PTK&PTT
目标检测中的Classificition Loss
自学脚手架——《热学》 by 李椿(第一,二,三,四,五章)
模式识别 学习笔记:第八章 特征提取
Replication监控及自动故障切换
One article to understand configuration management (CM)
300万招标!青岛市医疗保障局主机数据库中间件运行维护服务项目
Supervisor 后台进程管理
消防安全知识培训讲座