广州市综治平台前端
xusd
7 days ago 544148eddae96db824423cd059ebecb9d13c392e
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
import React, { Fragment, useState, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { Button, Space } from 'antd';
import { Modal } from '@arco-design/web-react';
import { CloseCircleFilled, DownloadOutlined } from '@ant-design/icons';
import { IconAttachment } from '@arco-design/web-react/icon';
import { Document, Page, pdfjs } from 'react-pdf';
import * as docx from 'docx-preview';
import * as $$ from '@/utils/utility';
import PreviewImage from '@/components/PreviewImage';
import './index.less';
 
// 设置pdf.js工作器路径
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;
 
const appUrl = $$.appUrl;
const MyPDF = ({ name, fileUrl, fileType }) => {
    const [visible, setVisible] = useState(false);
    const [numPages, setNumPages] = useState(null);
    const [pageNumber, setPageNumber] = useState(1);
    const containerRef = useRef(null);
 
    // 成功加载PDF文件时的回调
    function onDocumentLoadSuccess({ numPages }) {
        setNumPages(numPages);
        setPageNumber(1);
    }
 
    function changePage(offset) {
        setPageNumber((prevPageNumber) => prevPageNumber + offset);
    }
 
    function previousPage() {
        changePage(-1);
    }
 
    function nextPage() {
        changePage(1);
    }
 
    return (
        <>
            {fileType === 'jpg' || fileType === 'png' ? (
                <PreviewImage name={name} src={fileUrl} />
            ) : (
                <div>
                    {fileUrl ? (
                        <div
                            onClick={() => {
                                setVisible(true);
                                if (fileType === 'docx' || fileType === 'doc') {
                                    const renderDocx = async () => {
                                        try {
                                            const response = await fetch(`${appUrl.fileUrl}/${appUrl.sys}${fileUrl}`);
                                            const blob = await response.blob();
                                            await docx.renderAsync(blob, containerRef.current);
                                        } catch (error) {
                                            console.error('文档加载失败', error);
                                        }
                                    };
                                    renderDocx();
                                }
                            }}
                            className="pdf-title"
                        >
                            <IconAttachment style={{ color: '#1A6FB8' }} />
                            <div className="pdf-name">{name}</div>
                        </div>
                    ) : (
                        '-'
                    )}
 
                    <Modal
                        style={{ width: '80%' }}
                        closable={false}
                        title={name}
                        footer={
                            <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', gap: '10px' }}>
                                <button
                                    type="button"
                                    disabled={pageNumber <= 1}
                                    onClick={previousPage}
                                    style={{ padding: '4px 15px', cursor: pageNumber <= 1 ? 'not-allowed' : 'pointer' }}
                                >
                                    上一页
                                </button>
                                <p style={{ margin: 0 }}>
                                    第 {pageNumber} 页 / 共 {numPages || '--'} 页
                                </p>
                                <button
                                    type="button"
                                    disabled={pageNumber >= numPages}
                                    onClick={nextPage}
                                    style={{ padding: '4px 15px', cursor: pageNumber >= numPages ? 'not-allowed' : 'pointer' }}
                                >
                                    下一页
                                </button>
                                <Button
                                    type="primary"
                                    icon={<DownloadOutlined />}
                                    onClick={() => {
                                        window.open(`${appUrl.fileUrl}${appUrl.fileDownUrl}${fileUrl.split('/').pop()}`);
                                    }}
                                    style={{ marginLeft: '10px' }}
                                >
                                    下载
                                </Button>
                            </div>
                        }
                        centered
                        unmountOnExit={true}
                        visible={visible}
                        onCancel={() => {
                            setVisible(false);
                        }}
                    >
                        <div style={{ width: '100%', height: 'calc(100vh - 200px)', display: 'flex', justifyContent: 'center', overflow: 'auto' }}>
                            {fileType === 'pdf' && (
                                <Document
                                    file={`${appUrl.fileUrl}/${appUrl.sys}${fileUrl}`}
                                    onLoadSuccess={onDocumentLoadSuccess}
                                    loading={<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}>PDF加载中...</div>}
                                    error={
                                        <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}>PDF加载失败,请稍后重试</div>
                                    }
                                >
                                    <Page pageNumber={pageNumber} renderTextLayer={false} renderAnnotationLayer={false} scale={1.2} />
                                </Document>
                            )}
                            {fileType === 'docx' && <div ref={containerRef} className="docx-container" />}
                            {fileType === 'doc' && <div ref={containerRef} className="docx-container" />}
                        </div>
                    </Modal>
                </div>
            )}
        </>
    );
};
 
MyPDF.propTypes = {
    title: PropTypes.any,
    visible: PropTypes.bool,
    onCancel: PropTypes.func,
    onOk: PropTypes.func,
    footer: PropTypes.any,
    onText: PropTypes.string,
    cancelText: PropTypes.string,
    zIndex: PropTypes.number,
    bodyStyle: PropTypes.object,
    closable: PropTypes.bool,
    centered: PropTypes.bool,
};
 
export default MyPDF;