1
0
go-view-fetch/src/utils/router.ts

255 lines
5.6 KiB
TypeScript
Raw Normal View History

2025-08-20 12:09:24 +08:00
import { useRoute } from 'vue-router'
import { ResultEnum, RequestHttpHeaderEnum } from '@/enums/httpEnum'
import { ErrorPageNameMap, PageEnum, PreviewEnum } from '@/enums/pageEnum'
import { docPath, giteeSourceCodePath } from '@/settings/pathConst'
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
import { StorageEnum } from '@/enums/storageEnum'
import { clearLocalStorage, getLocalStorage, clearCookie } from './storage'
import router from '@/router'
import { logoutApi } from '@/api/path'
/**
* *
* @param pageName
* @param isReplace
* @param windowOpen
*/
export const routerTurnByName = (pageName: string, isReplace?: boolean, windowOpen?: boolean) => {
if (windowOpen) {
const path = fetchPathByName(pageName, 'href')
openNewWindow(path)
return
}
if (isReplace) {
router.replace({
name: pageName
})
return
}
router.push({
name: pageName
})
}
/**
* *
* @param pageName
* @param pageName
*/
export const fetchPathByName = (pageName: string, p?: string) => {
try {
const pathData = router.resolve({
name: pageName
})
return p ? (pathData as any)[p] : pathData
} catch (error) {
window['$message'].warning('查询路由信息失败,请联系管理员!')
}
}
/**
* *
* @param path
* @param query
* @param isReplace
* @param windowOpen
*/
export const routerTurnByPath = (
path: string,
query?: Array<string | number>,
isReplace?: boolean,
windowOpen?: boolean
) => {
let fullPath = ''
if (query?.length) {
fullPath = `${path}/${query.join('/')}`
}
if (windowOpen) {
return openNewWindow(fullPath)
}
if (isReplace) {
router.replace({
path: fullPath
})
return
}
router.push({
path: fullPath
})
}
/**
* *
* @param path
* @param query
* @param isReplace
* @param windowOpen
*/
export const routerTurnByPath2 = (
path: string,
query?: Array<string | number>,
param?: string,
isReplace?: boolean,
windowOpen?: boolean
) => {
let fullPath = ''
if (query?.length) {
fullPath = `${path}/${query.join('/')}`
}
// if (param?.length) {
// fullPath = `${fullPath}?${param.join('&')}`
// }
if (windowOpen) {
return openNewWindow(fullPath)
}
if (isReplace) {
// router.push({
// path: fullPath,
// query: {
// pageId: param
// }
// }).then(() => {
// window.location.reload();
// });
window.open(fullPath + '?pageId=' + param, '_self')
// window.location.reload();
return
}
router.push({
path: fullPath
})
}
/**
* *
* @param icon
* @returns
*/
export const redirectErrorPage = (code: ResultEnum) => {
if (!code) return false
const pageName = ErrorPageNameMap.get(code)
if (!pageName) return false
routerTurnByName(pageName)
}
/**
* *
*/
export const reloadRoutePage = () => {
routerTurnByName(PageEnum.RELOAD_NAME)
}
/**
* * 退
*/
export const logout = async () => {
try {
const res = await logoutApi()
if (res && res.code === ResultEnum.SUCCESS) {
window['$message'].success(window['$t']('global.logout_success'))
clearCookie(RequestHttpHeaderEnum.COOKIE)
clearLocalStorage(StorageEnum.GO_SYSTEM_STORE)
routerTurnByName(PageEnum.BASE_LOGIN_NAME)
}
} catch (error) {
window['$message'].success(window['$t']('global.logout_failure'))
}
}
/**
* *
* @param url
*/
export const openNewWindow = (url: string) => {
return window.open(url, '_blank')
}
/**
* *
* @param url
*/
export const openDoc = () => {
openNewWindow(docPath)
}
/**
* *
* @param url
*/
export const openGiteeSourceCode = () => {
openNewWindow(giteeSourceCodePath)
}
/**
* *
* @returns boolean
*/
export const isPreview = () => {
return document.location.href.includes('preview') || (document.location.href.includes('view') && !document.location.href.includes('chart')) || (document.location.href.includes('bigscreen') && !document.location.href.includes('chart'))
}
/**
* *
* @returns object
*/
export const fetchRouteParams = () => {
try {
const route = useRoute()
return route.params
} catch (error) {
window['$message'].warning('查询路由信息失败,请联系管理员!')
}
}
/**
* *
* @returns object
*/
export const fetchRouteParamsLocation = () => {
try {
// 防止添加query参数的时候解析ID异常
return document.location.hash.split('?')[0].split('/').pop() || ''
} catch (error) {
window['$message'].warning('查询路由信息失败,请联系管理员!')
return ''
}
}
/**
* *
* @param confirm
*/
export const goHome = () => {
routerTurnByName(PageEnum.BASE_HOME_NAME)
}
/**
* *
* @return boolean
*/
export const loginCheck = () => {
try {
const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
if (!info) return false
if (info[SystemStoreEnum.USER_INFO][SystemStoreUserInfoEnum.USER_TOKEN]) {
return true
}
return false
} catch (error) {
return false
}
}
/**
* *
* @returns
*/
export const previewPath = (id?: string | number) => {
const { origin, pathname } = document.location
const path = fetchPathByName(PreviewEnum.CHART_PREVIEW_NAME, 'href')
const previewPath = `${origin}${pathname}${path}/${id || fetchRouteParamsLocation()}`
return previewPath
}