广州矛调粤政易端
xusd
7 days ago d27794814b69d18aeb8ee96a46cae91d5613570c
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
/*
 * @Company: hugeInfo
 * @Author: ldh
 * @Date: 2022-08-09 11:44:57
 * @LastEditTime: 2024-12-01 17:01:39
 * @LastEditors: lwh
 * @Version: 1.0.0
 * @Description: 附件展示组件
 */
import React, { useState, useEffect, useMemo, useRef } from 'react';
import { appUrl, ax, openFiles, showToast, confirmModal } from '../../utils/utility';
import { pdf, word, excel, file, delEliminate } from '../../assets/img';
import MyModal from '../../components/MyModal';
import { img_none } from '../../assets/icon';
import './index.less';
 
// 删除附件
function delFileApi(id) {
  return ax.request({ url: `sys/attachment/removeById?id=${id}`, type: 'get', service: 'ninex' });
}
 
/**
 * fileType: string, // 附件类型,不传则默认是图片
 * fileId: string, // 附件id
 * img: string, // 当前展示图片(附件)接口格式链接一般是showUrl
 * imageUrl: string, // 完整的图片(附件)链接
 * allImg: array, [''], // 全部展示图片,便于图片查看
 * title: string, // 标题
 * noTitle: bool, // 是否有标题
 * width: number, // 图片的宽度,高度
 * isShowBig: bool, // 是否可以放大查看
 * isDelete: bool, // 是否可以删除
 * defaultImg:string //默认展示图片
 * onDelCallback: func // 删除文件反馈函数
 * noEllipsis:图片名称展示不折叠
 */
const ImgShow = ({
  fileType = '22_00017-3',
  fileId,
  img,
  imageUrl,
  allImg = [],
  title,
  noTitle = false,
  width = 54,
  isShowBig = true,
  isDelete = false,
  defaultImg = img_none,
  onDelCallback,
  noEllipsis = false
}) => {
  const imgRef = useRef();
 
  const [modalImg, setModalImg] = useState({ visible: false, url: '' });
 
  // 判断附件链接
  const imgUrl = imageUrl ? imageUrl : img ? `${appUrl.fileUrl}${img}` : null;
 
  // 点击查看附件
  function handleShowFile() {
    console.log('11111');
    
    if (!imgUrl || !isShowBig) return;
    console.log(imgUrl);
    // openFiles({ type: fileType, url: imgUrl, urls: allImg });
    setModalImg({ visible: true, url: imgUrl })
  }
 
  // 点击删除附件
  async function handleDelFile(value) {
    confirmModal({
      title: '删除附件确认',
      subtitle: (
        <span>
          确认删除该附件吗?
        </span>
      ),
      okText: '确定删除',
      onOk: async () => {
        global.setSpinning(true);
        const res = await delFileApi(fileId);
        global.setSpinning(false);
        if (res.type) {
          showToast({ type: 'success', content: '删除成功' });
          // console.log('删除成功onDelCallback');
          onDelCallback && onDelCallback(fileId);
        }
      },
    });
 
  }
 
  // 图片初始化
  useEffect(() => {
    if (imgUrl && fileType === '22_00017-3') {
      let temp = new Image();
      temp.src = imgUrl;
      temp.onload = () => {
        imgRef.current.src = imgUrl;
      };
    }
  }, [imgUrl, fileType]);
 
 
 
  // 判断除图片外的附件
  const fileImg = useMemo(() => {
    let img = '';
    switch (fileType) {
      case '22_00017-4':
        img = word;
        break;
      case '22_00017-5':
        img = excel;
        break;
      case '22_00017-6':
        img = pdf;
        break;
      default:
        img = file;
    }
    return img;
  }, [fileType]);
 
  return (
    <div className="fileShow" style={{ width: `${width}px`, height: `${width}px` }}>
      {fileType !== '22_00017-3' ? (
        // 非图片
        <div className="fileShow-elseFile">
          <img src={fileImg} alt="" className="fileShow-img" />
        </div>
      ) : (
        // 图片
        <div className="fileShow-imgBox">
          {!img && !imageUrl ? (
            <img src={defaultImg} alt="" className="fileShow-img" />
          ) : (
            <img ref={imgRef} onClick={handleShowFile} src={defaultImg} alt="加载中..." className="fileShow-img" />
          )}
        </div>
      )}
      {/* 右上角删除icon */}
      {isDelete && fileId && (
        <div onClick={handleDelFile} className="fileShow-del">
          {/* <EliminateFilled /> */}
          <img style={{ width: '18px', height: '18px' }} src={delEliminate} alt="" />
        </div>
      )}
      {/* 名称展示 */}
      {!noTitle && (
        <div className="fileShow-title">
          <div className={'fileShow-title-text'}>{title || '未上传'}</div>
        </div>
      )}
      <MyModal visible={modalImg.visible} center title="查看图片" onClose={() => setModalImg({ visible: false, url: '' })}>
        <div style={{ height: '1px', width: '100%', backgroundColor: 'rgba(126, 134, 142, 0.16)' }}></div>
        <div style={{ height: '300px' }}>
          <img src={modalImg.url} alt="" style={{ width: '100%', height: '100%', objectFit: 'contain' }} />
        </div>
      </MyModal>
    </div>
  );
};
 
export default ImgShow;