当前位置:网站首页>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);
}
});
}
边栏推荐
- Madonna "hellent" bought $1.3 million NFT boring ape, which is now considered too expensive
- “山东大学移动互联网开发技术教学网站建设”项目实训日志七
- H5 semantic label
- Use of file upload (2) -- upload to Alibaba cloud OSS file server
- 并发编程学习笔记 之 工具类CountDownLatch、CyclicBarrier详解
- 赓续新征程,共驭智存储
- Performance comparison | FASS iSCSI vs nvme/tcp
- 剑指核心-TaoCloud全闪SDS助力构建高性能云服务
- 中海油集团,桌面云&网盘存储系统应用案例
- Dao race track is booming. What are the advantages of m-dao?
猜你喜欢

C# 连接 SharepointOnline WebService

Android Studio 实现登录注册-源代码 (连接MySql数据库)

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

Reporting Service 2016 自定义身份验证

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

From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run

主流实时流处理计算框架Flink初体验。

mysql在查询字符串类型的时候带单引号和不带的区别和原因

Flink connector Oracle CDC 实时同步数据到MySQL(Oracle19c)

北京宝德&TaoCloud共建信创之路
随机推荐
Training log III of "Shandong University mobile Internet development technology teaching website construction" project
数组的基础使用--遍历循环数组求出数组最大值,最小值以及最大值下标,最小值下标
Under the bear market of encrypted assets, platofarm's strategy can still obtain stable income
Power BI Report Server 自定义身份验证
xSAN高可用—XDFS与SAN融合焕发新生命力
在uni-app项目中,如何实现微信小程序openid的获取
与张小姐的春夏秋冬(4)
C# 连接 SharepointOnline WebService
SSM integration
Starfish OS: create a new paradigm of the meta universe with reality as the link
Ribbon学习笔记二
Performance comparison | FASS iSCSI vs nvme/tcp
手撕ORM 框架(泛型+注解+反射)
Training log 4 of the project "construction of Shandong University mobile Internet development technology teaching website"
mysql插入百万数据(使用函数和存储过程)
day02 作业之文件权限
Super simple integration HMS ml kit face detection to achieve cute stickers
Detailed steps of JDBC connection to database
Thinkphp6 pipeline mode pipeline use
Markdown语法