当前位置:网站首页>Wechat applet authorized login (including obtaining basic information and binding mobile number)
Wechat applet authorized login (including obtaining basic information and binding mobile number)
2022-07-25 03:36:00 【Snow falling Xiaoxuan Han】
1、 Authorize to obtain wechat personal information
wx.getUserProfile({
desc: ' Display user information ',
success: (res) => {
this.setData({
nickName: res.userInfo.nickName,
avatarUrl: res.userInfo.avatarUrl
})
},
fail: res => {
wx.showToast({
title: ' You have refused to authorize , Please click again and authorize ',
icon:'none'
})
}
})
2、 call wx.login() Get temporary login credentials code
wx.login({
success: res => {
this.setData({
jsCode: res.code
})
}
})
3、 use code Call the interface in exchange for the unique identification of the user openId、 The unique identification of the user under the wechat open platform account unionId And session key session_key
(1) Front end access
wx.request({
// Wechat developer tool check - Do not verify legal domain name
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
data:{
},
header:{
'content-type':'json'
},
success:function(res) {
console.log(res);
}
})
Be careful : This method will not be used in normal development , It will call wechat public platform interface through background acquisition openid, In order to protect some sensitive information, such as appid,secret The key is not compromised . The front end can be used in testing to achieve results .
(2) Call the background interface to get
getOpenId({
jsCode:this.data.jsCode}).then(res => {
if (res.data.code === 1) {
this.setData({
openid: res.data.data.openid,
sessionKey: res.data.data.sessionKey,
unionid: res.data.data.unionid
})
}
})
4、 Authorization to obtain mobile phone number
(1) Use button The button triggers the authorization pop-up
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"> Get your phone number </button>
getPhoneNumber(e){
console.log(e.detail.iv);// Initial vector of encryption algorithm
console.log(e.detail.encryptedData);// Encrypted data of complete user information
console.log(e.detail.code);// Get voucher by mobile phone number
}
(2) Through parameters encryptedData,iv,sessionKey Or is it code,sessionKey Request backend interface , The back end decrypts and returns the user's mobile phone number Be careful : If you pass code To get , When testing with wechat developer tools , May not show code, This is the reason for debugging the basic library , There will be no problem in real machine debugging .
5、 Bind wechat
Just authorize and use the acquired openid,unionid,sessionKey Call back-end interface
wx.getUserProfile({
desc: ' Display user information ',
success: (res) => {
let data = {
}
data.openid = this.data.openid
data.unionid = this.data.unionid
data.sessionKey = this.data.sessionKey
miniBind(data).then(res => {
if (res.data.code === 1) {
wx.showToast({
title: ' Authorized success !'
})
} else {
wx.showToast({
icon: "none",
title: res.data.msg,
})
}
})
}
})
边栏推荐
- Network construction and application in 2020 -- the answer of samba in Guosai
- Secondary vocational network security skills competition P100 web penetration test
- One question per day
- MIM command
- Openlayers ol ext: Transform object, rotate, stretch, zoom in
- Moveit2 - 8. Motion planning API
- How chemical enterprises choose digital service providers with dual prevention mechanism
- What is technical support| Daily anecdotes
- Query the information of students whose grades are above 80
- [brother hero July training] day 19: binary tree
猜你喜欢

基于SSM实现后勤报修系统

Chrome process architecture

Use reflection to convert RDD to dataframe

基于SSH婴幼儿产品销售系统

Fifth day of force deduction

How does Jupiter notebook change themes and font sizes?

What should testers do if they encounter a bug that is difficult to reproduce?

Introduction to Apache Doris grafana monitoring indicators

mysql_ Master slave synchronization_ Show slave status details

05 - MVVM model
随机推荐
One question per day
Sword finger offer II 041. Average value of sliding window_____ Using queue / loop array implementation
Flowlayout in compose
Take a note: Oracle conditional statement
Force deduction problem 238. product of arrays other than itself
Introduction to Apache Doris grafana monitoring indicators
It took me 2 years from Foxconn assembly line to Tencent software testing post~
Page performance: how to optimize pages systematically?
What should testers do if they encounter a bug that is difficult to reproduce?
A code takes you to draw multi format sangjimei pictures such as interactive +pdf+png
Implementation principle of virtual DOM
[Flink] protocol operator reduce
Use reflection to convert RDD to dataframe
Swagger key configuration items
Experience sharing of system architecture designers in preparing for the exam: how to prepare for the exam effectively
C language_ Structure introduction
Analysis of browser working principle
使用 “display: flex;justify-content: center;align-items: center; ” 解决流式栅格布局无法居中的问题
Modulenotfounderror: no module named 'pyemd' solution
Database transactions (often asked)