From 10ae0bce65e6b33dbaec80d702372a39233775df Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期六, 01 一月 2022 09:50:35 +0800 Subject: [PATCH] 用户修改减少一次角色列表关联查询 --- ruoyi-ui/src/utils/request.js | 72 ++++++++++++++++++++++++++---------- 1 files changed, 52 insertions(+), 20 deletions(-) diff --git a/ruoyi-ui/src/utils/request.js b/ruoyi-ui/src/utils/request.js index ae89f24..12e535a 100644 --- a/ruoyi-ui/src/utils/request.js +++ b/ruoyi-ui/src/utils/request.js @@ -1,8 +1,14 @@ 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; +// 鏄惁鏄剧ず閲嶆柊鐧诲綍 +let isReloginShow; axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' // 鍒涘缓axios瀹炰緥 @@ -12,6 +18,7 @@ // 瓒呮椂 timeout: 10000 }) + // request鎷︽埅鍣� service.interceptors.request.use(config => { // 鏄惁闇�瑕佽缃� token @@ -21,22 +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)) { - let params = propName + '[' + key + ']'; - var 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; @@ -53,17 +45,31 @@ 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('鐧诲綍鐘舵�佸凡杩囨湡锛屾偍鍙互缁х画鐣欏湪璇ラ〉闈紝鎴栬�呴噸鏂扮櫥褰�', '绯荤粺鎻愮ず', { + if (!isReloginShow) { + isReloginShow = true; + MessageBox.confirm('鐧诲綍鐘舵�佸凡杩囨湡锛屾偍鍙互缁х画鐣欏湪璇ラ〉闈紝鎴栬�呴噸鏂扮櫥褰�', '绯荤粺鎻愮ず', { confirmButtonText: '閲嶆柊鐧诲綍', cancelButtonText: '鍙栨秷', type: 'warning' } ).then(() => { + isReloginShow = false; store.dispatch('LogOut').then(() => { - location.href = '/index'; + // 濡傛灉鏄櫥褰曢〉闈笉闇�瑕侀噸鏂板姞杞� + if (window.location.hash.indexOf("#/login") != 0) { + location.href = '/index'; + } }) - }) + }).catch(() => { + isReloginShow = false; + }); + } + return Promise.reject('鏃犳晥鐨勪細璇濓紝鎴栬�呬細璇濆凡杩囨湡锛岃閲嶆柊鐧诲綍銆�') } else if (code === 500) { Message({ message: msg, @@ -100,4 +106,30 @@ } ) +// 閫氱敤涓嬭浇鏂规硶 +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 { + const resText = await data.text(); + const rspObj = JSON.parse(resText); + const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'] + Message.error(errMsg); + } + downloadLoadingInstance.close(); + }).catch((r) => { + console.error(r) + Message.error('涓嬭浇鏂囦欢鍑虹幇閿欒锛岃鑱旂郴绠$悊鍛橈紒') + downloadLoadingInstance.close(); + }) +} + export default service -- Gitblit v1.9.3