UNPKG

icx-meum-vue-common-module

Version:

icx-meum-vue-common-module

60 lines (54 loc) 1.95 kB
import axios from 'axios' import qs from 'qs' import { checkIsNullOrEmpty } from '../../icx/utils' import { INT_LOGIN_STRONG, INT_LOGIN_WEAK, STRING_ERROR_TIP, STRING_ERROR_NET } from '../../icx/const' // axios 配置 axios.defaults.timeout = 15000 axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' axios.defaults.baseURL = 'https://mainapi.icarbonx.com' axios.defaults.withCredentials = true axios.defaults.crossDomain = true // POST传参序列化 axios.interceptors.request.use((config) => { // type是为了有的接口希望RequestBody是json if (config.method === 'post' && config.type !== 'json' && typeof config.data === 'object') { config.data = qs.stringify(config.data) } return config }, (error) => { return Promise.reject(error) }) // http response 拦截器 axios.interceptors.response.use((res) => { let code = 0 if (res && res.data) { code = !checkIsNullOrEmpty(res.data.errorCode) ? res.data.errorCode : res.data.code // 如果是强登陆或者若登陆 if (code === INT_LOGIN_STRONG || code === INT_LOGIN_WEAK) { window.location.replace('https://account.meum.icarbonx.com/#/auth?cb=' + encodeURIComponent(encodeURIComponent(window.location.href))) } else if (code !== 0 && typeof code !== 'undefined') { return Promise.reject({ errorCode: code, errorMsg: res.data.errMsg || STRING_ERROR_TIP }) } } return res }, () => { return Promise.reject({errorCode: -1, errorMsg: STRING_ERROR_NET}) }) function request (url = '', data = {}, type = 'GET', config) { type = type.toLowerCase() let axiosRequest if (type === 'get' && checkIsNullOrEmpty(data)) { axiosRequest = axios[type](url, config) } else { axiosRequest = axios[type](url, data, config) } return axiosRequest.then(function (respnonse) { return respnonse.data }).catch(function (e) { return e }) } export default request