1. 程式人生 > 其它 >uniapp獲取微信手機號碼

uniapp獲取微信手機號碼

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>