| | |
| | | import { saveAs } from 'file-saver' |
| | | import axios from 'axios' |
| | | import {Loading, Message} from 'element-ui' |
| | | import { saveAs } from 'file-saver' |
| | | import { getToken } from '@/utils/auth' |
| | | import { Message } from 'element-ui' |
| | | import errorCode from '@/utils/errorCode' |
| | | import { blobValidate } from "@/utils/ruoyi"; |
| | | |
| | | const baseURL = process.env.VUE_APP_BASE_API |
| | | let downloadLoadingInstance; |
| | | |
| | | 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(async (res) => { |
| | | const isLogin = await this.blobValidate(res.data); |
| | | if (isLogin) { |
| | | const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }) |
| | | this.saveAs(blob, decodeURI(res.headers['download-filename'])) |
| | | } else { |
| | | Message.error('无效的会话,或者会话已过期,请重新登录。'); |
| | | } |
| | | }) |
| | | }, |
| | | oss(ossId) { |
| | | var url = baseURL + '/system/oss/download/' + ossId |
| | | downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", }) |
| | | axios({ |
| | | method: 'get', |
| | | url: url, |
| | | responseType: 'blob', |
| | | headers: { 'Authorization': 'Bearer ' + getToken() } |
| | | }).then(async (res) => { |
| | | const isLogin = await this.blobValidate(res.data); |
| | | if (isLogin) { |
| | | }).then((res) => { |
| | | const isBlob = blobValidate(res.data); |
| | | if (isBlob) { |
| | | const blob = new Blob([res.data], { type: 'application/octet-stream' }) |
| | | this.saveAs(blob, decodeURI(res.headers['download-filename'])) |
| | | this.saveAs(blob, decodeURIComponent(res.headers['download-filename'])) |
| | | } else { |
| | | Message.error('无效的会话,或者会话已过期,请重新登录。'); |
| | | this.printErrMsg(res.data); |
| | | } |
| | | downloadLoadingInstance.close(); |
| | | }).catch((r) => { |
| | | console.error(r) |
| | | Message.error('下载文件出现错误,请联系管理员!') |
| | | downloadLoadingInstance.close(); |
| | | }) |
| | | }, |
| | | zip(url, name) { |
| | |
| | | method: 'get', |
| | | url: url, |
| | | responseType: 'blob', |
| | | headers: { 'Authorization': 'Bearer ' + getToken() } |
| | | }).then(async (res) => { |
| | | const isLogin = await this.blobValidate(res.data); |
| | | if (isLogin) { |
| | | headers: { |
| | | 'Authorization': 'Bearer ' + getToken(), |
| | | 'datasource': localStorage.getItem("dataName") |
| | | } |
| | | }).then((res) => { |
| | | const isBlob = blobValidate(res.data); |
| | | if (isBlob) { |
| | | const blob = new Blob([res.data], { type: 'application/zip' }) |
| | | this.saveAs(blob, name) |
| | | } else { |
| | | Message.error('无效的会话,或者会话已过期,请重新登录。'); |
| | | this.printErrMsg(res.data); |
| | | } |
| | | }) |
| | | }, |
| | | saveAs(text, name, opts) { |
| | | saveAs(text, name, opts); |
| | | }, |
| | | async blobValidate(data) { |
| | | try { |
| | | const text = await data.text(); |
| | | JSON.parse(text); |
| | | return false; |
| | | } catch (error) { |
| | | return true; |
| | | } |
| | | }, |
| | | async printErrMsg(data) { |
| | | const resText = await data.text(); |
| | | const rspObj = JSON.parse(resText); |
| | | const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'] |
| | | Message.error(errMsg); |
| | | } |
| | | } |
| | | |