DetailDom.js 4.3 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
import { styleCount } from './config';
6
import { isJSON } from '@/webPublic/one_stop_public/copy';
7

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

const normalTextRender = (text, config) => {
17 18 19 20 21 22
	if (!text || text === 'undefined' || typeof text === 'undefined') {
		return null;
	}
	const configInfoJSON = (config.info && isJSON(config.info) && JSON.parse(config.info)) || {};
	const configTextStyle = configInfoJSON.textStyle || {}; // 文本的样式
	const configTextBreakLength = configInfoJSON.textBreakLength || undefined; // 字数过宽换行
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 61 62 63 64
	const width = config.wide ? config.wide + 'px' : 'auto';
	const height = config.high ? config.high + 'px' : 'auto';
	if (configTextBreakLength && text.length > configTextBreakLength) {
		text = [text.substr(0, configTextBreakLength), text.substr(configTextBreakLength)];
		return text.map((x, index) => {
			return (
				<div
					key={x}
					style={{
						...styleCount(config),
						marginBottom: '0',
						width,
						textAlign: 'right',
						...configTextStyle,
					}}>
					{x}
				</div>
			);
		});
	}
	if (config.mark && text.indexOf(config.mark) > -1) {
		// 换行分隔符
		text = text.split(config.mark);
		let length = text.length;
		return text.map((x, index) => {
			return (
				<div
					key={x}
					style={{
						...styleCount(config),
						marginBottom: '0',
						width,
						textAlign: 'right',
						...configTextStyle,
					}}>
					{x}
					{length > index + 1 ? config.mark : ''}
				</div>
			);
		});
	}
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	return (
		<div
			style={{
				...styleCount(config),
				width,
				height,
				lineHeight: 'normal',
				verticalAlign: 'top',
				textAlign: 'right',
				letterSpacing: '2px',
				...configTextStyle,
			}}>
			{text}
		</div>
	);
81 82
};

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

95
class BarCode extends React.Component {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	toJsBarcode() {
		// 调用 JsBarcode方法生成条形码
		const { value } = this.props;
		let info = value;
		if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
			info = 'error';
			console.log('条形码包含中文不能输出', value);
		}
		JsBarcode(this.barcode, info, {
			text: '',
			format: 'CODE128',
			displayValue: false,
			width: 1.0,
			height: 30,
			margin: 0,
		});
		return true;
	}
114

115 116 117
	componentDidMount() {
		this.toJsBarcode();
	}
钟是志's avatar
钟是志 committed
118

119 120 121 122 123 124 125 126 127 128 129
	render() {
		return (
			<div className="barcode-box">
				<svg
					ref={(ref) => {
						this.barcode = ref;
					}}
				/>
			</div>
		);
	}
130 131 132
}

export default function DetailDom({ config, data }) {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	const { x, y, transform } = config;
	const styleOutSide = {
		position: 'absolute',
		zIndex: 10,
		fontWeight: '600',
		left: x + 'px',
		top: y + 'px',
	};
	if (transform) {
		styleOutSide.transform = `rotate(${transform}deg)`;
		styleOutSide.WebkitTransform = `rotate(${transform}deg)`;
		styleOutSide.MozTransform = `rotate(${transform}deg)`;
		styleOutSide.MsTransform = `rotate(${transform}deg)`;
		//    styleOutSide.MsFilter = "progid:DXImageTransform.Microsoft.Matrix(M11=-1, M12=1.2246467991473532e-16, M21=-1.2246467991473532e-16, M22=-1, SizingMethod='auto expand')";
	}
钟是志's avatar
钟是志 committed
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	const outSideDom = (children) => {
		return (
			<div
				style={{
					...styleOutSide,
				}}
				key={`filed${config.id}`}>
				{children}
			</div>
		);
	};
	let children = null;
	switch (config.fieldType) {
		case '-2':
			children = <BarCode value={data} />;
			break;
		case '-1': // 常量
			children = normalTextRender(config.content, config);
			break;
		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);
			break;
		case '1': // 时间
			let time = data;
			time = moment(time).format(config.fieldPattern || 'YYYY-MM-DD');
			children = normalTextRender(time, config);
			break;
		case '2': // 照片 图章
			children = imageRender(data);
			break;
		default:
			console.error('未知的打印数据类型,无法渲染');
			children = null;
			break;
	}
	return outSideDom(children);
191
}