| 1 | import React, { ForwardedRef, forwardRef, memo, useRef, useState } from 'react'; |
| 2 | import styles from './updateLog.module.scss'; |
| 3 | import { Form, Input, message, Modal } from 'antd'; |
| 4 | import { CreatefeedbackParmas, operateApi } from '../../api/feedback'; |
| 5 | import Uploadimages, { IUploadimagesRef } from '../../components/UploadImages'; |
| 6 | import { ipcGetLogFlies } from '../../icp/tools'; |
| 7 | import { icpGetFileStream } from '../../icp/view'; |
| 8 | import { toolsApi } from '../../api/tools'; |
| 9 | |
| 10 | const FILE_BASE_URL = import.meta.env.VITE_APP_FILE_HOST; |
| 11 | |
| 12 | export interface IUpdatelogRef {} |
| 13 | |
| 14 | export interface IUpdatelogProps {} |
| 15 | |
| 16 | const { TextArea } = Input; |
| 17 | |
| 18 | const Updatelog = memo( |
| 19 | forwardRef(({}: IUpdatelogProps, ref: ForwardedRef<IUpdatelogRef>) => { |
| 20 | const [open, setOpen] = useState(false); |
| 21 | const [form] = Form.useForm(); |
| 22 | const [imgList, setImgList] = useState<string[]>([]); |
| 23 | const [loading, setLoading] = useState(false); |
| 24 | const uploadimagesRef = useRef<IUploadimagesRef>(null); |
| 25 | |
| 26 | return ( |
| 27 | <> |
| 28 | <Modal |
| 29 | open={open} |
| 30 | title="意见反馈" |
| 31 | width={600} |
| 32 | onCancel={() => setOpen(false)} |
| 33 | onOk={async () => { |
| 34 | await form.validateFields(); |
| 35 | setLoading(true); |
| 36 | let filelogs = await ipcGetLogFlies(); |
| 37 | // 只需要最近三天的日志日志 |
| 38 | filelogs = filelogs.slice(-3); |
| 39 | let fileList: string[] = []; |
| 40 | for (const filelog of filelogs) { |
| 41 | const buffer = await icpGetFileStream(filelog); |
| 42 | const blob = new Blob([buffer], { |
| 43 | type: `text/plain`, |
| 44 | }); |
| 45 | const res = await toolsApi.uploadFile(blob); |
| 46 | fileList = fileList.concat([`${FILE_BASE_URL}${res.name}`]); |
| 47 | } |
| 48 | const feddbackRes = await operateApi |
| 49 | .createfeedback({ |
| 50 | ...form.getFieldsValue(), |
| 51 | imgUrlList: imgList, |
| 52 | fileUrlList: fileList, |
| 53 | }) |
| 54 | .catch(() => false); |
| 55 | setLoading(false); |
| 56 | if (!feddbackRes) return; |
| 57 | |
| 58 | setOpen(false); |
| 59 | message.success('反馈成功,感谢您的反馈!'); |
| 60 | uploadimagesRef.current?.clear(); |
| 61 | form.resetFields(); |
| 62 | }} |
| 63 | confirmLoading={loading} |
| 64 | > |
| 65 | <Form |
| 66 | form={form} |
| 67 | labelCol={{ span: 4 }} |
| 68 | autoComplete="off" |
| 69 | style={{ marginTop: '20px' }} |
| 70 | > |
| 71 | <Form.Item<CreatefeedbackParmas> |
| 72 | label="反馈内容:" |
| 73 | name="content" |
| 74 | rules={[{ required: true, message: '请输入反馈内容!' }]} |
| 75 | > |
| 76 | <TextArea |
| 77 | placeholder="请详细描述您的反馈内容,它将成为我们改进产品的最大动力!" |
| 78 | maxLength={1000} |
| 79 | style={{ height: 150 }} |
| 80 | /> |
| 81 | </Form.Item> |
| 82 | <Form.Item<CreatefeedbackParmas> label="上传图片:"> |
| 83 | <Uploadimages |
| 84 | maxCount={4} |
| 85 | ref={uploadimagesRef} |
| 86 | onUploadChange={(fileList) => { |
| 87 | setImgList(fileList.map((v) => v.url) as string[]); |
| 88 | }} |
| 89 | /> |
| 90 | </Form.Item> |
| 91 | </Form> |
| 92 | </Modal> |
| 93 | <div |
| 94 | className={styles.updateLog} |
| 95 | onClick={() => { |
| 96 | setLoading(false); |
| 97 | setOpen(true); |
| 98 | }} |
| 99 | > |
| 100 | 意见反馈 |
| 101 | </div> |
| 102 | </> |
| 103 | ); |
| 104 | }), |
| 105 | ); |
| 106 | Updatelog.displayName = 'Updatelog'; |
| 107 | |
| 108 | export default Updatelog; |
| 109 |