| | |
| | | import { DndProvider } from 'react-dnd' |
| | | import { is, fromJS } from 'immutable' |
| | | import HTML5Backend from 'react-dnd-html5-backend' |
| | | import { Card, notification, Row, Button, Modal } from 'antd' |
| | | import { Card, notification, Row, Button, Modal, Input, Switch } from 'antd' |
| | | import { ArrowUpOutlined, ArrowDownOutlined, CaretRightOutlined, ArrowRightOutlined, ArrowLeftOutlined } from '@ant-design/icons' |
| | | import DragElement from './dragelement' |
| | | import MutilForm from './mutilform' |
| | | import SourceElement from './dragelement/source' |
| | |
| | | editItemType: '', |
| | | fields: [], |
| | | formlist: null, |
| | | saveloading: false |
| | | saveloading: false, |
| | | upPlus: 1, |
| | | upMinus: 1, |
| | | leftPlus: 1, |
| | | leftMinus: 1, |
| | | debug: true |
| | | } |
| | | |
| | | getclickpoint = (e) => { |
| | |
| | | let screenY = e.clientY + scrollTop |
| | | let offsetT = screenY - 75 |
| | | let _width = parseInt(document.getElementById('darea').style.width) |
| | | let offsetL = screenX - (document.body.offsetWidth - _width - 30) / 2 |
| | | let offsetL = screenX - (document.body.offsetWidth - _width - 40) / 2 |
| | | |
| | | let cx = Math.floor(offsetL / _width * config.width) |
| | | let cy = Math.floor(offsetT / parseInt(document.getElementById('darea').style.height) * config.height) |
| | |
| | | height = _c |
| | | } |
| | | if (width === 0) { |
| | | x -= 4 |
| | | width = 8 |
| | | x -= 2 |
| | | width = 4 |
| | | } |
| | | if (height === 0) { |
| | | y -= 4 |
| | | height = 8 |
| | | y -= 2 |
| | | height = 4 |
| | | } |
| | | if (cx >= x && cx <= x + width && cy >= y && cy <= y + height) { |
| | | _selectItem = element |
| | |
| | | |
| | | _selectItem.left = _left |
| | | _selectItem.top = _top |
| | | |
| | | let result = this.resetItem(_selectItem) |
| | | |
| | | let result = this.resetItem(fromJS(_selectItem).toJS()) |
| | | |
| | | if (!is(fromJS(result), fromJS(_preItem))) { |
| | | _preItem = JSON.parse(JSON.stringify(result)) |
| | |
| | | window.addEventListener('resize', this.handleResize) |
| | | |
| | | this.loadconfig() |
| | | |
| | | document.onkeydown = (event) => { |
| | | const { editItemId, editItemType } = this.state |
| | | let e = event || window.event |
| | | let keyCode = e.keyCode || e.which || e.charCode |
| | | |
| | | if (keyCode === 46 && editItemId && editItemType !== 'Template') { |
| | | this.deleteItem() |
| | | } |
| | | if (e.ctrlKey && keyCode === 81 && editItemId && editItemType !== 'Template') { |
| | | this.copyItem() |
| | | } |
| | | } |
| | | } |
| | | |
| | | handleResize = () => { |
| | |
| | | document.getElementById('darea').style.width = '700px' |
| | | } else if (document.body.offsetWidth < 1920) { |
| | | document.getElementById('darea').style.width = '800px' |
| | | } else { |
| | | document.getElementById('darea').style.width = '1000px' |
| | | } |
| | | } |
| | | |
| | | resetbox = () => { |
| | | const { config } = this.state |
| | | const { config, debug } = this.state |
| | | |
| | | let ratio = (config.height || 1) / (config.width || 1) |
| | | |
| | | document.getElementById('darea').style.height = parseInt(document.getElementById('darea').style.width) * ratio + 'px' |
| | | |
| | | printCtrl.sketch(config, null) |
| | | printCtrl.sketch(config, null, debug) |
| | | } |
| | | |
| | | resetview () { |
| | | const { config, editItemId } = this.state |
| | | const { config, editItemId, debug } = this.state |
| | | |
| | | printCtrl.sketch(config, editItemId) |
| | | printCtrl.sketch(config, editItemId, debug) |
| | | } |
| | | |
| | | /** |
| | |
| | | notification.warning({ |
| | | top: 92, |
| | | message: '配置信息解析错误!', |
| | | duration: 10 |
| | | duration: 5 |
| | | }) |
| | | _config = '' |
| | | } |
| | |
| | | _config.type = 'Template' |
| | | _config.uuid = Utils.getuuid() |
| | | |
| | | if (result.data && result.data[0] && result.data[0].TableName) { |
| | | this.loadFields(result.data[0].TableName) |
| | | let tables = [] |
| | | |
| | | if (result.data && result.data.length > 0) { |
| | | result.data.forEach(item => { |
| | | if (item.TableName) { |
| | | tables.push(item.TableName) |
| | | } |
| | | }) |
| | | |
| | | tables = Array.from(new Set(tables)) |
| | | } |
| | | |
| | | this.loadFields(tables) |
| | | |
| | | this.handleResize() |
| | | this.setState({ |
| | | config: _config, |
| | | editItemId: _config.uuid, |
| | |
| | | } else { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: result.ErrMesg, |
| | | duration: 10 |
| | | message: result.message, |
| | | duration: 5 |
| | | }) |
| | | } |
| | | } |
| | |
| | | /** |
| | | * @description 获取可用字段 |
| | | */ |
| | | async loadFields (TBName) { |
| | | let param = { |
| | | func: 'sPC_Get_FieldName', |
| | | TBName: TBName |
| | | loadFields (tables) { |
| | | if (tables.length === 0) { |
| | | let _fields = [{ |
| | | value: '', |
| | | text: '空', |
| | | type: '' |
| | | }, { |
| | | value: 'other_field', |
| | | text: '其他', |
| | | type: '' |
| | | }] |
| | | |
| | | this.setState({ |
| | | fields: _fields |
| | | }) |
| | | return |
| | | } |
| | | |
| | | let result = await Api.getCloudConfig(param) |
| | | |
| | | if (result.status) { |
| | | let deffers = tables.map(name => { |
| | | return new Promise(resolve => { |
| | | Api.getCloudConfig({func: 'sPC_Get_FieldName', TBName: name}).then(res => { |
| | | resolve(res) |
| | | }) |
| | | }) |
| | | }) |
| | | Promise.all(deffers).then(response => { |
| | | let _fields = [{ |
| | | value: '', |
| | | text: '空', |
| | | type: '' |
| | | }] |
| | | let error = null |
| | | let _f = new Map() |
| | | |
| | | result.FDName.forEach(item => { |
| | | if (item.FieldName && !_f.has(item.FieldName)) { |
| | | _f.set(item.FieldName, true) |
| | | |
| | | _fields.push({ |
| | | value: item.FieldName, |
| | | text: item.FieldDec, |
| | | type: item.FieldType |
| | | |
| | | response.forEach(res => { |
| | | if (res.status) { |
| | | res.FDName.forEach(item => { |
| | | if (item.FieldName && !_f.has(item.FieldName)) { |
| | | _f.set(item.FieldName, true) |
| | | |
| | | _fields.push({ |
| | | value: item.FieldName, |
| | | text: item.FieldDec + '(' + item.FieldName + ')', |
| | | type: item.FieldType |
| | | }) |
| | | } |
| | | }) |
| | | } else { |
| | | error = res |
| | | } |
| | | }) |
| | | |
| | | if (error) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: error.message, |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } |
| | | |
| | | _fields.push({ |
| | | value: 'other_field', |
| | | text: '其他', |
| | | type: '' |
| | | }) |
| | | |
| | | this.setState({ |
| | | fields: _fields |
| | | }) |
| | | } else { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: result.ErrMesg, |
| | | duration: 10 |
| | | }) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | dropcard = (item) => { |
| | |
| | | |
| | | } |
| | | |
| | | resetItem = (item) => { |
| | | let _item = JSON.parse(JSON.stringify(item)) |
| | | resetItem = (_item) => { |
| | | const { config } = this.state |
| | | let oriItem = fromJS(config.elements.filter(ele => ele.uuid === _item.uuid)[0]).toJS() // 源编辑元素 |
| | | _item.width = +_item.width |
| | | _item.height = +_item.height |
| | | |
| | | let _boxwidth = +config.width |
| | | let _boxheight = +config.height |
| | | let _left = +_item.left |
| | | let _top = +_item.top |
| | | let _width = +_item.width |
| | | let _height = +_item.height |
| | | let _width = _item.width |
| | | let _height = _item.height |
| | | |
| | | if (_left < 0) { |
| | | _item.left = 0 |
| | |
| | | } |
| | | |
| | | if (_item.type === 'barcode') { |
| | | if (+_item.barcodeWidth > +_item.width) { |
| | | _item.barcodeWidth = +_item.width |
| | | } |
| | | if (+_item.barcodeHeight > +_item.height) { |
| | | _item.barcodeHeight = +_item.height |
| | | _item.barcodeWidth = +_item.barcodeWidth |
| | | _item.barcodeHeight = +_item.barcodeHeight |
| | | |
| | | if (oriItem.width !== _item.width || oriItem.height !== _item.height) { |
| | | _item.barcodeWidth = _item.width * 0.8 |
| | | _item.barcodeHeight = _item.height * 0.6 |
| | | } else if (_item.barcodeLabel === 'true') { |
| | | if (_item.barcodeWidth > _item.width) { |
| | | _item.barcodeWidth = _item.width |
| | | } |
| | | let fsize = Math.floor(_item.fontSize / 2) |
| | | if (_item.barcodeHeight + fsize > _item.height) { |
| | | _item.barcodeHeight = _item.height - fsize |
| | | } |
| | | } else { |
| | | if (_item.barcodeWidth > _item.width) { |
| | | _item.barcodeWidth = _item.width |
| | | } |
| | | if (_item.barcodeHeight > _item.height) { |
| | | _item.barcodeHeight = _item.height |
| | | } |
| | | } |
| | | } else if (_item.type === 'qrcode') { |
| | | if (+_item.qrcodeWidth > +_item.width) { |
| | | _item.qrcodeWidth = +_item.width |
| | | } |
| | | if (+_item.qrcodeWidth > +_item.height) { |
| | | _item.qrcodeWidth = +_item.height |
| | | _item.qrcodeWidth = +_item.qrcodeWidth |
| | | |
| | | if (oriItem.width !== _item.width || oriItem.height !== _item.height) { |
| | | if (_item.height > _item.width) { |
| | | _item.qrcodeWidth = _item.width * 0.9 |
| | | } else if (_item.width > _item.height) { |
| | | _item.qrcodeWidth = _item.height * 0.9 |
| | | } |
| | | } else { |
| | | if (_item.qrcodeWidth > _item.width) { |
| | | _item.qrcodeWidth = _item.width |
| | | } |
| | | if (_item.qrcodeWidth > _item.height) { |
| | | _item.qrcodeWidth = _item.height |
| | | } |
| | | } |
| | | } else if (_item.type === 'image') { |
| | | if (+_item.imgWidth > +_item.width) { |
| | | _item.imgWidth = +_item.width |
| | | } |
| | | if (+_item.imgHeight > +_item.height) { |
| | | _item.imgHeight = +_item.height |
| | | _item.imgWidth = +_item.imgWidth |
| | | |
| | | if (oriItem.width !== _item.width || oriItem.height !== _item.height) { |
| | | _item.imgWidth = _item.width * 0.9 |
| | | _item.imgHeight = _item.height * 0.9 |
| | | } else { |
| | | if (_item.imgWidth > _item.width) { |
| | | _item.imgWidth = _item.width |
| | | } |
| | | if (_item.imgHeight > _item.height) { |
| | | _item.imgHeight = _item.height |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | handleSubmit = () => { |
| | | const { config } = this.state |
| | | |
| | | this.FormRef.handleConfirm().then(res => { |
| | | this.FormRef.handleConfirm().then(response => { |
| | | let res = fromJS(response).toJS() |
| | | if (res.type === 'Template') { |
| | | res.width = parseInt(res.width) |
| | | res.height = parseInt(res.height) |
| | |
| | | }) |
| | | } |
| | | |
| | | copyItem = () => { |
| | | const { editItemId, config, fields } = this.state |
| | | |
| | | let cell = config.elements.filter(item => item.uuid === editItemId)[0] |
| | | |
| | | if (!cell) return |
| | | |
| | | let item = {...cell} |
| | | |
| | | item.left = item.left + parseInt(item.width / 4) |
| | | item.top = item.top + parseInt(item.height / 4) |
| | | item.uuid = Utils.getuuid() |
| | | |
| | | if (item.left + item.width > config.width) { |
| | | item.left = config.width - item.width |
| | | } |
| | | if (item.top + item.height > config.height) { |
| | | item.top = config.height - item.height |
| | | } |
| | | |
| | | let _config = fromJS(config).toJS() |
| | | |
| | | _config.elements.push(item) |
| | | |
| | | let _formlist = [] |
| | | if (item.type === 'text') { |
| | | _formlist = getTextForm(item, fields) |
| | | } else if (item.type === 'barcode') { |
| | | _formlist = getBarcodeForm(item, fields) |
| | | } else if (item.type === 'qrcode') { |
| | | _formlist = getQrcodeForm(item, fields) |
| | | } else if (item.type === 'image') { |
| | | _formlist = getImageForm(item, fields) |
| | | } |
| | | |
| | | this.setState({ |
| | | config: _config, |
| | | editItemId: item.uuid, |
| | | editItemType: item.type, |
| | | formlist: _formlist |
| | | }, () => { |
| | | this.resetview() |
| | | }) |
| | | } |
| | | |
| | | deleteItem = () => { |
| | | const _this = this |
| | | const { editItemId, config } = this.state |
| | |
| | | notification.warning({ |
| | | top: 92, |
| | | message: '纸张纵横比不可超过10!', |
| | | duration: 10 |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } |
| | |
| | | |
| | | try { |
| | | _config = window.btoa(window.encodeURIComponent(JSON.stringify(config))) |
| | | } catch { |
| | | } catch (e) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: '编译错误!', |
| | | duration: 10 |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } |
| | |
| | | } |
| | | |
| | | new Promise(resolve => { |
| | | printCtrl.sketch(config, null).then(res => { |
| | | Api.fileuploadbase64(res, 'cloud').then(result => { // 图片上传,并获取图片路径 |
| | | printCtrl.sketch(config, null, false).then(res => { |
| | | let param = { |
| | | Base64Img: res |
| | | } |
| | | |
| | | if (window.GLOB.cloudServiceApi) { |
| | | param.rduri = window.GLOB.cloudServiceApi |
| | | param.userid = sessionStorage.getItem('CloudUserID') || '' |
| | | param.LoginUID = sessionStorage.getItem('CloudLoginUID') || '' |
| | | } |
| | | |
| | | Api.fileuploadbase64(param).then(result => { // 图片上传,并获取图片路径 |
| | | if (result.status) { |
| | | resolve(Utils.getcloudurl(result.Images)) |
| | | let url = result.Images |
| | | let baseurl = '' |
| | | |
| | | if (window.GLOB.cloudServiceApi) { |
| | | baseurl = window.GLOB.cloudServiceApi.replace(/webapi(.*)$/, '') |
| | | } else { |
| | | baseurl = window.GLOB.baseurl |
| | | } |
| | | |
| | | url = url.match(/^http/) || url.match(/^\/\//) ? url : baseurl + url |
| | | |
| | | resolve(url) |
| | | } else { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: result.ErrMesg, |
| | | duration: 10 |
| | | message: result.message, |
| | | duration: 5 |
| | | }) |
| | | this.setState({ |
| | | saveloading: false |
| | |
| | | } else { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: res.ErrMesg, |
| | | duration: 10 |
| | | message: res.message, |
| | | duration: 5 |
| | | }) |
| | | } |
| | | this.setState({ |
| | |
| | | }) |
| | | } |
| | | |
| | | change = (e, type) => { |
| | | let val = e.target.value |
| | | val = parseInt(val) |
| | | |
| | | if (isNaN(val)) { |
| | | val = '' |
| | | } |
| | | this.setState({[type]: val}) |
| | | } |
| | | |
| | | updatePosition = (type) => { |
| | | let val = 0 |
| | | if (type === 'upPlus') { |
| | | val = this.state.upPlus || 0 |
| | | } else if (type === 'upMinus') { |
| | | val = this.state.upMinus || 0 |
| | | } else if (type === 'leftPlus') { |
| | | val = this.state.leftPlus || 0 |
| | | } else if (type === 'leftMinus') { |
| | | val = this.state.leftMinus || 0 |
| | | } |
| | | |
| | | if (!val) return |
| | | |
| | | let config = fromJS(this.state.config).toJS() |
| | | |
| | | if (type === 'upPlus') { |
| | | config.elements = config.elements.map(item => { |
| | | item.top = (item.top || 0) + val |
| | | return item |
| | | }) |
| | | } else if (type === 'upMinus') { |
| | | config.elements = config.elements.map(item => { |
| | | item.top = (item.top || 0) - val |
| | | return item |
| | | }) |
| | | } else if (type === 'leftPlus') { |
| | | config.elements = config.elements.map(item => { |
| | | item.left = (item.left || 0) + val |
| | | return item |
| | | }) |
| | | } else if (type === 'leftMinus') { |
| | | config.elements = config.elements.map(item => { |
| | | item.left = (item.left || 0) - val |
| | | return item |
| | | }) |
| | | } |
| | | |
| | | this.setState({ |
| | | config: config, |
| | | editItemId: config.uuid, |
| | | editItemType: config.type, |
| | | formlist: getpageform(config) |
| | | }, () => { |
| | | this.resetview() |
| | | }) |
| | | } |
| | | |
| | | changeDebug = (val) => { |
| | | this.setState({ |
| | | debug: val |
| | | }, () => { |
| | | this.resetview() |
| | | }) |
| | | } |
| | | |
| | | render () { |
| | | const { debug } = this.state |
| | | |
| | | return ( |
| | | <div className="print-template"> |
| | | <DndProvider backend={HTML5Backend}> |
| | | <header className="print-header-container ant-menu-dark">模板制作</header> |
| | | <header className="print-header-container ant-menu-dark"> |
| | | 模板制作 |
| | | <div>快捷键:Delete(删除元素)、ctrl + q(复制元素)</div> |
| | | </header> |
| | | <aside className="tools"> |
| | | <Card title="工具栏"> |
| | | <Card className="tool-bar" title="工具栏"> |
| | | {printItems.map((item, index) => { |
| | | return (<SourceElement key={index} content={item}/>) |
| | | })} |
| | | </Card> |
| | | <Card className="move-bar" title="整体移动"> |
| | | <Input addonBefore={<ArrowUpOutlined/>} addonAfter={<CaretRightOutlined onClick={() => this.updatePosition('upMinus')}/>} onChange={(e) => this.change(e, 'upMinus')} value={this.state.upMinus} /> |
| | | <Input addonBefore={<ArrowDownOutlined/>} addonAfter={<CaretRightOutlined onClick={() => this.updatePosition('upPlus')}/>} onChange={(e) => this.change(e, 'upPlus')} value={this.state.upPlus} /> |
| | | <Input addonBefore={<ArrowLeftOutlined/>} addonAfter={<CaretRightOutlined onClick={() => this.updatePosition('leftMinus')}/>} onChange={(e) => this.change(e, 'leftMinus')} value={this.state.leftMinus} /> |
| | | <Input addonBefore={<ArrowRightOutlined/>} addonAfter={<CaretRightOutlined onClick={() => this.updatePosition('leftPlus')}/>} onChange={(e) => this.change(e, 'leftPlus')} value={this.state.leftPlus} /> |
| | | <div style={{marginTop: '10px'}}> |
| | | <span>调试模式:</span><Switch checked={debug} onChange={this.changeDebug} /> |
| | | </div> |
| | | </Card> |
| | | </aside> |
| | | <div className="switchbox" onClick={this.switchbox}></div> |
| | | <DragElement dropcard={this.dropcard} /> |