微信小程序 授权登陆
2019-01-25 本文已影响0人
哈斯勒
wxml:
<view class="container">
<view class="userinfo" wx:if="{{!hasUserInfo && canIUse}}">
<image src='/images/wx_login.png'></image>
<view class='line'> </view>
<view class='tip-container'>
<text class='tip'>申请获得以下权限</text>
<text class='tip detail' >获得您的公开信息(昵称,头像等)</text>
</view>
<button class='login' open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 授权登录 </button>
</view>
<block wx:else>
<view class="bg_ad" bindtap='goNext'> </view>
</block>
</view>
.js
onLoad: function (options) {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse){
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
}),
timer = setTimeout(function () {
wx.redirectTo({
url: '/pages/game/web',
})
}, 1000 * 5)
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
}),
timer = setTimeout(function () {
wx.redirectTo({
url: '/pages/game/web',
})
}, 1000 * 5)
}
})
}
},
getUserInfo: function(e) {
console.log(e)
console.log(e.detail.userInfo)
if(e.detail.userInfo){
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
}),
timer = setTimeout(function () {
wx.redirectTo({
url: '/pages/game/web',
})
}, 1000 * 5)
// wx.navigateTo({
// url: '/pages/program/shop'
// })
}else{
wx.showModal({
title: '温馨提醒',
content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
showCancel: false,
confirmText: '返回授权',
success: function (res) {
if (res.confirm) {
console.log('用户点击了“返回授权”')
}
}
})
}
},
goNext: function (e) {
clearTimeout(timer);
wx.redirectTo({
url: '/pages/game/web',
})
}