当前位置:网站首页>wx. Login and wx Getuserprofile simultaneous use problem
wx. Login and wx Getuserprofile simultaneous use problem
2022-06-12 12:33:00 【Fashion_ Barry】
When developing wechat applet , Will call first wx.login obtain code in the future call wx.getUserProfile() obtain rawData、signature、encryptedData、iv Etc , Go to the background for processing ;
But as the 4 month 28 Japan 24 After the release of the new version of the applet , Interface adjustment , If you write like this, you will report a mistake :error msg: ''getUserProfile:fail can only be invoked by user TAP gesture".
documentation :

common problem
Before we start talking, let's say that I have personally encountered an accident that cannot be decrypted encryptedData, The prompt signature fails
1、 hold wx.login() On the wx.getUserProfile() Pre execution ; To ensure access sessionKey stay getUserProfile Before , Otherwise, the decryption will fail ;
2、 stay wx.logon() Of success Package in callback wx.getUserProfile, To ensure access code and obtain encryptedData, So as to avoid decryption failure , But the official website clearly stipulates ,wx.getUserProfile() Only through tap Click to start , Otherwise error:getUserProfile:fail can only be invoked by user TAP gesture
3、 Refer to online practice hold wx.login() Put it in wx.getUserProfile() Of success Call back to execute , It is found that there will still be occasional Decryption failed ;
Realization : adopt Promise Serial guarantees sequential execution
Tips: Due to the use of uniapp Development , Writing and The syntax of the applet is different , But the thinking is the same
export default{
data(){
return{
}
},
methods:{
getUserProfile() {
return new Promise((resolve, reject) => {
uni.getUserProfile({
desc: ' Get your nickname 、 avatar 、 Region and gender ',
success: userRes => {
console.log('getUserProfile-res', userRes);
resolve(userRes);
},
fail: userErr => {
uni.showToast({
title: ' Authorization failed ',
icon: 'error'
});
console.log('getUserProfile-err', userErr);
reject(userErr);
}
});
});
},
getLoginCode() {
return new Promise((resolve, reject) => {
uni.login({
provider: 'weixin',
success: loginRes => {
console.log('loginRes', loginRes);
resolve(loginRes);
}
});
});
},
}
}html In the binding click event
<button @click.stop="goLogin" class="bottom-btn" type="default" size="mini"> Log in </button>goLogin Method used in promise.all To ensure sequential execution
export default{
data(){
return{
}
},
methods:{
goLogin() {
// Pay attention to the way you write functions , Avoid mistakes
let userProFile = this.getUserProfile();
let loginCode = this.getLoginCode();
loginCode
.then(code => {
return code;
})
.then(logCode => {
return new Promise((resolve, reject) => {
userProFile
.then(res => {
resolve({ logCode, iv: res.iv, rawData: res.rawData, encryptedData: res.encryptedData, signature: res.signature });
})
.catch(err => {
reject(err);
});
});
})
.then(res => {
console.log('promise-res', res);
})
.catch(err => {
console.log('userProfile-err', err);
});
}
}
}In this way, after several days of intermittent testing, no accidental The problem of decryption failure !
边栏推荐
- Introduction and test of MySQL partition table
- 爱可可AI前沿推介(6.12)
- Quic wire layout specification - packet types and formats | quic protocol standard Chinese translation (2) package type and format
- Influxdb2.x benchmark tool - influxdb comparisons
- Tron API wave field transfer query interface PHP version package based on thinkphp5 attached interface document 20220528 version
- Time series database - incluxdb2 docker installation
- Tron API wave field transfer query interface PHP version package based on thinkphp5 attached interface document 20220602 version deployed interface applicable to any development language
- 从小白入手,从已经训练好的模型中取出weight权重参数绘制柱状图
- Micro task, macro task and event loop of JS
- You can't just use console Log ()?
猜你喜欢

【C语言】关键字static&&多文件&&猜字游戏

Numpy数值计算基础

SEO optimization of web pages

JS string array converted to numeric array and how to add the numbers in the array

Lightweight ---project

itk 多分辨率图像 itk::RecursiveMultiResolutionPyramidImageFilter

Vs2019 set ctrl+/ as shortcut key for annotation and uncomment

Matlab install license manager error -8

Rust语言学习

KDD2022 | 边信息增强图Transformer
随机推荐
El select data echo, display only value but not label
MySQL 分区表介绍与测试
Principle of master-slave replication of redis
Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
Matlab install license manager error -8
sublime_text使用
#ifndef#define#endif防止头文件重复包含, 你不是真的懂
Redis的主从复制原理
VGg small convolution instead of large convolution vs deep separable convolution
Dom and BOM in JS
从小白入手,从已经训练好的模型中取出weight权重参数绘制柱状图
Rust语言学习
安装canvas遇到的问题和运行项目遇到的报错
一个ES设置操作引发的“血案”
SWI-Prolog的下载与使用
Pre order, middle order and post order traversal of tree
Quic wire layout specification - packet types and formats | quic protocol standard Chinese translation (2) package type and format
Kotlin扩展函数实现原理
itk 多分辨率图像 itk::RecursiveMultiResolutionPyramidImageFilter
JS将DOM导出为图片的方法