提交 | 用户 | 时间
|
fd2207
|
1 |
import axios from 'axios' |
懒 |
2 |
import { Notification, MessageBox, Message, Loading } from 'element-ui' |
|
3 |
import store from '@/store' |
|
4 |
import { getToken } from '@/utils/auth' |
|
5 |
import errorCode from '@/utils/errorCode' |
|
6 |
import { tansParams, blobValidate } from "@/utils/ruoyi"; |
|
7 |
import cache from '@/plugins/cache' |
|
8 |
import { saveAs } from 'file-saver' |
|
9 |
|
|
10 |
let downloadLoadingInstance; |
|
11 |
// 是否显示重新登录 |
|
12 |
export let isRelogin = { show: false }; |
|
13 |
|
|
14 |
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' |
|
15 |
// 创建axios实例 |
|
16 |
const service = axios.create({ |
|
17 |
// axios中请求配置有baseURL选项,表示请求URL公共部分 |
|
18 |
baseURL: process.env.VUE_APP_BASE_API, |
|
19 |
// 超时 |
|
20 |
timeout: 10000 |
|
21 |
}) |
|
22 |
|
|
23 |
// request拦截器 |
|
24 |
service.interceptors.request.use(config => { |
|
25 |
// 是否需要设置 token |
|
26 |
const isToken = (config.headers || {}).isToken === false |
|
27 |
// 是否需要防止数据重复提交 |
|
28 |
const isRepeatSubmit = (config.headers || {}).repeatSubmit === false |
|
29 |
if (getToken() && !isToken) { |
|
30 |
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 |
|
31 |
} |
|
32 |
// get请求映射params参数 |
|
33 |
if (config.method === 'get' && config.params) { |
|
34 |
let url = config.url + '?' + tansParams(config.params); |
|
35 |
url = url.slice(0, -1); |
|
36 |
config.params = {}; |
|
37 |
config.url = url; |
|
38 |
} |
|
39 |
if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) { |
|
40 |
const requestObj = { |
|
41 |
url: config.url, |
|
42 |
data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data, |
|
43 |
time: new Date().getTime() |
|
44 |
} |
|
45 |
const requestSize = Object.keys(JSON.stringify(requestObj)).length; // 请求数据大小 |
|
46 |
const limitSize = 5 * 1024 * 1024; // 限制存放数据5M |
|
47 |
if (requestSize >= limitSize) { |
|
48 |
console.warn(`[${config.url}]: ` + '请求数据大小超出允许的5M限制,无法进行防重复提交验证。') |
|
49 |
return config; |
|
50 |
} |
|
51 |
const sessionObj = cache.session.getJSON('sessionObj') |
|
52 |
if (sessionObj === undefined || sessionObj === null || sessionObj === '') { |
|
53 |
cache.session.setJSON('sessionObj', requestObj) |
|
54 |
} else { |
|
55 |
const s_url = sessionObj.url; // 请求地址 |
|
56 |
const s_data = sessionObj.data; // 请求数据 |
|
57 |
const s_time = sessionObj.time; // 请求时间 |
|
58 |
const interval = 1000; // 间隔时间(ms),小于此时间视为重复提交 |
|
59 |
if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) { |
|
60 |
const message = '数据正在处理,请勿重复提交'; |
|
61 |
console.warn(`[${s_url}]: ` + message) |
|
62 |
return Promise.reject(new Error(message)) |
|
63 |
} else { |
|
64 |
cache.session.setJSON('sessionObj', requestObj) |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
return config |
|
69 |
}, error => { |
|
70 |
console.log(error) |
|
71 |
Promise.reject(error) |
|
72 |
}) |
|
73 |
|
|
74 |
// 响应拦截器 |
|
75 |
service.interceptors.response.use(res => { |
|
76 |
// 未设置状态码则默认成功状态 |
|
77 |
const code = res.data.code || 200; |
|
78 |
// 获取错误信息 |
|
79 |
const msg = errorCode[code] || res.data.msg || errorCode['default'] |
|
80 |
// 二进制数据则直接返回 |
|
81 |
if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') { |
|
82 |
return res.data |
|
83 |
} |
|
84 |
if (code === 401) { |
|
85 |
if (!isRelogin.show) { |
|
86 |
isRelogin.show = true; |
|
87 |
MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => { |
|
88 |
isRelogin.show = false; |
|
89 |
store.dispatch('LogOut').then(() => { |
|
90 |
location.href = '/index'; |
|
91 |
}) |
|
92 |
}).catch(() => { |
|
93 |
isRelogin.show = false; |
|
94 |
}); |
|
95 |
} |
|
96 |
return Promise.reject('无效的会话,或者会话已过期,请重新登录。') |
|
97 |
} else if (code === 500) { |
|
98 |
Message({ message: msg, type: 'error' }) |
|
99 |
return Promise.reject(new Error(msg)) |
|
100 |
} else if (code === 601) { |
|
101 |
Message({ message: msg, type: 'warning' }) |
|
102 |
return Promise.reject('error') |
|
103 |
} else if (code !== 200) { |
|
104 |
Notification.error({ title: msg }) |
|
105 |
return Promise.reject('error') |
|
106 |
} else { |
|
107 |
return res.data |
|
108 |
} |
|
109 |
}, |
|
110 |
error => { |
|
111 |
console.log('err' + error) |
|
112 |
let { message } = error; |
|
113 |
if (message == "Network Error") { |
|
114 |
message = "后端接口连接异常"; |
|
115 |
} else if (message.includes("timeout")) { |
|
116 |
message = "系统接口请求超时"; |
|
117 |
} else if (message.includes("Request failed with status code")) { |
|
118 |
message = "系统接口" + message.substr(message.length - 3) + "异常"; |
|
119 |
} |
|
120 |
Message({ message: message, type: 'error', duration: 5 * 1000 }) |
|
121 |
return Promise.reject(error) |
|
122 |
} |
|
123 |
) |
|
124 |
|
|
125 |
// 通用下载方法 |
|
126 |
export function download(url, params, filename, config) { |
|
127 |
downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", }) |
|
128 |
return service.post(url, params, { |
|
129 |
transformRequest: [(params) => { return tansParams(params) }], |
|
130 |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
|
131 |
responseType: 'blob', |
|
132 |
...config |
|
133 |
}).then(async (data) => { |
|
134 |
const isBlob = blobValidate(data); |
|
135 |
if (isBlob) { |
|
136 |
const blob = new Blob([data]) |
|
137 |
saveAs(blob, filename) |
|
138 |
} else { |
|
139 |
const resText = await data.text(); |
|
140 |
const rspObj = JSON.parse(resText); |
|
141 |
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'] |
|
142 |
Message.error(errMsg); |
|
143 |
} |
|
144 |
downloadLoadingInstance.close(); |
|
145 |
}).catch((r) => { |
|
146 |
console.error(r) |
|
147 |
Message.error('下载文件出现错误,请联系管理员!') |
|
148 |
downloadLoadingInstance.close(); |
|
149 |
}) |
|
150 |
} |
|
151 |
|
|
152 |
export default service |