utils.js 5.77 KB
Newer Older
陈龙's avatar
陈龙 committed
1
const isObject = (obj) => {
程恺文's avatar
程恺文 committed
2
  return Object.prototype.toString.call(obj) === '[object Object]';
陈龙's avatar
陈龙 committed
3 4
};
const isString = (obj) => {
程恺文's avatar
程恺文 committed
5
  return Object.prototype.toString.call(obj) === '[object String]';
陈龙's avatar
陈龙 committed
6 7
};
const isNumber = (num) => {
程恺文's avatar
程恺文 committed
8
  return Object.prototype.toString.call(num) === '[object Number]';
陈龙's avatar
陈龙 committed
9 10
};
const isArray = (arr) => {
程恺文's avatar
程恺文 committed
11
  return Object.prototype.toString.call(arr) === '[object Array]';
陈龙's avatar
陈龙 committed
12 13
};
const hasMoney = (configItems) => {
程恺文's avatar
程恺文 committed
14 15
  if (!configItems) return false;
  let _items = configItems.split('|');
16
  return !!_items.find(item => item === '金额');
陈龙's avatar
陈龙 committed
17 18
};
const isSelect = (configItems) => {
程恺文's avatar
程恺文 committed
19 20
  if (!configItems) return false;
  let _items = configItems.split('|');
21
  return !!_items.find(item => item === 'renderAsSelect');
陈龙's avatar
陈龙 committed
22 23 24
};
// options=name1[.value1],name2[.value2],name3[.value3];
const returnOptions = (configItems) => {
程恺文's avatar
程恺文 committed
25 26
  if (!configItems) return false;
  let _items = configItems.split('|');
27
  let _options = _items.find(item => item.includes('options='));
程恺文's avatar
程恺文 committed
28 29
  if (!_options) return false;
  return _options.replace('options=', '').split(',');
陈龙's avatar
陈龙 committed
30 31
};
const returnRows = (configItems) => {
程恺文's avatar
程恺文 committed
32 33
  if (!configItems) return 3;
  let _items = configItems.split('|');
34
  let _options = _items.find(item => item.includes('rows='));
程恺文's avatar
程恺文 committed
35 36 37
  if (!_options) return 3;
  let _rows = Number(_options.replace('rows=', ''));
  return !isNaN(_rows) && _rows >= 1 ? _rows : 3;
陈龙's avatar
陈龙 committed
38 39
};
const returnCols = (configItems) => {
程恺文's avatar
程恺文 committed
40 41
  if (!configItems) return 1;
  let _items = configItems.split('|');
42
  let _options = _items.find(item => item.includes('cols='));
程恺文's avatar
程恺文 committed
43 44 45
  if (!_options) return 1;
  let _cols = Number(_options.replace('cols=', ''));
  return !isNaN(_cols) && _cols <= 3 && _cols >= 0 ? _cols : 1;
陈龙's avatar
陈龙 committed
46 47
};
/**
48 49 50 51 52
 *  @Description: 用来在summary中处理数值的配置
 *  @Params: 参数描述
 *  @Date: 2022/8/10
 *  @Author: ChenLong
 * */
陈龙's avatar
陈龙 committed
53
const returnHandledNumber = (configItems, num, isSummary) => {
程恺文's avatar
程恺文 committed
54
  // 精度、前缀、后缀、倍率
55
  // $_d|_d%|_d*0.0001|金额|0.00|_d_sum*0.01*百万
程恺文's avatar
程恺文 committed
56 57 58 59 60
  if (isNaN(num)) return '-';
  if (!configItems) return num;
  num = Number(num);
  let _items = configItems.split('|');
  /*  let prefix = '';
61
    let suffix = '';*/
程恺文's avatar
程恺文 committed
62 63 64
  let template = '_d';
  let precision = 0;
  let rate = 1;
65
  _items.forEach(item => {
程恺文's avatar
程恺文 committed
66
    let _arr = [];
67
    if (item.match(/_d[^(\*|_sum)]/)) {
程恺文's avatar
程恺文 committed
68 69 70 71
      // 后缀
      template = item;
    } else if (item.match(/^_d\*/)) {
      // 倍率
72 73 74
      // let _rate = item.replace(/_d\*/, '');
      // rate = (_rate && isSummary) ? Number(_rate) : 1; // 总结栏计算需要计算倍率,其他不用
      // edit by chenlong 2023年2月22日 需求变更,总结栏精度转换导致某些累加不够的情况,数据单位发生了变化,废弃当前做法,新增总结栏的倍率判断
程恺文's avatar
程恺文 committed
75 76 77
    } else if (item.match(/^0\./)) {
      // 精度
      precision = item.replace('0.', '').length;
78 79 80 81 82 83 84 85 86
    } else if (item.match(/_d_sum\*/)) {
      // 总结栏的倍率
      let _rateStr = item.replace(/_d_sum\*/, '');
      let _temp = _rateStr.split('*'); // ['0.01','百万']
      let _rate = _temp?.[0];
      rate = (_rate && !isNaN(Number(_rate)) && isSummary) ? (Number(_rate)) : 1;
      if (isSummary && _temp?.[1]) {
        template = `_d ${_temp[1]}`;
      }
程恺文's avatar
程恺文 committed
87 88 89
    }
  });
  // 可能存在NaN的问题
90 91
  let final = _items.includes('金额') ? Number((num * rate).toFixed(precision)).toLocaleString() : Number((num * rate).toFixed(precision)).toLocaleString();
  return template.replace(/_d/, isString(final) ? final : '-'); // 最后用_d%这个带了后缀的模板,将_d用计算后的数字替换掉,就得出带了后缀、单位的数值字符串
陈龙's avatar
陈龙 committed
92 93
};
/**
94 95 96 97 98 99 100 101 102
 *  @Description: 返回configItems内配置的默认值、默认模式等等
 *  @Params: 参数描述
 *  @Date: 2022/8/12
 *  @Author: ChenLong
 *  @params:
 *           configItems 报表字段的配置 例如 defaultValue=智慧水务    defaultDateModel=customer|defaultDateValue=2022-01-01,2022-12-31;
 *           keysArray 所需要返回的值的key的集合,比如你需要获取configItems中的’defaultValue‘,那么keysArray=['defaultValue'];
 *  @Returns:
 *          defaultValue 通用参数 默认值
陈龙's avatar
陈龙 committed
103 104
 *          defaultDateModel 时间参数 默认模式
 *          defaultDateValue 时间参数 默认时间
105
 * */
陈龙's avatar
陈龙 committed
106
const returnDefaultValueOrConfigs = (configItems = '', keysArray = []) => {
程恺文's avatar
程恺文 committed
107 108
  let _map = {};
  let _configItemsArray = configItems.split('|');
109 110
  keysArray.forEach(key => {
    _map[key] = _configItemsArray.find(item => item.includes(`${key}=`))?.replace(`${key}=`, '');
程恺文's avatar
程恺文 committed
111 112
  });
  return _map;
陈龙's avatar
陈龙 committed
113 114 115
};

function downloadFunc(url, name, target = '_self') {
程恺文's avatar
程恺文 committed
116 117 118 119 120 121
  const a = document.createElement('a');
  a.href = url;
  a.target = target;
  a.download = name;
  a.click();
  a.remove();
陈龙's avatar
陈龙 committed
122 123 124
}

/**
125 126 127 128 129 130
 *  @Description: 校验文件的名称是否包含特殊字符
 *  @Params: {Object: File} file file对象 { special:Boolean } 是否去除/的匹配
 *  @Date: 2021/12/8
 *  @Author: ChenLong
 *  @Return {Object} {type: error | success ,content: 提示...}
 * */
陈龙's avatar
陈龙 committed
131
function filenameVerification(file, special) {
程恺文's avatar
程恺文 committed
132 133
  // 文件名含有特殊字符  提示不能上传   {+,:/?#[]@!$&\\*+;=}
  // 规则对象(flag)
134
  var flag = !special ? new RegExp('[`~!@#$^&*=|{}\':;\',\\[\\]/?~!@#¥&*——|{}【】‘;:”“\'。,、?]') : new RegExp('[`~!@#$^&*=|{}\':;\',[\\]?~!@#¥&*——|{}【】‘;:”“\'。,、?]');
程恺文's avatar
程恺文 committed
135
  if (flag.test(file.name)) {
陈龙's avatar
陈龙 committed
136
    return {
程恺文's avatar
程恺文 committed
137
      type: 'error',
138
      content: `文件名格式错误,请检查文件名是否含有特殊字符${'~!@#$^&*=|{}\':;\',\\[\\]/?~!@#¥&*——|{}【】‘;:”“\'。,、?'}`,
陈龙's avatar
陈龙 committed
139
    };
程恺文's avatar
程恺文 committed
140 141 142 143 144
  }
  return {
    type: 'success',
    content: `上传成功!`,
  };
陈龙's avatar
陈龙 committed
145 146 147
}

export {
程恺文's avatar
程恺文 committed
148 149 150 151 152 153 154 155 156 157 158 159 160
  isObject,
  isString,
  isNumber,
  hasMoney,
  isArray,
  returnHandledNumber,
  returnDefaultValueOrConfigs,
  downloadFunc,
  filenameVerification,
  isSelect,
  returnOptions,
  returnRows,
  returnCols,
陈龙's avatar
陈龙 committed
161
};