import axios from 'axios' import qs from 'qs' import { MessageBox, Message, Loading } from 'element-ui' import { noEmpty } from './index.js' import store from '@/store' let notifyTimer = null // import { getToken } from '@/utils/auth' let loadingInit = null // create an axios instance let baseURL = process.env.VUE_APP_BASE_API const userApi = window.sessionStorage.getItem('testUrl') if (process.env.VUE_APP_BASE_API === 'http://api.t.antretail.cn' && userApi) { baseURL = userApi } const service = axios.create({ baseURL, // url = base url + request url // withCredentials: true, // send cookies when cross-domain requests timeout: 60000 // request timeout }) // request interceptor service.interceptors.request.use( config => { // do something before request is sent if (config.CT === 'json') { config.headers['Content-Type'] = 'application/json;charset=UTF-8' } // config.headers['Content-Type'] = 'application/json;charset=UTF-8' // config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' // const token = window.sessionStorage.getItem('fp_token') // if (token) { // config.headers['Cookie'] = 'shiro.sesssion=' + token // } if (config.data.loading) { loadingInit = Loading.service({ lock: true, text: '拼命加载中...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)' }) } if (config.method === 'post' && config.CT !== 'json') { config.data = qs.stringify(config.data) } return config }, error => { // do something with request error console.log(error) // for debug return Promise.reject(error) } ) // response interceptor service.interceptors.response.use( /** * If you want to get http information such as headers or status * Please return response => response */ /** * Determine the request status by custom code * Here is just an example * You can also judge the status by HTTP Status Code */ response => { const res = response.data if (loadingInit) loadingInit.close() if (res.errno !== 0) { if (res.errno === 405 || res.errno === 404) { window.sessionStorage.clear() // window.localStorage.clear() if (loadingInit) { loadingInit.close() } MessageBox.confirm(`${res.errmsg},是否重新登录?`, { type: 'error' }).then(() => { window.history.go(0) }) return } else { Message({ message: res.errmsg || '未知错误' + error, type: 'error', duration: 2 * 1000 }) return Promise.reject(new Error(res.errmsg || 'Error')) } } else { if (res.token) { return res } else { return res.data || res } } }, error => { const curR = JSON.parse(JSON.stringify(error)).response || {} const curRD = curR.data || {} if (loadingInit) { loadingInit.close() } Message({ message: curRD.errmsg || '未知错误' + error, type: 'error', duration: 2 * 1000 }) return Promise.reject(error) } ) function getRequest(url, data, otherParams, method) { const token = window.sessionStorage.getItem('fp_token') const params = Object.assign({ token, page_size: 10}, data) // const params = Object.assign({}, data) if (otherParams) params[otherParams] = otherParams const newUrl = url.toLowerCase() return service({ url: newUrl, data: params, method: method || 'post' }) // return service({ url, data: params, method: method || 'post', transformResponse: [data => {return JSON.parse(JSON.stringify(data))}] }) } function getRequestNoSort(url, data, otherParams, method) { const token = window.sessionStorage.getItem('fp_token') const params = Object.assign({ token }, data) // const params = Object.assign({}, data) if (otherParams) params[otherParams] = otherParams const newUrl = url.toLowerCase() return service({ url: newUrl, data: params, method: method || 'post' }) // return service({ url, data: params, method: method || 'post', transformResponse: [data => {return JSON.parse(JSON.stringify(data))}] }) } function nologinRequest(url, data, otherParams, method) { const params = Object.assign({}, data) if (otherParams) params[otherParams] = otherParams return service({ url, data: params, method: method || 'post' }) } function getJsonRequest(url, data, otherParams, method) { const token = window.sessionStorage.getItem('fp_token') const params = Object.assign({ }, data) if (otherParams) params[otherParams] = otherParams const newUrl = url.toLowerCase() return service({ url: newUrl + `?token=${token}`, data: params, method: method || 'post', CT: 'json' }) } export { nologinRequest, getRequest, getRequestNoSort, getJsonRequest, } export default service