goview_vue/src/utils/router.ts

185 lines
3.8 KiB
TypeScript
Raw Normal View History

import { useRoute } from 'vue-router'
import { ResultEnum, RequestHttpHeaderEnum } from '@/enums/httpEnum'
2021-12-18 16:36:43 +08:00
import { ErrorPageNameMap, PageEnum } from '@/enums/pageEnum'
import { docPath, giteeSourceCodePath } from '@/settings/pathConst'
2022-05-20 16:12:27 +08:00
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/system.api'
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) {
2022-03-07 01:04:29 +08:00
return openNewWindow(fullPath)
2021-12-21 15:56:35 +08:00
}
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
/**
* * 退
2021-12-18 16:36:43 +08:00
*/
export const logout = async () => {
const res:any = await logoutApi()
if(res.code === ResultEnum.SUCCESS) {
window['$message'].success((`${window.$t('global.logout_success')}!`))
}
clearCookie(RequestHttpHeaderEnum.COOKIE)
2022-05-20 16:12:27 +08:00
clearLocalStorage(StorageEnum.GO_SYSTEM_STORE)
2021-12-18 16:36:43 +08:00
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) => {
2022-03-07 01:04:29 +08:00
return 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 boolean
*/
export const isPreview = () => {
return document.location.hash.includes('preview')
}
/**
* *
* @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)
}
/**
* * login
* @return boolean
*/
export const loginCheck = () => {
try {
2022-05-20 16:12:27 +08:00
const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
if (!info) return false
2022-05-20 16:12:27 +08:00
if (info[SystemStoreEnum.USER_INFO][SystemStoreUserInfoEnum.USER_TOKEN]) {
return true
}
return false
} catch (error) {
return false
}
}