当前位置:网站首页>How to obtain openid of wechat applet in uni app project
How to obtain openid of wechat applet in uni app project
2022-07-29 05:57:00 【People with different feelings】
1、 problem
When developing wechat apps , We need the current wechat applet users to establish an association with our background users , This is the time , We need to easily obtain the user id provided by wechat through the login ability officially provided by wechat , Quickly build the user system within the applet .
We can get wechat applet users openid As a unique identifier , Bind with background users , The acquisition method is also relatively simple , Here is the record , It is convenient for the follow-up to quickly find and use .
2、uni Provided login() Method
Because it's based on uni-app Project development , So we need to understand uni Provided login() Method , Specific support is as follows :
About uni Of login() Detailed usage of method , You can refer to 《uni-app Official documents 》.
3、 Login of wechat applet
Login process sequence

First , call wx.login() obtain Temporary login credentials code , And back to the developer server .
then , call auth.code2Session Interface , Exchange for User unique identification OpenID 、 The user's unique identity under the wechat open platform account UnionID( If the current applet has been bound to wechat open platform account ) and session key session_key.
Please refer to the details 《 Wechat applet Official documents 》.
4、 Implementation code
4.1、 Back end code implementation
It is mainly used to call auth.code2Session Interface , Exchange for User unique identification OpenID. Because small programs are needed appid and secret Two constant values , These two values , In especial secret Confidential , Not hungry can leak , Therefore, background code is generally required , If you don't think about safety , The front-end code can also be obtained directly openid.
// obtain openId
//Service Definition in class
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(" Access to wechat users openId when , The lack of appid、secret or code Parameters !");
throw new WxBzException(SysErrorCode.SYS_ERROR_700000," Access to wechat users openId when , The lack of appid、secret or code Parameters !");
}
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();
}
/** * obtain openId * @return */
//Controller Definition in class
@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、 Front end code implementation
Front end code is to get code, And then use code Go to the background and call the above method , Exchange for openid that will do . because uni Of login() Method encapsulates wechat wx.login() Method , So what we call directly here is uni.login() Method , return code after , Re pass getOpenId() Method to call the background interface .
Here we put openid There are global variables 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);
}
});
}
边栏推荐
- 以‘智’提‘质|金融影像平台解决方案
- 钉钉告警脚本
- Detailed explanation of tool classes countdownlatch and cyclicbarrier of concurrent programming learning notes
- iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA
- 在uni-app项目中,如何实现微信小程序openid的获取
- 中海油集团,桌面云&网盘存储系统应用案例
- "Shandong University mobile Internet development technology teaching website construction" project training log V
- D3.JS 纵向关系图(加箭头,连接线文字描述)
- 全闪分布式,如何深度性能POC?
- 并发编程学习笔记 之 ReentrantLock实现原理的探究
猜你喜欢

Flutter正在被悄悄放弃?浅析Flutter的未来

Idea using JDBC to connect mysql database personal detailed tutorial

Super simple integration HMS ml kit face detection to achieve cute stickers

My ideal job, the absolute freedom of coder farmers is the most important - the pursuit of entrepreneurship in the future

量化开发必掌握的30个知识点【什么是分笔逐笔数据】?

"Shandong University mobile Internet development technology teaching website construction" project training log V

Gluster cluster management analysis

Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl解决办法

深度学习的趣味app简单优化(适合新手)

Markdown语法
随机推荐
How does PHP generate QR code?
Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl解决办法
Thinkphp6 output QR code image format to solve the conflict with debug
File文件上传的使用(2)--上传到阿里云Oss文件服务器
30 knowledge points that must be mastered in quantitative development [what is individual data]?
与张小姐的春夏秋冬(2)
Breaking through the hardware bottleneck (I): the development of Intel Architecture and bottleneck mining
C# 连接 SharepointOnline WebService
“山东大学移动互联网开发技术教学网站建设”项目实训日志四
超简单集成HMS ML Kit 人脸检测实现可爱贴纸
Process management of day02 operation
How to PR an open source composer project
Performance comparison | FASS iSCSI vs nvme/tcp
Most PHP programmers don't understand how to deploy safe code
Flutter正在被悄悄放弃?浅析Flutter的未来
完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
Laravel service container (inheritance and events)
机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
“山东大学移动互联网开发技术教学网站建设”项目实训日志五
Under the bear market of encrypted assets, platofarm's strategy can still obtain stable income