Files
fe_service_ebtp_frontend/src/utils/PdfModal/PdfModal.tsx
2022-03-10 14:24:13 +08:00

120 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useEffect, useState } from 'react';
import { Modal, Button } from 'antd';
import PDF from 'react-pdf-js';
import ProCard from '@ant-design/pro-card';
import FileDown from '@/utils/Download';
import { checkFileExist } from './service';
/**
* Pdf Modal弹窗公共组件
* 2021.8.25 zhoujianlong 根据定标结果通知书的弹出窗口改造初始版本,增加备注
*/
interface PdfModalProps {
fileId: string; //文档中心id
downLoadFileName: string; //下载后的文件名称
modalVisible: boolean; //Modal 弹出还是关闭
onCancel: () => void; //关闭Modal方法
}
const PdfModal: React.FC<PdfModalProps> = (props) => {
const { fileId, downLoadFileName, modalVisible, onCancel } = props;
const [page, setPage] = useState<number>(1); //页码
const [pages, setPages] = useState<number>(0); //总页数
const [fileExist, setFileExist] = useState<string>('0'); //0未验证1验证后有2验证后没有
useEffect(() => {
//判断文件是否存在
checkFileExist(fileId).then((res) => {
if (res !== undefined && res.length > 0) {
setFileExist('1');
} else {
setFileExist('2');
}
});
}, [fileId]);
//文档总页码
const onDocumentComplete = (pages: any) => {
setPage(1);
setPages(pages);
};
//上一页
const handlePrevious = () => {
setPage(page - 1);
};
//下一页
const handleNext = () => {
setPage(page + 1);
};
//下载链接
const downLoadFile = (fileId: string) => {
const returnUrl = '/api/core-service-ebtp-updownload/v1/attachment/download/bid/' + fileId;
return returnUrl;
};
const modalHeight = (window.innerHeight * 96) / 100;
return (
<Modal
destroyOnClose
visible={modalVisible}
title="预览"
onCancel={() => onCancel()}
width={'80%'}
centered
style={{ maxHeight: modalHeight }}
bodyStyle={{ maxHeight: modalHeight - 107, overflowY: 'auto' }}
footer={[
<>
<Button key="close" onClick={() => onCancel()}>
</Button>
{fileExist == '1' ? (
<>
<Button disabled={page === pages} key="nextBtn" onClick={() => handleNext()}>
{' '}
</Button>
<Button disabled={page === 1} key="preBtn" onClick={() => handlePrevious()}>
{' '}
</Button>
<FileDown
key="downLoadFile"
type="pdf"
apiUrl={downLoadFile(fileId)}
style={{ float: 'left' }}
fileName={downLoadFileName}
btnName="下载"
method="GET"
form={null}
/>
</>
) : null}
</>,
]}
>
<ProCard layout="center" direction="column" ghost>
{fileExist == '0' ? (
<div />
) : fileExist == '1' ? (
<>
<PDF
file={downLoadFile(fileId)}
scale={1.6}
onDocumentComplete={onDocumentComplete}
page={page}
/>
</>
) : (
<span></span>
)}
</ProCard>
</Modal>
);
};
export default PdfModal;