Blame view

src/pages/Order/OrderWarning/components/ImportModal.tsx 3.59 KB
zhongnanhuang authored
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
import { RESPONSE_CODE } from '@/constants/enum';
import { postServiceOrderImportExcel } from '@/services';
import { orderExport } from '@/services/order';
import { UploadOutlined } from '@ant-design/icons';
import { Button, Modal, Upload, message } from 'antd';
import { RcFile, UploadFile, UploadProps } from 'antd/es/upload';
import { useState } from 'react';
export default ({ onClose }) => {
  // const [form] = Form.useForm<{ name: string; company: string }>();
  const [messageApi, contextHolder] = message.useMessage();
  const [fileList, setFileList] = useState<UploadFile[]>([]);
  const [uploading, setUploading] = useState(false);
  const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) =>
    setFileList(newFileList);

  const exportLoading = (content: string) => {
    messageApi.open({
      type: 'loading',
      content: content,
      duration: 0,
    });
  };

  const exportLoadingDestory = () => {
    messageApi.destroy();
  };
  const downloadTemplate = async () => {
    exportLoading('正在下载模板...');
29
30
31
32
33
34
35
    orderExport(
      '/api/service/order/exportTemplate',
      '订单.xlsx',
      'post',
      {},
      exportLoadingDestory,
    );
zhongnanhuang authored
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
  };

  const handleUpload = async () => {
    const formData = new FormData();
    fileList.forEach((file) => {
      //originFileObj二进制文件
      formData.append('file', file.originFileObj as RcFile);
    });
    // console.log(fileList[0] as RcFile)
    // formData.append('file', fileList[0] as RcFile);
    setUploading(true);
    // You can use any AJAX library you like
    const res = await postServiceOrderImportExcel({
      data: formData,
      headers: {
        'Content-Type':
          'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq',
      },
    });

    if (res.result === RESPONSE_CODE.SUCCESS) {
      message.success(res.message);
      onClose();
    } else {
      if (res.message === '表格中没有数据') {
        setUploading(false);
        return;
      }
      //存在错误信息,下载错误信息模板
      exportLoading('正在下载错误信息...');
      orderExport(
        '/api/service/order/errorExcelInformation',
PurelzMgnead authored
68
69
        '订单.xlsx',
        'post',
zhongnanhuang authored
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
        formData,
        exportLoadingDestory,
      );
    }

    setUploading(false);
  };

  const props: UploadProps = {
    onRemove: (file) => {
      const index = fileList.indexOf(file);
      const newFileList = fileList.slice();
      newFileList.splice(index, 1);
      setFileList(newFileList);
    },
    beforeUpload: (file) => {
      setFileList([...fileList, file]);

      return false;
    },
    fileList,
    onChange: handleChange,
    accept: '.xlsx',
  };

  return (
    <>
      <Modal
        width={500}
        open
zhongnanhuang authored
100
        title="批量发货"
zhongnanhuang authored
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
        footer={[
          <Button key="cancel" onClick={onClose}>
            取消
          </Button>,
          <Button
            type="primary"
            key="ok"
            onClick={handleUpload}
            disabled={fileList.length === 0}
            loading={uploading}
          >
            {uploading ? '上传中' : '提交'}
          </Button>,
        ]}
        onCancel={async () => {
          onClose();
        }}
      >
        <div className="py-4 font-semibold">
          导入发货信息
          <Button type="link" onClick={downloadTemplate}>
            下载模板
          </Button>
        </div>
        <Upload {...props}>
          <Button icon={<UploadOutlined />} disabled={fileList.length > 0}>
            点击选择文件
          </Button>
        </Upload>
      </Modal>
      {contextHolder}
    </>
  );
};