index2.js 9.8 KB
Newer Older
1 2 3
import React, { Component, Fragment } from 'react';
import { queryConfig } from '../services';
import { getInfo } from '@/highOrderComponent/Service';
钟是志's avatar
钟是志 committed
4
import { message } from 'antd';
5 6 7 8
import Shell from '@/baseComponent/Shell';
import ButtonDiy from '@/baseComponent/ButtonDiy';
import router from 'umi/router';
import DetailDom from './DetailDom';
钟是志's avatar
钟是志 committed
9
import { A4Width, A4Height, getCLodopFuncJS, templateCode, defaultConfigInfo } from './config';
10 11
import RenderAuthorized from '@/components/Authorized';
import { getAuthority } from '@/utils/authority';
12
import { ModalInfo } from '@/baseComponent/Modal';
钟是志's avatar
钟是志 committed
13
import { isJSON } from '@/webPublic/one_stop_public/copy';
14 15

const Authorized = RenderAuthorized(getAuthority());
16 17

export default class ViewPrint extends Component {
18 19 20 21 22 23 24 25 26 27 28 29
	constructor(props) {
		super(props);
		this.state = {
			configAll: null,
			viewData: null,
			loading: true,
			printIndex: 0,
			showWindowPrint: false,
			preaparePrint: false,
		};
		this.getConfigInfo = queryConfig.bind(this);
	}
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
	getViewData = () => {
		const { selectedRows, dataQueryCallBack } = this.props;
		const ids = selectedRows.map((x) => x.id);
		this.getConfigInfo().then((x) => {
			if (x && x.queryUrl) {
				getInfo(
					{
						ids: ids.join(','),
					},
					x.queryUrl,
				).then((viewData) => {
					if (!viewData || !viewData.length) {
						message.warning('未查询到可打印的数据');
						console.error(`${x.queryUrl}接口报错或者没有返回数据`);
						return false;
					}
					if (dataQueryCallBack && typeof dataQueryCallBack === 'function') {
						viewData = dataQueryCallBack(viewData);
					}
					for (const item of viewData) {
						for (let z in item) {
							if (item[z] === null || item[z] === 'null') {
								item[z] = '';
							}
						}
					}
					this.setState({
						viewData,
					});
				});
			}
		});
	};
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78
	componentDidMount() {
		this.getViewData();
		getCLodopFuncJS().then((response) => {
			window.On_CLodop_Opened = function() {
				console.log('打印插件准备就绪,随时可以打印');
				window.On_CLodop_Opened = null;
			};
			if (response) {
				this.setState({
					loading: false,
				});
			}
		});
	}
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	detailDom = (data) => {
		const {
			configAll: { config },
		} = this.state;
		const res = [];
		for (const item of config) {
			res.push(
				DetailDom({
					config: item,
					data: data[item.fieldCode],
				}),
			);
		}
		return res;
	};
95

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	printOne = (optionType = 0) => {
		const { printIndex, configAll, viewData } = this.state;
		let { info } = configAll;
		if (isJSON(info)) {
			info = JSON.parse(info);
		} else {
			info = defaultConfigInfo;
		}
		const { printMeth } = configAll;
		const { LODOP } = window;
		let dom = document.getElementById(`printDomId-${printIndex}`);
		if (optionType >= 1) {
			dom = document.getElementById('printDomId-0');
		}
		if (!dom) {
			console.error('dom节点没找到');
			return false;
		}
		const strHTML = dom.innerHTML;
		if (!LODOP.PageDataEx || !LODOP.PageDataEx.companyname || !LODOP.PageDataEx.license) {
			console.log(LODOP.PageDataEx, 'license未注入成功,正在重新注册');
			LODOP.SET_SHOW_MODE('LANGUAGE', 0);
			LODOP.SET_LICENSES('成都市知用科技有限公司', '649677881727389907689190562356', '', '');
		}
		// LODOP.PRINT_INITA(0, 0, `${wide}cm`, `${high}cm`, `printDomId${printIndex}`);
		LODOP.PRINT_INIT(`${Math.random() * 10000 + 'test'}`);
		LODOP.SET_PRINT_MODE('RESELECT_PAGESIZE', true);
		LODOP.SET_PRINT_PAGESIZE(printMeth, info.paperWidth, info.paperHeight, 'CreateCustomPage'); // 打印方向 纸张大小.
		LODOP.ADD_PRINT_HTML(0, 0, '100%', '100%', strHTML); // HTML 打印
		switch (optionType) {
			case 1:
				LODOP.PREVIEW(); // 打印预览
				break;
			case 2:
				LODOP.PRINT_DESIGN(); // 打印设计
				break;
			case 0:
				LODOP.PRINT(); // 打印
				break;
			default:
				break;
		}
	};
钟是志's avatar
钟是志 committed
139

140 141 142 143
	printPreview = () => {
		// 打印预览
		this.printOne(1);
	};
144

145 146 147
	printSetUp = () => {
		this.printOne(2);
	};
钟是志's avatar
钟是志 committed
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	printedDataSave = (index) => {
		const { selectedRows } = this.props;
		const { configAll } = this.state;
		if (!selectedRows[index] || !selectedRows[index].id) {
			console.error('printedDataSave 没有正确的id 无法保存打印信息到后台');
			return false;
		}
		const ids = selectedRows[index].id;
		getInfo({ ids }, configAll.callUrl).then((x) => {
			if (x) {
				console.log(`${ids}的打印信息已保存到后台`);
				return true;
			} else {
				return false;
			}
		});
	};
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 191 192 193 194 195 196
	printOneByOne = () => {
		// 按队列打印
		const { viewData, printIndex } = this.state;
		if (viewData && viewData.length) {
			this.printOne();
			const { length } = viewData;
			let LODOPObj = window.LODOP;
			LODOPObj.On_Return_Remain = true;
			LODOPObj.On_Return = (TaskId, Value) => {
				console.log(TaskId);
				if (Number(Value) === 1) {
					message.info(`正在打印第${this.state.printIndex + 1}张, 共${viewData.length}张`);
					this.printedDataSave(printIndex);
				}
				this.setState({ printIndex: this.state.printIndex + 1 }, () => {
					if (this.state.printIndex < length) {
						this.printOne();
					} else {
						LODOPObj.On_Return_Remain = false;
						this.setState({
							loading: false,
							printIndex: 0,
						});
					}
				});
			};
		} else {
			console.error('暂无任何数据无法打印');
		}
	};
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	printByLodop = () => {
		if (!window.LODOP || !window.LODOP.PRINT_INIT) {
			console.error('打印服务未启动');
			ModalInfo('打印服务未启动');
			return false;
		}
		this.setState(
			{
				showWindowPrint: true,
				printIndex: 0,
				loading: true,
			},
			() => {
				this.printOneByOne();
			},
		);
	};
215

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	render() {
		const { configAll, viewData, showWindowPrint, loading, preaparePrint } = this.state;
		if (!viewData) {
			return (
				<Shell styleShell={{ marginTop: '0', marginBottom: '20px' }}>
					<div style={{ height: '54px', padding: '12px 0 12px 12px' }}>
						<ButtonDiy
							name={'返回'}
							handleClick={() => {
								router.goBack();
							}}
						/>
					</div>
				</Shell>
			);
		}
		if (!configAll.backgroundUrl) {
			console.error('没有设置模版图片无法使用');
			return null;
		}
		const { wide, high } = configAll;
		const imageStyle = {
			height: high ? `${high}cm` : A4Height,
			width: wide ? `${wide}cm` : A4Width,
			textAlign: 'center',
		};
		const { pathname } = this.props.location;
		const templateInfo = templateCode.find((x) => {
			return x.path === pathname;
		});
		if (!templateInfo) {
			console.error('未找到templateInfo', pathname);
		}
		return (
			<Fragment>
				<Shell styleShell={{ marginTop: '0', marginBottom: '20px' }}>
					<div style={{ height: '54px', padding: '12px 0 12px 12px' }}>
						<ButtonDiy
							name={'打印设备选择'}
							handleClick={() => {
								window.LODOP.SELECT_PRINTER();
								this.setState({
									preaparePrint: true,
								});
							}}
							loading={loading}
						/>
						<ButtonDiy
							name={'打印预览'}
							handleClick={() => {
								if (!preaparePrint) {
									message.warning('请先选择打印设备');
									window.LODOP.SELECT_PRINTER();
									this.setState({
										preaparePrint: true,
									});
									return false;
								}
								this.printPreview();
							}}
							loading={loading}
						/>
						<ButtonDiy
							name={'打印'}
							handleClick={() => {
								if (!preaparePrint) {
									window.LODOP.SELECT_PRINTER();
									this.setState({
										preaparePrint: true,
									});
									message.warning('请先选择打印设备');
									return false;
								}
								this.printByLodop();
							}}
							loading={loading}
						/>
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
						<span style={{ float: 'right' }}>
							<ButtonDiy
								name={'返回'}
								handleClick={() => {
									const { state } = this.props.location;
									if (state && state.returnUrl) {
										router.push({
											pathname: state.returnUrl,
											searchInfo: state.searchInfo,
										});
									} else {
										router.goBack();
									}
								}}
							/>
						</span>
310

311
						<Authorized authority={'/jc/setting/PrintPositionSetting'}>
312 313
							<span style={{ float: 'right' }}>
								<ButtonDiy
314 315 316 317 318 319
									name={'打印调试模式(实施)'}
									handleClick={() => {
										this.printSetUp();
									}}
									loading={loading}
								/>
320
							</span>
321 322
						</Authorized>
						<Authorized authority={'/jc/setting/PrintPositionSetting'}>
323 324
							<span style={{ float: 'right' }}>
								<ButtonDiy
325 326 327 328 329 330 331 332
									name={'打印模版设置(实施)'}
									handleClick={() => {
										router.push({
											pathname: '/jc/setting/PrintConfig',
											state: { code: templateInfo.code },
										});
									}}
								/>
333
							</span>
334
						</Authorized>
335

336
						<Authorized authority={'/jc/setting/PrintPositionSetting'}>
337 338
							<span style={{ float: 'right' }}>
								<ButtonDiy
339 340 341 342 343 344 345 346 347
									name={'字段位置设置(实施)'}
									handleClick={() => {
										router.push({
											//pathname: '/xg/xg_hard/AwardSet/printSetting', // 中医大这里也要改 TODO
											pathname: '/jc/setting/printSetting',
											state: { code: templateInfo.code },
										});
									}}
								/>
348
							</span>
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
						</Authorized>
					</div>
				</Shell>
				{viewData && viewData.length ? (
					<div
						ref={(node) => (this.content = node)}
						style={{
							display: showWindowPrint || true ? 'block' : 'none',
						}}>
						{viewData.map((info, index) => {
							return (
								<div
									key={`${index}divKey`}
									id={`printDomId-${index}`}
									style={{
										marginBottom: '10px',
									}}>
									<div
										style={{
											position: 'relative',
											pageBreakAfter: 'avoid',
											...imageStyle,
											backgroundColor: '#fff',
										}}>
										{this.detailDom(info)}
									</div>
								</div>
							);
						})}
					</div>
				) : null}
			</Fragment>
		);
	}
383
}