uni-app获取手机号

2021-02-26  本文已影响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'
                            });
                        }

                    }
                })
            },

上一篇下一篇

猜你喜欢

热点阅读