|
@@ -1,6 +1,16 @@
|
|
|
"use strict";
|
|
|
import axios from "axios";
|
|
|
import bus from "./bus.js";
|
|
|
+let store=null
|
|
|
+// 异步加载store模块 不异步加载的话,@/api/modules/oldApi.js的导入导出写法会有问题
|
|
|
+import('@/vuex/index.js')
|
|
|
+.then(module => {
|
|
|
+ store=module.default
|
|
|
+})
|
|
|
+.catch(err => {
|
|
|
+ console.error(err,'vuex加载错误');
|
|
|
+});
|
|
|
+
|
|
|
axios.defaults.withCredentials = true;
|
|
|
axios.defaults.timeout = 0;
|
|
|
axios.interceptors.request.use(
|
|
@@ -40,16 +50,29 @@ function getData(val, url) {
|
|
|
return val;
|
|
|
}
|
|
|
|
|
|
-function checkStatus(response) {
|
|
|
+function errorMsgShow(closableErrMsg,msg){
|
|
|
+ if(closableErrMsg){
|
|
|
+ let messageHint = bus.$message.error({
|
|
|
+ message:msg,
|
|
|
+ duration:0,
|
|
|
+ showClose:true
|
|
|
+ })
|
|
|
+ store.mutations.PUSH_CLOSABLE_HINT(store.state,messageHint)
|
|
|
+ }else{
|
|
|
+ bus.$message.error(msg);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function checkStatus(response,closableErrMsg) {
|
|
|
//处理响应数据
|
|
|
if (response && response.status === 200) {
|
|
|
// loading,如果http状态码正常,则直接返回数据
|
|
|
let res = bus.$parseData(response);
|
|
|
if (!res) {
|
|
|
- bus.$message.error("服务器开了个小差");
|
|
|
+ errorMsgShow(closableErrMsg,'服务器开了个小差')
|
|
|
return false;
|
|
|
} else if (res.Ret == 403) {
|
|
|
- bus.$message.error(res.Msg);
|
|
|
+ errorMsgShow(closableErrMsg,res.Msg)
|
|
|
} else if (res.Ret === 408) {
|
|
|
localStorage.setItem("auth", "")
|
|
|
localStorage.setItem("loginTime", "")
|
|
@@ -63,14 +86,19 @@ function checkStatus(response) {
|
|
|
}
|
|
|
return res;
|
|
|
} else {
|
|
|
- bus.$message.error("网络异常");
|
|
|
+ errorMsgShow(closableErrMsg,'网络异常')
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function checkCode(res) {
|
|
|
- bus.$message.error("网络异常");
|
|
|
+function checkCode(res,closableErrMsg) {
|
|
|
+ errorMsgShow(closableErrMsg,'网络异常')
|
|
|
}
|
|
|
+
|
|
|
+function isFormData(obj) {
|
|
|
+ return typeof obj === 'object' && obj instanceof FormData;
|
|
|
+}
|
|
|
+
|
|
|
// 请求时间需要过长,取消限制
|
|
|
const cancelTimeoutUrlPost = ["/cloud_disk/resource/upload"];
|
|
|
|
|
@@ -78,6 +106,16 @@ export default {
|
|
|
post(url, data) {
|
|
|
//post请求方式
|
|
|
let timeout = cancelTimeoutUrlPost.includes(url) ? 0 : 600000;
|
|
|
+ // 自定义参数
|
|
|
+ /**
|
|
|
+ * closableErrMsg123 -- 错误提示需要时可关闭类型的
|
|
|
+ */
|
|
|
+ let closableErrMsg=false
|
|
|
+ if(data){
|
|
|
+ // 区分formData格式
|
|
|
+ closableErrMsg = isFormData(data)?!!data.get('closableErrMsg123'):!!data.closableErrMsg123
|
|
|
+ isFormData(data)?data.delete('closableErrMsg123'):(data.closableErrMsg123=undefined)
|
|
|
+ }
|
|
|
return axios({
|
|
|
method: "post",
|
|
|
url: url,
|
|
@@ -87,13 +125,23 @@ export default {
|
|
|
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
|
})
|
|
|
.then((response) => {
|
|
|
- return checkStatus(response);
|
|
|
+ return checkStatus(response,closableErrMsg);
|
|
|
})
|
|
|
.catch((res) => {
|
|
|
- return checkCode(res);
|
|
|
+ return checkCode(res,closableErrMsg);
|
|
|
});
|
|
|
},
|
|
|
get(url, data, responseType = "json") {
|
|
|
+ // 自定义参数
|
|
|
+ /**
|
|
|
+ * closableErrMsg123 -- 错误提示需要时可关闭类型的
|
|
|
+ */
|
|
|
+ let closableErrMsg=false
|
|
|
+ if(data){
|
|
|
+ closableErrMsg = !!data.closableErrMsg123
|
|
|
+ data.closableErrMsg123=undefined
|
|
|
+ }
|
|
|
+
|
|
|
//get请求方式
|
|
|
return axios({
|
|
|
method: "get",
|
|
@@ -120,10 +168,10 @@ export default {
|
|
|
if(responseType==='blob'){
|
|
|
return response
|
|
|
}
|
|
|
- return checkStatus(response);
|
|
|
+ return checkStatus(response,closableErrMsg);
|
|
|
})
|
|
|
.catch((res) => {
|
|
|
- return checkCode(res);
|
|
|
+ return checkCode(res,closableErrMsg);
|
|
|
});
|
|
|
},
|
|
|
Base64,
|