当前位置:网站首页>在uni-app项目中,如何实现微信小程序openid的获取
在uni-app项目中,如何实现微信小程序openid的获取
2022-07-29 05:20:00 【姠惢荇者】
1、问题
在开发微信小程序的时候,我们需要当前的微信小程序用户和我们的后台用户建立关联,这个时候,我们就需要通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系。
我们可以获取微信小程序用户的openid作为唯一标识,和后台用户进行绑定,获取方式也比较简单,这里做一下记录,方便后续可以快速查找并使用。
2、uni提供的login()方法
因为要基于uni-app项目进行开发,所以我们需要了解uni提供的login()方法,具体支持如下:
关于uni的login()方法的详细用法,可以参考《uni-app 官方文档》。
3、微信小程序的登录
登录流程时序
首先,调用 wx.login() 获取 临时登录凭证code ,并回传到开发者服务器。
然后,调用 auth.code2Session 接口,换取 用户唯一标识 OpenID 、 用户在微信开放平台帐号下的唯一标识UnionID(若当前小程序已绑定到微信开放平台帐号) 和 会话密钥 session_key。
详细内容请参考《微信小程序 官方文档》。
4、实现代码
4.1、后端代码实现
主要实现调用 auth.code2Session 接口,换取 用户唯一标识 OpenID。因为需要用到小程序的appid和secret两个常量值,这两个值,尤其是secret需要保密的,不饿能够泄露,所以一般需要后台代码实现,如果不考虑安全问题,前端代码也可以直接获取openid。
//获取openId
//Service类中定义
private final static String GetOpenId = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&grant_type=authorization_code&js_code={code}";
public String getOpenId(String appid, String secret,String code){
if(StringUtils.isEmpty(appid) || StringUtils.isEmpty(secret) || StringUtils.isEmpty(code)){
logger.error("获取微信用户openId时,缺少appid、secret或code参数!");
throw new WxBzException(SysErrorCode.SYS_ERROR_700000,"获取微信用户openId时,缺少appid、secret或code参数!");
}
Map<String, Object> param = new HashMap<>();
param.put("appid",appid);
param.put("secret",secret);
param.put("code",code);
ResponseEntity resp = restTemplate.getForEntity(GetOpenId, String.class,param);
return resp.getBody().toString();
}
/** * 获取openId * @return */
//Controller类中定义
@RequestMapping("/getOpenId")
public String getOpenId(String code){
String openId = wxCommonService.getOpenId(WxConstants.WX_XCX_APPID,WxConstants.WX_XCX_SECRET,code);
return this.renderSuccessData(openId);
}
4.2、前端代码实现
前端代码就是获取code,然后再使用code去后台调用上述方法,换取openid即可。因为uni的login()方法封装了微信wx.login()方法,所以我们这里直接调用的是uni.login()方法,返回code后,再通过getOpenId()方法调用后台接口即可。
这里我们把openid存在了全局变量globalData
getUserEnter(){
uni.login({
success:(res)=>{
this.$u.api.getOpenId({
code:res.code
}).then( data => {
getApp().globalData.openid = JSON.parse(data.data).openid;
console.log(getApp().globalData.openid);
});
},
fail:(err) => {
console.log(err);
}
});
}
边栏推荐
- Thinkphp6 output QR code image format to solve the conflict with debug
- 农村品牌建设给年轻人的一些机会
- Laravel服务容器(继承与事件)
- Changed crying, and finally solved cannot read properties of undefined (reading 'parsecomponent')
- MOVE PROTOCOL全球健康宣言,将健康运动进行到底
- 『全闪实测』数据库加速解决方案
- Idea using JDBC to connect mysql database personal detailed tutorial
- The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere
- Laravel Swagger添加访问密码
- 北京宝德&TaoCloud共建信创之路
猜你喜欢
MOVE PROTOCOL全球健康宣言,将健康运动进行到底
运动健康深入人心,MOVE PROTOCOL引领品质生活
新手入门:手把手从PHP环境到ThinkPHP6框架下载
“山东大学移动互联网开发技术教学网站建设”项目实训日志四
Intelligent security of the fifth space ⼤ real competition problem ----------- PNG diagram ⽚ converter
Starfish OS: create a new paradigm of the meta universe with reality as the link
Laravel service container (Application of context binding)
Huawei 2020 school recruitment written test programming questions read this article is enough (Part 2)
Training log II of the project "construction of Shandong University mobile Internet development technology teaching website"
My ideal job, the absolute freedom of coder farmers is the most important - the pursuit of entrepreneurship in the future
随机推荐
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
Reporting Services- Web Service
C# 判断用户是手机访问还是电脑访问
马斯克推崇的柏拉图式元宇宙,PlatoFarm早已验证出答案
性能对比|FASS iSCSI vs NVMe/TCP
Study and research the way of programming
The LAAS protocol of defi 2.0 is the key to revitalizing the development of defi track
以‘智’提‘质|金融影像平台解决方案
Win10+opencv3.2+vs2015 configuration
The Platonic metauniverse advocated by musk has long been verified by platofarm
“山东大学移动互联网开发技术教学网站建设”项目实训日志六
XDFS&空天院HPC集群典型案例
Laravel服务容器(上下文绑定的运用)
win10+opencv3.2+vs2015配置
我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
Novice introduction: download from PHP environment to thinkphp6 framework by hand
Training log II of the project "construction of Shandong University mobile Internet development technology teaching website"
Laravel swagger add access password
“山东大学移动互联网开发技术教学网站建设”项目实训日志一
剑指核心-TaoCloud全闪SDS助力构建高性能云服务