import axios from 'axios' import { MessageBox } from 'element-ui' import RequestManager from './RequestManager.js'; export const BASE_HOST = "/gjjs" export const BASE_URI=BASE_HOST+""; export const AxiosInstance = axios.create({ }); //对全局的业务数据请求使用拦截器 AxiosInstance.interceptors.request.use(function(config){ if(window.GLOBAL_CACHE && window.GLOBAL_CACHE.PAGEID_CACHE){ const key = "CUR-PAGE-IDS"; let pageIds = [...window.GLOBAL_CACHE.PAGEID_CACHE].join(","); if(!config.headers){ config.headers = {key:pageIds}; }else if(!config.headers[key]){ config.headers[key] = pageIds; } } return config; },function(error){ return Promise.reject(error); }) const CODE_KEY = "respCode" const MESSAGE_KEY = "respMsg" const requestManager = new RequestManager() export default class Api { static pget(url,data) { let token = window.sessionStorage.token let userId = window.sessionStorage.userId let headers = {userId,token,"terminal":"WEB"}; if(url!= `${BASE_URI}login` && token) headers.token = token; return new Promise( (resolve, reject) =>{ AxiosInstance.get(url,data,{ headers, timeout: 50000, }) .then(response=>{ const {data} = response; resolve(data) }) .catch(error=> { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx console.log(error.response.data); console.log(error.response.status); console.log(error.response.headers); resolve(error.response.status); } else if (error.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js console.log(error.request); resolve('error.request'); } else { // Something happened in setting up the request that triggered an Error console.log('Error', error.message); resolve(error.message); } console.log(error.config); }); }) } static logout(){ console.log('asdasdasdasdasd') window.location.href="#/login"; } static async _post(url,data,method="post") { //debugger let token = window.sessionStorage.token let userId = window.sessionStorage.userId let headers = {userId,token,"terminal":"WEB"}; if(url!= `${BASE_URI}login` && token) headers.token = token; if (typeof data === "function") { data = await data() } let curPid = ""; let prePid = ""; if(data){ curPid = data.pageId || ""; prePid = data.prePageId || ""; } //设置PAGEID headers["CUR-PID"] = curPid; headers["PRE-PID"] = prePid; return new Promise( (resolve, reject) =>{ AxiosInstance.post(url,data,{ headers, method, timeout: 120000, }) .then(response=>{ const {data} = response; if(data && data[CODE_KEY] != SUCCESS) { console.log("错误码:"+data[CODE_KEY]+" 错误信息:"+data[MESSAGE_KEY]) } if(data && data[CODE_KEY] == 'L0003'){ window.sessionStorage.removeItem("token") window.sessionStorage.removeItem("userId") MessageBox.alert({ title:'会话超时', message:'即将退出,请重新登录', showClose:false, callback:()=>{ window.location.href= "/?#/"+"login" } }) return ; } resolve(data) }) .catch(error=> { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx console.log(error.response.data); console.log(error.response.status); console.log(error.response.headers); resolve({[CODE_KEY]:'500',[MESSAGE_KEY]:error.response.status}); } else if (error.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js console.log(error.request); resolve({[CODE_KEY]:'501'}); } else { // Something happened in setting up the request that triggered an Error console.log('Error', error.message); resolve({[CODE_KEY]:'502',[MESSAGE_KEY]:error.message}); } console.log(error.config); }); }); } static _get(url,data) { // return this._post(url,data,"get"); return requestManager.pushRequest(this._post.bind(this, url, data, "get")) } static post(url,data) { // return this._post(BASE_URI+url,data) return requestManager.pushRequest(this._post.bind(this, BASE_URI+url, data)) } static get(url,data) { return this._get(BASE_HOST+url,data) } static postAsync(url,data){ return this._post.call(url,data); } }