Api.js 5.86 KB
Newer Older
fukai committed
1 2
import axios from 'axios'
import { MessageBox } from 'element-ui'
3
import RequestManager from './RequestManager.js';
fukai committed
4

潘际乾 committed
5
export const BASE_HOST = "/gjjs"
fukai committed
6
export const BASE_URI=BASE_HOST+"";
fukai committed
7

fukai committed
8 9
export const AxiosInstance = axios.create({
    
fukai committed
10
  });
fukai committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 
//对全局的业务数据请求使用拦截器
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);
})
fukai committed
27

fukai committed
28 29 30
const CODE_KEY = "respCode"
const MESSAGE_KEY = "respMsg"

31 32
const requestManager = new RequestManager()

fukai committed
33 34
export default class Api 
{
傅凯 committed
35
    static pget(url,data)
fukai committed
36
    {
傅凯 committed
37 38 39 40 41 42
        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) =>{
fukai committed
43
          AxiosInstance.get(url,data,{
傅凯 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
                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);
              });
            
        })
fukai committed
75
    }
76
  	static logout(){
fukai committed
77 78 79
        console.log('asdasdasdasdasd')
        window.location.href="#/login";
    }
80 81
    static async _post(url,data,method="post")
    {
wanggang_yf committed
82
        //debugger
fukai committed
83 84
        let token = window.sessionStorage.token
        let userId =  window.sessionStorage.userId
fukai committed
85
        let headers = {userId,token,"terminal":"WEB"};
fukai committed
86 87
        if(url!= `${BASE_URI}login` && token)
            headers.token = token;
88 89 90
        if (typeof data === "function") {
          data = await data()
        }
fukai committed
91 92 93 94 95 96 97 98
        let curPid = "";
        let prePid = "";

        if(data){
          curPid = data.pageId || "";
          prePid = data.prePageId || "";
        }

fukai committed
99 100 101
        //设置PAGEID
        headers["CUR-PID"] = curPid;
        headers["PRE-PID"] = prePid;
fukai committed
102
        return new Promise( (resolve, reject) =>{
fukai committed
103
          AxiosInstance.post(url,data,{
fukai committed
104 105
                headers,
                method,
106
                timeout: 120000,
fukai committed
107 108 109
            })
            .then(response=>{
                const {data} = response;
fukai committed
110
                if(data && data[CODE_KEY] != SUCCESS)
fukai committed
111
                {
fukai committed
112
                    console.log("错误码:"+data[CODE_KEY]+" 错误信息:"+data[MESSAGE_KEY])
fukai committed
113
                }
fukai committed
114
                if(data && data[CODE_KEY] == 'L0003'){		
fukai committed
115 116
                    window.sessionStorage.removeItem("token")
                    window.sessionStorage.removeItem("userId")				
fukai committed
117
					          MessageBox.alert({
fukai committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                            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);
fukai committed
136
                  resolve({[CODE_KEY]:'500',[MESSAGE_KEY]:error.response.status});
fukai committed
137 138 139 140 141
                } 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);
fukai committed
142
                  resolve({[CODE_KEY]:'501'});
fukai committed
143 144 145
                } else {
                  // Something happened in setting up the request that triggered an Error
                  console.log('Error', error.message);
fukai committed
146
                  resolve({[CODE_KEY]:'502',[MESSAGE_KEY]:error.message});
fukai committed
147 148 149 150 151 152 153
                }
                console.log(error.config);
              });
            });
    }
    static _get(url,data)
    {
154
        // return this._post(url,data,"get");
fukai committed
155
        return requestManager.pushRequest(this._post.bind(this, url, data, "get"))
fukai committed
156 157 158
    }
    static post(url,data)
    {
159 160
      // return this._post(BASE_URI+url,data)
      return requestManager.pushRequest(this._post.bind(this, BASE_URI+url, data))
fukai committed
161 162 163 164
    }

    static get(url,data)
    {
165
      return this._get(BASE_HOST+url,data)
fukai committed
166
    }
fukai committed
167 168 169 170

    static postAsync(url,data){
      return this._post.call(url,data);
    }
fukai committed
171
}