



| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<view wx:if="{{isHide}}"> <view wx:if="{{canIUse}}" > <view class='header'> <image src='/images/wx_login.png'></image> </view> <view class='content'> <view>申請(qǐng)獲取以下權(quán)限</view> <text>獲得你的公開(kāi)信息(昵稱,頭像等)</text> </view> <button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo"> 授權(quán)登錄 </button> </view> <view wx:else>請(qǐng)升級(jí)微信版本</view></view> <view wx:else> <view>我的首頁(yè)內(nèi)容</view></view> |
.header {
margin: 90rpx 0 90rpx 50rpx;
border-bottom: 1px solid #ccc;
text-align: center;
width: 650rpx;
height: 300rpx;
line-height: 450rpx;
}
.header image {
width: 200rpx;
height: 200rpx;
}
.content {
margin-left: 50rpx;
margin-bottom: 90rpx;
}
.content text {
display: block;
color: #9d9d9d;
margin-top: 40rpx;
}
.bottom {
border-radius: 80rpx;
margin: 70rpx 50rpx;
font-size: 35rpx;
}
Page({
data: {
//判斷小程序的API,回調(diào),參數(shù),組件等是否在當(dāng)前版本可用。
canIUse: wx.canIUse('button.open-type.getUserInfo'),
isHide: false
},
onLoad: function() {
var that = this;
// 查看是否授權(quán)
wx.getSetting({
success: function(res) {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: function(res) {
// 用戶已經(jīng)授權(quán)過(guò),不需要顯示授權(quán)頁(yè)面,所以不需要改變 isHide 的值
// 根據(jù)自己的需求有其他操作再補(bǔ)充
// 我這里實(shí)現(xiàn)的是在用戶授權(quán)成功后,調(diào)用微信的 wx.login 接口,從而獲取code
wx.login({
success: res => {
// 獲取到用戶的 code 之后:res.code
cosole.log("用戶的code:" + res.code);
// 可以傳給后臺(tái),再經(jīng)過(guò)解析獲取用戶的 openid
// 或者可以直接使用微信的提供的接口直接獲取 openid ,方法如下:
// wx.request({
// // 自行補(bǔ)上自己的 APPID 和 SECRET
// url: 'https://api.weixin.qq.com/sns/jscode2session?appid=自己的APPID&secret=自己的SECRET&js_code=' + res.code + '&grant_type=authorization_code',
// success: res => {
// // 獲取到用戶的 openid
// console.log("用戶的openid:" + res.data.openid);
// }
// });
}
});
}
});
} else {
// 用戶沒(méi)有授權(quán)
// 改變 isHide 的值,顯示授權(quán)頁(yè)面
that.setData({
isHide: true
});
}
}
});
},
bindGetUserInfo: function(e) {
if (e.detail.userInfo) {
//用戶按了允許授權(quán)按鈕
var that = this;
// 獲取到用戶的信息了,打印到控制臺(tái)上看下
console.log("用戶的信息如下:");
console.log(e.detail.userInfo);
//授權(quán)成功后,通過(guò)改變 isHide 的值,讓實(shí)現(xiàn)頁(yè)面顯示出來(lái),把授權(quán)頁(yè)面隱藏起來(lái)
that.setData({
isHide: false
});
} else {
//用戶按了拒絕按鈕
wx.showModal({
title: '警告',
content: '您點(diǎn)擊了拒絕授權(quán),將無(wú)法進(jìn)入小程序,請(qǐng)授權(quán)之后再進(jìn)入!!!',
showCancel: false,
confirmText: '返回授權(quán)',
success: function(res) {
// 用戶沒(méi)有授權(quán)成功,不需要改變 isHide 的值
if (res.confirm) {
console.log('用戶點(diǎn)擊了“返回授權(quán)”');
}
}
});
}
}
})
關(guān)于 TabBar 的處理,只需要把上面寫(xiě)好的頁(yè)面設(shè)置到 app.json 里面即可。
https://github.com/yyzheng1729/loginDemo