2023-04-03 07:13:49 +00:00
|
|
|
|
import { ClientEnum } from '@/enums/appEnums'
|
2022-09-15 07:03:12 +00:00
|
|
|
|
import { BACK_URL } from '@/enums/cacheEnums'
|
|
|
|
|
|
import { useUserStore } from '@/stores/user'
|
2022-09-09 02:45:16 +00:00
|
|
|
|
import { getToken } from '@/utils/auth'
|
2022-09-15 07:03:12 +00:00
|
|
|
|
import cache from '@/utils/cache'
|
2023-04-03 07:13:49 +00:00
|
|
|
|
import { client } from '@/utils/client'
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
|
import wechatOa from '@/utils/wechat'
|
|
|
|
|
|
// #endif
|
2022-09-15 07:54:12 +00:00
|
|
|
|
import { routes } from './routes'
|
|
|
|
|
|
|
2022-09-15 07:03:12 +00:00
|
|
|
|
const whiteList = ['register', 'login', 'forget_pwd']
|
2022-09-09 06:44:55 +00:00
|
|
|
|
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
|
|
|
|
|
|
list.forEach((item) => {
|
|
|
|
|
|
uni.addInterceptor(item, {
|
|
|
|
|
|
invoke(e) {
|
|
|
|
|
|
// 获取要跳转的页面路径(url去掉"?"和"?"后的参数)
|
|
|
|
|
|
const url = e.url.split('?')[0]
|
|
|
|
|
|
const currentRoute = routes.find((item) => {
|
|
|
|
|
|
return url === item.path
|
|
|
|
|
|
})
|
|
|
|
|
|
// 需要登录并且没有token
|
|
|
|
|
|
if (currentRoute?.auth && !getToken()) {
|
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
|
url: '/pages/login/login'
|
2022-09-09 02:45:16 +00:00
|
|
|
|
})
|
2022-09-09 06:44:55 +00:00
|
|
|
|
return false
|
2022-09-09 02:45:16 +00:00
|
|
|
|
}
|
2022-09-09 06:44:55 +00:00
|
|
|
|
return e
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(err) {
|
|
|
|
|
|
// 失败回调拦截
|
|
|
|
|
|
console.log(err)
|
|
|
|
|
|
}
|
2022-09-09 02:45:16 +00:00
|
|
|
|
})
|
2022-09-09 06:44:55 +00:00
|
|
|
|
})
|
2022-09-15 07:03:12 +00:00
|
|
|
|
|
|
|
|
|
|
export function setupRouter() {
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
|
const app = getApp()
|
2022-09-15 07:27:07 +00:00
|
|
|
|
app.$router.afterEach((to: any, from: any) => {
|
2022-09-15 07:54:12 +00:00
|
|
|
|
const index = whiteList.findIndex((item) => from.path.includes(item) || from.path === '/')
|
2022-09-15 07:03:12 +00:00
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
|
if (index == -1 && !userStore.isLogin) {
|
|
|
|
|
|
//保存登录前的路径
|
2022-09-15 07:54:12 +00:00
|
|
|
|
cache.set(BACK_URL, from.fullPath)
|
2022-09-15 07:03:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2023-04-03 07:13:49 +00:00
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
|
if (client == ClientEnum.OA_WEIXIN) {
|
|
|
|
|
|
// jssdk配置
|
|
|
|
|
|
await wechatOa.config()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2022-09-15 07:03:12 +00:00
|
|
|
|
// #endif
|
|
|
|
|
|
}
|