handlers.js 6.78 KB
Newer Older
1
import style from '../ReportsManage.less';
陈龙's avatar
陈龙 committed
2 3 4 5
import extraComponents from '../extra/extraComponents';
import moment from 'moment';
import { Tag } from 'antd';
import { hasMoney, isNumber } from './utils';
陈龙's avatar
陈龙 committed
6
import { tagColors, tagBackgroundColors } from './constant';
陈龙's avatar
陈龙 committed
7 8

/**
陈龙's avatar
陈龙 committed
9 10
 * @params: config下的数值的configRule结构如下,[{最大值: 10,最小值: 0,颜色:'#AAAAAA'}];
 * @business: configRule有值,则按照configRule设置;没有,按照color设置; 有最大值,无最小值;['', 1]
陈龙's avatar
陈龙 committed
11 12
 *
 *     有最大值,有最小值;[2,
陈龙's avatar
陈龙 committed
13 14
 *
 *        10]
陈龙's avatar
陈龙 committed
15
 *            有最小值,无最大值;[11,'']
陈龙's avatar
陈龙 committed
16
 */
陈龙's avatar
陈龙 committed
17 18 19 20 21
// 链接 功能 弹窗功能待提出需求
// 函数名、属性、...
const clickLink = (config) => {
  return window.open(config.configItems, '_blank');
};
陈龙's avatar
陈龙 committed
22
const handleUrl = (allWidgets, config) => {};
陈龙's avatar
陈龙 committed
23 24
const handleConfigUrl = (config, record) => {
  let _configs = config.configItems.split('|');
陈龙's avatar
陈龙 committed
25
  // type = 替换|跳转;
陈龙's avatar
陈龙 committed
26
  let _type = '';
陈龙's avatar
陈龙 committed
27
  //url=project/pandawork/contractDetails/index?ageSize=100&accountName=软件订单台账|contractCode=${订单编号}|company=${分公司}
陈龙's avatar
陈龙 committed
28 29
  let _url = '';
  let _params = {};
陈龙's avatar
陈龙 committed
30
  _configs.forEach((item) => {
陈龙's avatar
陈龙 committed
31 32 33 34 35
    if (item.includes('type=')) _type = item.replace('type=', '');
    if (item.includes('url=')) {
      let _tempUrl = item.replace('url=', '');
      let _urlArray = _tempUrl.split('?');
      if (_urlArray[1]) {
陈龙's avatar
陈龙 committed
36
        _urlArray[1].split('&').forEach((param) => {
陈龙's avatar
陈龙 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
          let _paramArr = param.split('=');
          if (_paramArr[1] && _paramArr[1].includes('${') && _paramArr[1].includes('}')) {
            let _key = _paramArr[1].replace(/\$\{|\}/g, '');
            _params[_paramArr[0]] = record[_key];
          } else {
            _params[_paramArr[0]] = _paramArr[1];
          }
        });
      }
      _url = _urlArray[0];
    }
  });
  return { url: _url, type: _type, params: _params };
};
const clickWidget = (config, text, record, showComponents, setDetailsConfig) => {
  // 功能跳转接口
  // type=替换|url=product/ReportsManage/ReportsSetting?accountName=${ID}&pageSize=100&title=${name}
  if (!config.configItems) return message.error('未配置跳转信息');
  // setDetailsComponentVisible(true);
  showComponents(true);
  let urlConfig = handleConfigUrl(config, record);
  // setDetailConfig(urlConfig);
  setDetailsConfig(urlConfig);
};
const clickModal = (config, showModal, setExtra) => {
  let _splitArr = config.configItems.split('&');
  let _fn = _splitArr[0] || 'default';
  _splitArr.splice(0, 1);
  let _data = {};
陈龙's avatar
陈龙 committed
66
  _splitArr.forEach((item) => {
陈龙's avatar
陈龙 committed
67 68 69 70 71 72 73 74 75
    let _item = item.split('=');
    _data[_item[0]] = _item[1];
  });
  // setExtraModal(true);
  showModal(true);
  return setExtra(extraComponents[_fn](_data));
};
export const handleNumber = (config, number) => {
  let _color = '';
陈龙's avatar
陈龙 committed
76 77
  let _number;
  if (number) _number = Number(number); // 当设置精度后,会被转成字符串
陈龙's avatar
陈龙 committed
78
  if (config.numericalConfigs && config.numericalConfigs.length) {
陈龙's avatar
陈龙 committed
79 80
    config.numericalConfigs.forEach((item) => {
      // 接口对于数值类型的返回为null
陈龙's avatar
陈龙 committed
81 82 83 84
      if (!_color) {
        let _max = item.maxValue || '';
        let _min = item.minValue || '';
        if (_max !== '' && _min === '') {
陈龙's avatar
陈龙 committed
85
          _color = _number <= Number(_max) ? '' : item.color;
陈龙's avatar
陈龙 committed
86
        } else if (_min !== '' && _max === '') {
陈龙's avatar
陈龙 committed
87
          _color = _number > Number(_min) ? item.color : '';
陈龙's avatar
陈龙 committed
88
        } else {
陈龙's avatar
陈龙 committed
89
          _color = _number > Number(_min) && _number <= Number(_max) ? item.color : '';
陈龙's avatar
陈龙 committed
90 91 92 93 94 95
        }
      }
    });
  } else if (config.color) {
    _color = config.color;
  }
陈龙's avatar
陈龙 committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  return number ? (
    <span>
      <span
        className={style.prefixOrSuffix}
        style={{
          color: _color,
        }}
      >
        {config.prefix || ''}
      </span>
      <span
        style={{
          color: _color,
          margin: '0 5px',
        }}
      >
        {hasMoney(config?.configItems)
          ? number
            ? Number(number)?.toLocaleString()
            : number
          : number}
      </span>
      <span className={style.prefixOrSuffix} style={{ color: _color }}>
        {config.suffix || ''}
      </span>
    </span>
  ) : (
    '-'
  );
陈龙's avatar
陈龙 committed
125
};
陈龙's avatar
陈龙 committed
126
/** @params: 标签形态的configRule,[{标签值:'字符串',颜色: '#AAAAAA'}]; */
陈龙's avatar
陈龙 committed
127 128 129 130 131 132
export const handleTag = (config, text) => {
  let _color = '';
  let _map = {};
  // 标签需要设置分隔符 2022年7月13日 ChenLong
  let _configItems = config.configItems.split('|');
  let _configMap = {};
陈龙's avatar
陈龙 committed
133
  _configItems.forEach((item) => {
陈龙's avatar
陈龙 committed
134 135 136 137 138
    let _arr = item.split('=');
    _configMap[_arr[0]] = _arr[1];
  });
  // 处理label的颜色
  if (config.labelConfigs && config.labelConfigs.length) {
陈龙's avatar
陈龙 committed
139
    config.labelConfigs.forEach((item) => {
陈龙's avatar
陈龙 committed
140 141 142 143 144
      _map[item.labelValue] = item.color;
    });
  }
  _color = config.color || 'rgba(0,0,0,.85)';
  // String(text)为了解决可能存在数值类型的数据需要设置成标签的需求
陈龙's avatar
陈龙 committed
145
  // return String(text).split(_configMap['分隔符']).map(item => <Tag color={_map[item]}>{item}</Tag>);
陈龙's avatar
陈龙 committed
146 147
  return String(text)
    .split(_configMap['分隔符'])
陈龙's avatar
陈龙 committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161
    .map((item) => (
      <Tag
        style={{
          background: _map[item] || _color,
          border: `1px solid ${_map[item] || _color}`,
          borderRadius: 2,
          color: _map[item]
            ? tagColors[tagBackgroundColors.findIndex((c) => c === _map[item])]
            : `rgba(0, 0, 0, .65)`,
        }}
      >
        {item}
      </Tag>
    ));
陈龙's avatar
陈龙 committed
162 163 164 165 166
};
export const handleText = (config, text) => {
  return <span style={{ color: config.color || 'rgba(0,0,0,.85)' }}>{text}</span>;
};
export const handleLink = (config, text) => {
陈龙's avatar
陈龙 committed
167 168 169 170 171 172 173 174 175 176 177
  return (
    <span
      className={style.link}
      style={{
        color: config.color || 'rgba(0,0,0,.85)',
      }}
      onClick={() => clickLink(config)}
    >
      {text}
    </span>
  );
陈龙's avatar
陈龙 committed
178 179
};
export const handleWidget = (config, text, record, showComponents, setDetailsConfig) => {
陈龙's avatar
陈龙 committed
180 181 182 183 184 185 186 187 188
  return (
    <span
      style={{ color: config.color }}
      className={style.link}
      onClick={() => clickWidget(config, text, record, showComponents, setDetailsConfig)}
    >
      {text}
    </span>
  );
陈龙's avatar
陈龙 committed
189 190
};
export const handleModal = (config, text, showModal, setExtra) => {
陈龙's avatar
陈龙 committed
191 192 193 194 195 196 197 198 199
  return (
    <span
      className={style.link}
      style={{ color: config.color || 'rgba(0,0,0,.85)' }}
      onClick={() => clickModal(config, showModal, setExtra)}
    >
      {text}
    </span>
  );
陈龙's avatar
陈龙 committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
};
// 日期
export const handleDateString = (config, text) => {
  return text ? moment(text).format('YYYY-MM-DD') : '-';
};
// 时刻
export const handleDateTimeString = (config, text) => {
  return text ? moment(text).format('YYYY-MM-DD HH:mm:ss') : '-';
};
// 处理pageSize
export const handlePageSize = (numStr) => {
  return numStr && isNumber(Number(numStr)) ? Number(numStr) : false;
};
// 处理默认排序
export const handleSortFields = (sortFields) => {
215
  return (sortFields && sortFields.split(',')) || [];
陈龙's avatar
陈龙 committed
216
};