index.jsx 3.3 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3
/**
 * web端文件预览功能
 */
4 5
import React, {Component} from 'react';
import {checkIsImage} from '@/webPublic/one_stop_public/libs/UploadCom';
6
import { getModal, getPopconfirm, isFromIframe } from '@/webPublic/one_stop_public/utils/utils';
7

8 9
// const FileViewer = CLIENT_TYPE === 'mobile' ? null : require('react-file-viewer');
const FileViewer = null;
10
const Modal = getModal();
11
const Popconfirm = getPopconfirm();
12

徐立's avatar
徐立 committed
13

徐立's avatar
徐立 committed
14
export default class index extends Component {
15 16 17 18 19
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
20
    this.imageDom = null;
21 22 23
  }

  showModal = () => {
24
    if(isFromIframe()){
25 26
      return false;
    }
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    this.setState({
      visible: true,
    });
  };

  handleOk = (e) => {
    this.setState({
      visible: false,
    });
  };

  handleCancel = (e) => {
    this.setState({
      visible: false,
    });
  };
  download = () => {
44
    let {path, pathName} = this.props;
45 46 47 48 49
    if(window?.parent?.open && typeof window.parent.open === 'function'){
      window.parent.open(path, '_blank');
    }else{
      window.open(path, '_blank');
    }
50
  };
钟是志's avatar
钟是志 committed
51

52
  render() {
53
    let {path, pathName, width, height} = this.props;
54

55
    const {visible} = this.state;
56 57 58 59 60 61 62 63 64 65 66 67 68
    let isShow = false;
    let type;
    let isImg = checkIsImage(pathName);
    if (isImg) {
      isShow = true;
    } else {
      let find = ['.pdf', '.mp3', '.mp4', '.csv'].find((x) => {
        return pathName.indexOf(x) > -1;
      });
      if (find) {
        isShow = true;
      }
    }
69
    if (isShow) {
70 71 72
      type = pathName.split('.');
      type = Array.isArray(type) && type.length && type[type.length - 1];
    }
钟是志's avatar
钟是志 committed
73

74 75
    // return null;

76 77 78 79
    return (
      <>
        {isShow ? (
          <Popconfirm
80
            title='查看附件'
81 82
            onConfirm={this.download}
            okText='下载'
83 84

            onCancel={this.showModal}
85
            cancelText={ isFromIframe() ? '取消' : '预览'}>
86 87
            {isImg ? (
              <img
88
                ref={(node)=>{ this.imageDom = node;}}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                style={{
                  width: width ? width : '100px',
                  height: height ? height : 'auto',
                }}
                src={path}
                alt={pathName}
              />
            ) : (
              <a target='_blank' href={path}>
                {pathName}
              </a>
            )}
          </Popconfirm>
        ) : (
          <a target='_blank' href={path}>
            {pathName}
          </a>
        )}
钟是志's avatar
钟是志 committed
107 108
        {
          visible &&
109 110
        <Modal
          title={pathName}
111
          visible={true}
112 113
          width={1200}
          destroyOnClose
114
          centered={true}
115 116 117 118 119 120 121 122 123 124 125 126 127
          onOk={this.handleOk}
          onCancel={this.handleCancel}>
          {type === 'pdf' ? (
            <iframe
              style={{
                width: '100%',
                height: 600,
              }}
              src={path}
            />
          ) : (
            <div
              style={{
128
                height: 650,
129
                maxWidth: 1200,
130
              }}>
131
              <a onClick={this.download}>
132 133
                下载文件
              </a>
134
           {/*   {FileViewer && (
135 136 137 138
                <FileViewer
                  fileType={type}
                  filePath={path}
                />
139
              )}*/}
140 141 142
            </div>
          )}
        </Modal>
钟是志's avatar
钟是志 committed
143 144
        }

145 146 147
      </>
    );
  }
徐立's avatar
徐立 committed
148
}