一个项目中的common.js文件,仅以做收藏

(function () {
  var oldAjax = jQuery.ajax;
  var EMPTY = {};
  var defaultOpts = {
    contentType: 'application/json',
    dataType: 'json',
    processData: false,
    beforeSend: function (jqXHR, options) {
      if (options.contentType === 'application/json' && typeof options.data !== 'string') {
        options.data = JSON.stringify(options.data);
      }
    }
  };
  jQuery.ajax = function (url, options) {
    options = options || EMPTY;

    if (typeof url === 'object') options = url;
    else if (typeof url === 'string') options.url = url;
    if (!options.url) throw new Error('bad args.');
    return new Promise(function (resolve, reject) {
      //这里的链式调用使用的是原生promise?

      oldAjax(Object.assign({}, defaultOpts, options)).then(function (obj) {
        if (obj.statusCode === 0) {
          // 如果接口返回成功直接 resolve 成功数据
          return resolve(obj.data);
        }

        // 将 messages 默认 join 成 message,方便业务层直接使用。
        obj.message = obj.messages.join('\n');
        reject(obj);
      }).catch(function (xhr) {
        var obj = xhr.responseJSON;
        if (!obj) {
          obj = {
            statusCode: xhr.statusCode,
            messages: [xhr.statusText]
          };
        }
        obj.message = obj.messages.join('\n');
        reject(obj);
      });
    });
  };


  /**
   * 获取表单验证码,
   * 传入设置src的图片的jquery对象,
   * 传入需要再window上用于存储图片验证码的uuid的属性名
   * @param elm  jquery对象
   * @param string  window[属性名]
   */
  window.getValid = function (elm, string) {
    var count = 0;
    _getValid(count);

    function _getValid() {
      if (count >= 10) {
        anertTip({content: '获取验证码失败, 请检查您的网络, 或联系管理员'});
      }
      $.get('/verification_code').then(function (data) {
        elm.attr('src', data.url);
        window[string] = data.id;
      }).catch(function () {
        _getValid(++count);
      });
    }
  };


  /**
   *  传入元素选择器, 获取改元素的value, 用于获取元素最原始的值,(没有想getValuse哪样会判断other input的逻辑)
   * @param seletor
   * @returns {*}
   */
  window.getBaseValue = function getBaseValue(seletor) {
    var type = $(seletor).attr('type') ? $(seletor).attr('type').toLowerCase() : $(seletor)[0].tagName.toLowerCase();
    if (type === 'radio') {
      return $(seletor + ':checked').val();
    } else if (type === 'text' || type === 'tel' || type === 'email' || type === 'hidden' || type === 'textarea') {
      return $(seletor).val();
    } else if (type === 'checkbox') {
      return window.getCheckboxValues(seletor);
    } else if (type === 'select') {
      return $(seletor + ' option:selected').val();
    }
  };

  /**
   * 用于得到多选框的值
   *
   * @param {*} string jquery选择器
   * @returns 返回多选框的值组成的数组
   */
  window.getCheckboxValues = function getCheckboxValues(string) {
    var CheckboxValues = [];
    $(string).each(function (index, item) {
      if (item.checked) {
        CheckboxValues.push($(item).val());
      }
    });
    return CheckboxValues;
  };

  /**
   * 传入数组,使用getValue方法,获取表单元素的值
   * @param secletors  [{name: 'name', selector: 'selector'},/...]
   * @returns {name: value, name1: value1,...}
   */
  window.getValues = function getValues(selectors) {
    var map = {};
    selectors.forEach(function (item) {
      map[item.name] = window.getValue(item.selector);
    });
    return map;
  };

  /**
   * 用于获取一个问题的值, 传入一个选择其, 判断是否有data-ohter这个自定义属性;
   * 如果有, 获取data-other的值,并凭借判断当前值是否等于data-other的值, 如果等于就判断为,需要获取other input的值.
   */
  window.getValue = function getValue(selector) {
    var value, otherData;
    value = window.getBaseValue(selector);
    otherData = $(selector).data('other');
    if (otherData && value == otherData) {//如果元素没有data-other的值, 或在值为空(单选情况)
      value = window.getBaseValue(selector.slice(0, -1) + 'Value]');
    } else if (otherData && $.isArray(value) && $.inArray(otherData, value) !== -1) {//如果存在, 或otherData被包含在了getValue中(多选情况)
      for (var i = 0; i < value.length; i++) {
        if (value[i] === otherData) {
          value[i] = window.getBaseValue(selector.slice(0, -1) + 'Value]');
          break;
        }
      }
    }
    return value;
  };

  /**
   * 下载文件
   * @param url
   */
  window.downloadFile = function downloadFile(url) {

    //以a标签加download实现, 兼容性不确定
    /*  var a = $('');
      a.attr('href', url);
      $('body').append(a);
      a[0].click();
      a.remove();
  */
    //使用新开窗口实现, 只能下载不可被浏览器解析的文件
    window.open(url, '_blank');

    //使用iframe
    // var iframe = $('