export const util = {
  //判断obj是否为json对象
  isJson(obj){
    var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
    return isjson;
  },

  checkMobile: function(telphone){
    var isChinaMobile = /^1(3[4-9]|5[012789]|8[23478]|4[7]|7[8])\d{8}$/; //移动方面最新答复
    var isChinaUnion = /^1(3[0-2]|5[56]|8[56]|4[5]|7[6])\d{8}$/; //向联通微博确认并未回复
    var isChinaTelcom = /^1(3[3])|(8[019])\d{8}$/; //1349号段 电信方面没给出答复，视作不存在
    var isOtherTelphone  = /^170([059])\\d{7}$/;//其他运营商

    telphone = telphone.trim();
    if(telphone.length !== 11){
      return this.setReturnJson(false, '未检测到正确的手机号码');
    }
    else{
      if(isChinaMobile.test(telphone)){
        return this.setReturnJson(true, '移动', {name: 'ChinaMobile'});
      }
      else if(isChinaUnion.test(telphone)){
        return this.setReturnJson(true, '联通', {name: 'ChinaUnion'});
      }
      else if(isChinaTelcom.test(telphone)){
        return this.setReturnJson(true, '电信', {name: 'ChinaTelcom'});
      }
      else if(isOtherTelphone.test(telphone)){
        var num = isOtherTelphone.exec(telphone);
        return this.setReturnJson(true, '', {name: ''});
      }
      else{
        return this.setReturnJson(false, '未检测到正确的手机号码');
      }
    }
  },
  setReturnJson: function(status, msg, data){
    if(typeof status !== 'boolean' && typeof status !== 'number'){
      status = false;
    }
    if(typeof msg !== 'string'){
      msg = '';
    }
    return {
      'status': status,
      'msg': msg,
      'data': data
    };
  },
  downloadFile: function(data, fileName) {
    // 文件导出
    if (!data) {
      return
    }
    let url = window.URL.createObjectURL(new Blob([data]));
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', fileName + '.xls');
    document.body.appendChild(link);
    link.click()
  },

}

