Blame view

src/pages/Order/components/CheckModal.tsx 13.2 KB
zhongnanhuang authored
1
import { RESPONSE_CODE } from '@/constants/enum';
zhongnanhuang authored
2
import {
zhongnanhuang authored
3
  postServiceOrderAfterSalesCheck,
zhongnanhuang authored
4
  postServiceOrderCheckOrder,
5
  postServiceOrderFileProcess,
zhongnanhuang authored
6
  postServiceOrderFinanceCheckOrder,
zhongnanhuang authored
7
  postServiceOrderQueryAfterSalesInfoSnapshot,
zhongnanhuang authored
8
} from '@/services';
sanmu authored
9
import { ModalForm, ProFormTextArea } from '@ant-design/pro-components';
zhongnanhuang authored
10
import { Button, Col, Form, Modal, Row, UploadFile, message } from 'antd';
11
import Upload, { RcFile, UploadProps } from 'antd/es/upload';
zhongnanhuang authored
12
import { useEffect, useRef, useState } from 'react';
zhongnanhuang authored
13
14
15
16
17
import {
  AFTE_SALES_PLAN_OPTIONS,
  CHECK_TYPE,
  COMFIR_RECEIPT_IMAGES_NUMBER,
} from '../constant';
18
// import { cloneDeep } from 'lodash';
zhongnanhuang authored
19
20
21
22
23
import {
  enumValueToLabel,
  getAliYunOSSFileNameFromUrl,
  transImageFile,
} from '@/utils';
24
import { PlusOutlined } from '@ant-design/icons';
zhongnanhuang authored
25
import { cloneDeep } from 'lodash';
zhongnanhuang authored
26
27
28
29
30
export default ({
  setCheckVisible,
  data,
  subOrders,
  orderCheckType,
zhongnanhuang authored
31
  openOrderDrawer,
zhongnanhuang authored
32
33
  onClose,
}) => {
34
35
36
37
38
39
40
41
42
43
44
45
46
  const [previewOpen, setPreviewOpen] = useState(false);
  const [previewImage, setPreviewImage] = useState('');
  const [previewTitle, setPreviewTitle] = useState('');
  const fileListObj = useRef<UploadFile[]>([]); //使用引用类型,使得在useEffect里面设置监听事件后,不用更新监听事件也能保持obj与外界一致
  const getBase64 = (file: RcFile): Promise<string> =>
    new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => resolve(reader.result as string);
      reader.onerror = (error) => reject(error);
    });
  const [fileList, setFileList] = useState<UploadFile[]>([]);
  const handleCancel = () => setPreviewOpen(false);
47
  const [messageApi, contextHolder] = message.useMessage();
zhongnanhuang authored
48
49
  const [form] = Form.useForm<{ name: string; company: string }>();
  let subOrderIds: any[] = subOrders.map((subOrder) => subOrder.id);
zhongnanhuang authored
50
  const [mainOrderId] = useState(data.id);
zhongnanhuang authored
51
zhongnanhuang authored
52
  const [afterSalesInfo, setAfterSalesInfo] = useState<any>();
zhongnanhuang authored
53
zhongnanhuang authored
54
55
56
57
58
59
60
61
62
63
  /**
   * 审核类型
   */
  function checkType(check: string) {
    if (orderCheckType === check) {
      return true;
    }
    return false;
  }
zhongnanhuang authored
64
65
66
67
  const getOrderAfterSalesInfo = async () => {
    let res = await postServiceOrderQueryAfterSalesInfoSnapshot({
      data: { subOrderIds: subOrderIds },
    });
zhongnanhuang authored
68
zhongnanhuang authored
69
70
    //附件
    let annex = res?.data[0]?.afterSalesAnnexList;
zhongnanhuang authored
71
zhongnanhuang authored
72
73
74
75
76
77
78
    let annexLinks = annex?.map((f) => {
      return (
        <Button type="link" key="key" href={f}>
          {getAliYunOSSFileNameFromUrl(f)}
        </Button>
      );
    });
zhongnanhuang authored
79
zhongnanhuang authored
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    setAfterSalesInfo(
      <div className="my-5">
        <Row gutter={[16, 24]}>
          <Col span={6}>
            <span className="text-[#333333]">售后方案</span>
          </Col>
          <Col span={18}>
            {enumValueToLabel(
              res?.data[0]?.afterSalesPlan,
              AFTE_SALES_PLAN_OPTIONS,
            )}
          </Col>
          <Col span={6}>
            <span className="className='text-[#333333]'">售后原因</span>
          </Col>
          <Col span={18}>{res?.data[0]?.afterSalesNotes}</Col>
          <Col span={6}>
            <span className="className='text-[#333333]'">附件</span>
          </Col>
          <Col span={18}>{annexLinks}</Col>
        </Row>
      </div>,
    );
  };
zhongnanhuang authored
104
zhongnanhuang authored
105
106
107
  useEffect(() => {
    getOrderAfterSalesInfo();
  }, []);
zhongnanhuang authored
108
109
110
111
112
113
114
115
  const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => {
    //fileListObj得在change里变化,change的参数是已经处理过的file数组
    //beforeUpload中的参数file是未处理过,还需要Base64拿到文件数据处理
    fileListObj.current = newFileList;
    setFileList(newFileList);
  };
zhongnanhuang authored
116
117
118
119
120
121
122
123
  /** 粘贴快捷键的回调 */
  const onPaste = async (e: any) => {
    /** 获取剪切板的数据clipboardData */
    let clipboardData = e.clipboardData,
      i = 0,
      items,
      item,
      types;
124
zhongnanhuang authored
125
126
127
128
129
130
131
    /** 为空判断 */
    if (clipboardData) {
      items = clipboardData.items;
      if (!items) {
        message.info('您的剪贴板中没有照片');
        return;
      }
132
zhongnanhuang authored
133
134
135
136
137
138
139
140
141
      item = items[0];
      types = clipboardData.types || [];
      /** 遍历剪切板的数据 */
      for (; i < types.length; i++) {
        if (types[i] === 'Files') {
          item = items[i];
          break;
        }
      }
142
zhongnanhuang authored
143
144
145
146
147
148
149
150
151
152
153
154
155
      /** 判断文件是否为图片 */
      if (item && item.kind === 'file' && item.type.match(/^image\//i)) {
        const imgItem = item.getAsFile();
        const newFileList = cloneDeep(fileListObj.current);
        let filteredArray = newFileList.filter(
          (obj) => obj.status !== 'removed',
        ); //过滤掉状态为已删除的照片
        const listItem = {
          ...imgItem,
          status: 'done',
          url: await getBase64(imgItem),
          originFileObj: imgItem,
        };
156
zhongnanhuang authored
157
158
159
160
161
162
163
164
165
166
        if (filteredArray.length >= COMFIR_RECEIPT_IMAGES_NUMBER) {
          message.info('发货照片数量不能超过3');
          return;
        }
        fileListObj.current = filteredArray;
        filteredArray.push(listItem);
        setFileList(filteredArray);
        return;
      }
    }
167
zhongnanhuang authored
168
169
170
    message.info('您的剪贴板中没有照片');
  };
  useEffect(() => {
zhongnanhuang authored
171
    //回显售后信息
zhongnanhuang authored
172
173
174
    // if (checkType(CHECK_TYPE.AFTER_SALES)) {
    //   getOrderAfterSalesInfo();
    // }
zhongnanhuang authored
175
zhongnanhuang authored
176
177
178
179
180
    document.addEventListener('paste', onPaste);
    return () => {
      document.removeEventListener('paste', onPaste);
    };
  }, []);
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  const uploadButton = (
    <div>
      <PlusOutlined />
      <div style={{ marginTop: 8 }}>上传凭证</div>
    </div>
  );
  const handlePreview = async (file: UploadFile) => {
    if (!file.url && !file.preview) {
      file.preview = await getBase64(file.originFileObj as RcFile);
    }
    setPreviewImage(file.url || (file.preview as string));
    setPreviewOpen(true);
    setPreviewTitle(
      file.name ||
        file.originFileObj?.name ||
        file.url!.substring(file.url!.lastIndexOf('/') + 1),
    );
  };

  const handleBeforeUpload = (file: any) => {
    setFileList([...fileList, file]);
202
    return false;
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  };

  const props: UploadProps = {
    onRemove: (file) => {
      const index = fileList.indexOf(file);
      const newFileList = fileList.slice();
      newFileList.splice(index, 1);
      setFileList(newFileList);
    },
    beforeUpload: handleBeforeUpload,
    listType: 'picture-card',
    onPreview: handlePreview,
    fileList,
    onChange: handleChange,
    accept: 'image/png, image/jpeg, image/png',
218
    // action: '/api/service/order/fileProcess',
219
220
221
222
    name: 'files',
    headers: { Authorization: localStorage.getItem('token') },
  };
223
  async function doCheck(body: object) {
zhongnanhuang authored
224
    const data = await postServiceOrderCheckOrder({
225
226
      data: body,
    });
zhongnanhuang authored
227
228
    if (data.result === RESPONSE_CODE.SUCCESS) {
      message.success(data.message);
zhongnanhuang authored
229
      onClose();
230
231
    }
  }
zhongnanhuang authored
232
233
234
235
236

  /**
   *
   * @param body 财务审核
   */
237
  async function doFinancailCheck(values: any, isAgree: boolean) {
zhongnanhuang authored
238
239
240
241
    if (fileList.length <= 0) {
      message.error('凭证不能为空');
      return;
    }
242
243
244
245
246
247
248
249
250
    messageApi.open({
      type: 'loading',
      content: '正在上传图片...',
      duration: 0,
    });
    //附件处理
    let formData = new FormData();
    //附件处理
    for (let file of fileList) {
251
252
      if (file.originFileObj) {
        formData.append('files', file.originFileObj as RcFile);
253
      } else {
254
255
256
257
258
259
260
261
262
263
264
265
266
267
        //有url的话取url(源文件),没url取thumbUrl。有url的时候thumbUrl是略缩图
        if (file?.url === undefined || file?.url === null) {
          formData.append(
            'files',
            transImageFile(file?.thumbUrl),
            file?.originFileObj?.name,
          );
        } else {
          formData.append(
            'files',
            transImageFile(file?.url),
            file?.originFileObj?.name,
          );
        }
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
      }
    }
    let res = await postServiceOrderFileProcess({
      data: formData,
    });
    messageApi.destroy();
    if (res.result === RESPONSE_CODE.SUCCESS) {
      message.success('上传成功!');

      let fileUrls = res?.data?.map((item) => {
        return { url: item };
      });
      //财务审核
      const data = await postServiceOrderFinanceCheckOrder({
        data: {
          checkNotes: values.name,
          ids: subOrderIds,
          checkPassOrReject: isAgree,
          invoicingCheckAnnex: fileUrls,
        },
      });
      if (data.result === RESPONSE_CODE.SUCCESS) {
        message.success(data.message);
        onClose();
      }
    } else {
      message.success('上传失败');
    }
  }

  /**
   *
   * @param body 售后审核
   */
  async function doAfterSalesCheck(body: object) {
zhongnanhuang authored
303
    const data = await postServiceOrderAfterSalesCheck({
zhongnanhuang authored
304
305
306
307
308
309
310
311
      data: body,
    });
    if (data.result === RESPONSE_CODE.SUCCESS) {
      message.success(data.message);
      onClose();
    }
  }
sanmu authored
312
  return (
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    <>
      <ModalForm<{
        name: string;
        company: string;
      }>
        width={500}
        open
        title="审核"
        form={form}
        autoFocusFirstInput
        modalProps={{
          okText: '通过',
          cancelText: '驳回',
          destroyOnClose: true,
          onCancel: () => {
            setCheckVisible(false);
          },
        }}
        submitter={{
          render: (props, defaultDoms) => {
            let myDoms = [];
zhongnanhuang authored
334
335
            myDoms.push(
              <Button
336
                key="驳回"
337
                onClick={async () => {
338
339
340
341
342
343
344
345
346
347
                  if (checkType(CHECK_TYPE.NORMAL)) {
                    doCheck({
                      flag: false,
                      ids: subOrderIds,
                      externalProcurement: 0,
                      checkNotes: form.getFieldValue('name'),
                    });
                    return;
                  }
348
349
                  if (checkType(CHECK_TYPE.AFTER_SALES)) {
                    doAfterSalesCheck({
zhongnanhuang authored
350
351
                      isAfterSalesSuccess: false,
                      subOrderIds: subOrderIds,
zhongnanhuang authored
352
                      mainId: mainOrderId,
zhongnanhuang authored
353
                      afterSalesRejectionNotes: form.getFieldValue('name'),
354
355
356
357
358
359
360
361
                    });
                    return;
                  }

                  if (checkType(CHECK_TYPE.FINALCIAL)) {
                    let values = { name: form.getFieldValue('name') };
                    doFinancailCheck(values, false);
                  }
zhongnanhuang authored
362
363
                }}
              >
364
                驳回
zhongnanhuang authored
365
366
              </Button>,
            );
367
368
            //如果是仓库审核,那么显示这个外部采购
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
            if (checkType(CHECK_TYPE.NORMAL)) {
              myDoms.push(
                <Button
                  key="外部采购"
                  onClick={() => {
                    doCheck({
                      flag: false,
                      ids: subOrderIds,
                      externalProcurement: 1,
                      checkNotes: form.getFieldValue('name'),
                    });
                  }}
                >
                  外部采购
                </Button>,
              );
            }

            //确认
            myDoms.push(defaultDoms[1]);
            return myDoms;
          },
        }}
        submitTimeout={2000}
        onFinish={async (values) => {
          if (checkType(CHECK_TYPE.NORMAL)) {
zhongnanhuang authored
395
            //审核通过mainOrderId
396
397
398
399
400
401
            return doCheck({
              flag: true,
              ids: subOrderIds,
              externalProcurement: 0,
              checkNotes: values.name,
            });
zhongnanhuang authored
402
403
          }
404
405
406
          if (checkType(CHECK_TYPE.AFTER_SALES)) {
            //审核通过
            return doAfterSalesCheck({
zhongnanhuang authored
407
408
              isAfterSalesSuccess: true,
              subOrderIds: subOrderIds,
zhongnanhuang authored
409
              mainId: mainOrderId,
zhongnanhuang authored
410
              afterSalesRejectionNotes: values.name,
411
412
            });
          }
zhongnanhuang authored
413
414
415
416
          if (checkType(CHECK_TYPE.FINALCIAL)) {
            doFinancailCheck(values, true);
          }
417
418
419
        }}
        onOpenChange={setCheckVisible}
      >
zhongnanhuang authored
420
        {checkType(CHECK_TYPE.AFTER_SALES) ? (
zhongnanhuang authored
421
422
423
424
425
426
427
428
429
430
431
432
433
          <>
            {afterSalesInfo}
            <Button
              className="px-0"
              type="link"
              onClick={() => {
                console.log(data);
                openOrderDrawer('after-sales-check', mainOrderId);
              }}
            >
              查看旧订单
            </Button>
          </>
zhongnanhuang authored
434
435
436
        ) : (
          ''
        )}
zhongnanhuang authored
437
438
439
440
441
442
443
444
445
        <div>请特别注意订单总金额与订单金额。</div>
        <ProFormTextArea
          width="lg"
          name="name"
          placeholder="若驳回,请填写驳回理由"
        />
        {checkType(CHECK_TYPE.FINALCIAL) ? (
          <>
zhongnanhuang authored
446
447
448
            <div className="pb-4 text-xs decoration-gray-50">
              可复制照片粘贴
            </div>
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
            <Upload {...props}>
              {fileList.length < COMFIR_RECEIPT_IMAGES_NUMBER
                ? uploadButton
                : ''}
            </Upload>
          </>
        ) : (
          ''
        )}
      </ModalForm>

      <Modal
        open={previewOpen}
        title={previewTitle}
        footer={null}
        onCancel={handleCancel}
      >
        <img alt="图片预览" style={{ width: '100%' }} src={previewImage} />
      </Modal>
468
469

      {contextHolder}
470
    </>
sanmu authored
471
472
  );
};