2022-01-09 17:12:35 +08:00
|
|
|
|
import { useRoute } from 'vue-router'
|
2022-05-21 17:31:01 +08:00
|
|
|
|
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'
|
2022-04-13 21:31:18 +08:00
|
|
|
|
import { StorageEnum } from '@/enums/storageEnum'
|
2022-05-21 17:31:01 +08:00
|
|
|
|
import { clearLocalStorage, getLocalStorage, clearCookie } from './storage'
|
2022-04-13 21:31:18 +08:00
|
|
|
|
import router from '@/router'
|
2022-05-21 17:31:01 +08:00
|
|
|
|
import { logoutApi } from '@/api/path/system.api'
|
2021-12-14 16:41:43 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* * 根据名字跳转路由
|
2021-12-17 11:55:42 +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
|
|
|
|
|
}
|
2021-12-17 11:55:42 +08:00
|
|
|
|
if (isReplace) {
|
|
|
|
|
router.replace({
|
2022-01-09 17:12:35 +08:00
|
|
|
|
name: pageName,
|
2021-12-17 11:55:42 +08:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-14 16:41:43 +08:00
|
|
|
|
router.push({
|
2022-01-09 17:12:35 +08:00
|
|
|
|
name: pageName,
|
2021-12-14 16:41:43 +08:00
|
|
|
|
})
|
2021-12-17 11:55:42 +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({
|
2022-01-09 17:12:35 +08:00
|
|
|
|
path: fullPath,
|
2021-12-21 15:56:35 +08:00
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
router.push({
|
2022-01-09 17:12:35 +08:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 12:25:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* * 重新加载当前路由页面
|
|
|
|
|
*/
|
|
|
|
|
export const reloadRoutePage = () => {
|
|
|
|
|
routerTurnByName(PageEnum.RELOAD_NAME)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 16:36:43 +08:00
|
|
|
|
/**
|
2022-05-21 17:31:01 +08:00
|
|
|
|
* * 退出登录
|
2021-12-18 16:36:43 +08:00
|
|
|
|
*/
|
2022-05-21 17:31:01 +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
|
|
|
|
}
|
2022-01-09 17:12:35 +08:00
|
|
|
|
|
2022-04-13 21:31:18 +08:00
|
|
|
|
/**
|
|
|
|
|
* * 判断是否是预览页
|
|
|
|
|
* @returns boolean
|
|
|
|
|
*/
|
2022-03-24 14:19:07 +08:00
|
|
|
|
export const isPreview = () => {
|
|
|
|
|
return document.location.hash.includes('preview')
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 17:12:35 +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 17:12:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-09 19:22:55 +08:00
|
|
|
|
* * 回到主页面
|
2022-01-09 17:12:35 +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)
|
2022-01-09 17:12:35 +08:00
|
|
|
|
}
|
2022-04-13 21:31:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* * 判断是否登录(现阶段是有 login 数据即可)
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
export const loginCheck = () => {
|
|
|
|
|
try {
|
2022-05-20 16:12:27 +08:00
|
|
|
|
const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
|
2022-04-13 21:31:18 +08:00
|
|
|
|
if (!info) return false
|
2022-05-20 16:12:27 +08:00
|
|
|
|
if (info[SystemStoreEnum.USER_INFO][SystemStoreUserInfoEnum.USER_TOKEN]) {
|
2022-04-13 21:31:18 +08:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|