Files
fe_service_ebtp_frontend/src/utils/DownloadUtils.ts

226 lines
7.4 KiB
TypeScript
Raw Normal View History

2020-12-23 11:14:35 +08:00
/*
2022-04-01 20:06:34 +08:00
* @Author: zhoujianlong
* @Date: 2022-03-03 09:06:36
* @Last Modified by: zhoujianlong
* @Last Modified time: 2022-12-23 23:21:01
2020-12-23 11:14:35 +08:00
*/
2022-03-10 14:24:13 +08:00
2022-04-01 20:06:34 +08:00
import { getDownloadSecretKey, getFilelist, getFilelistBySecond } from "@/services/download_";
2022-08-30 16:16:46 +08:00
import { message } from "antd";
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
2022-04-01 20:06:34 +08:00
import { isEmpty, isNotEmpty } from "./CommonUtils";
2022-03-10 14:24:13 +08:00
2021-01-16 11:29:42 +08:00
/**
2022-04-01 20:06:34 +08:00
*
2021-01-16 11:29:42 +08:00
*/
2022-04-01 20:06:34 +08:00
export const uploadAttachmentPath = '/api/doc/v1.0/files/upload';
2021-01-16 11:29:42 +08:00
/**
2022-04-01 20:06:34 +08:00
*
2021-01-16 11:29:42 +08:00
*/
2022-04-01 20:06:34 +08:00
export const downloadSecretKeyPath = '/api/doc/api/data-service-document-center/outer/v1.0/files/getSecretKey';
/**
*
*/
export const downloadAttachmentPath = '/api/doc/api/data-service-document-center/outer/v1.0/files/getDownload';
/**
* id
*/
export const getSnowIdPath = '/api/core-service-ebtp-updownload/v1/business/id';
/**
*
*/
export const removeFilePath = '/api/doc/v1.0/files/disk';
/**
* objectId
*/
export const findFilelistPath = '/api/doc/v1.0/files/queryReturn';
/**
*
*/
export const pictureDisplayPath = '/api/doc/v1.0/files/display';
/**
*
*/
export const downloadPath = '/api/doc/v1.0/files/download';
/**
* 2.0 bid
*/
2023-01-06 14:27:37 +08:00
export const findFilelistPathBySecond = '/downloadfile/v1/file/getFileListById';
2022-04-01 20:06:34 +08:00
/**
* 2.0 bid fileId
*/
2023-01-06 14:27:37 +08:00
export const downloadPathBySecond = '/downloadfile/v1/file/downloadFileStreamById';
2022-03-10 14:24:13 +08:00
2022-04-01 20:06:34 +08:00
export interface FilelistProps {
uid: string,
name: string,
status: 'uploading' | 'done' | 'error' | 'removed',
url: string,
}
/**
* 3.0 ObjectId
* @param objectId
* @returns
*/
export const checkObjectId = (objectId: string) => {
2022-04-01 20:06:34 +08:00
const pattern = /[a-zA-Z]+/;
return objectId.length == 19 && !pattern.test(objectId);
}
/**
*
* @returns
*/
const getTimeStamp = () => {
const cipherMode = '1';
const publicKey = '045631900E382D3DA75BD3E977CD6B28556DA3DF15820945F9BFB69E0F7456DF22D4EAB9CCD45B3B4959B85CEB4A2C4BB03AB5E4C08BA6A79E5B7C6AB63C20D932';
const sm2 = require('sm-crypto').sm2;
const date = Math.round(new Date().getTime() / 1000);
return '04' + sm2.doEncrypt(date, publicKey, cipherMode);
}
/**
*
* @param {fileId} uid
*/
export const downloadFile = async ({ uid, name }: any) => {
if (isNotEmpty(uid)) {
if (checkObjectId(uid)) {//判断3.0 fileId
const keyRes = await getDownloadSecretKey({ fileId: uid });//获取key
keyRes?.success && (window.location.href = downloadAttachmentPath + '?fileId=' + uid + '&documentSecretKey=' + keyRes?.data);
} else {
window.location.href = downloadPathBySecond + '?id=' + uid + '&p=' + getTimeStamp();
}
// keyRes?.success && window.fetch(downloadAttachmentPath + '?fileId=' + uid + '&documentSecretKey=' + keyRes?.data, {
// method: "GET",
// headers: {
// Authorization: getUserToken(),
// currentRoleCode: getSessionRoleData()?.roleCode,
// },
// credentials: 'include',
// }).then((response) => {
// // 这里才是下载附件逻辑处理的地方
// response.blob().then(blob => {
// const blobUrl = window.URL.createObjectURL(blob);
// const aElement = document.createElement("a");
// aElement.href = blobUrl; // 设置a标签路径
// aElement.download = name;
// aElement.click();
// window.URL.revokeObjectURL(blobUrl);
// });
// });
}
}
2021-01-16 11:29:42 +08:00
/**
2022-04-01 20:06:34 +08:00
* objectId文件下载
* @param {Id} objectId
2021-01-16 11:29:42 +08:00
*/
2022-04-01 20:06:34 +08:00
export const downloadFileObjectId = (objectId: string) => {
if (isNotEmpty(objectId)) {
if (checkObjectId(objectId)) {//判断3.0 objectId
getFilelist([objectId]).then(res => {
if (res?.success && res?.data?.length > 0) {
downloadFile({ uid: res?.data[res?.data?.length - 1].fileId });
2022-08-30 16:16:46 +08:00
} else {
message.info("文档中心错误或文件不存在");
2022-04-01 20:06:34 +08:00
}
})
} else {
window.location.href = downloadPathBySecond + '?id=' + objectId + '&p=' + getTimeStamp();
}
}
}
/**
* objectId获取文件下载链接
* @param {Id} objectId
* @returns
*/
export const getDownloadFileUrl = async (objectId: string) => {
let url = null;
if (isNotEmpty(objectId)) {
if (checkObjectId(objectId)) {//判断3.0 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
url = downloadAttachmentPath + '?fileId=' + res?.data[res?.data?.length - 1].fileId + '&documentSecretKey=' + keyRes?.data
}
})
} else {
url = downloadPathBySecond + '?id=' + objectId + '&p=' + getTimeStamp();
}
}
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];
}
2022-04-01 20:06:34 +08:00
/**
*
* @param objectId
* @returns
*/
export async function getFileListByBid(objectId: string) {
let result: FilelistProps[] = [];
if (isEmpty(objectId)) {
return [];
}
if (checkObjectId(objectId)) {//判断3.0 objectId
return getFilelist([objectId]).then(res => {
if (res?.success && res?.data?.length > 0) {
2022-08-30 16:16:46 +08:00
res?.data?.forEach(({ fileId, originalName, filePath }: any) => {
2022-04-01 20:06:34 +08:00
result.push({
uid: fileId,
name: originalName,
status: 'done',
2022-08-30 16:16:46 +08:00
url: originalName.endsWith(".png") || originalName.endsWith(".jpg") || originalName.endsWith(".jpeg") ? pictureDisplayPath + '?filePath=' + filePath : 'javascript:void(0);',
2022-04-01 20:06:34 +08:00
})
});
return result;
}
return [];
})
} else {
return getFilelistBySecond(objectId, getTimeStamp()).then(response => response.json()).then(res => {
if (res?.length > 0) {
res?.forEach(({ id, bid, filename }: any) => {
result.push({
uid: id,
name: filename,
status: 'done',
url: 'javascript:void(0);',
})
});
return result;
}
return [];
})
}
2021-01-16 11:29:42 +08:00
}