niucloud/uni-app/utils/interceptor.ts
2023-06-08 18:20:41 +08:00

83 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { language } from '@/locale'
import { checkNeedLogin } from '@/utils/auth'
import { redirect, urlDeconstruction, getToken, getSiteId, setDocumentTitle } from '@/utils/common'
import { memberLog } from '@/api/auth'
import { nextTick } from 'vue'
/**
* 页面跳转拦截器
*/
export const redirectInterceptor = () => {
const routerApi : string[] = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
routerApi.forEach((name : string) => {
uni.addInterceptor(name, {
invoke(args) {
const route = urlDeconstruction(args.url)
// 加载语言包
language.loadLocaleMessages(route.path, uni.getLocale())
// 开发模式下如果未配置站点ID则跳转到开发环境配置页面
// #ifdef H5
if (process.env.NODE_ENV == 'development') {
if ((getSiteId(uni.getStorageSync('wap_site_id') || import.meta.env.VITE_SITE_ID) === '') && route.path != '/pages/index/develop') {
redirect({ url: '/pages/index/develop', mode: 'reLaunch' })
}
}
// 设置网站标题
setDocumentTitle(route.path)
// #endif
// 校验是否需要登录
checkNeedLogin(route)
// 添加会员访问日志
if (getToken()) memberLog({ route: route.path, params: JSON.stringify(route.query), pre_route: getCurrentPages()[0].route })
}
})
})
}
/**
* 应用初始化拦截器
*/
export const launchInterceptor = () => {
const launch = uni.getLaunchOptionsSync()
launch.path = `/${launch.path}`
// 开发模式下如果未配置站点ID则跳转到开发环境配置页面
// #ifdef H5
if (process.env.NODE_ENV == 'development') {
// 后台DIY装修页面时获取站点ID
if (location.search.indexOf('?mode=decorate&site_id=') != -1) {
uni.setStorageSync('wap_site_id', location.search.replace('?mode=decorate&site_id=',''));
}
if (location.search.indexOf('?mode=preview&site_id=') != -1) {
uni.setStorageSync('wap_site_id', location.search.replace('?mode=preview&site_id=',''));
}
if (getSiteId(uni.getStorageSync('wap_site_id') || import.meta.env.VITE_SITE_ID) === '') {
launch.path = '/pages/index/develop';
uni.setStorageSync('develop_before_path', launch.path);
redirect({ url: '/pages/index/develop', mode: 'reLaunch' })
}
}
// 设置网站标题
setDocumentTitle(launch.path)
// #endif
// 加载语言包
language.loadLocaleMessages(launch.path, uni.getLocale())
// 校验是否需要登录
checkNeedLogin(launch)
// 存储分享会员id
if (launch.query && launch.query.mid) {
uni.setStorageSync('pid', launch.query.mid)
}
// 添加会员访问日志
if (getToken()) memberLog({ route: launch.path, params: JSON.stringify(launch.query || {}), pre_route: '' })
}