|
|
@@ -1,180 +0,0 @@
|
|
|
-import axios from 'axios'
|
|
|
-import { Notification, MessageBox, Message } from 'element-ui'
|
|
|
-import store from '@/store'
|
|
|
-import { getToken } from '@/utils/auth'
|
|
|
-import errorCode from '@/utils/errorCode'
|
|
|
-import qs from 'qs'
|
|
|
-
|
|
|
-// create an axios instance
|
|
|
-let baseURL = window.PLATFROM_CONFIG.baseUrl
|
|
|
-const service = axios.create({
|
|
|
- baseURL: baseURL, // url = base url + request url
|
|
|
- // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
|
|
|
- // withCredentials: true, // send cookies when cross-domain requests
|
|
|
- timeout: 10000, // request timeout
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
|
|
|
- //'Content-Type': 'application/json'
|
|
|
- }
|
|
|
-})
|
|
|
-
|
|
|
-// request interceptor
|
|
|
-service.interceptors.request.use(
|
|
|
- config => {
|
|
|
- // 是否需要设置 token
|
|
|
- const isToken = (config.headers || {}).isToken === false
|
|
|
- if (getToken() && !isToken) {
|
|
|
- config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
|
|
- }
|
|
|
- if (config.method == 'get') {
|
|
|
- let ext_str = config.url.indexOf('?') == -1 ? '?' : '&';
|
|
|
- let obj_val = '';
|
|
|
- for (let k in config.data) {
|
|
|
- obj_val += k + "=" + config.data[k] + "&";
|
|
|
- }
|
|
|
- config.url += ext_str + obj_val;
|
|
|
- }
|
|
|
-
|
|
|
- return config
|
|
|
- },
|
|
|
- error => {
|
|
|
- // do something with request error
|
|
|
- console.log(error) // for debug
|
|
|
- return Promise.reject(error)
|
|
|
- }
|
|
|
-)
|
|
|
-
|
|
|
-// response interceptor
|
|
|
-service.interceptors.response.use(response => {
|
|
|
- console.log(response)
|
|
|
- if (response.data.status === 'SUCCESS') {
|
|
|
- console.log(1)
|
|
|
- return response.data;
|
|
|
- } else {
|
|
|
- console.log(1)
|
|
|
- if (response.data.status === 'ERROR' && response.data.code == "401") {
|
|
|
- console.log(3)
|
|
|
- MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
|
|
|
- confirmButtonText: '重新登录',
|
|
|
- cancelButtonText: '取消',
|
|
|
- type: 'warning'
|
|
|
- }).then(() => {
|
|
|
- store.dispatch('logout').then(() => {
|
|
|
- location.href = '/index/#/';
|
|
|
- })
|
|
|
- }).catch(() => {});
|
|
|
- } else {
|
|
|
- console.log(4)
|
|
|
- Message({
|
|
|
- showClose: true,
|
|
|
- message: response.data.msg || response.data.msg,
|
|
|
- type: 'error'
|
|
|
- });
|
|
|
- return {}
|
|
|
- }
|
|
|
- }
|
|
|
- // 未设置状态码则默认成功状态
|
|
|
- // let code = ''
|
|
|
- // if (res.data.status == "SUCCESS") {
|
|
|
- // console.log(3)
|
|
|
- // code = 200
|
|
|
- // return res.headers['content-type'].indexOf("application/vnd.ms-excel") != -1 ? res : res.data
|
|
|
- // } else {
|
|
|
- // if (res.data.code == "401") {
|
|
|
- // console.log(1)
|
|
|
- // MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
|
|
|
- // confirmButtonText: '重新登录',
|
|
|
- // cancelButtonText: '取消',
|
|
|
- // type: 'warning'
|
|
|
- // }).then(() => {
|
|
|
- // store.dispatch('LogOut').then(() => {
|
|
|
- // location.href = '/#/';
|
|
|
- // })
|
|
|
- // }).catch(() => {});
|
|
|
- // return Promise.reject()
|
|
|
- // } else {
|
|
|
- // console.log(2)
|
|
|
- // Message({
|
|
|
- // message: res.data.msg,
|
|
|
- // type: 'error'
|
|
|
- // })
|
|
|
- // return msg && Promise.reject(new Error(msg)) || ""
|
|
|
- // }
|
|
|
- // }
|
|
|
- // const code = res.code || 200;
|
|
|
- // 获取错误信息
|
|
|
- // const msg = errorCode[code] || res.data.msg || errorCode['default']
|
|
|
- // const msg = res.data.msg
|
|
|
- // if (code === 401) {
|
|
|
- // MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
|
|
|
- // confirmButtonText: '重新登录',
|
|
|
- // cancelButtonText: '取消',
|
|
|
- // type: 'warning'
|
|
|
- // }).then(() => {
|
|
|
- // store.dispatch('LogOut').then(() => {
|
|
|
- // location.href = '/#/';
|
|
|
- // })
|
|
|
- // }).catch(() => {});
|
|
|
- // return Promise.reject()
|
|
|
- // } else if (code === 500) {
|
|
|
- // Message({
|
|
|
- // message: msg,
|
|
|
- // type: 'error'
|
|
|
- // })
|
|
|
- // return msg && Promise.reject(new Error(msg)) || ""
|
|
|
- // } else if (code !== 200) {
|
|
|
- // Notification.error({
|
|
|
- // title: msg
|
|
|
- // })
|
|
|
- // return Promise.reject('error')
|
|
|
- // } else {
|
|
|
- // return res.headers['content-type'].indexOf("application/vnd.ms-excel") != -1 ? res : res.data
|
|
|
- // }
|
|
|
- },
|
|
|
- error => {
|
|
|
- console.log(error.response)
|
|
|
- if (error.response.data.status == "ERROR" && response.data.data.code == "401") {
|
|
|
- MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
|
|
|
- confirmButtonText: '重新登录',
|
|
|
- cancelButtonText: '取消',
|
|
|
- type: 'warning'
|
|
|
- }).then(() => {
|
|
|
- store.dispatch('logout').then(() => {
|
|
|
- location.href = '/index/#/';
|
|
|
- })
|
|
|
- }).catch(() => {});
|
|
|
- } else {
|
|
|
- Message({
|
|
|
- showClose: true,
|
|
|
- message: error.response.data.msg || error.response.data.msg,
|
|
|
- type: 'error'
|
|
|
- });
|
|
|
- return {}
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // console.log(error)
|
|
|
- // // console.log('err' + error)
|
|
|
- // let { message } = error;
|
|
|
- // if (message == "Network Error") {
|
|
|
- // message = "后端接口连接异常";
|
|
|
- // } else if (message.includes("timeout")) {
|
|
|
- // if (localStorage.getItem("timeout") == "大于10s") {
|
|
|
- // localStorage.removeItem("timeout")
|
|
|
- // return
|
|
|
- // } else {
|
|
|
- // message = "系统接口请求超时";
|
|
|
- // }
|
|
|
- // } else if (message.includes("Request failed with status code")) {
|
|
|
- // message = "系统接口" + message.substr(message.length - 3) + "异常";
|
|
|
- // }
|
|
|
- // Message({
|
|
|
- // message: message,
|
|
|
- // type: 'error',
|
|
|
- // duration: 5 * 1000
|
|
|
- // })
|
|
|
- // return Promise.reject(error)
|
|
|
- }
|
|
|
-)
|
|
|
-
|
|
|
-export default service
|