疯狂的狮子li
2021-09-27 b082da73a1e0259738ccb33ebf7d80cad3dc3a10
ruoyi-ui/src/plugins/download.js
对比新文件
@@ -0,0 +1,71 @@
import { saveAs } from 'file-saver'
import axios from 'axios'
import { getToken } from '@/utils/auth'
const baseURL = process.env.VUE_APP_BASE_API
export default {
  excel(url, params) {
    // get璇锋眰鏄犲皠params鍙傛暟
    if (params) {
      let urlparams = url + '?';
      for (const propName of Object.keys(params)) {
        const value = params[propName];
        var part = encodeURIComponent(propName) + "=";
        if (value !== null && typeof(value) !== "undefined") {
          if (typeof value === 'object') {
            for (const key of Object.keys(value)) {
              if (value[key] !== null && typeof (value[key]) !== 'undefined') {
                let params = propName + '[' + key + ']';
                let subPart = encodeURIComponent(params) + '=';
                urlparams += subPart + encodeURIComponent(value[key]) + '&';
              }
            }
          } else {
            urlparams += part + encodeURIComponent(value) + "&";
          }
        }
      }
      urlparams = urlparams.slice(0, -1);
      url = urlparams;
    }
    url = baseURL + url
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
      headers: { 'Authorization': 'Bearer ' + getToken() }
    }).then(res => {
      const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
      this.saveAs(blob, decodeURI(res.headers['download-filename']))
    })
  },
  oss(ossId, name) {
    var url = baseURL + '/system/oss/download/' + ossId
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
      headers: { 'Authorization': 'Bearer ' + getToken() }
    }).then(res => {
      const blob = new Blob([res.data], { type: 'application/octet-stream' })
      this.saveAs(blob, name)
    })
  },
  zip(url, name) {
    var url = baseURL + url
    axios({
      method: 'get',
      url: url,
      responseType: 'blob',
      headers: { 'Authorization': 'Bearer ' + getToken() }
    }).then(res => {
      const blob = new Blob([res.data], { type: 'application/zip' })
      this.saveAs(blob, name)
    })
  },
  saveAs(text, name, opts) {
    saveAs(text, name, opts);
  }
}