'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; // require (PrototypeObject.js)


var _axios = require('axios');

var _axios2 = _interopRequireDefault(_axios);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// axios 默认配置
// axios.defaults.timeout = 5000
_axios2.default.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

// 构建get请求参数, get请求需要把url和data拼接起来
function buildGetUrl(url, params) {
  if (!params || Object.isEmptyObject(params)) {
    return url;
  }
  if (typeof params === 'string') {
    return url + '?' + params;
  }
  if (Object.type(params) === 'json') {
    return url + '?' + Object.params(params);
  }
  return url;
}

// 封装成Api类
var Api = {
  fail: function fail(error) {
    console.warn(error);
  },
  setBaseURL: function setBaseURL(baseURL) {
    _axios2.default.defaults.baseURL = baseURL;
  },
  request: function request(url) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    // 设置method
    var method = params.method;
    if (method !== 'get' && method !== 'post' && method !== 'jsonp') method = 'get';
    // 如果是jsonp, 则其它参数都没有意义了
    if (method === 'jsonp') {
      return new Promise(function (resolve, reject) {
        window.jsonCallBack = function (result) {
          resolve(result);
        };
        var JSONP = document.createElement('script');
        JSONP.type = 'text/javascript';
        JSONP.src = url + '&callback=jsonCallBack';
        document.getElementsByTagName('head')[0].appendChild(JSONP);
        setTimeout(function () {
          document.getElementsByTagName('head')[0].removeChild(JSONP);
        }, 500);
      });
    }
    // 设置头
    var head = params.head || {};
    // 设置options
    var options = params.options || {};
    // 设置data
    var data = params.data || {};
    return (0, _axios2.default)(_extends({
      url: method === 'get' ? buildGetUrl(url, data) : url,
      method: method,
      headers: _extends({}, head),
      data: method === 'get' ? null : data
    }, options));
  },
  post: function post(url) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    return this.request(url, Object.assign({}, params, { method: 'post' }));
  },
  get: function get(url) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    return this.request(url, Object.assign({}, params, { method: 'get' }));
  },
  all: function all(requests) {
    var _this = this;

    // requests: [{url: '', params: {}}]
    var methods = requests.map(function (request) {
      return _this.request(request.url, request.params);
    });
    return _axios2.default.all(methods);
  },
  jsonp: function jsonp(url) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    return this.request(url, Object.assign({}, params, { method: 'jsonp' }));
  }

  // 响应拦截器
};_axios2.default.interceptors.response.use(function (response) {
  var result = response.data || response;
  if (typeof result === 'string') {
    try {
      return JSON.parse(result);
    } catch (error) {
      console.log('result转换JSON失败' + error);
      return result;
    }
  }
  return result;
}, function (error) {
  if (Api.fail) Api.fail({ errMsg: error });
  return Promise.reject(error);
});

exports.default = Api;
module.exports = exports['default'];