goview_vue/src/utils/router.ts

152 lines
2.8 KiB
TypeScript
Raw Normal View History

import { useRoute } from 'vue-router'
import { ResultEnum } from '@/enums/httpEnum'
2021-12-18 16:36:43 +08:00
import { ErrorPageNameMap, PageEnum } from '@/enums/pageEnum'
import router from '@/router'
2021-12-18 16:36:43 +08:00
import { docPath, giteeSourceCodePath } from '@/settings/pathConst'
2021-12-14 16:41:43 +08:00
/**
* *
* @param pageName
2021-12-21 15:56:35 +08:00
* @param isReplace
* @param windowOpen
2021-12-14 16:41:43 +08:00
*/
2021-12-21 15:56:35 +08:00
export const routerTurnByName = (
pageName: string,
isReplace?: boolean,
windowOpen?: boolean
) => {
2021-12-21 14:57:31 +08:00
if (windowOpen) {
2021-12-21 15:56:35 +08:00
const path = fetchPathByName(pageName, 'href')
openNewWindow(path)
2021-12-21 14:57:31 +08:00
return
}
if (isReplace) {
router.replace({
name: pageName,
})
return
}
2021-12-14 16:41:43 +08:00
router.push({
name: pageName,
2021-12-14 16:41:43 +08:00
})
}
2021-12-18 16:36:43 +08:00
2021-12-21 15:56:35 +08:00
/**
* *
* @param pageName
* @param pageName
*/
export const fetchPathByName = (pageName: string, p?: string) => {
2022-03-06 02:08:14 +08:00
try {
const pathData = router.resolve({
name: pageName,
})
return p ? (pathData as any)[p] : pathData
} catch (error) {
window['$message'].warning('查询路由信息失败,请联系管理员!')
}
2021-12-21 15:56:35 +08:00
}
/**
* *
2021-12-21 15:57:39 +08:00
* @param path
* @param query
2021-12-21 15:56:35 +08:00
* @param isReplace
2021-12-21 15:57:39 +08:00
* @param windowOpen
2021-12-21 15:56:35 +08:00
*/
export const routerTurnByPath = (
path: string,
query?: Array<string | number>,
isReplace?: boolean,
windowOpen?: boolean
) => {
let fullPath = ''
if (query?.length) {
fullPath = `${path}/${query.join('/')}`
}
if (windowOpen) {
openNewWindow(fullPath)
return
}
if (isReplace) {
router.replace({
path: fullPath,
2021-12-21 15:56:35 +08:00
})
return
}
router.push({
path: fullPath,
2021-12-21 15:56:35 +08:00
})
}
2021-12-18 16:36:43 +08:00
/**
* *
* @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)
}
2021-12-18 16:36:43 +08:00
/**
* * 退
*/
export const logout = () => {
routerTurnByName(PageEnum.BASE_LOGIN_NAME)
}
/**
2021-12-21 14:57:31 +08:00
* *
2021-12-18 16:36:43 +08:00
* @param url
*/
2021-12-21 15:56:35 +08:00
export const openNewWindow = (url: string) => {
window.open(url, '_blank')
2021-12-18 16:36:43 +08:00
}
/**
2021-12-21 14:57:31 +08:00
* *
2021-12-18 16:36:43 +08:00
* @param url
*/
2021-12-21 14:57:31 +08:00
export const openDoc = () => {
openNewWindow(docPath)
2021-12-18 16:36:43 +08:00
}
/**
2021-12-21 14:57:31 +08:00
* *
2021-12-18 16:36:43 +08:00
* @param url
*/
2021-12-21 14:57:31 +08:00
export const openGiteeSourceCode = () => {
openNewWindow(giteeSourceCodePath)
2021-12-18 16:36:43 +08:00
}
/**
* *
* @returns object
*/
export const fetchRouteParams = () => {
2022-03-06 02:08:14 +08:00
try {
const route = useRoute()
return route.params
} catch (error) {
window['$message'].warning('查询路由信息失败,请联系管理员!')
}
}
/**
2022-01-09 19:22:55 +08:00
* *
* @param confirm
*/
2022-02-03 22:54:31 +08:00
export const goHome = () => {
2022-01-09 19:22:55 +08:00
routerTurnByName(PageEnum.BASE_HOME_NAME)
}