Skip to content

Commit 5472945

Browse files
committed
refactor: improve tab instance handling and code formatting
1 parent 4bb1d9c commit 5472945

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/hooks/useTabSaveSuccess.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ type OptionsType = {
1414
export const useTabSaveSuccess = (code: string = '', options?: OptionsType) => {
1515
const id = ref('tab-save-success' + randomString(8))
1616
const menuStore = useMenuStore()
17-
let tabInstance: WindowProxy
17+
const tabInstance = ref<WindowProxy | null>(null)
1818

1919
function formatPath(str: string, obj: Record<string, string>) {
2020
return str.replace(/:([a-zA-Z_]\w*)/g, (_, key) => {
21-
return obj.hasOwnProperty(key) ? obj[key] : '';
22-
});
21+
return obj.hasOwnProperty(key) ? obj[key] : ''
22+
})
2323
}
2424

25-
const onOpen = (params: Record<string, any>, _options: {
26-
menuCode?: string,
27-
menuParams?: Record<string, any>
28-
} = {}): Promise<any> => {
25+
const onOpen = (
26+
params: Record<string, any>,
27+
_options: {
28+
menuCode?: string
29+
menuParams?: Record<string, any>
30+
} = {}
31+
): Promise<any> => {
2932
return new Promise((resolve, reject) => {
3033
const menuItem = menuStore.getMenu(_options.menuCode || code)
3134

@@ -34,30 +37,33 @@ export const useTabSaveSuccess = (code: string = '', options?: OptionsType) => {
3437
return
3538
}
3639

37-
const _params = new URLSearchParams({...params, sourceId: id.value})
40+
const _params = new URLSearchParams({ ...params, sourceId: id.value })
3841
const hash = location.hash ? '/#' : ''
39-
const path = _options.menuParams ? formatPath(menuItem.path, _options.menuParams) : menuItem.path
42+
const path = _options.menuParams ? formatPath(menuItem.path, _options.menuParams) : menuItem.path
4043
const url = [location.origin, hash, path, '?', _params.toString()].join('')
4144

42-
if (isSubApp) { // 微前端
43-
const globalData = (window as any).microApp.getGlobalData() as { api: Record<string, any>}
45+
if (isSubApp) {
46+
// 微前端
47+
const globalData = (window as any).microApp.getGlobalData() as { api: Record<string, any> }
4448
if (globalData?.api?.onTabSaveSuccess) {
4549
globalData.api.onTabSaveSuccess(id.value, url, options)
4650
}
4751
} else {
48-
tabInstance = window.open(url) as WindowProxy
49-
(tabInstance as any).onTabSaveSuccess = (_sourceId: string, value: any) => {
52+
tabInstance.value = window.open(url) as WindowProxy
53+
;(tabInstance.value as any).onTabSaveSuccess = (_sourceId: string, value: any) => {
5054
if (_sourceId === id.value) {
5155
options?.onSuccess?.(value)
5256
resolve(value)
5357
}
5458
}
59+
resolve(tabInstance.value)
5560
}
5661
})
5762
}
5863

5964
return {
6065
onOpen,
66+
tabInstance
6167
}
6268
}
6369

@@ -71,12 +77,12 @@ export const useTabSaveSuccessBack = () => {
7177
const isClose = options?.onBefore ? await options.onBefore() : true
7278
if (isClose) {
7379
if (isSubApp) {
74-
globalData = (window as any).microApp.getGlobalData() as { api: Record<string, any>}
80+
globalData = (window as any).microApp.getGlobalData() as { api: Record<string, any> }
7581
if (globalData?.api?.onTabSaveSuccessBack) {
7682
globalData.api.onTabSaveSuccessBack(sourceId, data)
7783
}
7884
} else if ((window as any).onTabSaveSuccess) {
79-
(window as any).onTabSaveSuccess(sourceId, data)
85+
;(window as any).onTabSaveSuccess(sourceId, data)
8086
setTimeout(() => window.close(), 300)
8187
}
8288
}

0 commit comments

Comments
 (0)