沈丘营商办后台前端项目
346149741
2024-06-18 9fb6a0ff49c2af567be2e3adaf93c4c301b3f102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import axios from "axios";
import { ElMessage } from "element-plus";
import { FileSaverOptions, saveAs } from "file-saver";
import { getToken } from "@/utils/auth";
import errorCode from "@/utils/errorCode";
import { blobValidate } from "@/utils/ruoyi";
 
const baseURL = import.meta.env.VITE_APP_BASE_API;
 
export default {
  name(name: string, isDelete = true) {
    // prettier-ignore
    var url = baseURL + "/common/download?fileName=" + encodeURI(name) + "&delete=" + isDelete
    axios({
      method: "get",
      url: url,
      responseType: "blob",
      headers: { Authorization: "Bearer " + getToken() },
    }).then(async (res) => {
      const isLogin = await blobValidate(res.data);
      if (isLogin) {
        const blob = new Blob([res.data]);
        this.saveAs(blob, decodeURI(res.headers["download-filename"]), null);
      } else {
        this.printErrMsg(res.data);
      }
    });
  },
  resource(resource: string) {
    // prettier-ignore
    var url = baseURL + "/common/download/resource?resource=" + encodeURI(resource);
    axios({
      method: "get",
      url: url,
      responseType: "blob",
      headers: { Authorization: "Bearer " + getToken() },
    }).then(async (res) => {
      const isLogin = await blobValidate(res.data);
      if (isLogin) {
        const blob = new Blob([res.data]);
        this.saveAs(blob, decodeURI(res.headers["download-filename"]), null);
      } else {
        this.printErrMsg(res.data);
      }
    });
  },
  zip(url: string, name: any) {
    axios({
      method: "get",
      url: baseURL + url,
      responseType: "blob",
      headers: { Authorization: "Bearer " + getToken() },
    }).then(async (res) => {
      const isLogin = await blobValidate(res.data);
      if (isLogin) {
        const blob = new Blob([res.data], { type: "application/zip" });
        this.saveAs(blob, name, null);
      } else {
        this.printErrMsg(res.data);
      }
    });
  },
  saveAs(text: string | Blob, name: string | undefined, opts?: any) {
    saveAs(text, name, opts);
  },
  async printErrMsg(data: { text: () => any }) {
    const resText = await data.text();
    const rspObj = JSON.parse(resText);
    // prettier-ignore
    const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
    ElMessage.error(errMsg);
  },
};