import axios from 'axios'; import { Md5 } from 'ts-md5/dist/md5'; import config from '@/config' let noTimeoutLimitArr = [ ]; // create an axios instance const service = axios.create({ baseURL: config.baseUrl, // url = base url + request url withCredentials: false, // send cookies when cross-domain requests timeout: 20000, // request timeout }); export let needLoadingRequestCount = 0; export function showFullScreenLoading(config) { if (needLoadingRequestCount === 0) { store.state.isLoading = true } needLoadingRequestCount++; } export function tryHideFullScreenLoading() { if (needLoadingRequestCount <= 0) { return; } needLoadingRequestCount--; if (needLoadingRequestCount === 0) { store.state.isLoading = false } } // request interceptor service.interceptors.request.use( config => { // do something before request is sent // console.log(config); function setTimeoutunlimit(config) { // if (_.indexOf(noTimeoutLimitArr, config.url) > -1) { // config.timeout = 9999999999; // } } function mixSignAndToken(config) { // debugger // console.log(config.data, process.env.VUE_APP_BASE_API, process.env.VUE_APP_SECRET_KEY); const checkurl = function (url) { if (url === '/common/active') { return false; } // 用户端白名单 if (url.indexOf('/common') > -1) { return true; } // 管理端白名单 if (url === '/admin/user/auth') { return true; } // 短信服务白名单 if (url === 'sms/sendCode') { return true; } return false; }; if (checkurl(config.url)) { if(config.url.indexOf('/open' >-1)){ }else { config.headers['X-AUTH-SIGN'] = Md5.hashStr(JSON.stringify(config.data) + '123'); } } else { if (store.state.auth) { config.headers['X-AUTH-SIGN'] = Md5.hashStr(JSON.stringify(config.data) + store.state.auth.secret); config.headers['X-AUTH-TOKEN'] = store.state.auth.token; } else { // 非登录操作,但没有auth,跳转登录 router.push('/' + store.state.tenantCode + '/login'); } } } // if (!JSON.stringify(config.data.customLoadingSwitch)) { // // 如不需要在data中 LoadingSwitch:false // console.log('有loading'); // showFullScreenLoading(config); // } mixSignAndToken(config); setTimeoutunlimit(config); /*if (store.getters.token) { // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['X-Token'] = getToken(); }*/ 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 the custom code is not 20000, it is judged as an error. if (res.code !== 0) { // 401:登录超时 if (res.code === 401) { setTimeout(function () { if (window.loading) { window.loading.close(); } // console.log(router); /*if (router.app.$route.path.indexOf('/c/') === 0) { router.push('/c/' + store.state.tenantCode + '/login'); } else { router.push('/a/' + store.state.tenantCode + '/login'); }*/ store.state.showBottomNav = true; router.push('/login'); }, 2000); } if (res.code === 405) { setTimeout(function () { if (window.loading) { window.loading.close(); } store.state.showBottomNav = true; router.push('/login'); }, 2000); } if (res.code === 502) { // Toast.fail('bad gateway'); } if (res.code === 500 || res.code === 1001|| res.code ===1002) { tryHideFullScreenLoading(); return Promise.reject('error' + res.code + '-' + (res.message === undefined ? 'nomessage': res.message)); } tryHideFullScreenLoading(); return Promise.reject(res.message || 'error'); } else { tryHideFullScreenLoading(); return res; } }, error => { if (error == 'Error: Network Error') { // Toast.fail('网络断开,请检查网络'); } else { // Toast.fail('其他错误,请联系管理员'); } tryHideFullScreenLoading(); return Promise.reject(error); }, ); export default service;