http.js 3.9 KB
Newer Older
godwithdh's avatar
godwithdh committed
1 2 3
import methodMap from './apiMap';
import AsInst from './axios';
import HSHttpClient from './crypto'
张锡奇's avatar
张锡奇 committed
4 5
import store from '@/store/modules/app.js'
import apiURL from './host.js'
godwithdh's avatar
godwithdh committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

class Http {
}
Http.install = function (Vue) {
  /**
   * 全局请求接口
   * @param method 方法
   * @param opts 参数
   * @param toast 是否提示
   * @returns {string}
   */
  Vue.prototype.request = function (method, opts, toast,header) {
    let m = methodMap[method];
    if (m) {
      let optsType = typeof (opts);
      if (optsType === null || optsType !== 'object') {
        opts = {};
      }
      if (typeof m.method === 'undefined') {
        console.log('method 错误', '缺少请求 method 方法', '\n');
        return false;
      }
      //如果有给 toast 参数则显示 loading 加载数据
      if (toast && typeof (toast) === 'boolean') {
        loading();
      } else if (toast && typeof (toast) === 'string') {
        loading(toast);
      }
      let _params = opts.hasOwnProperty('params') ? opts.params : {};
张锡奇's avatar
张锡奇 committed
35
      let url = new HSHttpClient(apiURL(m.host) + m.url,{},_params).newUrl;
godwithdh's avatar
godwithdh committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

      if (m.method === 'get') {
        return Vue.prototype.apiGet(url,opts.data,toast,header);
      } else if (m.method === 'post') {
        return Vue.prototype.apiPost(url, opts.data,toast,header);
      } else {
        return false;
      }
    } else {
      closeLoading();
      console.log('url 错误', '返回结果:err = ', '无法请求,无效的请求!', '\n');
    }
  };
  /**
   * POST 请求 无提示
   * @param url 请求URL
   * @param data 请求数据
   * @param toast 是否显示 modal
   * @returns {Promise}
   */
  Vue.prototype.apiPost = function (url, data, toast = false,header) {
张锡奇's avatar
张锡奇 committed
57 58
    for(let x in header){
      AsInst.defaults.headers.common[x] = header[x];
godwithdh's avatar
godwithdh committed
59 60 61 62 63 64 65 66 67 68 69 70 71
    }
    if (toast && typeof (toast) === 'boolean') {
      loading();
    } else if (toast && typeof (toast) === 'string') {
      loading(toast);
    }
    return new Promise((resolve, reject) => {
      AsInst.post(url, data).then((response) => {
        closeLoading();
        resolve(response.data);
      }).catch((error) => {
        if (error.status === 200) {
          if (!error.data) {
godwithdh's avatar
godwithdh committed
72
            console.log('Customize Notice post_1', error);
godwithdh's avatar
godwithdh committed
73 74
          } else if (error.data && error.data.code == '0') {
            resolve(error.data)
godwithdh's avatar
godwithdh committed
75
            console.log('Customize Notice post_2', error);
godwithdh's avatar
godwithdh committed
76 77
          }
        } else if (error.status === 500) {
张锡奇's avatar
张锡奇 committed
78
          resolve(response.data);
godwithdh's avatar
godwithdh committed
79
          console.log('Customize Notice post_3', error);
godwithdh's avatar
godwithdh committed
80
        } else {
godwithdh's avatar
godwithdh committed
81
          console.log('Customize Notice post_4', error);
godwithdh's avatar
godwithdh committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        }
        closeLoading();
        reject(error);
      });
    });
  };
  /**
   * GET 请求 无提示
   * @param url 请求URL
   * @param data 请求数据
   * @returns {Promise}
   */
  Vue.prototype.apiGet = function (url, data, toast = false,header) {
    if (toast && typeof (toast) === 'boolean') {
      loading();
    } else if (toast && typeof (toast) === 'string') {
      loading(toast);
    }
    return new Promise((resolve, reject) => {
张锡奇's avatar
张锡奇 committed
101 102
      for(let x in header){
        AsInst.defaults.headers.common[x] = header[x];
godwithdh's avatar
godwithdh committed
103 104 105 106 107 108 109 110 111
      }
      AsInst.get(url, {
        params: data
      }).then((response) => {
        setTimeout(() => closeLoading(), 800);
        resolve(response.data);
      }).catch((error) => {
        if (error.status === 200) {
          if (!error.data) {
godwithdh's avatar
godwithdh committed
112
            console.log('Customize Notice get_1', error);
godwithdh's avatar
godwithdh committed
113 114
          }
        } else if (error.status === 500) {
张锡奇's avatar
张锡奇 committed
115
          resolve(response.data);
godwithdh's avatar
godwithdh committed
116
          console.log('Customize Notice get_2', error);
godwithdh's avatar
godwithdh committed
117
        } else {
godwithdh's avatar
godwithdh committed
118
          console.log('Customize Notice get_3', error);
godwithdh's avatar
godwithdh committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        }
        closeLoading();
        reject(error);
      });
    });
  };
  /**
   * 关闭方法
   */
  function loading(str){
    Vue.prototype.$loading(str || '')
  }
  function closeLoading() {
    Vue.prototype.$loading.close();
  }
};
export default Http;