Files
fe_service_ebtp_frontend/src/utils/DownloadUtils.ts

226 lines
7.4 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.

/*
* @Author: zhoujianlong
* @Date: 2022-03-03 09:06:36
* @Last Modified by: zhoujianlong
* @Last Modified time: 2022-12-23 23:21:01
*/
import { getDownloadSecretKey, getFilelist, getFilelistBySecond } from "@/services/download_";
import { message } from "antd";
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
import { isEmpty, isNotEmpty } from "./CommonUtils";
/**
* 文件上传
*/
export const uploadAttachmentPath = '/api/doc/v1.0/files/upload';
/**
* 获取密钥
*/
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
*/
export const findFilelistPathBySecond = '/api/v1/file/getFileListById';
/**
* 文件下载根据2.0 bid fileId
*/
export const downloadPathBySecond = '/api/v1/file/downloadFileStreamById';
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) => {
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);
// });
// });
}
}
/**
* objectId文件下载只含一个文件
* @param {业务Id} objectId
*/
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 });
} else {
message.info("文档中心错误或文件不存在");
}
})
} 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];
}
/**
* 获取文件列表
* @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) {
res?.data?.forEach(({ fileId, originalName, filePath }: any) => {
result.push({
uid: fileId,
name: originalName,
status: 'done',
url: originalName.endsWith(".png") || originalName.endsWith(".jpg") || originalName.endsWith(".jpeg") ? pictureDisplayPath + '?filePath=' + filePath : 'javascript:void(0);',
})
});
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 [];
})
}
}