import OSS from "ali-oss"; // import { ElMessage } from 'element-plus' import request from "../../common/request"; import { PUBLIC_MODULES_PATH as prefix } from "@/api/AppPath"; export default class OSSSerive { 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 }, }); } 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 }, }); } 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 }, }); } } export const client = new OSS({ // region: 'oss-cn-hangzhou', // oss地址 // accessKeyId: 'LTAI5tQDWoM9c1WyJNPs86rX', // 通过阿里云控制台创建的AccessKey ID。 // accessKeySecret: '84dDIx4edT1n78KUOqqSmDZ35pchJv', // 通过阿里云控制台创建的AccessKey Secret。 // bucket: 'xgxm-test', // 仓库名字 region: "oss-cn-hangzhou", // oss地址 accessKeyId: "LTAI5tKa6kzGr5EyPWJB4EcD", // 通过阿里云控制台创建的AccessKey ID。 accessKeySecret: "arHxB7ZPhizrBYf4844TtyaRctPMgW", // 通过阿里云控制台创建的AccessKey Secret。 bucket: "xg-ccpm", // 仓库名字 // 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 beforeAvatarUpload(file, fileList, maxValue) { // 文件大小校验 const isLt2M = file.size / 1024 / 1024 < maxValue; if (!isLt2M) { fileList.splice(fileList.length - 1, 1); // console.log('文件大小不能超过 ' + maxValue + ' MB!') } return isLt2M; } // export function exnameFix (file, isShow, names) { // // console.log('格式校验') // const tmpcnt = file.name.lastIndexOf('.') // const exname = file.name.substring(tmpcnt + 1) // 获取后缀名 // if (names.indexOf(exname) < 0) { // if (isShow === '1') { // ElMessage({ // message: '不支持的格式!', // 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 }, }); } 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; }); if (suffix === "pdf") { window.open( "https://view.xdocin.com/xdoc?_xdoc=" + encodeURIComponent(rowUrl), "_blank" ); } else if ( suffix === "rar" || suffix === "zip" || suffix === "jar" || suffix === "7z" ) { window.open( "https://view.xdocin.com/xdoc?_xdoc=" + encodeURIComponent(rowUrl), "_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); }); }); }