common.js 4.8 KB
/**
 * 公共方法
 * author:jack
 * date:2018-07-03
 * */

import axios from 'axios';
const common = {

  /**
   * 导出excel
   * */
  exportExcel: function(options) {
    if (!options) {
      return false;
    }
    let oParams = {
      method: options.method || 'post',
      url: options.url,
      responseType: 'blob'
    }
    if (options.method == 'GET' || options.method == 'get') {
      oParams.params = options.data
    } else {
      oParams.data = options.data;
    }
    console.log(options.data);
    axios(oParams).then(response => {
      if (!response) {
        return
      };
      console.log(response)
      let a = document.createElement('a')
      let blob = new Blob([response.data], { type: "application/vnd.ms-excel" })
      let objectUrl = URL.createObjectURL(blob)
      a.setAttribute("href", objectUrl)
      a.setAttribute("download", '订单信息.xls')
      a.click()
        // let url = window.URL.createObjectURL(new Blob([response.data], {
        // 	type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
        // }));
        // let link = document.createElement('a');
        // link.style.display = 'none';
        // link.href = url;
        // link.setAttribute('download', options.fileName || 'excel.xlsx');
        // document.body.appendChild(link);
        // link.click();
      if (options.success) {
        (options.success)();
      }
    }).catch((error) => {
      if (options.errorFunc) {
        (options.errorFunc)(error);
      }
    })
  },

  /**
   * 下载zip
   */
  downPackFile: function(options) {
    if (!options) {
      return false;
    };

    /* let iframe = document.createElement('a')
    iframe.style.display = 'none'
    iframe.src = options.url
    document.body.appendChild(link);
    link.click(); */
    window.location.href = options.url;
    //document.body.appendChild(iframe)
  },


  /**
   * 获取时间对象
   */
  getDateInfo: function() {
    var o = new Date();
    var y = o.getFullYear();
    var m = o.getMonth() + 1;
    var d = o.getDate();
    var M = m;
    var D = d;

    if (m < 10) {
      M = '0' + m;
    };

    if (d < 10) {
      D = '0' + d;
    };

    return {
      y: y,
      m: m,
      M: M,
      d: d,
      D: D,
      today: y + '-' + M + '-' + D
    };
  },

  /**
   * 判断按钮是否有权限
   */
  hasPermission: function(id) {
    if (!id) {
      return false
    };
    let res = sessionStorage.getItem('MENURESOUCE');
    let mres = '';
    let resObj = {};
    if (res) {
      mres = JSON.parse(res);
    };

    if (mres && mres.length > 0) {
      for (let i = 0, len = mres.length; i < len; i++) {
        let child = mres[i].children;
        resObj[mres[i].menuId] = mres[i];
        let abc = Common.processData(child);
        Object.assign(resObj, abc);
      }
    };
    if (resObj[id]) {
      return true;
    } else {
      return false;
    }
  },

  /**
   * 递归处理数据
   */
  processData: function(o) {
    let list = {};
    if (o && o.length > 0) {
      for (let i = 0, len = o.length; i < len; i++) {
        let child = o[i].children;
        list[o[i].menuId] = o[i];
        if (child && child.length > 0) {
          let mn = Common.processData(child);
          Object.assign(list, mn);
        }
      }
    }
    return list;
  },

  /**
   * 递归获取子节点数据
   */
  processChildData: function(oInfo) {
    let resObj = {};
    if (!oInfo) {
      return false;
    }
    if (oInfo && oInfo.length > 0) {
      for (let i = 0, len = oInfo.length; i < len; i++) {
        let child = oInfo[i].children;
        resObj[oInfo[i].menuId] = oInfo[i];
        let abc = Common.processData(child);
        Object.assign(resObj, abc);
      }
    };
    return resObj;
  },

  /**
   * 数字转换成货币格式
   */
  formatMoney: function(number, places, symbol, thousand, decimal) {
    number = number || 0;
    places = !isNaN(places = Math.abs(places)) ? places : 2;
    symbol = symbol !== undefined ? symbol : "$";
    thousand = thousand || ",";
    decimal = decimal || ".";
    var negative = number < 0 ? "-" : "",
      i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
      j = (j = i.length) > 3 ? j % 3 : 0;
    return negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" +
      thousand) + (places ? '' : "");
  },


  /**
   * 字符串截取
   */
  /**
   * 截取字符串
   */
  subStrByLength: function(str, length) {
    var retStr = str;
    var len = 3;

    if (retStr) {
      if (length) {
        len = length;
      };

      if (str.length > len) {
        retStr = str.substring(0, len) + '...';
      }
    };

    return retStr;
  }


};

export const exportExcel = common.exportExcel;
export const subStrByLength = common.subStrByLength;
export const formatMoney = common.formatMoney;