当前位置:网站首页>在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);
}
});
}
边栏推荐
- Simple optimization of interesting apps for deep learning (suitable for novices)
- The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere
- Xsan is highly available - xdfs and San are integrated with new vitality
- Bare metal cloud FASS high performance elastic block storage solution
- IDEA使用JDBC连接MySQL数据库个人详细教程
- 突破硬件瓶颈(一):Intel体系架构的发展与瓶颈挖掘
- rsync+inotyfy实现数据单项监控实时同步
- The difference between link and @import importing external styles
- 华为2020校招笔试编程题 看这篇就够了(下)
- The bear market is slow, and bit.store provides stable stacking products to help you get through the bull and bear market
猜你喜欢

华为2020校招笔试编程题 看这篇就够了(下)

DAO赛道异军突起,M-DAO的优势在哪里?

Laravel服务容器(继承与事件)

“山东大学移动互联网开发技术教学网站建设”项目实训日志四

XDFS&中国日报社在线协同编辑平台典型案例

The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey

手撕ORM 框架(泛型+注解+反射)

Go|Gin 快速使用Swagger

Novice introduction: download from PHP environment to thinkphp6 framework by hand

Sliding switch of tab of uniapp component
随机推荐
如何零代码制作深度学习的趣味app(适合新手)
Win10+opencv3.2+vs2015 configuration
July 28 ens/usd Value Forecast: ENS attracts huge profits
XDFS&中国日报社在线协同编辑平台典型案例
钉钉告警脚本
My ideal job, the absolute freedom of coder farmers is the most important - the pursuit of entrepreneurship in the future
我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
性能对比|FASS iSCSI vs NVMe/TCP
与张小姐的春夏秋冬(3)
Flink connector Oracle CDC 实时同步数据到MySQL(Oracle12c)
Research and implementation of flash loan DAPP
C# 连接 SharepointOnline WebService
Machine learning makes character recognition easier: kotlin+mvvm+ Huawei ml Kit
iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA
DCAT batch operation popup and parameter transfer
与张小姐的春夏秋冬(5)
Huawei 2020 school recruitment written test programming questions read this article is enough (Part 1)
CMD window under Windows connects to MySQL and operates the table
Bare metal cloud FASS high performance elastic block storage solution
Xsan is highly available - xdfs and San are integrated with new vitality