参考文章:https://www.jianshu.com/p/8f33a38a671a
代码
/**
* 页面路由鉴权相关
*/
const isExpired = (auth) => {
if (auth.token === '') return false
return auth.expireTime - new Date().getTime() > 0
}
const globalData = getApp().globalData;
export const Router = (page) => {
let show = page.onShow;
page.onShow = function() {
if (isExpired(globalData.auth)) {
show.call(this)
} else {
wx.redirectTo({
url: '/pages/login/login',
})
}
}
return Page(page)
}
在需要鉴权的页面,用
import { Router} from ‘…/…/auth/auth.js’
Router({})代替Page
存在问题
切换页面路由时比较慢,正在查询原因