$checkAuthSetting 在全局检测授权情况:
已授权 --> $getUserInfo 获取用户信息 —— 检测 seesion_key 状态(未过期)
$getUserInfo 获取用户信息 —— 检测 seesion_key 状态(已过期)—— 调用微信 $wxLogin 登录
未授权 --> $getCurrentPageUrlWithArgsPromise 获取当前页面带参数的url —— 判断是否白名单(是)
$getCurrentPageUrlWithArgsPromise 获取当前页面带参数的url —— 判断是否白名单(否)—— 跳转登录授权界面
// 全局判断 app.js
App({
...
onShow: function (e) {
// 调用全局检测授权情况
this.globalAuthSetting()
},
// 全局检测授权情况
globalAuthSetting () {
$checkAuthSetting().then(res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权操作
console.log('----APP 全局检测:已授权----')
// todo 获取用户信息并保存
$getUserInfo().then(res => {
let userInfo = res
this.globalData.userInfo = userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(userInfo)
}
return userInfo
}).then(userInfo => {
let loginParams = userInfo
// 检测 session_key状态
$checkSession().then(sessionRes => {
console.log('----APP session_key 未过期 ----', sessionRes)
}).catch(sessionErr => {
console.log('----APP session_key 已过期 ----', sessionErr)
// 过期重新调用微信wx.login登录
$wxLogin().then(wxRes => {
console.log('----APP 重新登录-> wxlogin:----', wxRes)
let code = wxRes.code
wx.setStorageSync("code", code)
loginParams.code = code
this.func.$post('login', loginParams, reqRes => {
let {code, token} = reqRes
if (code == 0) {
wx.setStorageSync("token", token)
wx.setStorageSync('nendRefreshChannel', true)
} else {
$toast('网络错误!')
}
})
})
})
})
} else {
// 未授权跳转授权页面
console.log('----APP 未授权 ----')
$getCurrentPageUrlWithArgsPromise().then(res => {
console.log('----APP 当前打开页面URL:----', res)
let currentUrl = $getCurrentPageUrl()
let isInPageAuthWhite = checkAuthWhitePage(currentUrl, CONFIG.authWhitePages)
return isInPageAuthWhite ? isInPageAuthWhite : res
}).then(res => {
if (res !== true) {
console.log('----APP 非白名单页面跳转授权页面 ----')
let loginCallBackUrl = res
$routerPage('/pages/login/login', 'redirectTo').then(() => {
let isHasLoginCallBackUrl = wx.getStorageSync('loginCallBackUrl') || ''
if (!isHasLoginCallBackUrl) {
wx.setStorageSync('loginCallBackUrl', loginCallBackUrl)
}
})
} else {
console.log('----APP 白名单页面不跳转授权页面 ----')
}
})
}
}).catch(err => {
console.log('----APP $checkAuthSetting error ----', err)
$toast('微信授权接口请求失败!')
})
}
...
})
使用小程序 button,open-type 属性值为 getUserInfo,获取用户基本信息;
<button class="btn submit-btn" open-type="getUserInfo" bindgetuserinfo="getUserInfo" lang="zh_CN">立即授权</button>
判断用户是否点击允许授权,允许则执行微信登录 $wxLogin
//未授权点击弹出授权
getUserInfo (e) {
console.log('点击授权 getUserInfo:', e)
if (e.detail.errMsg === 'getUserInfo:ok') {
let userInfo = e.detail
app.globalData.userInfo = userInfo
this.setData({
userInfo: userInfo,
hasUserInfo: true
})
$wxLogin().then(res => {
let code = res.code
wx.setStorageSync("code", code)
return code
}).then(res => {
let params = Object.assign(userInfo, { code: res })
app.func.$post('login', params, reqRes => {
let {code, token} = reqRes
if (code === $SUCCESS) {
wx.setStorageSync("token", token)
// 返回上一级页面
let loginCallBackUrl = wx.getStorageSync('loginCallBackUrl') || DEFAULT_PATH
if (loginCallBackUrl) {
loginCallBackUrl = loginCallBackUrl.startsWith(AUTH_PATH) ? DEFAULT_PATH : loginCallBackUrl
$routerPage('/' + loginCallBackUrl, isTabBarPage(loginCallBackUrl) ? 'switchTab' : 'redirectTo').then(res => {
wx.removeStorageSync('loginCallBackUrl')
})
}
} else {
$toast('网络错误!')
}
})
})
} else {
// 取消授权
$toast('取消授权')
}
}