11.18 修改获取下载路径的方法,让页面内pdf组件正常获取文件链接

This commit is contained in:
jl-zhoujl2
2022-11-18 17:22:37 +08:00
parent bb9013a612
commit fe5f9dbb2d
4 changed files with 45 additions and 10 deletions

View File

@ -3,7 +3,7 @@ import { Modal, Button } from 'antd';
import PDF from 'react-pdf-js'; import PDF from 'react-pdf-js';
import ProCard from '@ant-design/pro-card'; import ProCard from '@ant-design/pro-card';
import FileDown from '@/utils/Download'; import FileDown from '@/utils/Download';
import { getDownloadFileUrl, getFileListByBid } from '@/utils/DownloadUtils'; import { getFileListByBid, useDownLoadUrl } from '@/utils/DownloadUtils';
interface PreviewJgtzsModalProps { interface PreviewJgtzsModalProps {
fileId: string, fileId: string,
@ -14,6 +14,7 @@ interface PreviewJgtzsModalProps {
const PreviewJgtzsModal: React.FC<PreviewJgtzsModalProps> = (props) => { const PreviewJgtzsModal: React.FC<PreviewJgtzsModalProps> = (props) => {
const { fileId, downLoadFileName, modalVisible, onCancel } = props const { fileId, downLoadFileName, modalVisible, onCancel } = props
const [url] = useDownLoadUrl(fileId);
const [page, setPage] = useState<number>(1); const [page, setPage] = useState<number>(1);
const [pages, setPages] = useState<number>(0); const [pages, setPages] = useState<number>(0);
const [fileExist, setFileExist] = useState<string>("0");//0未验证1验证后有2验证后没有 const [fileExist, setFileExist] = useState<string>("0");//0未验证1验证后有2验证后没有
@ -81,10 +82,10 @@ const PreviewJgtzsModal: React.FC<PreviewJgtzsModalProps> = (props) => {
> >
{fileExist == "0" ? ( {fileExist == "0" ? (
<div /> <div />
) : fileExist == "1" ? ( ) : fileExist == "1" ? url != "" && (
<> <>
<PDF <PDF
file={getDownloadFileUrl(fileId)} file={url}
scale={1.6} scale={1.6}
onDocumentComplete={onDocumentComplete} onDocumentComplete={onDocumentComplete}
page={page} page={page}

View File

@ -3,7 +3,7 @@ import { Modal, Button } from 'antd';
import PDF from 'react-pdf-js'; import PDF from 'react-pdf-js';
import ProCard from '@ant-design/pro-card'; import ProCard from '@ant-design/pro-card';
import FileDown from '@/utils/Download'; import FileDown from '@/utils/Download';
import { getDownloadFileUrl, getFileListByBid } from '@/utils/DownloadUtils'; import { getFileListByBid, useDownLoadUrl } from '@/utils/DownloadUtils';
interface PreviewJgtzsModalProps { interface PreviewJgtzsModalProps {
fileId: string, fileId: string,
@ -13,6 +13,7 @@ interface PreviewJgtzsModalProps {
const PreviewJgtzsModal: React.FC<PreviewJgtzsModalProps> = (props) => { const PreviewJgtzsModal: React.FC<PreviewJgtzsModalProps> = (props) => {
const { fileId, modalVisible, onCancel } = props const { fileId, modalVisible, onCancel } = props
const [url] = useDownLoadUrl(fileId);
const [page, setPage] = useState<number>(1); const [page, setPage] = useState<number>(1);
const [pages, setPages] = useState<number>(0); const [pages, setPages] = useState<number>(0);
const [fileExist, setFileExist] = useState<string>("0");//0未验证1验证后有2验证后没有 const [fileExist, setFileExist] = useState<string>("0");//0未验证1验证后有2验证后没有
@ -79,10 +80,10 @@ const PreviewJgtzsModal: React.FC<PreviewJgtzsModalProps> = (props) => {
> >
{fileExist == "0" ? ( {fileExist == "0" ? (
<div /> <div />
) : fileExist == "1" ? ( ) : fileExist == "1" ? url != "" && (
<> <>
<PDF <PDF
file={getDownloadFileUrl(fileId)} file={url}
scale={1.6} scale={1.6}
onDocumentComplete={onDocumentComplete} onDocumentComplete={onDocumentComplete}
page={page} page={page}

View File

@ -2,10 +2,11 @@
* @Author: zhoujianlong * @Author: zhoujianlong
* @Date: 2022-03-03 09:06:36 * @Date: 2022-03-03 09:06:36
* @Last Modified by: zhoujianlong * @Last Modified by: zhoujianlong
* @Last Modified time: 2022-05-18 17:23:26 * @Last Modified time: 2022-11-18 17:05:16
*/ */
import { getDownloadSecretKey, getFilelist, getFilelistBySecond } from "@/services/download_"; import { getDownloadSecretKey, getFilelist, getFilelistBySecond } from "@/services/download_";
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
import { isEmpty, isNotEmpty } from "./CommonUtils"; import { isEmpty, isNotEmpty } from "./CommonUtils";
/** /**
@ -146,6 +147,37 @@ export const getDownloadFileUrl = async (objectId: string) => {
} }
return url; return url;
} }
/**
* objectId获取文件下载链接
* @param objectId
* @returns
*/
export const useDownLoadUrl = (objectId: string): string[] => {
const [url, setUrl] = useState<string>("");
useEffect(() => {
const getFileUrl = async () => {
console.log("objectId", objectId);
await getFilelist([objectId]).then(async res => {
if (res?.success && res?.data?.length > 0) {
const keyRes = await getDownloadSecretKey({ fileId: res?.data[res?.data?.length - 1].fileId });//获取key
let url = downloadAttachmentPath + '?fileId=' + res?.data[res?.data?.length - 1].fileId + '&documentSecretKey=' + keyRes?.data;
setUrl(url);
}
})
}
if (isNotEmpty(objectId)) {
if (checkObjectId(objectId)) {//判断3.0 objectId
getFileUrl();
} else {
let url = downloadPathBySecond + '?id=' + objectId + '&p=' + getTimeStamp();
setUrl(url);
}
}
}, [objectId])
return [url];
}
/** /**
* 获取文件列表 * 获取文件列表
* @param objectId * @param objectId

View File

@ -3,7 +3,7 @@ import { Modal, Button } from 'antd';
import PDF from 'react-pdf-js'; import PDF from 'react-pdf-js';
import ProCard from '@ant-design/pro-card'; import ProCard from '@ant-design/pro-card';
import FileDown from '@/utils/Download'; import FileDown from '@/utils/Download';
import { getDownloadFileUrl, getFileListByBid } from '../DownloadUtils'; import { getFileListByBid, useDownLoadUrl } from '../DownloadUtils';
/** /**
* Pdf Modal弹窗公共组件 * Pdf Modal弹窗公共组件
* 2021.8.25 zhoujianlong 根据定标结果通知书的弹出窗口改造初始版本,增加备注 * 2021.8.25 zhoujianlong 根据定标结果通知书的弹出窗口改造初始版本,增加备注
@ -17,6 +17,7 @@ interface PdfModalProps {
const PdfModal: React.FC<PdfModalProps> = (props) => { const PdfModal: React.FC<PdfModalProps> = (props) => {
const { fileId, downLoadFileName, modalVisible, onCancel } = props; const { fileId, downLoadFileName, modalVisible, onCancel } = props;
const [url] = useDownLoadUrl(fileId);
const [page, setPage] = useState<number>(1); //页码 const [page, setPage] = useState<number>(1); //页码
const [pages, setPages] = useState<number>(0); //总页数 const [pages, setPages] = useState<number>(0); //总页数
const [fileExist, setFileExist] = useState<string>('0'); //0未验证1验证后有2验证后没有 const [fileExist, setFileExist] = useState<string>('0'); //0未验证1验证后有2验证后没有
@ -93,10 +94,10 @@ const PdfModal: React.FC<PdfModalProps> = (props) => {
<ProCard layout="center" direction="column" ghost> <ProCard layout="center" direction="column" ghost>
{fileExist == '0' ? ( {fileExist == '0' ? (
<div /> <div />
) : fileExist == '1' ? ( ) : fileExist == '1' ? url != "" && (
<> <>
<PDF <PDF
file={getDownloadFileUrl(fileId)} file={url}
scale={1.6} scale={1.6}
onDocumentComplete={onDocumentComplete} onDocumentComplete={onDocumentComplete}
page={page} page={page}