第三篇 uniapp如何獲取微信手機號碼


<template>
    <view>
        <!-- bindgetphonenumber -->
        <button type="primary" open-type="getUserInfo" @click="getuserinfo" withCredentials="true">微信登錄</button>
        <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">獲取電話號碼</button>
 
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
 
            };
        },
        onLoad: function() {
            uni.login({
                success: function(res) {
                    // 獲取code
                    // console.log(JSON.stringify(res));
                }
            });
        },
 
        methods: {
            getPhoneNumber: function(e) {
                console.log(e);
                if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
 
                } else {
 
                }
 
                //                 console.log(JSON.stringify(e.encryptedData));
                //                 console.log(JSON.stringify(e.iv));
            },
            getUserInfo: function(loginType, cb) {
                var that = this
                if (this.globalData.userInfo) {
                    typeof cb == "function" && cb(this.globalData.userInfo, true);
                } else {
                    //1.調用登錄接口
                    wx.login({
                        success: function() {
                            wx.getUserInfo({
                                success: function(res) {
                                    that.globalData.userInfo = res.userInfo;
                                    typeof cb == "function" && cb(that.globalData.userInfo, true);
                                },
                                fail: function() {
                                    //2.第一次登陸不強制授權,直接返回
                                    if (loginType == 0) {
                                        typeof cb == "function" && cb(that.globalData.userInfo, false);
                                    } else {
                                        //3.授權友好提示
                                        wx.showModal({
                                            title: '提示',
                                            content: "您還未授權登陸,部分功能將不能使用,是否重新授權?",
                                            showCancel: true,
                                            cancelText: "否",
                                            confirmText: "是",
                                            success: function(res) {
                                                //4.確認授權調用wx.openSetting
                                                if (res.confirm) {
                                                    if (wx.openSetting) { //當前微信的版本 ,是否支持openSetting
                                                        wx.openSetting({
                                                            success: (res) => {
                                                                if (res.authSetting["scope.userInfo"]) { //如果用戶重新同意了授權登錄
                                                                    wx.getUserInfo({
                                                                        success: function(res) {
                                                                            that.globalData.userInfo = res.userInfo;
                                                                            typeof cb == "function" && cb(that.globalData.userInfo, true);
                                                                        }
                                                                    })
                                                                } else { //用戶還是拒絕
                                                                    typeof cb == "function" && cb(that.globalData.userInfo, false);
                                                                }
                                                            },
                                                            fail: function() { //調用失敗,授權登錄不成功
                                                                typeof cb == "function" && cb(that.globalData.userInfo, false);
                                                            }
                                                        })
                                                    } else {
                                                        typeof cb == "function" && cb(that.globalData.userInfo, false);
                                                    }
                                                } else {
                                                    typeof cb == "function" && cb(that.globalData.userInfo, false);
                                                }
                                            }
                                        })
                                    }
                                }
                            })
                        }
                    })
                }
            }
        }
    }
</script>
 
<style>
 
</style>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM