提交 1d99fc59 authored 作者: 钟是志's avatar 钟是志

打印预览修改

上级 b241d72f
import React from 'react'; import React from 'react';
import JsBarcode from 'jsbarcode'; import JsBarcode from 'jsbarcode';
import styles from '../index.less'; import styles from '../index.less';
import moment from 'moment';
/** /**
* -2 条形码 * -2 条形码
* -1 常量 * -1 常量
...@@ -16,29 +16,46 @@ const normalTextRender = (text, config) => { ...@@ -16,29 +16,46 @@ const normalTextRender = (text, config) => {
} }
const style = { const style = {
fontSize: `${config.fieldFontSize || 16}px`, // 默认 16px fontSize: `${config.fieldFontSize || 16}px`, // 默认 16px
fontFamily: `${config.fieldFont || '黑体'}`, // 默认黑体 fontFamily: `${config.fieldFont || '黑体'}`, // 默认黑体 包括 ('宋体','楷体','黑体','隶书','微软雅黑等字体'}
}; };
return <span style={style}> return <span style={style}>
{text} {text}
</span>; </span>;
}; };
const imageRender = (imageUrl) => {
return <img src={imageUrl} // TODO 这里图片样式需要考虑怎么兼容所有业务
style={{
height: '100%',
width: 'auto',
}}
/>;
};
class BarCode extends React.Component { class BarCode extends React.Component {
componentDidMount() {
this.toJsBarcode();
}
toJsBarcode() { toJsBarcode() {
// 调用 JsBarcode方法生成条形码 // 调用 JsBarcode方法生成条形码
JsBarcode(this.barcode, this.props.value || '1234567890', { const { value } = this.props;
let info = value;
if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
info = 'error';
console.log('条形码包含中文不能输出', value);
}
JsBarcode(this.barcode, info, {
text: '', text: '',
format: 'CODE39', format: 'CODE128',
displayValue: false, displayValue: false,
width: 1.0, width: 1.0,
height: 30, height: 30,
margin: 0, margin: 0,
}); });
return true;
}
componentDidMount() {
this.toJsBarcode();
} }
render() { render() {
...@@ -72,23 +89,32 @@ export default function DetailDom({ config, data }) { ...@@ -72,23 +89,32 @@ export default function DetailDom({ config, data }) {
}; };
let children = null; let children = null;
switch (config.fieldType) { switch (config.fieldType) {
case '-2': // 条形码 case '-2':
children = <BarCode value={data}/>; children = <BarCode value={data}/>;
break; break;
case '-1': // 常量 case '-1': // 常量
children = normalTextRender(config.content, config); children = normalTextRender(config.content, config);
break; break;
case '0': // 文字 case '0': // 文字 带参数
children = normalTextRender(data, config); 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; break;
case '1': // 时间 case '1': // 时间
children = normalTextRender(config.content, config); let time = data;
time = moment(time).format(config.fieldPattern || 'YYYY-MM-DD');
children = normalTextRender(time, config);
break; break;
case '2': // 照片 case '2': // 照片 图章
children = normalTextRender(config.content, config); children = imageRender(data);
break; break;
default: default:
children = normalTextRender(config.content, config); console.error('未知的打印数据类型,无法渲染');
children = null;
break; break;
} }
return outSideDom(children); return outSideDom(children);
......
...@@ -16,7 +16,7 @@ export default class ViewPrint extends Component { ...@@ -16,7 +16,7 @@ export default class ViewPrint extends Component {
configAll: null, configAll: null,
viewData: null, viewData: null,
loading: false, loading: false,
minHeight: 700, minHeight: 0,
showWindowPrint: false, showWindowPrint: false,
}; };
this.getConfigInfo = queryConfig.bind(this); this.getConfigInfo = queryConfig.bind(this);
...@@ -50,6 +50,7 @@ export default class ViewPrint extends Component { ...@@ -50,6 +50,7 @@ export default class ViewPrint extends Component {
if (!viewData || !viewData.length) { if (!viewData || !viewData.length) {
message.warning('未查询到可打印的奖状数据'); message.warning('未查询到可打印的奖状数据');
console.error(`${x.queryUrl}接口报错或者没有返回数据`); console.error(`${x.queryUrl}接口报错或者没有返回数据`);
return false;
} }
this.setState({ this.setState({
viewData, viewData,
...@@ -68,8 +69,8 @@ export default class ViewPrint extends Component { ...@@ -68,8 +69,8 @@ export default class ViewPrint extends Component {
configAll: { config }, configAll: { config },
} = this.state; } = this.state;
const res = []; const res = [];
for (let item of config) { console.log(config);
if (item.fieldCode && data[item.fieldCode]) { for (const item of config) {
res.push( res.push(
DetailDom({ DetailDom({
config: item, config: item,
...@@ -77,7 +78,6 @@ export default class ViewPrint extends Component { ...@@ -77,7 +78,6 @@ export default class ViewPrint extends Component {
}), }),
); );
} }
}
return res; return res;
}; };
...@@ -118,7 +118,11 @@ export default class ViewPrint extends Component { ...@@ -118,7 +118,11 @@ export default class ViewPrint extends Component {
showWindowPrint, showWindowPrint,
minHeight, minHeight,
} = this.state; } = this.state;
if (!configAll) { if (!viewData || !minHeight) {
return null;
}
if(!configAll.backgroundUrl){
console.error('没有设置模版图片无法使用');
return null; return null;
} }
const { hasPrintBackground, backgroundUrl } = configAll; const { hasPrintBackground, backgroundUrl } = configAll;
...@@ -188,7 +192,7 @@ export default class ViewPrint extends Component { ...@@ -188,7 +192,7 @@ export default class ViewPrint extends Component {
className={styles.outSideDivPrint} className={styles.outSideDivPrint}
key={`${index}divKey`} key={`${index}divKey`}
style={{ style={{
minHeight, minHeight: minHeight || 700,
}}> }}>
{hasPrintBackground ? ( {hasPrintBackground ? (
<img <img
......
...@@ -60,6 +60,7 @@ export default class Index extends Component { ...@@ -60,6 +60,7 @@ export default class Index extends Component {
if (!configAll || !configAll.backgroundUrl) { if (!configAll || !configAll.backgroundUrl) {
return null; return null;
} }
console.log(configAll);
return ( return (
<Shell styleShell={{ marginTop: '0', marginBottom: '20px' }}> <Shell styleShell={{ marginTop: '0', marginBottom: '20px' }}>
<div style={{ height: '54px', padding: '12px 0 24px 12px' }}> <div style={{ height: '54px', padding: '12px 0 24px 12px' }}>
......
...@@ -20,8 +20,12 @@ export const queryConfig = function() { ...@@ -20,8 +20,12 @@ export const queryConfig = function() {
return false; return false;
} }
x.config = x.config.filter((y) => { // 过滤不显示的字段. x.config = x.config.filter((y) => { // 过滤不显示的字段.
return y.hasHidden === false; return y.hasHidden !== true;
}); });
if(!x.config.length){
console.error('没有有效的配置项');
return false;
}
this.setState({ this.setState({
configAll: x, configAll: x,
}); });
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论