forked from gzzfw/frontEnd/gzDyh

liuwh
2024-09-10 4e3b493f07b4bc7f325a4b8ebc91704ec8e902a9
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
321
322
323
324
325
/*
 * @Company: hugeInfo
 * @Author: ldh
 * @Date: 2022-03-11 11:03:44
 * @LastEditTime: 2022-11-08 14:26:02
 * @LastEditors: ldh
 * @Version: 1.0.0
 * @Description: 材料附件查看
 * 附件类型对照
    NULL("22_00017-0","未分类"),
    AUDIO("22_00017-1", "音频"),
    VIDEO("22_00017-2", "视频"),
    IMAGE("22_00017-3", "图片"),
    WORD("22_00017-4", "Word文档"),
    EXCEL("22_00017-5", "Excel表格"),
    PDF("22_00017-6", "PDF"),
    TXT("22_00017-7", "txt文本"),
    ZIP("22_00017--8", "压缩文件"),
    POWERPOINT("22_00017-9", "PowerPoint"),
    UNKNOWN("22_00017-99", "其它文件");
 */
import React, { useState, useEffect, useRef } from 'react';
import { Button, Menu, Tooltip } from 'antd';
import { useSearchParams } from 'react-router-dom';
import {
    FolderOpenOutlined,
    FolderOutlined,
    FileOutlined,
    FileImageOutlined,
    FilePdfOutlined,
    FileWordOutlined,
    FileExcelOutlined,
    RotateRightOutlined,
    RotateLeftOutlined,
    DownloadOutlined,
    LeftOutlined,
    RightOutlined,
} from '@ant-design/icons';
import * as $$ from '../../utils/utility';
 
const { SubMenu } = Menu;
 
// 获取附件
function getFileListDataApi(submitData) {
    return $$.ax.request({ url: `caseInfo/listCaseFile?caseId=${submitData}`, type: 'get', service: 'mediate' });
}
 
const FilesCheck = ({ caseId }) => {
    let appUrl = $$.appUrl;
 
    const [searchParams] = useSearchParams();
 
    const fileId = searchParams.get('fileId');
 
    const [data, setData] = useState([]);
 
    const [openKeys, setOpenKeys] = useState([]);
 
    // files数组
    const [files, setFiles] = useState([{}]);
 
    // 当前点击的file的index
    const [fileIndex, setFileIndex] = useState(0);
 
    const imgBgRef = useRef();
 
    const imgRef = useRef();
 
    // 图片旋转
    function handleRotateImg(type) {
        if (!imgRef || !imgRef.current) {
            return false;
        }
        let transform = imgRef.current.style.transform,
            reg = /(-)?[0-9][0-9]*/g,
            arr = transform.split('rotate'),
            rotate = Number(transform.match(reg)[4] || 0),
            num = 0;
        if (type === 'right') {
            num = rotate + 90;
        } else {
            num = rotate + -90;
        }
        imgRef.current.style.transform = `${arr[0]}rotate(${num}deg)`;
    }
 
    // 监听图片滚动事件放大,缩小
    function imgScrollFunc(e) {
        if (!imgRef || !imgRef.current) {
            return false;
        }
        let transform = imgRef.current.style.transform,
            reg = /\((.+?)\)/g,
            scale3d = transform.match(reg)[0],
            scale3dNum = scale3d.substring(1, 2);
        let num = Number(scale3dNum);
        if (e.wheelDelta) {
            //判断浏览器IE,谷歌滑轮事件
            if (e.wheelDelta > 0) {
                //当滑轮向上滚动时
                num = num + 1;
            }
            if (e.wheelDelta < 0) {
                //当滑轮向下滚动时
                num = num - 1;
            }
        } else if (e.detail) {
            //Firefox滑轮事件
            if (e.detail > 0) {
                //当滑轮向上滚动时
                num = num + 1;
            }
            if (e.detail < 0) {
                //当滑轮向下滚动时
                num = num - 1;
            }
        }
        let res = `scale3d(${num < 1 ? 1 : num},${num < 1 ? 1 : num},1) rotate${transform.match(reg)[1]}`;
        imgRef.current.style.transform = res;
    }
 
    // 判断文件的icon
    function iconType(fileType) {
        let result = <FileOutlined />;
        if (fileType === '22_00017-3') {
            result = <FileImageOutlined />;
        }
        if (fileType === '22_00017-5') {
            result = <FileExcelOutlined />;
        }
        if (fileType === '22_00017-4') {
            result = <FileWordOutlined />;
        }
        if (fileType === '22_00017-6') {
            result = <FilePdfOutlined />;
        }
        return result;
    }
 
    // 切换下一个,上一个
    async function handleNext(type) {
        global.setSpinning(true);
        let index = fileIndex + (type === 'next' ? 1 : -1);
        if (files[index]) {
            setFileIndex(index);
        } else {
            global.setSpinning(false);
            if (index < 0) {
                setFileIndex(files.length - 1);
                return;
            }
            setFileIndex(0);
        }
    }
 
    // 获取附件数据
    async function getFileListData() {
        global.setSpinning(true);
        const res = await getFileListDataApi(caseId || searchParams.get('caseId'));
        global.setSpinning(false);
        if (res.type) {
            let resData = res.data?.dataList || [];
            let arr = [];
            let filesArr = [];
            let index = 0;
            resData.forEach((x, t) => {
                arr.push(x.ownerTypeName);
                filesArr = filesArr.concat(x.fileList || []);
            });
            forEach: for (let i = 0; i < filesArr.length - 1; i++) {
                if (filesArr[i].id === fileId) {
                    index = i;
                    break forEach;
                }
            }
            setData({ data: resData, caseNo: res.data?.caseNo });
            setFileIndex(index);
            setFiles(filesArr);
            setOpenKeys(arr);
        }
    }
 
    useEffect(() => {
        getFileListData();
    }, []);
 
    // 监听鼠标滚动事件
    useEffect(() => {
        if (imgRef.current) {
            imgRef.current.onload = () => {
                imgBgRef.current.onmousewheel = imgScrollFunc;
            };
        }
    }, [files]);
 
    // 切换文件加载
    useEffect(() => {
        if (imgRef.current) {
            imgRef.current.src = `${appUrl.fileUrl}${appUrl.fileShowUrl}${files[fileIndex]?.id}`;
            imgRef.current.onload = () => {
                global.setSpinning(false, 'only');
            };
        } else {
            global.setSpinning(false, 'only');
        }
    }, [appUrl.fileShowUrl, fileIndex, appUrl.baseUrl, files]);
 
    return (
        <>
            <nav className="filesCheck-nav">
                <div className="filesCheck-nav-title">{data.caseNo || '-'}</div>
                <Menu
                    className="filesCheck-nav-menu"
                    style={{ width: '200px' }}
                    onOpenChange={(openKeys) => setOpenKeys(openKeys)}
                    mode="inline"
                    selectedKeys={[files[fileIndex]?.id]}
                    openKeys={openKeys}
                >
                    {data.data?.map((x, t) => {
                        return (
                            <SubMenu
                                key={x.ownerTypeName}
                                icon={openKeys.includes(x.ownerTypeName) ? <FolderOpenOutlined /> : <FolderOutlined />}
                                title={x.ownerTypeName}
                            >
                                {x.fileList?.map((y, z) => {
                                    return (
                                        <Menu.Item
                                            onClick={async () => {
                                                global.setSpinning(true);
                                                for (let i = 0; i < files.length; i++) {
                                                    if (files[i].id === y.id) {
                                                        setFileIndex(i);
                                                        break;
                                                    }
                                                }
                                            }}
                                            icon={iconType(y.cat)}
                                            key={y.id}
                                        >
                                            {y.name}
                                        </Menu.Item>
                                    );
                                })}
                            </SubMenu>
                        );
                    })}
                </Menu>
            </nav>
            {files[fileIndex] ? (
                <main className="filesCheck-main">
                    {/* 头部操作区 */}
                    <div className="filesCheck-main-action">
                        <div className="filesCheck-main-action-title">
                            <h3>{files[fileIndex]?.name}</h3>
                        </div>
                        {files[fileIndex]?.cat === '22_00017-3' && (
                            <>
                                <div className="filesCheck-main-action-item">
                                    <Tooltip title="左转">
                                        <RotateLeftOutlined onClick={() => handleRotateImg('left')} />
                                    </Tooltip>
                                </div>
                                <div className="filesCheck-main-action-item">
                                    <Tooltip title="右转">
                                        <RotateRightOutlined onClick={() => handleRotateImg('right')} />
                                    </Tooltip>
                                </div>
                            </>
                        )}
                        <div className="filesCheck-main-action-item">
                            <Tooltip title="下载">
                                <a href={`${appUrl.fileUrl}${appUrl.fileDownUrl}${files[fileIndex]?.id}`}>
                                    <DownloadOutlined />
                                </a>
                            </Tooltip>
                        </div>
                    </div>
                    {files[fileIndex]?.cat === '22_00017-3' ? (
                        <div className="filesCheck-main-imgBg" ref={imgBgRef}>
                            <img
                                ref={imgRef}
                                style={{ transform: 'scale3d(1,1,1) rotate(0deg)' }}
                                className="filesCheck-main-img"
                                src={`${appUrl.fileUrl}${appUrl.fileShowUrl}${files[fileIndex]?.id}`}
                                alt="图片加载中..."
                            />
                        </div>
                    ) : (
                        <div className="filesCheck-main-other">
                            <div className="filesCheck-main-other-icon">{iconType(files[fileIndex]?.cat)}</div>
                            <div className="filesCheck-main-other-text">
                                您所查看的附件不支持预览,请下载查看{files[fileIndex]?.cat === '22_00017-6' ? '或跳转预览' : ''}。
                            </div>
                            <div>
                                {files[fileIndex]?.cat === '22_00017-6' && (
                                    <Button
                                        className="public-buttonMargin"
                                        onClick={() => window.open(`${appUrl.fileUrl}${appUrl.fileShowUrl}${files[fileIndex]?.id}`)}
                                    >
                                        跳转查看
                                    </Button>
                                )}
                                <a href={`${appUrl.fileUrl}${appUrl.fileDownUrl}${files[fileIndex]?.id}`}>
                                    <Button type="primary">下载</Button>
                                </a>
                            </div>
                        </div>
                    )}
                    <div className="filesCheck-imgLeft" onClick={() => handleNext('back')}>
                        <LeftOutlined />
                    </div>
                    <div className="filesCheck-imgRight" onClick={() => handleNext('next')}>
                        <RightOutlined />
                    </div>
                </main>
            ) : (
                <div style={{ width: '100%', paddingTop: '20%' }}>{$$.MyEmpty()}</div>
            )}
        </>
    );
};
 
export default FilesCheck;