import cacheHelper from '../helper/cacheHelper'; import axios from "axios"; import env from "../http/env"; import { Loading } from 'element-ui'; import { WindowsToaster } from "node-notifier"; let loading; const service = axios.create({ baseURL: env.dev.baseUrl, settimeout: 3000, }); const request = function () { var parameters = []; var exp = []; for (var i = 0; i < arguments.length; i++) { parameters.push(arguments[i]); exp.push("parameters[" + i + "]"); } var exp = "service(" + exp.join(",") + ")"; var promise = eval(exp); promise.realThen = promise.then; //拦截then方法 promise.requestParameters = parameters; promise.requestExpression = exp; promise.then = function (arg) { return new Promise(resolve => { promise.realThen(result => { if (result.code != 401) { promise.realThen(arg); return; } axios.get(env.dev.baseUrl + "/authorize/webLogin") .then(function (response) { if (response.data.code == "0") { console.log("auto login"); cacheHelper.setToken(response.data.data.token); setTimeout(function () { cacheHelper.getBasicInfo(); }, 1); var parameters = promise.requestParameters; var exp = promise.requestExpression; eval(exp).then(arg); //重调then方法 } }); }); }); }; return promise; } export default request;