From 785c7495c11b76b60aa86545eecc3ec3b870d5a5 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期一, 24 十月 2022 09:43:35 +0800
Subject: [PATCH] Merge remote-tracking branch 'ruoyi-vue/master' into dev
---
ruoyi-ui/src/utils/request.js | 97 +++++++++++++++++++++++++++++++++++++-----------
1 files changed, 75 insertions(+), 22 deletions(-)
diff --git a/ruoyi-ui/src/utils/request.js b/ruoyi-ui/src/utils/request.js
index 5ce0d00..2473bc1 100644
--- a/ruoyi-ui/src/utils/request.js
+++ b/ruoyi-ui/src/utils/request.js
@@ -1,8 +1,15 @@
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 cache from '@/plugins/cache'
+import { saveAs } from 'file-saver'
+
+let downloadLoadingInstance;
+// 鏄惁鏄剧ず閲嶆柊鐧诲綍
+export let isRelogin = { show: false };
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 瀵瑰簲鍥介檯鍖栬祫婧愭枃浠跺悗缂�
@@ -14,36 +21,45 @@
// 瓒呮椂
timeout: 10000
})
+
// request鎷︽埅鍣�
service.interceptors.request.use(config => {
// 鏄惁闇�瑕佽缃� token
const isToken = (config.headers || {}).isToken === false
+ // 鏄惁闇�瑕侀槻姝㈡暟鎹噸澶嶆彁浜�
+ const isRepeatSubmit = (config.headers || {}).repeatSubmit === false
if (getToken() && !isToken) {
config.headers['Authorization'] = 'Bearer ' + getToken() // 璁╂瘡涓姹傛惡甯﹁嚜瀹氫箟token 璇锋牴鎹疄闄呮儏鍐佃嚜琛屼慨鏀�
}
// 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;
+ }
+ if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
+ const requestObj = {
+ url: config.url,
+ data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
+ time: new Date().getTime()
+ }
+ const sessionObj = cache.session.getJSON('sessionObj')
+ if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
+ cache.session.setJSON('sessionObj', requestObj)
+ } else {
+ const s_url = sessionObj.url; // 璇锋眰鍦板潃
+ const s_data = sessionObj.data; // 璇锋眰鏁版嵁
+ const s_time = sessionObj.time; // 璇锋眰鏃堕棿
+ const interval = 1000; // 闂撮殧鏃堕棿(ms)锛屽皬浜庢鏃堕棿瑙嗕负閲嶅鎻愪氦
+ if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
+ const message = '鏁版嵁姝e湪澶勭悊锛岃鍕块噸澶嶆彁浜�';
+ console.warn(`[${s_url}]: ` + message)
+ return Promise.reject(new Error(message))
+ } else {
+ cache.session.setJSON('sessionObj', requestObj)
+ }
+ }
}
return config
}, error => {
@@ -57,17 +73,27 @@
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 (!isRelogin.show) {
+ isRelogin.show = true;
+ MessageBox.confirm('鐧诲綍鐘舵�佸凡杩囨湡锛屾偍鍙互缁х画鐣欏湪璇ラ〉闈紝鎴栬�呴噸鏂扮櫥褰�', '绯荤粺鎻愮ず', {
confirmButtonText: '閲嶆柊鐧诲綍',
cancelButtonText: '鍙栨秷',
type: 'warning'
}
).then(() => {
+ isRelogin.show = false;
store.dispatch('LogOut').then(() => {
- location.href = process.env.VUE_APP_CONTEXT_PATH;
+ location.href = process.env.VUE_APP_CONTEXT_PATH + "index";
})
- }).catch(() => {});
+ }).catch(() => {
+ isRelogin.show = false;
+ });
+ }
return Promise.reject('鏃犳晥鐨勪細璇濓紝鎴栬�呬細璇濆凡杩囨湡锛岃閲嶆柊鐧诲綍銆�')
} else if (code === 500) {
Message({
@@ -105,4 +131,31 @@
}
)
+// 閫氱敤涓嬭浇鏂规硶
+export function download(url, params, filename, config) {
+ 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',
+ ...config
+ }).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