// 白名单 const whiteList = [, // 注意入口页必须直接写 '/' '/pages/login/login' ] export default async function() { const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'] // 添加全局路由拦截 list.forEach(item => { uni.addInterceptor(item, { // 路由前拦截 invoke(args) { console.log('navigateTo路由跳转成功', args) const token = uni.getStorageSync('tonken') const url = args.url.split('?')[0] if (token) { if (args.url.includes('/pages/login/login')) { //跳转到首页 uni.navigateTo({ url: '/pages/index/index', }) return false } return args } else { let pass if (whiteList) { pass = whiteList.some(whiteItem => { if (typeof(whiteItem) === 'object' && whiteItem.pattern) { return whiteItem.pattern.test(url) } return url === whiteItem }) } console.log('pass', pass) if (!pass) { uni.showModal({ title: '未登录', content: '您未登录,需要登录后才能继续', showCancel: false, confirmText: '确定', success: res => { if (res.confirm) { uni.reLaunch({ url: '/pages/login/login', }) } }, }) return false } return args } }, // 路由后拦截 success(res) {}, fail(err) {} }) }) }