当前位置:网站首页>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,
})
}
})
}
})
边栏推荐
- Force the resumption of game 302 of the week
- Lombok detailed introduction
- Moveit2 - 8. Motion planning API
- Question B: shunzi date
- Database transactions (often asked)
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- Fiddler grabs packets and displays err_ TUNNEL_ CONNECTION_ FAILED
- Leetcode programming practice -- Tencent selected 50 questions (I)
- Force deduction brush question 14. Longest common prefix
- JS password combination rule - 8-16 digit combination of numbers and characters, not pure numbers and pure English
猜你喜欢

B. Making Towers

144. Preorder traversal of binary tree

Acwing 870. approximate number

Solution: owner's smart site supervision cloud platform

What is technical support| Daily anecdotes

Introduction to Apache Doris grafana monitoring indicators

P100 MSSQL database penetration test of secondary vocational network security skills competition

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

Modulenotfounderror: no module named 'pyemd' solution

Unity: test rotation function
随机推荐
Test question f: statistical submatrix
Force deduction brush question 26. Delete duplicates in the ordered array
List type to string type
B. All Distinct
[file upload] parse text files and store them in batches through JDBC connection (dynamic table creation and dynamic storage)
JS password combination rule - 8-16 digit combination of numbers and characters, not pure numbers and pure English
Moveit2 - 8. Motion planning API
Force button brushing question 61. rotating linked list
Use of CCleaner
Take a database statement note: when the field is empty, assign the default value to the result
原创 | ueditor1.4.3-asmx绕过waf
Swagger key configuration items
[Flink] protocol operator reduce
How chemical enterprises choose digital service providers with dual prevention mechanism
A code takes you to draw multi format sangjimei pictures such as interactive +pdf+png
Many local and municipal supervision departments carried out cold drink sampling inspection, and Zhong Xue's high-quality products were all qualified
NVM installation and use
[Flink] submit the jar package to the Flink cluster and run it
55k is stable, and the recommendation system will always drop God!
Visio use