<!--index.wxml-->
<!-- 授权界面 -->
<
cover-view
class=
‘auth_wrap‘
wx:if=
"{{hasUserInfo && canIUse}}">
<
cover-view
class=
‘con‘>
</
cover-image
>
<
button
class=
‘btn‘
type=
‘primary‘
open-type=
"getUserInfo"
bindgetuserinfo=
"getUserInfo"> 授权登录
</
button
>
</
cover-view
>
</
cover-view
>
//app.js
App({
onLaunch:
function () {
},
globalData: {
‘hasUserInfo‘:
false,
‘code‘:
null,
‘userInfo‘:
null
}
})
//index.js
const app = getApp();
Page({
data: {
userInfo: {},
canIUse: wx.canIUse(
‘button.open-type.getUserInfo‘)
//判断小程序的API,回调,参数,组件等是否在当前版本可用。
},
onLoad:
function () {
var self=
this;
wx.getSetting({
success: res => {
if (res.authSetting[
‘scope.userInfo‘]) {
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
app.globalData.userInfo = res.userInfo
// console.log(app.globalData.userInfo);
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId,sessionKey,unionId
getApp().globalData.code = res.code;
console.log(getApp().globalData.code);
wx.request({
url:
‘https:xxxxxxxxxxxxxxxxxxxxxx‘,
data: {
‘code‘: getApp().globalData.code,
},
method:
‘POST‘,
success:
function (res) {
console.log(res);
getApp().globalData.uid = res.data.data.uid;
console.log(
‘uid====‘,getApp().globalData.uid);
}
})
}
})
}
})
}
else {
// 用户没有授权
self.setData({
hasUserInfo:
true
});
}
}
})
},
getUserInfo:
function (e) {
if (e.detail.userInfo) {
console.log(e.detail.userInfo);
//用户按了允许授权按钮
var self =
this;
self.setData({
userInfo: e.detail.userInfo,
hasUserInfo:
false
});
}
else {
//用户按了拒绝按钮
wx.showModal({
title:
‘警告‘,
content:
‘您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!‘,
showCancel:
false,
confirmText:
‘返回授权‘,
success:
function (res) {
// 用户没有授权成功,不需要改变 isHide 的值
if (res.confirm) {
console.log(
‘用户点击了“返回授权”‘);
}
}
});
}
},
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。