微信登录、wx.getUserProfile返回信息解密失败

815 阅读1分钟

关于微信小程序新登录接口wx.getUserProfile的使用方法
和手机号码有几率解密失败的解决办法

微信小程序登录auth.vue

<script>
    export default {
        name: "auth",
        data() {
            return {};
        },
        methods: {
            getCode() {
                this.disabled = true;
                uni.login({
                    provider: 'weixin',
                    timeout: 3000,
                    success: res => {
                        if (res.code) {
                            this.code = res.code;
                            this.userInfo = res;
                            this.getOpenId();
                        }
                    },
                    fail: (res) => {
                        this.disabled = false;
                        uni.$showMsg(res.errMsg);
                    }
                })
            },
            getOpenId() {
                uni.$http.sendRequest({
                    url: 'xxx',
                    data: {
                        code: this.code
                    },
                    method: 'GET',
                    success: res => {
                        if (0 !== res.data.code) {
                            this.openid = res.data.data.openid;
                            this.sessionKey = res.data.data.session_key;
                            uni.setStorageSync('session', res.data.data.session_key);
                            this.login();
                        } else {
                            uni.hideLoading();
                            this.disabled = false;
                        }
                    }
                });
            },
            login() {
                uni.$http.sendRequest({
                    url: 'xxx',
                    data: {
                        encryptedData: this.userInfo.encryptedData,
                        iv: this.userInfo.iv,
                        sessionKey: this.sessionKey,
                        openid: this.openid
                    },
                    success: res => {
                        if (0 !== res.data.code) {
                            uni.setStorageSync('token', res.data.data.token);
                            uni.$showMsg('登录成功', 'success');
                            // 跳转首页
                        }
                        uni.hideLoading();
                        this.disabled = false;
                    }
                });
            },
        }
    }
</script>
wx.getUserProfile({
    //新接口获取用户的开放信息(头像,昵称等,旧接口wx.getUserInfo获取
    //的信息皆为匿名)
    success:res=>{
            //用户头像
            //res.userInfo.avatarUrl
            //用户昵称
            //res.userInfo.nickName
            //调用完新接口后使用wx.login获取加密code
            wx.login({
                    success:res=>{
                            //res.code
                            //拿着code去获取openid 和 sessionKey
                            
                            //注意!!!!调用完wx.login之后还需调用旧接口
                            //wx.getUserInfo并且必须按照这样的顺序,
                            //不然会出现手机号码有几率解密失败的情况
                            wx.getUserInfo({
                                    success:res=>{
                                            //res.encryptedData
                                            //res.iv
                                            //拿着openid、sessionKey、encryptedData、iv 去登录
                                    }
                            })
                    }
            })
    }
})

总结:手机号码有几率解密失败的解决办法:
先调用 wx.getUserProfile 
再调用 wx.login 
再调用 wx.getUserInfo
顺序一定不能错,否则就会出现手机号码有几率解密失败的问题

来源链接:blog.csdn.net/Candy_3/art…