DetailDom.js 3.8 KB
Newer Older
1 2 3
import React from 'react';
import JsBarcode from 'jsbarcode';
import styles from '../index.less';
钟是志's avatar
钟是志 committed
4
import moment from 'moment';
5

6 7 8 9 10 11 12 13
/**
 * -2 条形码
 * -1 常量
 * 0  文字
 * 1 时间
 * 2 照片
 * */

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
export const fieldTypeList = [
  {
    key: '-2',
    name: '条形码',
  },
  {
    key: '-1',
    name: '常量',
  },
  {
    key: '0',
    name: '文字',
  },
  {
    key: '1',
    name: '时间',
  },
  {
    key: '2',
    name: '照片',
  },
];

export const fontFamilyList = [
  {
    key: '宋体',
    name: '宋体',
  },
  {
    key: '楷体',
    name: '楷体',
  },
  {
    key: '黑体',
    name: '黑体',
  },
  {
    key: '隶书',
    name: '隶书',
  },
  {
    key: '微软雅黑',
    name: '微软雅黑',
  },
];


61
const normalTextRender = (text, config) => {
钟是志's avatar
钟是志 committed
62
  if (!text || text === 'undefined' || typeof text === 'undefined') {
63 64 65 66
    return null;
  }
  const style = {
    fontSize: `${config.fieldFontSize || 16}px`, // 默认 16px
钟是志's avatar
钟是志 committed
67
    fontFamily: `${config.fieldFont || '黑体'}`, // 默认黑体 包括 ('宋体','楷体','黑体','隶书','微软雅黑等字体'}
68
  };
69 70 71 72 73 74 75 76 77 78 79 80
  if (config.mark && text.indexOf(config.mark) > -1) { // 换行分隔符
    text = text.split(config.mark);
    return <span style={style}>
          {text.map((x) => {
            return <p
              key={x}
              style={{ marginBottom: '0' }}>
              {x}
            </p>;
          })}
        </span>;
  }
81 82 83 84 85
  return <span style={style}>
    {text}
  </span>;
};

钟是志's avatar
钟是志 committed
86 87 88 89 90 91 92 93 94
const imageRender = (imageUrl) => {
  return <img src={imageUrl} //  TODO 这里图片样式需要考虑怎么兼容所有业务
              style={{
                height: '100%',
                width: 'auto',
              }}
  />;
};

95 96 97 98 99

class BarCode extends React.Component {

  toJsBarcode() {
    // 调用 JsBarcode方法生成条形码
钟是志's avatar
钟是志 committed
100 101 102 103 104 105 106
    const { value } = this.props;
    let info = value;
    if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
      info = 'error';
      console.log('条形码包含中文不能输出', value);
    }
    JsBarcode(this.barcode, info, {
107
      text: '',
钟是志's avatar
钟是志 committed
108
      format: 'CODE128',
109 110 111 112 113
      displayValue: false,
      width: 1.0,
      height: 30,
      margin: 0,
    });
钟是志's avatar
钟是志 committed
114 115 116 117 118
    return true;
  }

  componentDidMount() {
    this.toJsBarcode();
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  }

  render() {
    return (
      <div className="barcode-box">
        <svg
          ref={(ref) => {
            this.barcode = ref;
          }}
        />
      </div>
    );
  }
}


export default function DetailDom({ config, data }) {
136
  const { x, y, fieldCode, transform } = config;
137 138 139 140
  const outSideDom = (children) => {
    return (
      <div
        className={styles.inSideItemPrint}
141
        key={`filed${config.id}`}
142 143 144
        style={{
          left: x + 'px',
          top: y + 'px',
145
          transform: transform ? `rotate(${transform}deg)` : 'none',
146 147 148 149 150 151 152
        }}>
        {children}
      </div>
    );
  };
  let children = null;
  switch (config.fieldType) {
钟是志's avatar
钟是志 committed
153
    case '-2':
154 155 156 157 158
      children = <BarCode value={data}/>;
      break;
    case '-1': // 常量
      children = normalTextRender(config.content, config);
      break;
钟是志's avatar
钟是志 committed
159 160 161 162 163 164 165 166
    case '0': // 文字 带参数
      let text = data;
      const strPlace = config.content && config.content.indexOf('${');
      if (strPlace > -1) {
        const str = `\${${config.fieldCode}}`;
        text = config.content.replace(str, text);
      }
      children = normalTextRender(text, config);
167 168
      break;
    case '1': // 时间
钟是志's avatar
钟是志 committed
169 170 171
      let time = data;
      time = moment(time).format(config.fieldPattern || 'YYYY-MM-DD');
      children = normalTextRender(time, config);
172
      break;
钟是志's avatar
钟是志 committed
173 174
    case '2': // 照片 图章
      children = imageRender(data);
175 176
      break;
    default:
钟是志's avatar
钟是志 committed
177 178
      console.error('未知的打印数据类型,无法渲染');
      children = null;
179 180 181 182
      break;
  }
  return outSideDom(children);
}