import OSS from "ali-oss"; import { ElMessage } from "element-plus"; import request from "@/utils/httpRequest"; import { PUBLIC_MODULES_PATH as prefix } from "../AppPath"; export default class OSSSerive { queryById(id) { return request({ url: prefix + "/oss/file/queryById", method: "get", params: { id: id }, }); } saveMsg(param) { return request({ url: prefix + "/oss/file/saveMsg", method: "post", data: param, }); } findFileList(attachmentId) { return request({ url: prefix + "/oss/file/findFileList", method: "get", params: { attachmentId: attachmentId }, }); } getTemporaryUrl(url) { return request({ url: prefix + "/oss/file/getTemporaryUrl", method: "get", params: { url: url }, }); } getFileSizeByUrl(url) { return request({ url: prefix + "/oss/file/getFileSizeByUrl", method: "get", params: { url: url }, }); } getSFZSizeByUrl(url) { return request({ url: prefix + "/oss/file/getSFZSizeByUrl", method: "get", params: { url: url }, }); } /** * 根据url获取临时文件带水印、虚化原图地址 * @param {*} url * @returns */ getFileTemporaryLookUrlWithWatermark(url) { return request({ url: prefix + "/oss/file/getFileTemporaryLookUrlWithWatermark", method: "get", params: { url: url }, }); } /** * 根据url获取临时文件带水印、虚化、缩略图地址 * @param {*} url * @returns */ getThumbnailTemporaryWithWatermarkDimLookUrl(url) { return request({ url: prefix + "/oss/file/getThumbnailTemporaryWithWatermarkDimLookUrl", method: "get", params: { url: url }, }); } deleteMsgById(id) { return request({ url: prefix + "/oss/file/deleteMsgById", method: "get", params: { id: id }, }); } downLoadAttach(url) { return request({ url: prefix + "/oss/file/downLoadAttach", method: "get", responseType: "blob", params: { file: url }, }); } disposeXmlFile(data) { return request({ url: prefix + "/oss/file/disposeXmlFile", method: "post", data: data, }); } downLoadFileDisposeXmlFile(url) { return request({ url: prefix + "/oss/file/downLoadFileDisposeXmlFile", method: "post", params: { file: url }, }); } isUsedByInvoiceNumber(invoiceNumber) { return request({ url: prefix + "/oss/file/isUsedByInvoiceNumber", method: "get", params: { invoiceNumber: invoiceNumber }, }); } downloadAndUploadToMyCollect(data) { return request({ url: prefix + "/oss/file/downloadAndUploadToMyCollect", method: "post", data: data, }); } } export const client = new OSS({ region: "oss-cn-hangzhou", // oss地址 accessKeyId: "LTAI5tKa6kzGr5EyPWJB4EcD", // 通过阿里云控制台创建的AccessKey ID。 accessKeySecret: "arHxB7ZPhizrBYf4844TtyaRctPMgW", // 通过阿里云控制台创建的AccessKey Secret。 // bucket: "xgxm-test", // 仓库名字 bucket: 'xg-pg', // 仓库名字 useFetch: true, // 支持上传大于100KB的文件 secure: false, // 返回的url为https }); export const headers = { // 指定该Object被下载时网页的缓存行为。 "Cache-Control": "no-cache", // 指定该Object被下载时的名称。 "Content-Disposition": "inline", // 指定该Object被下载时的内容编码格式。 "Content-Encoding": "UTF-8", // 指定过期时间。 Expires: "Wed, 08 Jul 2023 16:57:01 GMT", // 指定Object的存储类型。 "x-oss-storage-class": "Standard", // 指定Object的访问权限。 // 'x-oss-object-acl': 'private', // 设置Object的标签,可同时设置多个标签。 "x-oss-tagging": "Tag1=1&Tag2=2", // 指定CopyObject操作时是否覆盖同名目标Object。此处设置为true,表示禁止覆盖同名Object。 "x-oss-forbid-overwrite": "true", // 'secure': 'true' }; export function onChange(file, files) { // console.log(file, 'file', files, 'Filest') return files; } // eslint-disable-next-line no-unused-vars export function handleUploadSuccess(response, file, fileS) { // console.log(response, file, fileS, 'response, file, fileList') } export function handleRemove(e, files) { return files; } export function fileName(file) { // console.log('文件名称处理') const tmpcnt = file.file.name.lastIndexOf("."); let fileName = file.file.name.substring(0, tmpcnt); // 将单引号‘’都转换成',将双引号“”都转换成" fileName = fileName.replace(/’|‘/g, "'").replace(/“|”/g, ""); // 将中括号【】转换成[],将大括号{}转换成{} fileName = fileName .replace(/【/g, "(") .replace(/】/g, ")") .replace(/{/g, "(") .replace(/}/g, ")"); fileName = fileName .replace(/\[/g, "(") .replace(/]/g, ")") .replace(/{/g, "(") .replace(/}/g, ")"); // 将逗号,转换成,,将:转换成: fileName = fileName.replace(/,/g, ",").replace(/:/g, ":"); // 将中文——转换为英文- fileName = fileName.replace(/—/g, "-"); fileName = fileName.replace(/……/g, ""); fileName = fileName.replace(/±/g, ""); fileName = fileName.replace(/#/g, ""); fileName = fileName.replace(/%/g, ""); fileName = fileName.replace(/Π/g, ""); fileName = fileName.replace(/π/g, ""); fileName = fileName.replace(/·/g, "."); // console.log('文件名处理结果', fileName) return fileName; } export function fileNameInvoice(file) { // console.log('文件名称处理') if (!file.name) { file = file.file; } const tmpcnt = file.name.lastIndexOf("."); let fileName = file.name.substring(0, tmpcnt); // 将单引号‘’都转换成',将双引号“”都转换成" fileName = fileName.replace(/’|‘/g, "'").replace(/“|”/g, ""); // 将中括号【】转换成[],将大括号{}转换成{} fileName = fileName .replace(/【/g, "(") .replace(/】/g, ")") .replace(/{/g, "(") .replace(/}/g, ")"); fileName = fileName .replace(/\[/g, "(") .replace(/]/g, ")") .replace(/{/g, "(") .replace(/}/g, ")"); // 将逗号,转换成,,将:转换成: fileName = fileName.replace(/,/g, ",").replace(/:/g, ":"); // 将中文——转换为英文- fileName = fileName.replace(/—/g, "-"); fileName = fileName.replace(/……/g, ""); fileName = fileName.replace(/±/g, ""); fileName = fileName.replace(/#/g, ""); fileName = fileName.replace(/%/g, ""); fileName = fileName.replace(/Π/g, ""); fileName = fileName.replace(/π/g, ""); fileName = fileName.replace(/·/g, "."); // console.log('文件名处理结果', fileName) return fileName; } export function beforeAvatarUpload(file, fileList, maxValue) { if (file) { // 文件大小校验 const isLt2M = file.size / 1024 / 1024 < maxValue; if (!isLt2M) { fileList.splice(fileList.length - 1, 1); // console.log('文件大小不能超过 ' + maxValue + ' MB!') } return isLt2M; } return 0; } export function exnameFix(file, isShow, names) { const tmpcnt = file.name.lastIndexOf("."); const exname = file.name.substring(tmpcnt + 1); // 获取后缀名 if (names.indexOf(exname) < 0) { if (isShow === "1") { ElMessage({ message: "不支持的格式。文件仅支持 " + names + "格式!", type: "error", }); } return false; } return true; } export async function httpRequest(file, name, type, maxValue) { // 阿里云OSS上传 if (!beforeAvatarUpload(file.file, [], maxValue)) { return; } // console.log('开始上传') // 判断扩展名 const tmpcnt = file.file.name.lastIndexOf("."); // 获取.的下标 const exname = file.file.name.substring(tmpcnt + 1); // 获取后缀名 // console.log(exname, '后缀名') const now = new Date(); const year = now.getFullYear(); const month = now.getMonth() + 1; const day = now.getDate() > 10 ? now.getDate() : "0" + now.getDate(); const filePath = "/attachment-file/assess/" + type + "/" + year + "/" + month + "/" + day + "/" + now.getTime(); // console.log(filePath, '文件存储路径') const fileName = filePath + name + "." + exname; // console.log(fileName, '文件名') await client .multipartUpload(fileName, file.file, { // eslint-disable-next-line no-unused-vars progress: await function (p, checkpoint) { file.onProgress({ percent: Math.floor(p * 100) }); // 触发el-upload组件的onProgress方法 }, // mime: type, }) .then( await function (result) { // console.log(result) if (result.res.status === 200) { // file.onSuccess(result) // 触发el-upload组件的onSuccess方法 file.file.url = fileName; } // eslint-disable-next-line handle-callback-err,no-unused-vars } ) .catch(function (err) { console.log("上传失败", err); file.onError("上传失败"); // 触发el-upload组件的onError方法,此方法会移除文件列表 }); } // eslint-disable-next-line no-unused-vars function getTemporaryByUrl(url) { return request({ url: prefix + "/oss/file/getTemporaryUrl", method: "get", params: { url: url }, }); } function getFileTemporaryLookUrlWithWatermark(url) { return request({ url: prefix + "/oss/file/getFileTemporaryLookUrlWithWatermark", method: "get", params: { url: url }, }); } export async function openWindowOnUrl(row) { if (row.url === undefined || row.url === null || row.url === "") { // Message.error('没有获取到文件的url') return; } let rowUrl = ""; let suffix = row.name.substring(row.name.lastIndexOf(".") + 1); if ( suffix === "jpg" || suffix === "png" || suffix === "gif" || suffix === "bmp" || suffix === "jpeg" ) { await getTemporaryByUrl(row.url).then((data) => { // eslint-disable-next-line no-undef onPreview(data); }); return; } await getTemporaryByUrl(row.url).then((data) => { rowUrl = data; }); suffix = suffix.toLowerCase(); if ( suffix === "pdf" || suffix === "xml" || suffix === "rar" || suffix === "zip" || suffix === "jar" || suffix === "7z" ) { var url = "https://view.xdocin.com/xdoc?_xdoc=" + encodeURIComponent(rowUrl); //预览参数 var ops = { // "pdf": true, //word文档尝试以pdf方式显示,默认false // "img": true, //尝试以图片方式显示,默认false // "watermark": "XDOC文档预览", //水印文本,显示水印 // "saveable": true, //是否允许保存源文件,默认false // "printable": false, //是否允许打印,默认true // "copyable": false, //是否允许选择复制内容,默认true // "toolbar": false, //是否显示工具条,默认true // "title": "文档预览", //自定义标题 expire: 1, //预览链接有效期,单位分钟,默认永久有效 // "limit": "1,3", //限制页数,如:“5”表示只显示前5页,“2,5”表示从第2页开始的5页,对pdf/doc/docx/ppt/pptx有效 // "mtime": 1633093801, //文件修改时间戳(精确到秒)或修改时间(如:2021-10-01 21:10:01),值改变刷新缓存,实时预览 // "referer": "", //读取http源文件时传入的referer值 // "header": "", //读取http源文件时传入的header值,格式为JSON字符串 // "ip": "", //限制ip预览,多个用","分割,支持*,如:192.168.* // "password": "", //口令,用户需要输入正确的口令,才能预览 }; //传入预览参数 for (var a in ops) { url += "&" + a + "=" + encodeURIComponent(ops[a]); } window.open(url, "_blank"); } else { window.open( "https://view.officeapps.live.com/op/view.aspx?src=" + encodeURIComponent(rowUrl), "_blank" ); } } export async function toHref(row) { if (row.url === null || row.url === undefined || row.url === "") { // Message.error('没有获取到文件的url') return; } const link = document.createElement("a"); await getTemporaryByUrl(row.url).then((data) => { const url = data; // 完整的url则直接使用 // 这里是将url转成blob地址, fetch(url) .then((res) => res.blob()) .then((blob) => { // 将链接地址字符内容转变成blob地址 link.href = URL.createObjectURL(blob); link.download = row.name || ""; // 下载文件的名字 // a.download = url.split('/')[url.split('/').length -1] // // 下载文件的名字 document.body.appendChild(link); link.click(); // 在资源下载完成后 清除 占用的缓存资源 window.URL.revokeObjectURL(link.href); document.body.removeChild(link); }); }); } /** * 身份证下载,虚化加水印 * @param {*} row * @returns */ export async function toSFZHref(row) { if (row.url === null || row.url === undefined || row.url === "") { // Message.error('没有获取到文件的url') return; } const link = document.createElement("a"); await getFileTemporaryLookUrlWithWatermark(row.url).then((data) => { const url = data; // 完整的url则直接使用 // 这里是将url转成blob地址, fetch(url) .then((res) => res.blob()) .then((blob) => { // 将链接地址字符内容转变成blob地址 link.href = URL.createObjectURL(blob); link.download = row.name || ""; // 下载文件的名字 // a.download = url.split('/')[url.split('/').length -1] // // 下载文件的名字 document.body.appendChild(link); link.click(); // 在资源下载完成后 清除 占用的缓存资源 window.URL.revokeObjectURL(link.href); document.body.removeChild(link); }); }); } export async function toHrefByUrlAndName(url, name) { if (url === null || url === undefined || url === "") { // Message.error('没有获取到文件的url') return; } const link = document.createElement("a"); await getTemporaryByUrl(url).then((data) => { const url = data; // 完整的url则直接使用 // 这里是将url转成blob地址, fetch(url) .then((res) => res.blob()) .then((blob) => { // 将链接地址字符内容转变成blob地址 link.href = URL.createObjectURL(blob); link.download = name || ""; // 下载文件的名字 // a.download = url.split('/')[url.split('/').length -1] // // 下载文件的名字 document.body.appendChild(link); link.click(); // 在资源下载完成后 清除 占用的缓存资源 window.URL.revokeObjectURL(link.href); document.body.removeChild(link); }); }); }