当前位置:网站首页>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);
}
});
}
边栏推荐
- Elastic box flex
- Laravel service container (Application of context binding)
- Training log 6 of the project "construction of Shandong University mobile Internet development technology teaching website"
- Training log III of "Shandong University mobile Internet development technology teaching website construction" project
- 完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
- yum本地源制作
- C# 判断用户是手机访问还是电脑访问
- Machine learning makes character recognition easier: kotlin+mvvm+ Huawei ml Kit
- Thinkphp6 pipeline mode pipeline use
- Rsync+inotyfy realize real-time synchronization of single data monitoring
猜你喜欢

Training log 4 of the project "construction of Shandong University mobile Internet development technology teaching website"

Markdown syntax

并发编程学习笔记 之 ReentrantLock实现原理的探究

Ribbon学习笔记二

Refresh, swagger UI theme changes

极致通缩和永动机模型,将推动 PlatoFarm 爆发

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

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

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

Research and implementation of flash loan DAPP
随机推荐
July 28 ens/usd Value Forecast: ENS attracts huge profits
并发编程学习笔记 之 Lock锁及其实现类ReentrantLock、ReentrantReadWriteLock和StampedLock的基本用法
win10+opencv3.2+vs2015配置
识变!应变!求变!
Madonna "hellent" bought $1.3 million NFT boring ape, which is now considered too expensive
Training log 6 of the project "construction of Shandong University mobile Internet development technology teaching website"
Some opportunities for young people in rural brand building
初探fastJson的AutoType
“山东大学移动互联网开发技术教学网站建设”项目实训日志四
Laravel service container (Application of context binding)
华为2020校招笔试编程题 看这篇就够了(下)
Use of xtrabackup
centos7 静默安装oracle
SSM integration
极致通缩和永动机模型,将推动 PlatoFarm 爆发
超简单集成HMS ML Kit 人脸检测实现可爱贴纸
Nailing alarm script
30 knowledge points that must be mastered in quantitative development [what is individual data]?
Detailed explanation of atomic operation class atomicinteger in learning notes of concurrent programming
Research and implementation of flash loan DAPP