common.js 3.99 KB
import Vue from 'vue'
import VueResource from 'vue-resource'

import FileSaver from "file-saver";
import XLSX from "xlsx";

Vue.use(VueResource)
//const httpUrl = 'http://1.1.1.1:8080/'


/**
 * 格式化枚举中文显示
 * 其中枚举是单个字符
 */
function formatterInfo(value, options) {
    if(value==null || value==""){
        return "";
    }
    let res = value;
    for (let i = 0; i < options.length; i++) {
        if (value == options[i].value) {
            res = options[i].label;
            break;
        }
    }
    return res;
}
/**
 * 格式化枚举类型中文显示
 * 其中枚举可能是多个字符
 */
function formatterMoreInfo(value, options) {
    if(value==null || value==""){
        return "";
    }
    let res = "";
    let arrValue = value.split("");
    for (let j = 0; j < arrValue.length; j++) {
        let val = arrValue[j];
        for (let i = 0; i < options.length; i++) {
            if (val == options[i].value) {
                res += "," + options[i].label;
                break;
            }
        }
    }
    if (res == "") {
        return value;
    } else {
        return res.substring(1);
    }
}
/**
 * 对部分枚举可能值是0开头的,但是表数据不规范的时候,导致01变为1这种情况进行解决
 * @param {} value
 * @param {*} options
 */
function formatterInfoChangInt(value, options) {
    if(value==null || value==""){
        return "";
    }
    let res = value;
    for (let i = 0; i < options.length; i++) {
        if (parseInt(value) == parseInt(options[i].value)) {
            res = options[i].label;
            break;
        }
    }
    return res;
}

/**
 * 处理图片访问地址的,目的是解决谷歌高版本浏览器不支持ftp协议
 * @param {*} path
 */
function imgPathAnalysis(serip,path){
    if(path !=null && path != "" && path.length > 10){
        var path = path.trim().toLowerCase();
        if(path.indexOf("ftp://") == 0){
            return serip+"/image/showByByte"+"?ipath="+path;
        }else{
            return path;
        }
    }else{
        return "";
    }
}

function formatDateFun(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}

function exportExcel(title) {
    /* generate workbook object from table */
    let wb = XLSX.utils.table_to_book(
      document.querySelector("#rebateSetTable")
    );
    /* get binary string as output */
    let wbout = XLSX.write(wb, {
      bookType: "xlsx",
      bookSST: true,
      type: "array"
    });
    try {
      FileSaver.saveAs(
        new Blob([wbout], { type: "application/octet-stream" }),
        title+".xlsx"
      );
    } catch (e) {
      if (typeof console !== "undefined") console.log(e, wbout);
    }
    return wbout;
  }
 function getEndDay(dateType,date) {
   let newEndDay=''
   switch (dateType){
     case "month":{
       let dateArr=date.split('-');
       var maxDay = new Date(dateArr[0],dateArr[1],0).getDate();   //获取date月最大天数
       var selectDate=new Date(Date.parse((date+'-'+maxDay).replace(/-/g,"/")));

       var curDate=new Date();
       if(selectDate > curDate){
           newEndDay=formatDateFun(new Date(),'yyyy-MM-dd')
       }else{
          newEndDay = date+'-'+maxDay
       }
       return newEndDay;
     }
       break;
     default:
       break;
   }
   if(dateType=="month"){

   }
 }
export default {
    formatterInfo,
    formatterMoreInfo,
    formatterInfoChangInt,
    imgPathAnalysis,
    formatDateFun,
    exportExcel,
    getEndDay
}