ImportDialog.tsx
5.48 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
import { useState } from 'react';
import { useMutation } from '@tanstack/react-query';
import { UAlert, UButton, UModal, UText, UUpload, useUMessage } from '@uj/ui';
import { withdrawApi, type WithdrawImportResult } from '@/api';
import { errorMessage } from '@/features/shared/format';
import { isExcel, readLegacyFile, sizeError } from '@/features/shared/files';
/** 老系统写死在模版下载地址上的那张表,字段顺序即后端解析顺序。 */
const TEMPLATE_URL = 'https://img01.glsx.com.cn/weapp/resource/saas/scrm/xlsx/提现导入模版.xlsx';
export interface ImportDialogProps {
open: boolean;
/** 导入成功后刷新列表(老系统在确认里 handleReset,等价于重查当前条件)。 */
onSuccess: () => void;
onClose: () => void;
}
interface State {
fileName: string;
payload: string;
result?: WithdrawImportResult;
}
const EMPTY_STATE: State = { fileName: '', payload: '' };
/**
* 提现导入:老系统四段状态(选文件 → 正在处理中 → 处理成功/失败)逐段平移。
*
* 有一处对不上:老页面在提交前还会用 xlsx 解析表头(从 A9 起)与 10 个固定列名比对,
* 不一致就提示「模版错误」。本仓库的依赖目录里没有 xlsx,装它要改工作区外的
* pnpm-workspace.yaml,因此这一步交给后端:文件不合规时导入接口会回 errorUrl,
* 弹窗按失败态展示可下载的失败模版。
*/
export function ImportDialog({ open, onSuccess, onClose }: ImportDialogProps) {
const message = useUMessage();
const [state, setState] = useState<State>(EMPTY_STATE);
const [phase, setPhase] = useState<'pick' | 'running' | 'done'>('pick');
const reset = () => {
setState(EMPTY_STATE);
setPhase('pick');
};
const close = () => {
reset();
onClose();
};
const upload = useMutation({
mutationFn: async (file: File) => {
const tooLarge = sizeError(file);
if (tooLarge) throw new Error(tooLarge);
if (!isExcel(file.name)) throw new Error('上传格式不正确');
const payload = await readLegacyFile(file);
if (!payload) throw new Error('文件读取失败,请重新选择');
setState({ fileName: file.name, payload });
return file.name;
},
onError: (error) => void message.error(errorMessage(error)),
});
const submit = useMutation({
mutationFn: async (payload: string) => {
setPhase('running');
const result = await withdrawApi.importXlsx(payload);
if (Number(result?.importStatue) === 0) {
await withdrawApi.confirmImport(String(result.batchNo ?? ''));
onSuccess();
}
return result;
},
onSuccess: () => setPhase('done'),
onError: (error) => {
setPhase('pick');
void message.error(errorMessage(error));
},
});
const failed = phase === 'done' && Number(submit.data?.importStatue) !== 0;
const succeeded = phase === 'done' && !failed;
return (
<UModal
title="提现导入"
open={open}
okText="确定"
confirmLoading={submit.isPending}
cancelButtonProps={{ style: { display: phase === 'pick' ? undefined : 'none' } }}
onCancel={close}
onOk={() => {
if (phase === 'done') {
close();
return;
}
if (!state.payload) {
void message.error('请先选择文件');
return;
}
submit.mutate(state.payload);
}}
>
{phase === 'pick' ? (
<>
<UText type="secondary" className="uj-points-dialog-label">
上传文件
</UText>
<div className="uj-points-import-row">
<UUpload
accept=".xls,.xlsx"
showUploadList={false}
maxCount={1}
disabled={upload.isPending}
beforeUpload={(file) => {
upload.mutate(file);
return false;
}}
>
<UButton loading={upload.isPending}>选择文件</UButton>
</UUpload>
<UText>{state.fileName || '未选择文件'}</UText>
<UButton type="link" href={TEMPLATE_URL} target="_blank" rel="opener">
下载模版
</UButton>
</div>
</>
) : (
<div className="uj-points-import-state">
{phase === 'running' ? (
<UAlert
type="info"
showIcon
title="正在处理中"
description="系统正在加急导入中,请耐心等待;导入过程中请不要退出当前操作,否则本次提现导入将无效。"
/>
) : succeeded ? (
<UAlert
type="success"
showIcon
title="处理成功"
description={`已自动为您导入 ${submit.data?.successCount ?? 0} 条提现记录,可返回列表查看。`}
/>
) : (
<UAlert
type="error"
showIcon
title="处理失败"
description={
<>
可点击
<UButton
type="link"
href={submit.data?.errorUrl}
target="_blank"
rel="opener"
disabled={!submit.data?.errorUrl}
>
下载失败模版
</UButton>
查看失败原因。
</>
}
/>
)}
</div>
)}
</UModal>
);
}