request.js 7.45 KB
Newer Older
liuxin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import axios from 'axios'
import {
  MessageBox,
  Notification,
} from 'element-ui'

export const BASE_HOST = ""
export const BASE_URI = BASE_HOST + "";

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

wangren committed
16
export default class request {
liuxin committed
17 18 19 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  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) => {
      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);
        });

    })
  }
  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
  //   let headers = {
  //     userId,
  //     token,
  //     "terminal": "WEB"
  //   };
  //   if (url != `${BASE_URI}login` && token)
  //     headers.token = token;
  //   return new Promise((resolve, reject) => {
  //     axios.post(url, data, {
  //         headers,
  //         method,
  //         timeout: 50000,
  //       })
  //       .then(response => {
  //         const {
  //           data
  //         } = response;
  //         if (data && data.code != SUCCESS) {
  //           console.log("错误码:" + data.code + " 错误信息:" + data.message)
  //         }
  //         if (data && data.code == '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({
wangguangchao committed
113
  //             respCode: '500',
liuxin committed
114 115 116 117 118 119 120 121
  //             retmsg: 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({
wangguangchao committed
122
  //             respCode: '501'
liuxin committed
123 124 125 126 127
  //           });
  //         } else {
  //           // Something happened in setting up the request that triggered an Error
  //           console.log('Error', error.message);
  //           resolve({
wangguangchao committed
128
  //             respCode: '502',
liuxin committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  //             retmsg: error.message
  //           });
  //         }
  //         console.log(error.config);
  //       });
  //   });
  // }
  // static _get(url, data) {

  //   return this._post(url, data, "get");
  // }

  static _base(url, method, data, params) {

    let token = window.sessionStorage.token
    let userId = window.sessionStorage.userId
    
    let headers = {
      userId,
      token,
      "terminal": "WEB"
    };
    // 统一处理前端生成的交易流水号,加到 headers 里面
    if (params && params.serialNo) {
      headers['n-serial-no'] = params.serialNo
      delete params.serialNo
    }
    if (data && data.serialNo) {
      headers['n-serial-no'] = data.serialNo
      delete data.serialNo
    }
    
    if (url != `${BASE_URI}login` && token)
      headers.token = token;

    return new Promise((resolve, reject) => {
      axios({
          url: BASE_HOST + url,
          method: method,
          data,
          params,
          headers: headers,
          timeout: 50000,
        }).then(response => {
          const {
            data
          } = response;
          if (data && data.code != SUCCESS) {
            Notification({
              type: 'error',
              title: '提示',
              message: data.msg
            })
            console.log("错误码:" + data.code + " 错误信息:" + data.msg)
          }
          if (data && data.code == '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({
wangguangchao committed
207
              respCode: '500',
liuxin committed
208 209 210 211 212 213 214 215
              retmsg: 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({
wangguangchao committed
216
              respCode: '501'
liuxin committed
217 218 219 220 221
            });
          } else {
            // Something happened in setting up the request that triggered an Error
            console.log('Error', error.message);
            resolve({
wangguangchao committed
222
              respCode: '502',
liuxin committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
              retmsg: error.message
            });
          }
          console.log(error.config);
        });
    });
  }
  static post(url, data = {}, params = {}) {
    return this._base(url, 'post', data, params, BASE_HOST)
  }

  static get(url, params) {
    return this._base(url, 'get', {}, params)
  }

  static delete(url, data = {}, params = {}) {
    return this._base(url, 'delete', data, params)
  }

  static put(url, data = {}, params = {}) {
    return this._base(url, 'put', data, params)
  }
}