美文网首页
uni-app获取手机号

uni-app获取手机号

作者: 追逐繁星的阿忠 | 来源:发表于2021-02-26 10:05 被阅读0次

    2021-02-26

    <view class="tl-checkBut">
                            <button class="tl-btn-270" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号授权</button>
                        </view>
    
    
    image.png

    //方案一,先login,然后取手机号

    login() {
                    uni.login({
                        success: (res) => {
                            console.log("login", JSON.stringify(res));
                            this.jsCode = res.code;
                            //console.log('res.code',res.code)
                        }
                    })
                },
    
    
                twoLogin(e){
                    let self = this;
                    uni.login({
                        success: (res) => {
                            console.log("login", JSON.stringify(res));
                            this.jsCode = res.code;
                            //console.log('res.code',res.code)
                            self.getPhoneNumber(e);
                        }
                    })
                },
    
    
                //获取手机号授权——@zxyuns2021-02-26
                getPhoneNumber(e) {
                    console.log(e.detail.errMsg);
                    console.log(e.detail.iv);
                    console.log(e.detail.encryptedData);
    
                    let self = this;
                    //后台解密获取用户的手机号码
                    request({
                        url: inter.getPhoneNumber,
                        data: {
                            jsCode: self.jsCode,
                            encryptedData: e.detail.encryptedData,
                            iv: e.detail.iv,
                        },
                        callback: (res) => {
                            console.log('res', res)
                            if (res.CODE == 0) {
                                self.phone = res.RESULT.phoneNumber;
                                //self.login();//重新去调一次登录,更新jsCode
                            } else {
                                uni.showToast({
                                    title: res.MESSAGE,
                                    mask: true,
                                    icon: 'none'
                                });
                            }
    
                        }
                    })
                },
    
    
    

    方案二: 每次获取成功后,直接更新当前的JsCOde

    //获取手机号授权——@zxyuns2021-02-26
                getPhoneNumber(e) {
                    console.log(e.detail.errMsg);
                    console.log(e.detail.iv);
                    console.log(e.detail.encryptedData);
    
                    let self = this;
                    //后台解密获取用户的手机号码
                    request({
                        url: inter.getPhoneNumber,
                        data: {
                            jsCode: self.jsCode,
                            encryptedData: e.detail.encryptedData,
                            iv: e.detail.iv,
                        },
                        callback: (res) => {
                            console.log('res', res)
                            if (res.CODE == 0) {
                                self.phone = res.RESULT.phoneNumber;
                                self.login();//重新去调一次登录,更新jsCode
                            } else {
                                uni.showToast({
                                    title: res.MESSAGE,
                                    mask: true,
                                    icon: 'none'
                                });
                            }
    
                        }
                    })
                },
    
    

    相关文章

      网友评论

          本文标题:uni-app获取手机号

          本文链接:https://www.haomeiwen.com/subject/khhmfltx.html