router.ts
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { useRoute } from 'vue-router'
import { ResultEnum } from '@/enums/httpEnum'
import { ErrorPageNameMap, PageEnum } from '@/enums/pageEnum'
import { docPath, giteeSourceCodePath } from '@/settings/pathConst'
import { cryptoDecode } from './crypto'
import { StorageEnum } from '@/enums/storageEnum'
import { clearLocalStorage, getLocalStorage } from './storage'
import router from '@/router'
/**
* * 根据名字跳转路由
* @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 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 = () => {
clearLocalStorage(StorageEnum.GO_LOGIN_INFO_STORE)
routerTurnByName(PageEnum.BASE_LOGIN_NAME)
}
/**
* * 新开页面
* @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.hash.includes('preview')
}
/**
* * 获取当前路由下的参数
* @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)
}
/**
* * 判断是否登录(现阶段是有 login 数据即可)
* @return boolean
*/
export const loginCheck = () => {
try {
const info = getLocalStorage(StorageEnum.GO_LOGIN_INFO_STORE)
if (!info) return false
const decodeInfo = cryptoDecode(info)
if (decodeInfo) {
return true
}
return false
} catch (error) {
return false
}
}