From aad94b236ee3d6996cc3cfe1bfd6334a0c3557e9 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期一, 19 九月 2022 13:22:52 +0800
Subject: [PATCH] 通用下载方法新增config配置选项

---
 src/utils/request.js |   47 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/src/utils/request.js b/src/utils/request.js
index ffd5146..b0a528f 100644
--- a/src/utils/request.js
+++ b/src/utils/request.js
@@ -1,12 +1,15 @@
 import axios from 'axios'
 import { ElNotification , ElMessageBox, ElMessage, ElLoading } from 'element-plus'
-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'
+import useUserStore from '@/store/modules/user'
 
 let downloadLoadingInstance;
+// 鏄惁鏄剧ず閲嶆柊鐧诲綍
+export let isRelogin = { show: false };
 
 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
 // 鍒涘缓axios瀹炰緥
@@ -21,6 +24,8 @@
 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 璇锋牴鎹疄闄呮儏鍐佃嚜琛屼慨鏀�
   }
@@ -30,6 +35,29 @@
     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 => {
@@ -48,16 +76,22 @@
       return res.data
     }
     if (code === 401) {
-      ElMessageBox.confirm('鐧诲綍鐘舵�佸凡杩囨湡锛屾偍鍙互缁х画鐣欏湪璇ラ〉闈紝鎴栬�呴噸鏂扮櫥褰�', '绯荤粺鎻愮ず', {
+      if (!isRelogin.show) {
+        isRelogin.show = true;
+        ElMessageBox.confirm('鐧诲綍鐘舵�佸凡杩囨湡锛屾偍鍙互缁х画鐣欏湪璇ラ〉闈紝鎴栬�呴噸鏂扮櫥褰�', '绯荤粺鎻愮ず', {
           confirmButtonText: '閲嶆柊鐧诲綍',
           cancelButtonText: '鍙栨秷',
           type: 'warning'
         }
       ).then(() => {
-        store.dispatch('LogOut').then(() => {
+        isRelogin.show = false;
+        useUserStore().logOut().then(() => {
           location.href = '/index';
         })
-      }).catch(() => {});
+      }).catch(() => {
+        isRelogin.show = false;
+      });
+    }
       return Promise.reject('鏃犳晥鐨勪細璇濓紝鎴栬�呬細璇濆凡杩囨湡锛岃閲嶆柊鐧诲綍銆�')
     } else if (code === 500) {
       ElMessage({
@@ -96,12 +130,13 @@
 )
 
 // 閫氱敤涓嬭浇鏂规硶
-export function download(url, params, filename) {
+export function download(url, params, filename, config) {
   downloadLoadingInstance = ElLoading.service({ text: "姝e湪涓嬭浇鏁版嵁锛岃绋嶅��", 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'
+    responseType: 'blob',
+    ...config
   }).then(async (data) => {
     const isLogin = await blobValidate(data);
     if (isLogin) {

--
Gitblit v1.9.3