PayDialog.tsx
10.5 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { useEffect, useMemo, useState } from 'react';
import { useQuery, useMutation } from '@tanstack/react-query';
import {
UButton,
UCascader,
UForm,
UInput,
UModal,
URadio,
USelect,
UText,
useUMessage,
} from '@uj/ui';
import { withdrawApi, type PayChannelOption } from '@/api';
import { errorMessage } from '@/features/shared/format';
import { useChannelAdmins, usePayChannelTree } from '@/features/shared/options';
/** 身边云是按服务商(翔禾/雅钧/博晟/晨景)付款的渠道,老系统把它从渠道列表里单独摘出来。 */
const SHENBIANYUN = 'SHENBIANYUN';
export interface PayDialogProps {
open: boolean;
/** batch 按勾选的 id 付款;all 按当前查询条件一键付款。 */
scope: 'batch' | 'all';
ids: string[];
/** 一键付款要带上的查询条件(老系统的 tableSearchForm)。 */
filters: Record<string, unknown>;
/** 付款前已经校验过的合计金额(元)。 */
payMoney: string;
/** 所选记录的提现类型与方式:决定渠道候选走级联树还是渠道单选。 */
transferType?: string | number;
mode?: string | number;
onClose: () => void;
onSuccess: () => void;
}
/** 级联选中的四个 code:渠道 / 付款主体 / 代征主体 / 代征渠道(老系统 selectAddAttr 的顺序)。 */
interface Selection {
channel: string;
paymentEntityCode: string;
levyCode: string;
levyChannelCode: string;
channelProvider: string;
}
const EMPTY_SELECTION: Selection = {
channel: '',
paymentEntityCode: '',
levyCode: '',
levyChannelCode: '',
channelProvider: '',
};
/**
* 付款弹窗。老系统按提现类型分成两套渠道控件:
* 积分提现(0)与阳光激励(2)用渠道级联树,其余用渠道单选 + 身边云服务商单选;
* 验证码固定发给所选渠道的管理员,默认选中第一个。
*/
export function PayDialog({
open,
scope,
ids,
filters,
payMoney,
transferType,
mode,
onClose,
onSuccess,
}: PayDialogProps) {
const message = useUMessage();
const useTree = String(transferType) === '0' || String(transferType) === '2';
const [selection, setSelection] = useState<Selection>(EMPTY_SELECTION);
const [path, setPath] = useState<string[]>([]);
const [captcha, setCaptcha] = useState('');
const [remark, setRemark] = useState('');
const [phone, setPhone] = useState('');
const [seconds, setSeconds] = useState(0);
const tree = usePayChannelTree(
{ transferType: String(transferType ?? ''), mode: String(mode ?? '') },
open && useTree,
);
const channels = useQuery({
queryKey: ['points', 'pay-channels', String(transferType ?? ''), String(mode ?? '')],
queryFn: () => withdrawApi.payChannels({ transferType, mode }),
enabled: open && !useTree,
staleTime: 0,
});
const options = useMemo<PayChannelOption[]>(() => {
const list = channels.data ?? [];
return list.filter((item) => item.channel !== SHENBIANYUN);
}, [channels.data]);
const provider = useMemo(
() => (channels.data ?? []).find((item) => item.channel === SHENBIANYUN),
[channels.data],
);
const balance = useQuery({
queryKey: [
'points',
'channel-balance',
selection.channel,
selection.channelProvider,
selection.paymentEntityCode,
selection.levyCode,
selection.levyChannelCode,
],
queryFn: () => withdrawApi.balance({ ...selection }),
enabled: open && !!selection.channel,
staleTime: 0,
});
const admins = useChannelAdmins(selection.channel || undefined, open && !!selection.channel);
/** 渠道一变就换管理员,并作废已发的验证码(老系统 changeAttr 里干的三件事)。 */
const pickChannel = (channel: string, providerCode = '') => {
setSelection((current) => ({ ...current, channel, channelProvider: providerCode }));
setCaptcha('');
setSeconds(0);
};
useEffect(() => {
if (!options.length || selection.channel) return;
setSelection((current) => ({ ...current, channel: String(options[0].channel ?? '') }));
}, [options, selection.channel]);
useEffect(() => {
// 只有身边云一个渠道时,渠道与服务商都取默认值(老系统 payChannelList 为空的那个分支)。
if (!provider || options.length || selection.channel) return;
setSelection((current) => ({
...current,
channel: String(provider.channel ?? ''),
channelProvider: String(provider.channelProviderList?.[0]?.value ?? ''),
}));
}, [provider, options.length, selection.channel]);
useEffect(() => {
const admin = admins.admins[0];
if (admin) setPhone(String(admin.adminPhone ?? admin.adminId ?? ''));
}, [admins.admins]);
useEffect(() => {
if (seconds <= 0) return;
const timer = window.setInterval(
() => setSeconds((value) => (value <= 1 ? 0 : value - 1)),
1000,
);
return () => window.clearInterval(timer);
}, [seconds]);
const code = seconds > 0 ? `${seconds}s` : '获取验证码';
const sms = useMutation({
mutationFn: () => withdrawApi.sendAdminSms({ channel: selection.channel, phone }),
onSuccess: () => {
void message.success('短信验证码发送成功');
setSeconds(60);
},
onError: (error) => void message.error(errorMessage(error)),
});
const pay = useMutation({
mutationFn: () => {
if (!selection.channel) throw new Error('请选择付款渠道');
if (!phone) throw new Error('请选择管理员');
if (!captcha) throw new Error('请输入验证码');
return scope === 'all'
? withdrawApi.payAll({
...filters,
phone,
payChannel: selection.channel,
captcha,
remark,
levyChannelCode: selection.levyChannelCode,
payPaymentEntityCode: selection.paymentEntityCode,
payLevyCode: selection.levyCode,
payLevyChannelCode: selection.levyChannelCode,
})
: withdrawApi.payBatch({
ids,
phone,
channel: selection.channel,
captcha,
remark,
channelProvider: selection.channelProvider,
levyChannelCode: selection.levyChannelCode,
paymentEntityCode: selection.paymentEntityCode,
levyCode: selection.levyCode,
});
},
onSuccess: () => {
void message.success('付款成功');
onSuccess();
},
onError: (error) => void message.error(errorMessage(error)),
});
const close = () => {
setSelection(EMPTY_SELECTION);
setPath([]);
setCaptcha('');
setRemark('');
setPhone('');
setSeconds(0);
onClose();
};
return (
<UModal
title="付款"
open={open}
okText="确定"
confirmLoading={pay.isPending}
onCancel={close}
onOk={() => pay.mutate()}
>
<UForm layout="horizontal" labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
<UForm.Item label="付款总金额">
<UText strong>{payMoney || '0.00'}</UText>
<UText type="secondary"> 元</UText>
</UForm.Item>
<UForm.Item label="付款渠道" required>
{useTree ? (
<UCascader
value={path}
options={tree.cascader}
placeholder="请选择"
showSearch
allowClear
style={{ width: '100%' }}
onChange={(value) => {
const next = (value as unknown[]).map(String);
setPath(next);
const [channel = '', entity = '', levy = '', levyChannel = ''] = next;
setSelection({
channel,
paymentEntityCode: entity,
levyCode: levy,
levyChannelCode: levyChannel,
channelProvider: '',
});
setCaptcha('');
setSeconds(0);
}}
/>
) : (
<URadio.Group
value={selection.channel}
onChange={(event) => pickChannel(String(event.target.value))}
options={options.map((item) => ({
value: String(item.channel ?? ''),
label: String(item.channelName ?? item.channel ?? ''),
}))}
/>
)}
</UForm.Item>
{!useTree && provider?.channelProviderList?.length ? (
<UForm.Item label="服务商">
<URadio.Group
value={selection.channelProvider}
onChange={(event) =>
pickChannel(String(provider.channel ?? ''), String(event.target.value))
}
options={(provider.channelProviderList ?? []).map((item) => ({
value: String(item.value ?? ''),
label: String(item.text ?? item.value ?? ''),
}))}
/>
</UForm.Item>
) : null}
<UForm.Item label="可用余额">
<UText>
{balance.data === undefined || balance.data === '' ? '—' : String(balance.data)}
</UText>
{balance.data ? <UText type="secondary"> 元</UText> : null}
</UForm.Item>
<UForm.Item label="管理员" required>
<USelect
value={phone || undefined}
placeholder="请选择管理员"
style={{ width: '100%' }}
loading={admins.isLoading}
options={admins.admins.map((item) => ({
value: String(item.adminPhone ?? item.adminId ?? ''),
label: `${item.adminName ?? ''} ${item.adminPhone ?? ''}`.trim(),
}))}
onChange={(value) => {
setPhone(String(value ?? ''));
setCaptcha('');
setSeconds(0);
}}
/>
</UForm.Item>
<UForm.Item label="验证码" required>
<div className="uj-points-captcha">
<UInput
value={captcha}
placeholder="请输入"
maxLength={6}
onChange={(event) => setCaptcha(event.target.value)}
/>
<UButton
type="link"
disabled={seconds > 0 || !selection.channel || !phone}
loading={sms.isPending}
onClick={() => sms.mutate()}
>
{code}
</UButton>
</div>
</UForm.Item>
<UForm.Item label="备注">
<UInput.TextArea
rows={3}
maxLength={50}
showCount
value={remark}
placeholder="请输入备注"
onChange={(event) => setRemark(event.target.value)}
/>
</UForm.Item>
</UForm>
</UModal>
);
}