当前位置:网站首页>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);
}
});
}
边栏推荐
- IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
- 【数据库】数据库课程设计一一疫苗接种数据库
- Strategic cooperation with many institutions shows the strength of the leading company of platofarm yuancosmos
- Gluster集群管理小分析
- 数组的基础使用--遍历循环数组求出数组最大值,最小值以及最大值下标,最小值下标
- Get the number of daffodils
- Huawei 2020 school recruitment written test programming questions read this article is enough (Part 2)
- Markdown syntax
- Training log III of "Shandong University mobile Internet development technology teaching website construction" project
- 极致通缩和永动机模型,将推动 PlatoFarm 爆发
猜你喜欢

Machine learning makes character recognition easier: kotlin+mvvm+ Huawei ml Kit

xSAN高可用—XDFS与SAN融合焕发新生命力

nacos外置数据库的配置与使用

【数据库】数据库课程设计一一疫苗接种数据库

完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸

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

Fantom (FTM) surged 45% before the FOMC meeting

Markdown语法

iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA

Reporting Service 2016 自定义身份验证
随机推荐
yum本地源制作
ReportingService WebService Form身份验证
与张小姐的春夏秋冬(1)
Power BI Report Server 自定义身份验证
CMD window under Windows connects to MySQL and operates the table
钉钉告警脚本
超简单集成HMS ML Kit 人脸检测实现可爱贴纸
中海油集团,桌面云&网盘存储系统应用案例
主流实时流处理计算框架Flink初体验。
Laravel service container (inheritance and events)
深度学习的趣味app简单优化(适合新手)
与张小姐的春夏秋冬(5)
Process management of day02 operation
Training log 4 of the project "construction of Shandong University mobile Internet development technology teaching website"
Win10+opencv3.2+vs2015 configuration
闪贷Dapp的调研及实现
超简单集成HMS ML Kit 实现parental control
Under the bear market of encrypted assets, platofarm's strategy can still obtain stable income
Lock lock of concurrent programming learning notes and its implementation basic usage of reentrantlock, reentrantreadwritelock and stampedlock
Detailed explanation of atomic operation class atomicinteger in learning notes of concurrent programming