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