| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- 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);
- });
- });
- }
|