Ver código fonte

解析blob响应是否登录失效

RuoYi 4 anos atrás
pai
commit
3901695a6f

+ 11 - 4
ruoyi-ui/src/plugins/download.js

@@ -1,6 +1,8 @@
1
-import { saveAs } from 'file-saver'
2 1
 import axios from 'axios'
2
+import { Message } from 'element-ui'
3
+import { saveAs } from 'file-saver'
3 4
 import { getToken } from '@/utils/auth'
5
+import { blobValidate } from "@/utils/ruoyi";
4 6
 
5 7
 const baseURL = process.env.VUE_APP_BASE_API
6 8
 
@@ -12,9 +14,14 @@ export default {
12 14
       url: url,
13 15
       responseType: 'blob',
14 16
       headers: { 'Authorization': 'Bearer ' + getToken() }
15
-    }).then(res => {
16
-      const blob = new Blob([res.data], { type: 'application/zip' })
17
-      this.saveAs(blob, name)
17
+    }).then(async (res) => {
18
+      const isLogin = await blobValidate(res.data);
19
+      if (isLogin) {
20
+        const blob = new Blob([res.data], { type: 'application/zip' })
21
+        this.saveAs(blob, name)
22
+      } else {
23
+        Message.error('无效的会话,或者会话已过期,请重新登录。');
24
+      }
18 25
     })
19 26
   },
20 27
   saveAs(text, name, opts) {

+ 13 - 5
ruoyi-ui/src/utils/request.js

@@ -3,7 +3,7 @@ import { Notification, MessageBox, Message, Loading } from 'element-ui'
3 3
 import store from '@/store'
4 4
 import { getToken } from '@/utils/auth'
5 5
 import errorCode from '@/utils/errorCode'
6
-import { tansParams } from "@/utils/ruoyi";
6
+import { tansParams, blobValidate } from "@/utils/ruoyi";
7 7
 import { saveAs } from 'file-saver'
8 8
 
9 9
 let downloadLoadingInstance;
@@ -43,6 +43,10 @@ service.interceptors.response.use(res => {
43 43
     const code = res.data.code || 200;
44 44
     // 获取错误信息
45 45
     const msg = errorCode[code] || res.data.msg || errorCode['default']
46
+    // 二进制数据则直接返回
47
+    if(res.request.responseType ===  'blob' || res.request.responseType ===  'arraybuffer'){
48
+      return res.data
49
+    }
46 50
     if (code === 401) {
47 51
       MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
48 52
           confirmButtonText: '重新登录',
@@ -98,10 +102,14 @@ export function download(url, params, filename) {
98 102
     transformRequest: [(params) => { return tansParams(params) }],
99 103
     headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
100 104
     responseType: 'blob'
101
-  }).then((data) => {
102
-    const content = data
103
-    const blob = new Blob([content])
104
-    saveAs(blob, filename)
105
+  }).then(async (data) => {
106
+    const isLogin = await blobValidate(data);
107
+    if (isLogin) {
108
+      const blob = new Blob([data])
109
+      saveAs(blob, filename)
110
+    } else {
111
+      Message.error('无效的会话,或者会话已过期,请重新登录。');
112
+    }
105 113
     downloadLoadingInstance.close();
106 114
   }).catch((r) => {
107 115
     console.error(r)

+ 11 - 0
ruoyi-ui/src/utils/ruoyi.js

@@ -214,3 +214,14 @@ export function tansParams(params) {
214 214
 	}
215 215
 	return result
216 216
 }
217
+
218
+// 验证是否为blob格式
219
+export async function blobValidate(data) {
220
+    try {
221
+      const text = await data.text();
222
+      JSON.parse(text);
223
+      return false;
224
+    } catch (error) {
225
+      return true;
226
+    }
227
+}