123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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;
|