downloadCenter.ts
3.27 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
import { subappBus } from './subapp';
import {
DOWNLOAD_CENTER_REGISTER_EVENT,
DOWNLOAD_CENTER_CANCEL_EVENT,
type AsyncExportPayload,
type Base64Payload,
type BlobPayload,
type CommonDownloadInput,
type DownloadKind,
type RegisterDownloadInput,
type RegisterExportTaskInput,
type StreamPayload,
type UrlPayload,
} from '@uj/contracts';
/** 各便捷方法的入参 = 公共字段 + 该 kind 的 payload,业务侧拿到完整类型提示。 */
export type AsyncExportInput = CommonDownloadInput & AsyncExportPayload;
export type StreamInput = CommonDownloadInput & StreamPayload;
export type UrlInput = CommonDownloadInput & UrlPayload;
export type BlobInput = CommonDownloadInput & BlobPayload;
export type Base64Input = CommonDownloadInput & Base64Payload;
const COMMON_KEYS = [
'source',
'sourceLabel',
'label',
'fileName',
'autoDownload',
'silent',
'dedupeKey',
'channel',
] as const;
function split(input: object): {
common: CommonDownloadInput;
payload: Record<string, unknown>;
} {
const common: Record<string, unknown> = {};
const payload: Record<string, unknown> = {};
Object.entries(input).forEach(([key, value]) => {
if ((COMMON_KEYS as readonly string[]).includes(key)) common[key] = value;
else payload[key] = value;
});
return { common: common as unknown as CommonDownloadInput, payload };
}
function emit(kind: DownloadKind, input: object): void {
const { common, payload } = split(input);
const task: RegisterDownloadInput = { ...common, kind, payload };
subappBus()?.$emit(DOWNLOAD_CENTER_REGISTER_EVENT, task);
}
/**
* 子应用侧全系统下载入口。
*
* 业务页只「登记任务」,真正的请求 / 轮询 / 通知 / 面板 / 历史全在主壳下载中心。
* 请求因此统一走主壳 props.request(加签 / 验签 / 401 统一登出),不绕过任何拦截器。
*
* 后端零改动:asyncExport 的 method 即业务导出 method 串,会被包进 exportParam.method。
*/
export const downloadCenter = {
/** 统一登记(显式 kind);兼容旧写法:不带 kind 但带 method 时默认 async-export。 */
register(input: RegisterDownloadInput | RegisterExportTaskInput): void {
if ('kind' in input) {
const { kind, payload, ...common } = input;
const extra = payload && typeof payload === 'object' ? payload : {};
emit(kind, { ...common, ...extra });
} else {
emit('async-export', input);
}
},
/** 异步导出任务:提交 uuid -> 轮询 -> 通知 -> 自动下载。 */
asyncExport(input: AsyncExportInput): void {
emit('async-export', input);
},
/** 同源、需主壳签名的文件流下载(绝不能用 <a href> 直开)。 */
stream(input: StreamInput): void {
emit('stream', input);
},
/** 自带鉴权的直链 / OSS 预签名 URL。 */
url(input: UrlInput): void {
emit('url', input);
},
/** 前端本地 Blob。Blob 经 wujie bus 是同源引用传递;主壳页面建议直接 registerDownload。 */
blob(input: BlobInput): void {
emit('blob', input);
},
/** base64 / data URI。同上,主壳页面建议直接 registerDownload。 */
base64(input: Base64Input): void {
emit('base64', input);
},
cancel(taskId: string): void {
subappBus()?.$emit(DOWNLOAD_CENTER_CANCEL_EVENT, taskId);
},
};