1***@qq.com
1***@qq.com
  • 发布:2019-01-10 11:40
  • 更新:2021-01-06 13:45
  • 阅读:22777

uniapp获取微信手机号码

分类:uni-app
<template>  
    <view>  
        <!-- bindgetphonenumber -->  
        <button type="primary" open-type="getUserInfo" @click="getuserinfo" withCredentials="true">微信登录</button>  
        <button open-type="getPhoneNumber" bindgetphonenumber="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>  
2019-01-10 11:40 1 条评论 负责人:无 分享
已邀请:
4***@qq.com

4***@qq.com

我复制了页面 一点用都没有 完全看不到手机

  • 1***@qq.com (作者)

    <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>

    2019-06-04 16:21

Trust

Trust - 少说废话

@getphonenumber="getPhoneNumber"
  • 博浪

    谢谢分享。

    uniapp直接调用微信原生的bindgetphonenumber="getPhoneNumber"会报错

    2019-05-10 10:10

  • 加微shuju578

    我也在做,况且现在也用不到代码什么的了,我能做到,直接抓取网页的浏览客户,抓取app的登录客户,看头来加

    2019-11-15 09:47

1***@qq.com

1***@qq.com

Cannot read property 'userInfo' of undefined;

加微shuju578

加微shuju578

我也在做,况且现在也用不到代码什么的了,我能做到,直接抓取网页的浏览客户,抓取app的登录客户,看头来加

9***@qq.com

9***@qq.com

请问为何点击获取手机号码会显示该appid没有权限?

ayersWhale

ayersWhale

请问这个点击没有反应是什么原因,怎么解决的

冰冷de夏天

冰冷de夏天

获取微信用户绑定的手机号不对个人开发者生效...

  • tuanup

    微信真的是,好歹开发阶段给开放出来测试下啊

    2021-01-10 01:05

ligsa

ligsa

写的不知道什么东西,一堆bug

该问题目前已经被锁定, 无法添加新回复