wxml文件--页面
<view wx:if="{{canIUse}}">
<view class='header'>
<image src='/images/212.png'></image>
</view>
<view class='content'>
<view>申请获取以下权限</view>
<text>获得你的公开信息(昵称,头像等)</text>
</view>
<button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">
授权登录
</button>
</view>
<view wx:else>请升级微信版本</view>
js部分:
Page({
data: {
//判断小程序的API,回调,参数,组件等是否在当前版本可用。
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function () {
var that = this;
// 查看是否授权
wx.getSetting({
success: function (res) {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: function (res) {
//从数据库获取用户信息
that.queryUsreInfo();
//用户已经授权过
wx.switchTab({
url: pages/index/index
})
}
});
}
}
})
},
bindGetUserInfo: function (e) {
if (e.detail.userInfo) {
//用户按了允许授权按钮
var that = this;
//插入登录的用户的相关信息到数据库
// wx.request({
// url: '这里换成自己后台的链接',
// data: {
// // 根据自己的需求传参数
// // 例如:openid: getApp().globalData.openid
// },
// header: {
// 'content-type': 'application/json'
// },
// success: function (res) {
// //从数据库获取用户信息
// that.queryUsreInfo();
// console.log("插入小程序登录用户信息成功!");
// }
// });
//授权成功后,跳转进入小程序首页
wx.switchTab({
url: baseUrl + api.pageUrl.INDEX_PAGE_URL
})
} else {
//用户按了拒绝按钮
wx.showModal({
title: '警告',
content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
showCancel: false,
confirmText: '返回授权',
success: function (res) {
if (res.confirm) {
console.log('用户点击了“返回授权”')
}
}
})
}
},
//获取用户信息接口
queryUsreInfo: function () {
// wx.request({
// url: '这里换成自己后台的链接',
// data: {
// // 根据自己的需求传参数
// // 例如:openid: getApp().globalData.openid
// },
// header: {
// 'content-type': 'application/json'
// },
// success: function (res) {
// // 拿到自己后台传过来的数据,自己作处理
// console.log(res.data);
// }
// });
},