Blame view

src/pages/Order/OrderWarning/components/UploadPayBillModal.tsx 7.4 KB
boyang authored
1
2
3
4
5
6
7
import { RESPONSE_CODE } from '@/constants/enum';
import {
  postServiceOrderFileProcess,
  postServiceOrderUploadPaymentReceipt,
} from '@/services';
import { transImageFile } from '@/utils';
import { PlusOutlined } from '@ant-design/icons';
zhongnanhuang authored
8
9
10
11
12
import { ModalForm } from '@ant-design/pro-components';
import { Form, Modal, Upload, UploadFile, UploadProps, message } from 'antd';
import { RcFile } from 'antd/lib/upload';
import { cloneDeep } from 'lodash';
import { useEffect, useRef, useState } from 'react';
boyang authored
13
import { COMFIR_RECEIPT_IMAGES_NUMBER } from '../../constant';
zhongnanhuang authored
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// import { cloneDeep } from 'lodash';
export default ({ setVisible, subOrders, mainOrder, onClose }) => {
  const [form] = Form.useForm();
  const [previewOpen, setPreviewOpen] = useState(false);
  const [previewImage, setPreviewImage] = useState('');
  const [previewTitle, setPreviewTitle] = useState('');
  const handleCancel = () => setPreviewOpen(false);
  const [fileList, setFileList] = useState<UploadFile[]>([]);
  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);
    });
boyang authored
31
32
33
  const subOrderIds = subOrders?.map((item: any) => {
    return item.id;
  });
zhongnanhuang authored
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
  const fileListObj = useRef<UploadFile[]>([]); //使用引用类型,使得在useEffect里面设置监听事件后,不用更新监听事件也能保持obj与外界一致
  const handleBeforeUpload = (file: any) => {
    setFileList([...fileList, file]);
    return false;
  };
  const uploadButton = (
    <div>
      <PlusOutlined />
      <div style={{ marginTop: 8 }}>上传凭证</div>
    </div>
  );
  /** 粘贴快捷键的回调 */
  const onPaste = async (e: any) => {
    /** 获取剪切板的数据clipboardData */
    let clipboardData = e.clipboardData,
      i = 0,
      items,
      item,
      types;

    /** 为空判断 */
    if (clipboardData) {
      items = clipboardData.items;
      if (!items) {
        message.info('您的剪贴板中没有照片');
        return;
      }

      item = items[0];
      types = clipboardData.types || [];
      /** 遍历剪切板的数据 */
      for (; i < types.length; i++) {
        if (types[i] === 'Files') {
          item = items[i];
          break;
        }
      }

      /** 判断文件是否为图片 */
      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,
        };

        if (filteredArray.length >= COMFIR_RECEIPT_IMAGES_NUMBER) {
          message.info('发货照片数量不能超过3');
          return;
        }
        fileListObj.current = filteredArray;
        filteredArray.push(listItem);
        setFileList(filteredArray);
        return;
      }
    }

    message.info('您的剪贴板中没有照片');
  };
  const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => {
    //fileListObj得在change里变化,change的参数是已经处理过的file数组
    //beforeUpload中的参数file是未处理过,还需要Base64拿到文件数据处理
    fileListObj.current = newFileList;
    setFileList(newFileList);
  };
  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 ||
boyang authored
113
114
        file.originFileObj?.name ||
        file.url!.substring(file.url!.lastIndexOf('/') + 1),
zhongnanhuang authored
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
    );
  };
  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',
    // action: '/api/service/order/fileProcess',
    name: 'files',
    headers: { Authorization: localStorage.getItem('token') },
  };

  useEffect(() => {
    document.addEventListener('paste', onPaste);
    return () => {
      document.removeEventListener('paste', onPaste);
    };
  }, []);
  return (
    <>
      <ModalForm<{
        filePaths: any;
      }>
        width={500}
        open
        title="回款凭证上传"
        form={form}
        autoFocusFirstInput
        modalProps={{
          okText: '提交',
          cancelText: '取消',
          destroyOnClose: true,
          onCancel: () => {
            setVisible(false);
          },
        }}
        onFinish={async () => {
          if (fileList.length <= 0) {
            message.error('请上传至少一张凭证');
            return;
          }
          message.open({
            type: 'loading',
            content: '正在上传凭证...',
            duration: 0,
          });
          //附件处理
          let formData = new FormData();
          //附件处理
          for (let file of fileList) {
            if (file.originFileObj) {
              formData.append('files', file.originFileObj as RcFile);
            } else {
              //有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,
                );
              }
            }
          }
          let res = await postServiceOrderFileProcess({
            data: formData,
          });
          message.destroy();
          if (res.result === RESPONSE_CODE.SUCCESS) {
            let fileUrls = res?.data?.map((item) => {
              return { url: item };
            });
            //财务审核
            const data = await postServiceOrderUploadPaymentReceipt({
              data: {
                subOrderIds: subOrderIds,
                filePaths: fileUrls,
              },
            });
            if (data.result === RESPONSE_CODE.SUCCESS) {
              message.success(data.message);
              onClose();
            }
          } else {
            message.success('上传失败');
boyang authored
213
            console.log(mainOrder);
zhongnanhuang authored
214
215
216
217
218
          }
          onClose();
        }}
        onOpenChange={setVisible}
      >
boyang authored
219
        <div className="pb-4 text-xs decoration-gray-50">可复制照片粘贴</div>
zhongnanhuang authored
220
        <Upload {...props}>
boyang authored
221
          {fileList.length < COMFIR_RECEIPT_IMAGES_NUMBER ? uploadButton : ''}
zhongnanhuang authored
222
223
224
225
226
227
228
229
230
231
232
233
234
235
        </Upload>
      </ModalForm>

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