|
@@ -1,45 +1,48 @@
|
|
-import ElementUI from 'element-ui'
|
|
|
|
-import axios from 'axios'
|
|
|
|
-import { Message } from 'element-ui'
|
|
|
|
-import store from '@/store'
|
|
|
|
-import { getToken } from '@/utils/auth'
|
|
|
|
-import cleanDeep from 'clean-deep'
|
|
|
|
-import qs from 'querystring'
|
|
|
|
|
|
+import ElementUI from "element-ui";
|
|
|
|
+import axios from "axios";
|
|
|
|
+import { Message } from "element-ui";
|
|
|
|
+import store from "@/store";
|
|
|
|
+import { getToken } from "@/utils/auth";
|
|
|
|
+import cleanDeep from "clean-deep";
|
|
|
|
+import qs from "querystring";
|
|
// import { Loading } from 'element-ui'
|
|
// import { Loading } from 'element-ui'
|
|
-import { showFullScreenLoading, tryHideFullScreenLoading } from './request-loading'
|
|
|
|
-import router from '@/router/index'
|
|
|
|
-import Vue from 'vue'
|
|
|
|
-const showMessage = Symbol('showMessage')
|
|
|
|
|
|
+import {
|
|
|
|
+ showFullScreenLoading,
|
|
|
|
+ tryHideFullScreenLoading
|
|
|
|
+} from "./request-loading";
|
|
|
|
+import router from "@/router/index";
|
|
|
|
+import Vue from "vue";
|
|
|
|
+const showMessage = Symbol("showMessage");
|
|
class DonMessage {
|
|
class DonMessage {
|
|
- success (options, single = true) {
|
|
|
|
- this[showMessage]('success', options, single)
|
|
|
|
|
|
+ success(options, single = true) {
|
|
|
|
+ this[showMessage]("success", options, single);
|
|
}
|
|
}
|
|
- warning (options, single = true) {
|
|
|
|
- this[showMessage]('warning', options, single)
|
|
|
|
|
|
+ warning(options, single = true) {
|
|
|
|
+ this[showMessage]("warning", options, single);
|
|
}
|
|
}
|
|
- info (options, single = true) {
|
|
|
|
- this[showMessage]('info', options, single)
|
|
|
|
|
|
+ info(options, single = true) {
|
|
|
|
+ this[showMessage]("info", options, single);
|
|
}
|
|
}
|
|
- error (options, single = true) {
|
|
|
|
- this[showMessage]('error', options, single)
|
|
|
|
|
|
+ error(options, single = true) {
|
|
|
|
+ this[showMessage]("error", options, single);
|
|
}
|
|
}
|
|
- [showMessage] (type, options, single) {
|
|
|
|
|
|
+ [showMessage](type, options, single) {
|
|
if (single) {
|
|
if (single) {
|
|
// 判断是否已存在Message
|
|
// 判断是否已存在Message
|
|
- if (document.getElementsByClassName('el-message').length === 0) {
|
|
|
|
- Message[type](options)
|
|
|
|
|
|
+ if (document.getElementsByClassName("el-message").length === 0) {
|
|
|
|
+ Message[type](options);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- Message[type](options)
|
|
|
|
|
|
+ Message[type](options);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-Vue.use(ElementUI)
|
|
|
|
|
|
+Vue.use(ElementUI);
|
|
// 命名根据需要,DonMessage只是在文章中使用
|
|
// 命名根据需要,DonMessage只是在文章中使用
|
|
-Vue.prototype.$message = new DonMessage()
|
|
|
|
|
|
+Vue.prototype.$message = new DonMessage();
|
|
|
|
|
|
-let vue = new Vue()
|
|
|
|
|
|
+let vue = new Vue();
|
|
|
|
|
|
// let loading //定义loading变量
|
|
// let loading //定义loading变量
|
|
|
|
|
|
@@ -60,52 +63,48 @@ let vue = new Vue()
|
|
//调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
|
|
//调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
|
|
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
|
|
|
|
-
|
|
|
|
// create an axios instance
|
|
// create an axios instance
|
|
const service = axios.create({
|
|
const service = axios.create({
|
|
- baseURL: '', // url = base url + request url
|
|
|
|
|
|
+ baseURL: "", // url = base url + request url
|
|
// withCredentials: true, // send cookies when cross-domain requests
|
|
// withCredentials: true, // send cookies when cross-domain requests
|
|
- timeout: 180000, // request timeout
|
|
|
|
-})
|
|
|
|
|
|
+ timeout: 180000 // request timeout
|
|
|
|
+});
|
|
// { fullscreen: true, text: '努力加载中', spinner: 'el-icon-loading' }
|
|
// { fullscreen: true, text: '努力加载中', spinner: 'el-icon-loading' }
|
|
// request interceptor
|
|
// request interceptor
|
|
service.interceptors.request.use(
|
|
service.interceptors.request.use(
|
|
- async config => {
|
|
|
|
|
|
+ async config => {
|
|
// do something before request is sent
|
|
// do something before request is sent
|
|
- await showFullScreenLoading(store)
|
|
|
|
|
|
+ await showFullScreenLoading(store);
|
|
if (store.getters.token) {
|
|
if (store.getters.token) {
|
|
// let each request carry token
|
|
// let each request carry token
|
|
// ['X-Token'] is a custom headers key
|
|
// ['X-Token'] is a custom headers key
|
|
// please modify it according to the actual situation
|
|
// please modify it according to the actual situation
|
|
- config.headers['Authorization'] = getToken()
|
|
|
|
|
|
+ config.headers["Authorization"] = getToken();
|
|
// config.headers['content-type'] = "application/x-www-form-urlencoded"
|
|
// config.headers['content-type'] = "application/x-www-form-urlencoded"
|
|
}
|
|
}
|
|
- let tenantConfig = sessionStorage.getItem('tenantConfig')
|
|
|
|
- tenantConfig = tenantConfig ? JSON.parse(tenantConfig) : {}
|
|
|
|
- if(tenantConfig.tenantId && tenantConfig.tenantId != 'undefined') {
|
|
|
|
- config.headers['tenantId'] = tenantConfig.tenantId
|
|
|
|
|
|
+ let tenantConfig = sessionStorage.getItem("tenantConfig");
|
|
|
|
+ tenantConfig = tenantConfig ? JSON.parse(tenantConfig) : {};
|
|
|
|
+ if (tenantConfig.tenantId && tenantConfig.tenantId != "undefined") {
|
|
|
|
+ config.headers["tenantId"] = tenantConfig.tenantId;
|
|
}
|
|
}
|
|
- config.params = cleanDeep(config.params)
|
|
|
|
|
|
+ config.params = cleanDeep(config.params);
|
|
// params: cleanDeep(options.params),
|
|
// params: cleanDeep(options.params),
|
|
// (config)
|
|
// (config)
|
|
- return config
|
|
|
|
|
|
+ return config;
|
|
},
|
|
},
|
|
error => {
|
|
error => {
|
|
// do something with request error
|
|
// do something with request error
|
|
- return Promise.reject(error)
|
|
|
|
-
|
|
|
|
|
|
+ return Promise.reject(error);
|
|
}
|
|
}
|
|
-)
|
|
|
|
|
|
+);
|
|
|
|
|
|
// response interceptor
|
|
// response interceptor
|
|
service.interceptors.response.use(
|
|
service.interceptors.response.use(
|
|
- async res => {
|
|
|
|
|
|
+ async res => {
|
|
//res.code !== 200
|
|
//res.code !== 200
|
|
- await tryHideFullScreenLoading(store)
|
|
|
|
|
|
+ await tryHideFullScreenLoading(store);
|
|
if (res.data) {
|
|
if (res.data) {
|
|
-
|
|
|
|
-
|
|
|
|
- let data = JSON.parse(JSON.stringify(res.data))
|
|
|
|
|
|
+ let data = JSON.parse(JSON.stringify(res.data));
|
|
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
|
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
|
if (data.code == 401 || data.code == 403) {
|
|
if (data.code == 401 || data.code == 403) {
|
|
// Message({
|
|
// Message({
|
|
@@ -113,48 +112,46 @@ service.interceptors.response.use(
|
|
// type: 'error',
|
|
// type: 'error',
|
|
// duration: 5 * 1000
|
|
// duration: 5 * 1000
|
|
// })
|
|
// })
|
|
- vue.$message.error(`登录过期,请重新登录!`)
|
|
|
|
- setTimeout(() => {
|
|
|
|
-
|
|
|
|
- store.dispatch('user/resetToken').then(() => {
|
|
|
|
- location.reload()
|
|
|
|
- })
|
|
|
|
|
|
+ vue.$message.error(`登录过期,请重新登录!`);
|
|
|
|
+ setTimeout(async () => {
|
|
|
|
+ await store.dispatch("user/resetToken").then(() => {
|
|
|
|
+ location.reload();
|
|
|
|
+ });
|
|
}, 1000);
|
|
}, 1000);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if (data.code == 404) {
|
|
if (data.code == 404) {
|
|
- router.push('/404')
|
|
|
|
|
|
+ router.push("/404");
|
|
}
|
|
}
|
|
- if (data.code < 200&&data.code != 100||data.code >= 300&&data.code != 100) {
|
|
|
|
|
|
+ if (
|
|
|
|
+ (data.code < 200 && data.code != 100) ||
|
|
|
|
+ (data.code >= 300 && data.code != 100)
|
|
|
|
+ ) {
|
|
// Message({
|
|
// Message({
|
|
// message: data.msg || `请求失败code码为${ data.code }`,
|
|
// message: data.msg || `请求失败code码为${ data.code }`,
|
|
// type: 'error',
|
|
// type: 'error',
|
|
// duration: 5 * 1000
|
|
// duration: 5 * 1000
|
|
// })
|
|
// })
|
|
- let str = data.msg || `请求失败code码为${data.code}`
|
|
|
|
|
|
+ let str = data.msg || `请求失败code码为${data.code}`;
|
|
|
|
|
|
- vue.$message.error(str)
|
|
|
|
|
|
+ vue.$message.error(str);
|
|
|
|
|
|
- return Promise.reject(new Error(data.msg || 'Error'))
|
|
|
|
|
|
+ return Promise.reject(new Error(data.msg || "Error"));
|
|
} else {
|
|
} else {
|
|
-
|
|
|
|
- return data
|
|
|
|
-
|
|
|
|
|
|
+ return data;
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
-
|
|
|
|
-
|
|
|
|
- return Promise.reject()
|
|
|
|
|
|
+ return Promise.reject();
|
|
}
|
|
}
|
|
},
|
|
},
|
|
- async error => {
|
|
|
|
- if (error.message == 'Network Error') {
|
|
|
|
- vue.$message.error('网络异常,请检查网络连接')
|
|
|
|
|
|
+ async error => {
|
|
|
|
+ if (error.message == "Network Error") {
|
|
|
|
+ vue.$message.error("网络异常,请检查网络连接");
|
|
} else {
|
|
} else {
|
|
- vue.$message.error(error.message)
|
|
|
|
|
|
+ vue.$message.error(error.message);
|
|
}
|
|
}
|
|
- await tryHideFullScreenLoading(store)
|
|
|
|
- return Promise.reject(error)
|
|
|
|
|
|
+ await tryHideFullScreenLoading(store);
|
|
|
|
+ return Promise.reject(error);
|
|
}
|
|
}
|
|
-)
|
|
|
|
-export default service
|
|
|
|
|
|
+);
|
|
|
|
+export default service;
|