OSSService.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. import OSS from "ali-oss";
  2. import { ElMessage } from "element-plus";
  3. import request from "@/utils/httpRequest";
  4. import { PUBLIC_MODULES_PATH as prefix } from "../AppPath";
  5. export default class OSSSerive {
  6. queryById(id) {
  7. return request({
  8. url: prefix + "/oss/file/queryById",
  9. method: "get",
  10. params: { id: id },
  11. });
  12. }
  13. saveMsg(param) {
  14. return request({
  15. url: prefix + "/oss/file/saveMsg",
  16. method: "post",
  17. data: param,
  18. });
  19. }
  20. findFileList(attachmentId) {
  21. return request({
  22. url: prefix + "/oss/file/findFileList",
  23. method: "get",
  24. params: { attachmentId: attachmentId },
  25. });
  26. }
  27. getTemporaryUrl(url) {
  28. return request({
  29. url: prefix + "/oss/file/getTemporaryUrl",
  30. method: "get",
  31. params: { url: url },
  32. });
  33. }
  34. getFileSizeByUrl(url) {
  35. return request({
  36. url: prefix + "/oss/file/getFileSizeByUrl",
  37. method: "get",
  38. params: { url: url },
  39. });
  40. }
  41. getSFZSizeByUrl(url) {
  42. return request({
  43. url: prefix + "/oss/file/getSFZSizeByUrl",
  44. method: "get",
  45. params: { url: url },
  46. });
  47. }
  48. /**
  49. * 根据url获取临时文件带水印、虚化原图地址
  50. * @param {*} url
  51. * @returns
  52. */
  53. getFileTemporaryLookUrlWithWatermark(url) {
  54. return request({
  55. url: prefix + "/oss/file/getFileTemporaryLookUrlWithWatermark",
  56. method: "get",
  57. params: { url: url },
  58. });
  59. }
  60. /**
  61. * 根据url获取临时文件带水印、虚化、缩略图地址
  62. * @param {*} url
  63. * @returns
  64. */
  65. getThumbnailTemporaryWithWatermarkDimLookUrl(url) {
  66. return request({
  67. url:
  68. prefix +
  69. "/oss/file/getThumbnailTemporaryWithWatermarkDimLookUrl",
  70. method: "get",
  71. params: { url: url },
  72. });
  73. }
  74. deleteMsgById(id) {
  75. return request({
  76. url: prefix + "/oss/file/deleteMsgById",
  77. method: "get",
  78. params: { id: id },
  79. });
  80. }
  81. downLoadAttach(url) {
  82. return request({
  83. url: prefix + "/oss/file/downLoadAttach",
  84. method: "get",
  85. responseType: "blob",
  86. params: { file: url },
  87. });
  88. }
  89. disposeXmlFile(data) {
  90. return request({
  91. url: prefix + "/oss/file/disposeXmlFile",
  92. method: "post",
  93. data: data,
  94. });
  95. }
  96. downLoadFileDisposeXmlFile(url) {
  97. return request({
  98. url: prefix + "/oss/file/downLoadFileDisposeXmlFile",
  99. method: "post",
  100. params: { file: url },
  101. });
  102. }
  103. isUsedByInvoiceNumber(invoiceNumber) {
  104. return request({
  105. url: prefix + "/oss/file/isUsedByInvoiceNumber",
  106. method: "get",
  107. params: { invoiceNumber: invoiceNumber },
  108. });
  109. }
  110. downloadAndUploadToMyCollect(data) {
  111. return request({
  112. url: prefix + "/oss/file/downloadAndUploadToMyCollect",
  113. method: "post",
  114. data: data,
  115. });
  116. }
  117. }
  118. export const client = new OSS({
  119. region: "oss-cn-hangzhou", // oss地址
  120. accessKeyId: "LTAI5tKa6kzGr5EyPWJB4EcD", // 通过阿里云控制台创建的AccessKey ID。
  121. accessKeySecret: "arHxB7ZPhizrBYf4844TtyaRctPMgW", // 通过阿里云控制台创建的AccessKey Secret。
  122. // bucket: "xgxm-test", // 仓库名字
  123. bucket: 'xg-pg', // 仓库名字
  124. useFetch: true, // 支持上传大于100KB的文件
  125. secure: false, // 返回的url为https
  126. });
  127. export const headers = {
  128. // 指定该Object被下载时网页的缓存行为。
  129. "Cache-Control": "no-cache",
  130. // 指定该Object被下载时的名称。
  131. "Content-Disposition": "inline",
  132. // 指定该Object被下载时的内容编码格式。
  133. "Content-Encoding": "UTF-8",
  134. // 指定过期时间。
  135. Expires: "Wed, 08 Jul 2023 16:57:01 GMT",
  136. // 指定Object的存储类型。
  137. "x-oss-storage-class": "Standard",
  138. // 指定Object的访问权限。
  139. // 'x-oss-object-acl': 'private',
  140. // 设置Object的标签,可同时设置多个标签。
  141. "x-oss-tagging": "Tag1=1&Tag2=2",
  142. // 指定CopyObject操作时是否覆盖同名目标Object。此处设置为true,表示禁止覆盖同名Object。
  143. "x-oss-forbid-overwrite": "true",
  144. // 'secure': 'true'
  145. };
  146. export function onChange(file, files) {
  147. // console.log(file, 'file', files, 'Filest')
  148. return files;
  149. }
  150. // eslint-disable-next-line no-unused-vars
  151. export function handleUploadSuccess(response, file, fileS) {
  152. // console.log(response, file, fileS, 'response, file, fileList')
  153. }
  154. export function handleRemove(e, files) {
  155. return files;
  156. }
  157. export function fileName(file) {
  158. // console.log('文件名称处理')
  159. const tmpcnt = file.file.name.lastIndexOf(".");
  160. let fileName = file.file.name.substring(0, tmpcnt);
  161. // 将单引号‘’都转换成',将双引号“”都转换成"
  162. fileName = fileName.replace(/’|‘/g, "'").replace(/“|”/g, "");
  163. // 将中括号【】转换成[],将大括号{}转换成{}
  164. fileName = fileName
  165. .replace(/【/g, "(")
  166. .replace(/】/g, ")")
  167. .replace(/{/g, "(")
  168. .replace(/}/g, ")");
  169. fileName = fileName
  170. .replace(/\[/g, "(")
  171. .replace(/]/g, ")")
  172. .replace(/{/g, "(")
  173. .replace(/}/g, ")");
  174. // 将逗号,转换成,,将:转换成:
  175. fileName = fileName.replace(/,/g, ",").replace(/:/g, ":");
  176. // 将中文——转换为英文-
  177. fileName = fileName.replace(/—/g, "-");
  178. fileName = fileName.replace(/……/g, "");
  179. fileName = fileName.replace(/±/g, "");
  180. fileName = fileName.replace(/#/g, "");
  181. fileName = fileName.replace(/%/g, "");
  182. fileName = fileName.replace(/Π/g, "");
  183. fileName = fileName.replace(/π/g, "");
  184. fileName = fileName.replace(/·/g, ".");
  185. // console.log('文件名处理结果', fileName)
  186. return fileName;
  187. }
  188. export function fileNameInvoice(file) {
  189. // console.log('文件名称处理')
  190. if (!file.name) {
  191. file = file.file;
  192. }
  193. const tmpcnt = file.name.lastIndexOf(".");
  194. let fileName = file.name.substring(0, tmpcnt);
  195. // 将单引号‘’都转换成',将双引号“”都转换成"
  196. fileName = fileName.replace(/’|‘/g, "'").replace(/“|”/g, "");
  197. // 将中括号【】转换成[],将大括号{}转换成{}
  198. fileName = fileName
  199. .replace(/【/g, "(")
  200. .replace(/】/g, ")")
  201. .replace(/{/g, "(")
  202. .replace(/}/g, ")");
  203. fileName = fileName
  204. .replace(/\[/g, "(")
  205. .replace(/]/g, ")")
  206. .replace(/{/g, "(")
  207. .replace(/}/g, ")");
  208. // 将逗号,转换成,,将:转换成:
  209. fileName = fileName.replace(/,/g, ",").replace(/:/g, ":");
  210. // 将中文——转换为英文-
  211. fileName = fileName.replace(/—/g, "-");
  212. fileName = fileName.replace(/……/g, "");
  213. fileName = fileName.replace(/±/g, "");
  214. fileName = fileName.replace(/#/g, "");
  215. fileName = fileName.replace(/%/g, "");
  216. fileName = fileName.replace(/Π/g, "");
  217. fileName = fileName.replace(/π/g, "");
  218. fileName = fileName.replace(/·/g, ".");
  219. // console.log('文件名处理结果', fileName)
  220. return fileName;
  221. }
  222. export function beforeAvatarUpload(file, fileList, maxValue) {
  223. if (file) {
  224. // 文件大小校验
  225. const isLt2M = file.size / 1024 / 1024 < maxValue;
  226. if (!isLt2M) {
  227. fileList.splice(fileList.length - 1, 1);
  228. // console.log('文件大小不能超过 ' + maxValue + ' MB!')
  229. }
  230. return isLt2M;
  231. }
  232. return 0;
  233. }
  234. export function exnameFix(file, isShow, names) {
  235. const tmpcnt = file.name.lastIndexOf(".");
  236. const exname = file.name.substring(tmpcnt + 1); // 获取后缀名
  237. if (names.indexOf(exname) < 0) {
  238. if (isShow === "1") {
  239. ElMessage({
  240. message: "不支持的格式。文件仅支持 " + names + "格式!",
  241. type: "error",
  242. });
  243. }
  244. return false;
  245. }
  246. return true;
  247. }
  248. export async function httpRequest(file, name, type, maxValue) {
  249. // 阿里云OSS上传
  250. if (!beforeAvatarUpload(file.file, [], maxValue)) {
  251. return;
  252. }
  253. // console.log('开始上传')
  254. // 判断扩展名
  255. const tmpcnt = file.file.name.lastIndexOf("."); // 获取.的下标
  256. const exname = file.file.name.substring(tmpcnt + 1); // 获取后缀名
  257. // console.log(exname, '后缀名')
  258. const now = new Date();
  259. const year = now.getFullYear();
  260. const month = now.getMonth() + 1;
  261. const day = now.getDate() > 10 ? now.getDate() : "0" + now.getDate();
  262. const filePath =
  263. "/attachment-file/assess/" +
  264. type +
  265. "/" +
  266. year +
  267. "/" +
  268. month +
  269. "/" +
  270. day +
  271. "/" +
  272. now.getTime();
  273. // console.log(filePath, '文件存储路径')
  274. const fileName = filePath + name + "." + exname;
  275. // console.log(fileName, '文件名')
  276. await client
  277. .multipartUpload(fileName, file.file, {
  278. // eslint-disable-next-line no-unused-vars
  279. progress: await function (p, checkpoint) {
  280. file.onProgress({ percent: Math.floor(p * 100) }); // 触发el-upload组件的onProgress方法
  281. },
  282. // mime: type,
  283. })
  284. .then(
  285. await function (result) {
  286. // console.log(result)
  287. if (result.res.status === 200) {
  288. // file.onSuccess(result) // 触发el-upload组件的onSuccess方法
  289. file.file.url = fileName;
  290. }
  291. // eslint-disable-next-line handle-callback-err,no-unused-vars
  292. }
  293. )
  294. .catch(function (err) {
  295. console.log("上传失败", err);
  296. file.onError("上传失败"); // 触发el-upload组件的onError方法,此方法会移除文件列表
  297. });
  298. }
  299. // eslint-disable-next-line no-unused-vars
  300. function getTemporaryByUrl(url) {
  301. return request({
  302. url: prefix + "/oss/file/getTemporaryUrl",
  303. method: "get",
  304. params: { url: url },
  305. });
  306. }
  307. function getFileTemporaryLookUrlWithWatermark(url) {
  308. return request({
  309. url: prefix + "/oss/file/getFileTemporaryLookUrlWithWatermark",
  310. method: "get",
  311. params: { url: url },
  312. });
  313. }
  314. export async function openWindowOnUrl(row) {
  315. if (row.url === undefined || row.url === null || row.url === "") {
  316. // Message.error('没有获取到文件的url')
  317. return;
  318. }
  319. let rowUrl = "";
  320. let suffix = row.name.substring(row.name.lastIndexOf(".") + 1);
  321. if (
  322. suffix === "jpg" ||
  323. suffix === "png" ||
  324. suffix === "gif" ||
  325. suffix === "bmp" ||
  326. suffix === "jpeg"
  327. ) {
  328. await getTemporaryByUrl(row.url).then((data) => {
  329. // eslint-disable-next-line no-undef
  330. onPreview(data);
  331. });
  332. return;
  333. }
  334. await getTemporaryByUrl(row.url).then((data) => {
  335. rowUrl = data;
  336. });
  337. suffix = suffix.toLowerCase();
  338. if (
  339. suffix === "pdf" ||
  340. suffix === "xml" ||
  341. suffix === "rar" ||
  342. suffix === "zip" ||
  343. suffix === "jar" ||
  344. suffix === "7z"
  345. ) {
  346. var url =
  347. "https://view.xdocin.com/xdoc?_xdoc=" + encodeURIComponent(rowUrl);
  348. //预览参数
  349. var ops = {
  350. // "pdf": true, //word文档尝试以pdf方式显示,默认false
  351. // "img": true, //尝试以图片方式显示,默认false
  352. // "watermark": "XDOC文档预览", //水印文本,显示水印
  353. // "saveable": true, //是否允许保存源文件,默认false
  354. // "printable": false, //是否允许打印,默认true
  355. // "copyable": false, //是否允许选择复制内容,默认true
  356. // "toolbar": false, //是否显示工具条,默认true
  357. // "title": "文档预览", //自定义标题
  358. expire: 1, //预览链接有效期,单位分钟,默认永久有效
  359. // "limit": "1,3", //限制页数,如:“5”表示只显示前5页,“2,5”表示从第2页开始的5页,对pdf/doc/docx/ppt/pptx有效
  360. // "mtime": 1633093801, //文件修改时间戳(精确到秒)或修改时间(如:2021-10-01 21:10:01),值改变刷新缓存,实时预览
  361. // "referer": "", //读取http源文件时传入的referer值
  362. // "header": "", //读取http源文件时传入的header值,格式为JSON字符串
  363. // "ip": "", //限制ip预览,多个用","分割,支持*,如:192.168.*
  364. // "password": "", //口令,用户需要输入正确的口令,才能预览
  365. };
  366. //传入预览参数
  367. for (var a in ops) {
  368. url += "&" + a + "=" + encodeURIComponent(ops[a]);
  369. }
  370. window.open(url, "_blank");
  371. } else {
  372. window.open(
  373. "https://view.officeapps.live.com/op/view.aspx?src=" +
  374. encodeURIComponent(rowUrl),
  375. "_blank"
  376. );
  377. }
  378. }
  379. export async function toHref(row) {
  380. if (row.url === null || row.url === undefined || row.url === "") {
  381. // Message.error('没有获取到文件的url')
  382. return;
  383. }
  384. const link = document.createElement("a");
  385. await getTemporaryByUrl(row.url).then((data) => {
  386. const url = data; // 完整的url则直接使用
  387. // 这里是将url转成blob地址,
  388. fetch(url)
  389. .then((res) => res.blob())
  390. .then((blob) => {
  391. // 将链接地址字符内容转变成blob地址
  392. link.href = URL.createObjectURL(blob);
  393. link.download = row.name || ""; // 下载文件的名字
  394. // a.download = url.split('/')[url.split('/').length -1] // // 下载文件的名字
  395. document.body.appendChild(link);
  396. link.click();
  397. // 在资源下载完成后 清除 占用的缓存资源
  398. window.URL.revokeObjectURL(link.href);
  399. document.body.removeChild(link);
  400. });
  401. });
  402. }
  403. /**
  404. * 身份证下载,虚化加水印
  405. * @param {*} row
  406. * @returns
  407. */
  408. export async function toSFZHref(row) {
  409. if (row.url === null || row.url === undefined || row.url === "") {
  410. // Message.error('没有获取到文件的url')
  411. return;
  412. }
  413. const link = document.createElement("a");
  414. await getFileTemporaryLookUrlWithWatermark(row.url).then((data) => {
  415. const url = data; // 完整的url则直接使用
  416. // 这里是将url转成blob地址,
  417. fetch(url)
  418. .then((res) => res.blob())
  419. .then((blob) => {
  420. // 将链接地址字符内容转变成blob地址
  421. link.href = URL.createObjectURL(blob);
  422. link.download = row.name || ""; // 下载文件的名字
  423. // a.download = url.split('/')[url.split('/').length -1] // // 下载文件的名字
  424. document.body.appendChild(link);
  425. link.click();
  426. // 在资源下载完成后 清除 占用的缓存资源
  427. window.URL.revokeObjectURL(link.href);
  428. document.body.removeChild(link);
  429. });
  430. });
  431. }
  432. export async function toHrefByUrlAndName(url, name) {
  433. if (url === null || url === undefined || url === "") {
  434. // Message.error('没有获取到文件的url')
  435. return;
  436. }
  437. const link = document.createElement("a");
  438. await getTemporaryByUrl(url).then((data) => {
  439. const url = data; // 完整的url则直接使用
  440. // 这里是将url转成blob地址,
  441. fetch(url)
  442. .then((res) => res.blob())
  443. .then((blob) => {
  444. // 将链接地址字符内容转变成blob地址
  445. link.href = URL.createObjectURL(blob);
  446. link.download = name || ""; // 下载文件的名字
  447. // a.download = url.split('/')[url.split('/').length -1] // // 下载文件的名字
  448. document.body.appendChild(link);
  449. link.click();
  450. // 在资源下载完成后 清除 占用的缓存资源
  451. window.URL.revokeObjectURL(link.href);
  452. document.body.removeChild(link);
  453. });
  454. });
  455. }