Api.js 4.73 KB
Newer Older
fukai committed
1 2 3
import axios from 'axios'
import { MessageBox } from 'element-ui'

fukai committed
4 5
export const BASE_HOST = "/gjjs/"
export const BASE_URI=BASE_HOST+"";
fukai committed
6 7 8 9 10 11 12

const instance = axios.create({
    baseURL: BASE_URI,
    timeout: 5000,
    method: 'post',
  });

fukai committed
13 14 15
const CODE_KEY = "respCode"
const MESSAGE_KEY = "respMsg"

fukai committed
16 17
export default class Api 
{
傅凯 committed
18
    static pget(url,data)
fukai committed
19
    {
傅凯 committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        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) =>{
            axios.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);
              });
            
        })
fukai committed
58 59 60 61 62 63 64 65 66
    }
	static logout(){
        console.log('asdasdasdasdasd')
        window.location.href="#/login";
    }
    static _post(url,data,method="post")
    {    
        let token = window.sessionStorage.token
        let userId =  window.sessionStorage.userId
fukai committed
67
        let headers = {userId,token,"terminal":"WEB"};
fukai committed
68 69 70 71 72 73
        if(url!= `${BASE_URI}login` && token)
            headers.token = token;
        return new Promise( (resolve, reject) =>{
            axios.post(url,data,{
                headers,
                method,
fukai committed
74
                timeout: 50000,
fukai committed
75 76 77
            })
            .then(response=>{
                const {data} = response;
fukai committed
78
                if(data && data[CODE_KEY] != SUCCESS)
fukai committed
79
                {
fukai committed
80
                    console.log("错误码:"+data[CODE_KEY]+" 错误信息:"+data[MESSAGE_KEY])
fukai committed
81
                }
fukai committed
82
                if(data && data[CODE_KEY] == 'L0003'){		
fukai committed
83 84
                    window.sessionStorage.removeItem("token")
                    window.sessionStorage.removeItem("userId")				
fukai committed
85
					          MessageBox.alert({
fukai committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                            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
104
                  resolve({[CODE_KEY]:'500',[MESSAGE_KEY]:error.response.status});
fukai committed
105 106 107 108 109
                } 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
110
                  resolve({[CODE_KEY]:'501'});
fukai committed
111 112 113
                } else {
                  // Something happened in setting up the request that triggered an Error
                  console.log('Error', error.message);
fukai committed
114
                  resolve({[CODE_KEY]:'502',[MESSAGE_KEY]:error.message});
fukai committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
                }
                console.log(error.config);
              });
            });
    }
    static _get(url,data)
    {
    
        return this._post(url,data,"get");
    }
    static post(url,data)
    {
       return this._post(BASE_URI+url,data)
    }

    static get(url,data)
    {
       return this._get(BASE_HOST+url,data)
    }
}