提交 | 用户 | age
|
215d8b
|
1 |
import type { RouteLocationRaw, Router } from 'vue-router'; |
2f6253
|
2 |
|
bab28a
|
3 |
import { PageEnum } from '@/enums/pageEnum'; |
2f6253
|
4 |
import { unref } from 'vue'; |
c303ec
|
5 |
|
215d8b
|
6 |
import { useRouter } from 'vue-router'; |
bab28a
|
7 |
import { REDIRECT_NAME } from '@/router/constant'; |
895352
|
8 |
import { isHttpUrl } from '@/utils/is'; |
J |
9 |
import { openWindow } from '@/utils'; |
2f6253
|
10 |
|
1e34d3
|
11 |
import { useMultipleTabStore } from '@/store/modules/multipleTab'; |
X |
12 |
|
02c469
|
13 |
export type PathAsPageEnum<T> = T extends { path: string } ? T & { path: PageEnum } : T; |
T |
14 |
export type RouteLocationRawEx = PathAsPageEnum<RouteLocationRaw>; |
2f6253
|
15 |
|
陈 |
16 |
function handleError(e: Error) { |
|
17 |
console.error(e); |
|
18 |
} |
|
19 |
|
1e34d3
|
20 |
export enum GoType { |
X |
21 |
'replace', |
|
22 |
'after', |
|
23 |
} |
|
24 |
|
02c469
|
25 |
/** |
T |
26 |
* page switch |
|
27 |
*/ |
215d8b
|
28 |
export function useGo(_router?: Router) { |
1e34d3
|
29 |
const { push, replace, currentRoute } = _router || useRouter(); |
X |
30 |
|
|
31 |
function go(opt?: RouteLocationRawEx): void; |
|
32 |
function go(opt: RouteLocationRawEx, isReplace: boolean): void; |
|
33 |
function go(opt: RouteLocationRawEx, goType: GoType): void; |
|
34 |
function go( |
|
35 |
opt: RouteLocationRawEx = PageEnum.BASE_HOME, |
|
36 |
goTypeOrIsReplace: boolean | GoType = false, |
|
37 |
) { |
215d8b
|
38 |
if (!opt) { |
V |
39 |
return; |
|
40 |
} |
895352
|
41 |
let path = unref(opt) as string; |
J |
42 |
if (path[0] === '/') { |
|
43 |
path = path.slice(1); |
|
44 |
} |
|
45 |
if (isHttpUrl(path)) { |
|
46 |
return openWindow(path); |
|
47 |
} |
1e34d3
|
48 |
|
X |
49 |
const isReplace = goTypeOrIsReplace === true || goTypeOrIsReplace === GoType.replace; |
|
50 |
const isAfter = goTypeOrIsReplace === GoType.after; |
|
51 |
|
|
52 |
if (isReplace) { |
|
53 |
replace(opt).catch(handleError); |
|
54 |
} else if (isAfter) { |
|
55 |
const tabStore = useMultipleTabStore(); |
|
56 |
const currentName = unref(currentRoute).name; |
|
57 |
// 当前 tab |
|
58 |
const currentIndex = tabStore.getTabList.findIndex((item) => item.name === currentName); |
|
59 |
// 当前 tab 数量 |
|
60 |
const currentCount = tabStore.getTabList.length; |
|
61 |
push(opt) |
|
62 |
.then(() => { |
|
63 |
if (tabStore.getTabList.length > currentCount) { |
|
64 |
// 产生新 tab |
|
65 |
// 新 tab(也是最后一个) |
|
66 |
const targetIndex = tabStore.getTabList.length - 1; |
|
67 |
// 新 tab 在 当前 tab 的后面 |
|
68 |
if (currentIndex > -1 && targetIndex > currentIndex) { |
|
69 |
// 移动 tab |
|
70 |
tabStore.sortTabs(targetIndex, currentIndex + 1); |
|
71 |
} |
|
72 |
} |
|
73 |
}) |
|
74 |
.catch(handleError); |
|
75 |
} else { |
|
76 |
push(opt).catch(handleError); |
|
77 |
} |
2f6253
|
78 |
} |
陈 |
79 |
return go; |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* @description: redo current page |
|
84 |
*/ |
215d8b
|
85 |
export const useRedo = (_router?: Router) => { |
583304
|
86 |
const { replace, currentRoute } = _router || useRouter(); |
2dd3d8
|
87 |
const { query, params = {}, name, fullPath } = unref(currentRoute.value); |
a65ad9
|
88 |
function redo(): Promise<boolean> { |
V |
89 |
return new Promise((resolve) => { |
123597
|
90 |
if (name === REDIRECT_NAME) { |
无 |
91 |
resolve(false); |
|
92 |
return; |
|
93 |
} |
2dd3d8
|
94 |
if (name && Object.keys(params).length > 0) { |
afacf6
|
95 |
params['_origin_params'] = JSON.stringify(params ?? {}); |
2dd3d8
|
96 |
params['_redirect_type'] = 'name'; |
无 |
97 |
params['path'] = String(name); |
|
98 |
} else { |
|
99 |
params['_redirect_type'] = 'path'; |
|
100 |
params['path'] = fullPath; |
|
101 |
} |
583304
|
102 |
replace({ name: REDIRECT_NAME, params, query }).then(() => resolve(true)); |
2f6253
|
103 |
}); |
陈 |
104 |
} |
|
105 |
return redo; |
|
106 |
}; |