/** axios封装
|
* 请求拦截、相应拦截、错误统一处理
|
*/
|
import axios from 'axios';
|
import router from '@/router/index';
|
import store from '@/store/index';
|
import { Message, MessageBox } from 'element-ui';
|
|
const isDev = process.env.NODE_ENV === 'development';
|
|
axios.defaults.baseURL = isDev ? window.location.origin + process.env.VUE_APP_CTS_API_PREFIX : process.env.VUE_APP_API_ORIGIN + process.env.VUE_APP_CTS_API_PREFIX;
|
|
axios.defaults.withCredentials = true;
|
|
// 请求超时时间
|
axios.defaults.timeout = 30000;
|
|
// post请求头
|
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
|
|
// 响应拦截器
|
// axios.interceptors.response.use(
|
// response => {
|
// // 您的登录已失效
|
// if (typeof response.data === 'object') {
|
// const { errorCode, code, data, ext } = response.data;
|
// const eCode = code || errorCode;
|
// const eData = ext || data;
|
// // {"data":{"loginUrl":"http://10.10.16.114/iPortal/login.html"},"errorCode":"003","errorDesc":"您的登录已失效,请重新登录!","resultCode":"0"}
|
// if (eCode === '003' && typeof eData === 'object') {
|
// parent.location.reload();
|
// return Promise.reject(response.data);
|
// }
|
// }
|
// if (response.data.code == '00') {
|
// return Promise.resolve(response);
|
// } else {
|
// store.commit('close', true);
|
// if (response.data.code == 'CTS0116') {
|
// MessageBox.alert(response.data.msg, '提示', {
|
// confirmButtonText: '确定',
|
// center: true,
|
// callback: action => {}
|
// });
|
// } else {
|
// // 因为部分接口需要另外处理,不能展示提示语
|
// if (
|
// response.config.url.indexOf('/nextSubmit') == -1 &&
|
// response.config.url.indexOf('/notOpinionSubmit') == -1 &&
|
// response.data.code
|
// ) {
|
// Message({
|
// message: response.data.msg,
|
// type: 'warning',
|
// duration: 6000
|
// });
|
// }
|
// }
|
// return Promise.resolve(response);
|
// }
|
// },
|
// // 服务器状态码不是200的情况
|
// error => {
|
// if (
|
// error.code == 'ECONNABORTED' &&
|
// error.message.indexOf('timeout') != -1 &&
|
// !error.config._retry
|
// ) {
|
// Message({
|
// message: '请求超时',
|
// type: 'error',
|
// duration: 3000
|
// });
|
// }
|
// if (error.response) {
|
// switch (error.response.status) {
|
// // 401: 未登录
|
// // 未登录则跳转登录页面,并携带当前页面的路径
|
// // 在登录成功后返回当前页面,这一步需要在登录页操作。
|
// case 401:
|
// router.replace({
|
// path: '/login',
|
// query: { redirect: router.currentRoute.fullPath }
|
// });
|
// break;
|
// // 403 token过期
|
// // 登录过期对用户进行提示
|
// // 清除本地token和清空vuex中token对象
|
// // 跳转登录页面
|
// case 403:
|
// Message({
|
// message: '登录过期,请重新登录',
|
// type: 'error',
|
// duration: 6000
|
// });
|
// // 清除token
|
// // localStorage.removeItem('token')
|
// // store.commit('loginSuccess', null)
|
// // 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
|
// setTimeout(() => {
|
// router.replace({
|
// path: '/login',
|
// query: {
|
// redirect: router.currentRoute.fullPath
|
// }
|
// });
|
// }, 1000);
|
// break;
|
// // 404请求不存在
|
// case 404:
|
// Message({
|
// message: '网络请求不存在',
|
// type: 'error',
|
// duration: 6000
|
// });
|
// break;
|
// // 其他错误,直接抛出错误提示
|
// default:
|
// Message({
|
// message: error.response.data.message,
|
// type: 'error',
|
// duration: 6000
|
// });
|
// }
|
// }
|
// return Promise.reject(error.response);
|
// }
|
// );
|
|
/**
|
* addMenuId api地址添加全局参数menuId
|
* @param {String} url 接口地址
|
*/
|
function addMenuId(url) {
|
// 本地调试使用
|
const menuId = parent.g_menu_id || '';
|
const contactStr = url.includes('?') ? '&' : '?';
|
if (url.includes('menuId=')) {
|
return url;
|
}
|
// return `${url}${contactStr}menuId=${menuId}`
|
// 本地连189环境配置具体的目录menuId
|
return `${url}${contactStr}menuId=620`;
|
}
|
|
/**
|
* get方法,对应get请求
|
* @param {String} url [请求的url地址]
|
* @param {Object} params [请求时携带的参数]
|
*/
|
export function get(url, params) {
|
return new Promise((resolve, reject) => {
|
axios
|
.get(addMenuId(url), {
|
params: params
|
})
|
.then(res => {
|
resolve(res.data);
|
})
|
.catch(err => {
|
reject(err.data);
|
});
|
});
|
}
|
/**
|
* post方法,对应post请求
|
* @param {String} url [请求的url地址]
|
* @param {Object} params [请求时携带的参数]
|
*/
|
export function post(url, params, responseType) {
|
return new Promise((resolve, reject) => {
|
axios
|
.post(addMenuId(url), params, responseType)
|
.then(res => {
|
if (res.data.code) {
|
resolve(res.data);
|
} else {
|
resolve(res);
|
}
|
})
|
.catch(err => {
|
reject(err.data);
|
});
|
});
|
}
|