From 42295ef2ac0a5e78674cf24b62d6834138f0ffdc Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期一, 29 十一月 2021 13:56:25 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into satoken --- ruoyi-ui/src/utils/request.js | 55 +++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 35 insertions(+), 20 deletions(-) diff --git a/ruoyi-ui/src/utils/request.js b/ruoyi-ui/src/utils/request.js index e8b3b5f..d376a76 100644 --- a/ruoyi-ui/src/utils/request.js +++ b/ruoyi-ui/src/utils/request.js @@ -1,8 +1,12 @@ import axios from 'axios' -import { Notification, MessageBox, Message } from 'element-ui' +import { Notification, MessageBox, Message, Loading } from 'element-ui' import store from '@/store' import { getToken } from '@/utils/auth' import errorCode from '@/utils/errorCode' +import { tansParams, blobValidate } from "@/utils/ruoyi"; +import { saveAs } from 'file-saver' + +let downloadLoadingInstance; axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' // 瀵瑰簲鍥介檯鍖栬祫婧愭枃浠跺悗缂� @@ -14,6 +18,7 @@ // 瓒呮椂 timeout: 10000 }) + // request鎷︽埅鍣� service.interceptors.request.use(config => { // 鏄惁闇�瑕佽缃� token @@ -23,24 +28,7 @@ } // get璇锋眰鏄犲皠params鍙傛暟 if (config.method === 'get' && config.params) { - let url = config.url + '?'; - for (const propName of Object.keys(config.params)) { - const value = config.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) + '='; - url += subPart + encodeURIComponent(value[key]) + '&'; - } - } - } else { - url += part + encodeURIComponent(value) + "&"; - } - } - } + let url = config.url + '?' + tansParams(config.params); url = url.slice(0, -1); config.params = {}; config.url = url; @@ -57,6 +45,10 @@ const code = res.data.code || 200; // 鑾峰彇閿欒淇℃伅 const msg = errorCode[code] || res.data.msg || errorCode['default'] + // 浜岃繘鍒舵暟鎹垯鐩存帴杩斿洖 + if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){ + return res.data + } if (code === 401) { MessageBox.confirm('鐧诲綍鐘舵�佸凡杩囨湡锛屾偍鍙互缁х画鐣欏湪璇ラ〉闈紝鎴栬�呴噸鏂扮櫥褰�', '绯荤粺鎻愮ず', { confirmButtonText: '閲嶆柊鐧诲綍', @@ -65,7 +57,7 @@ } ).then(() => { store.dispatch('LogOut').then(() => { - location.href = this.$router.options.base + '/index'; + location.href = process.env.VUE_APP_CONTEXT_PATH + "index"; }) }).catch(() => {}); return Promise.reject('鏃犳晥鐨勪細璇濓紝鎴栬�呬細璇濆凡杩囨湡锛岃閲嶆柊鐧诲綍銆�') @@ -105,4 +97,27 @@ } ) +// 閫氱敤涓嬭浇鏂规硶 +export function download(url, params, filename) { + downloadLoadingInstance = Loading.service({ text: "姝e湪涓嬭浇鏁版嵁锛岃绋嶅��", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", }) + return service.post(url, params, { + transformRequest: [(params) => { return tansParams(params) }], + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + responseType: 'blob' + }).then(async (data) => { + const isLogin = await blobValidate(data); + if (isLogin) { + const blob = new Blob([data]) + saveAs(blob, filename) + } else { + Message.error('鏃犳晥鐨勪細璇濓紝鎴栬�呬細璇濆凡杩囨湡锛岃閲嶆柊鐧诲綍銆�'); + } + downloadLoadingInstance.close(); + }).catch((r) => { + console.error(r) + Message.error('涓嬭浇鏂囦欢鍑虹幇閿欒锛岃鑱旂郴绠$悊鍛橈紒') + downloadLoadingInstance.close(); + }) +} + export default service -- Gitblit v1.9.3