utils.js 7.42 KB
Newer Older
陈龙's avatar
陈龙 committed
1 2 3
import style from '../ReportsManage.less';
import { downloadFileUrl } from '../../api/service/workflow';

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


const returnSummaryNumber = (configItems, num, isSummary) => {
  // 总结栏配置规则
  // $_d_sum*0.01*百万
  if (isNaN(Number(num))) return '-';
  if (!configItems) return num;
  num = Number(num);
  let _items = configItems.split('|');
  /*  let prefix = '';
    let suffix = '';*/
  let template = '_d';
  let precision = 0;
  let rate = 1;
  let combine = '_combine_'; // 连接符
  let _prefix = '';
  _items.forEach(item => {
    if (item.match(/_d_sum\*/)) {
103
      // 总结栏的倍率
陈龙's avatar
陈龙 committed
104 105
      let hasPrefix = item.match(/(\s+)?_d_sum/);
      _prefix = hasPrefix ? item.split('_d_sum')?.[0] : '';
106 107 108
      let _rateStr = item.replace(/_d_sum\*/, '');
      let _temp = _rateStr.split('*'); // ['0.01','百万']
      let _rate = _temp?.[0];
陈龙's avatar
陈龙 committed
109
      rate = (_rate && !isNaN(Number(_rate)) && isSummary) ? Number(_rate) : 1;
110
      if (isSummary && _temp?.[1]) {
陈龙's avatar
陈龙 committed
111
        template = `_d${combine}${_temp[1]}`;
112
      }
程恺文's avatar
程恺文 committed
113 114 115
    }
  });
  // 可能存在NaN的问题
陈龙's avatar
陈龙 committed
116 117 118 119 120 121 122
  let final = _items.includes('金额') ? Number((num * rate).toFixed(precision)).toLocaleString() : (num * rate).toFixed(precision);
  let _template = template.split(combine);
  return <>
    <span className={style.prefixOrSuffix}>{_prefix || ''}</span>
    {template.replace(/_d(.+)?/, isString(final) ? final : '-')}
    <span className={style.prefixOrSuffix}>{_template[1] || ''}</span>
  </>;
陈龙's avatar
陈龙 committed
123 124
};
/**
125 126 127 128 129 130 131 132 133
 *  @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
134 135
 *          defaultDateModel 时间参数 默认模式
 *          defaultDateValue 时间参数 默认时间
136
 * */
陈龙's avatar
陈龙 committed
137
const returnDefaultValueOrConfigs = (configItems = '', keysArray = []) => {
程恺文's avatar
程恺文 committed
138 139
  let _map = {};
  let _configItemsArray = configItems.split('|');
140 141
  keysArray.forEach(key => {
    _map[key] = _configItemsArray.find(item => item.includes(`${key}=`))?.replace(`${key}=`, '');
程恺文's avatar
程恺文 committed
142 143
  });
  return _map;
陈龙's avatar
陈龙 committed
144 145 146
};

function downloadFunc(url, name, target = '_self') {
程恺文's avatar
程恺文 committed
147 148 149 150 151 152
  const a = document.createElement('a');
  a.href = url;
  a.target = target;
  a.download = name;
  a.click();
  a.remove();
陈龙's avatar
陈龙 committed
153 154 155
}

/**
156 157 158 159 160 161
 *  @Description: 校验文件的名称是否包含特殊字符
 *  @Params: {Object: File} file file对象 { special:Boolean } 是否去除/的匹配
 *  @Date: 2021/12/8
 *  @Author: ChenLong
 *  @Return {Object} {type: error | success ,content: 提示...}
 * */
陈龙's avatar
陈龙 committed
162
function filenameVerification(file, special) {
程恺文's avatar
程恺文 committed
163 164
  // 文件名含有特殊字符  提示不能上传   {+,:/?#[]@!$&\\*+;=}
  // 规则对象(flag)
165
  var flag = !special ? new RegExp('[`~!@#$^&*=|{}\':;\',\\[\\]/?~!@#¥&*——|{}【】‘;:”“\'。,、?]') : new RegExp('[`~!@#$^&*=|{}\':;\',[\\]?~!@#¥&*——|{}【】‘;:”“\'。,、?]');
程恺文's avatar
程恺文 committed
166
  if (flag.test(file.name)) {
陈龙's avatar
陈龙 committed
167
    return {
程恺文's avatar
程恺文 committed
168
      type: 'error',
169
      content: `文件名格式错误,请检查文件名是否含有特殊字符${'~!@#$^&*=|{}\':;\',\\[\\]/?~!@#¥&*——|{}【】‘;:”“\'。,、?'}`,
陈龙's avatar
陈龙 committed
170
    };
程恺文's avatar
程恺文 committed
171 172 173 174 175
  }
  return {
    type: 'success',
    content: `上传成功!`,
  };
陈龙's avatar
陈龙 committed
176 177
}

陈龙's avatar
陈龙 committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
function handleFiles(file) {
  let _filesArr = !file ? [] : file.split(',').filter(item => item);
  let fileList = [];
  _filesArr.forEach((item, index) => {
    if (item && filenameVerification({ name: item }, true).type !== 'error') { // @Tips: 直接过滤掉名字中有异常字符的文件
      let _obj = {
        uid: index + '_' + Math.random(),
        value: item,
        name: item.includes('\\') ? item.split('\\').reverse()[0] : item.split('/').reverse()[0],
        type: 'file',
        status: 'done',
        url: `${downloadFileUrl}?filePath=${item}`,
        sourcePath: item,
      };
      fileList.push(_obj);
    }
  });
  return fileList
}

陈龙's avatar
陈龙 committed
198
export {
程恺文's avatar
程恺文 committed
199 200 201 202 203 204
  isObject,
  isString,
  isNumber,
  hasMoney,
  isArray,
  returnHandledNumber,
陈龙's avatar
陈龙 committed
205
  returnSummaryNumber,
程恺文's avatar
程恺文 committed
206 207 208 209 210 211 212
  returnDefaultValueOrConfigs,
  downloadFunc,
  filenameVerification,
  isSelect,
  returnOptions,
  returnRows,
  returnCols,
陈龙's avatar
陈龙 committed
213
  handleFiles,
陈龙's avatar
陈龙 committed
214
};