From bd1dfc9e6c9b9f8076ca2783ce598e0936b4c664 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期三, 22 十二月 2021 14:36:03 +0800
Subject: [PATCH] 2021-12-22

---
 src/tabviews/zshare/actionList/printbutton/index.jsx |  373 +++++++++++++++++++++++++++++++----------------------
 1 files changed, 219 insertions(+), 154 deletions(-)

diff --git a/src/tabviews/zshare/actionList/printbutton/index.jsx b/src/tabviews/zshare/actionList/printbutton/index.jsx
index 95a955f..2c2e490 100644
--- a/src/tabviews/zshare/actionList/printbutton/index.jsx
+++ b/src/tabviews/zshare/actionList/printbutton/index.jsx
@@ -3,7 +3,7 @@
 import moment from 'moment'
 import {connect} from 'react-redux'
 import { is, fromJS } from 'immutable'
-import { Button, Modal, notification, message, Icon } from 'antd'
+import { Button, Modal, notification, message } from 'antd'
 
 import Api from '@/api'
 import Utils from '@/utils/utils.js'
@@ -13,7 +13,8 @@
 import asyncSpinComponent from '@/utils/asyncSpinComponent'
 import { updateForm } from '@/utils/utils-update.js'
 import MKEmitter from '@/utils/events.js'
-import './index.scss'
+import MkIcon from '@/components/mk-icon'
+// import './index.scss'
 
 const MutilForm = asyncSpinComponent(() => import('@/tabviews/zshare/mutilform'))
 const { confirm } = Modal
@@ -37,11 +38,13 @@
     dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
     visible: false,
     formdata: null,
-    tabledata: null,
+    selines: null,
     btnconfig: null,
     loading: false,
     disabled: false,
-    loadingNumber: ''
+    hidden: false,
+    loadingNumber: '',
+    autoMatic: false
   }
 
   UNSAFE_componentWillMount () {
@@ -55,7 +58,7 @@
           disabled = true
         }
       })
-      this.setState({disabled})
+      this.setState({disabled, hidden: disabled && btn.control === 'hidden'})
     }
   }
 
@@ -64,10 +67,12 @@
   }
 
   componentDidMount () {
-    const { position } = this.props
+    const { btn } = this.props
 
-    if (position === 'toolbar') {
-      MKEmitter.addListener('triggerBtnId', this.actionTrigger)
+    MKEmitter.addListener('triggerBtnId', this.actionTrigger)
+
+    if (btn.autoMatic) {
+      MKEmitter.addListener('triggerBtnPopSubmit', this.triggerBtnPopSubmit)
     }
   }
 
@@ -85,7 +90,7 @@
           }
         })
       }
-      this.setState({disabled})
+      this.setState({disabled, hidden: disabled && btn.control === 'hidden'})
     }
   }
 
@@ -94,31 +99,25 @@
       return
     }
     MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
+    MKEmitter.removeListener('triggerBtnPopSubmit', this.triggerBtnPopSubmit)
   }
 
-  /**
-   * @description 鎸夐挳鐘舵�佹敼鍙�
-   */
-  updateStatus = (type) => {
-    if (type === 'start') {
-      this.setState({
-        loading: true
-      })
-    } else if (type === 'over') {
-      this.setState({
-        loading: false
-      })
-    }
+  triggerBtnPopSubmit = (id) => {
+    const { btn } = this.props
+
+    if (btn.uuid !== id) return
+
+    this.handleOk()
   }
   
   /**
    * @description 瑙﹀彂鎸夐挳鎿嶄綔
    */
-  actionTrigger = (triggerId, record) => {
+  actionTrigger = (triggerId, record, type) => {
     const { Tab, BID, btn, selectedData, setting } = this.props
-    const { loading } = this.state
+    const { loading, disabled } = this.state
 
-    if ((triggerId && btn.uuid !== triggerId) || loading) return
+    if ((triggerId && btn.uuid !== triggerId) || loading || disabled) return
 
     if (((Tab && Tab.supMenu) || setting.supModule) && !BID) {
       notification.warning({
@@ -127,7 +126,13 @@
         duration: 5
       })
       return
+    } else if (type === 'linkbtn' && selectedData && selectedData.length === 1) {
+      if (record[0].$Index !== selectedData[0].$Index) {
+        return
+      }
     }
+
+    this.setState({autoMatic: type === 'autoMatic'})
 
     let _this = this
     let data = record || selectedData || []
@@ -165,28 +170,31 @@
       return
     }
 
+    this.setState({
+      selines: data
+    })
+
     if (btn.execMode === 'pop') {
-      this.updateStatus('start')
       let modal = this.state.btnconfig
       if (!modal && btn.modal) {
         modal = this.handleModelConfig(btn.modal)
       }
 
       this.setState({
-        tabledata: data,
+        loading: true,
         btnconfig: modal
       }, () => {
         this.improveAction()
       })
     } else if (btn.execMode === 'prompt') {
-      this.updateStatus('start')
+      this.setState({ loading: true })
       confirm({
         title: this.state.dict['main.action.confirm.tip'],
         onOk() {
           _this.triggerPrint(data)
         },
         onCancel() {
-          _this.updateStatus('over')
+          _this.setState({ loading: false })
         }
       })
     } else {
@@ -379,8 +387,8 @@
 
     try {
       // eslint-disable-next-line
-      let func = new Function('data', 'form', 'printer', 'notification', btn.verify.printFunc)
-      func(printlist, formdata, btn.verify, notification)
+      let func = new Function('data', 'form', 'printer', 'notification', 'Api', 'systemType', btn.verify.printFunc)
+      func(printlist, formdata, btn.verify, notification, Api, window.GLOB.systemType)
 
       // 鑷畾涔夋墦鍗扮ず渚�
       // let defaultPrinter = printer.defaultPrinter || 'lackprinter'
@@ -391,8 +399,7 @@
       //   for (let i = 0; i < 32; i++) {
       //     uuid.push(_options.substr(Math.floor(Math.random() * 0x20), 1))
       //   }
-      //   uuid = uuid.join('')
-      //   return uuid
+      //   return uuid.join('')
       // }
       // if (printer.printerTypeList && printer.printerTypeList.length > 0) {
       //   printer.printerTypeList.forEach(cell => {
@@ -402,29 +409,43 @@
       //   })
       // }
 
-      // let jdList = []
-      // let otherList = []
-      // data.forEach(item => {
-      //   if (item.CustomData) {
-      //     item.CustomData = JSON.parse(item.CustomData.replace(/\n/g,"\\n").replace(/\r/g,"\\r"))
-      //   }
-      //   if (item.PrintData) {
-      //     item.PrintData = JSON.parse(item.PrintData.replace(/\n/g,"\\n").replace(/\r/g,"\\r"))
-      //     item.PrintData.data = {...form, ...item.PrintData.data}
-      //   }
+      // let jdList = [];
+      // let jdNewList = [];
+      // let otherList = [];
+      // let _map = new Map()
+      // data.forEach(m => {
+      //   if (!m.print_data || m.print_data.length === 0) return
         
-      //   if (!item.PrintData) {
-      //     return
-      //   }
+      //   m.print_data.forEach(n => {
+      //     if (n.InsideBill) {
+      //       if (_map.has(n.InsideBill)) {
+      //         return
+      //       }
+      //       _map.set(n.InsideBill, true)
+      //     }
+      //     if (n.CustomData) {
+      //       n.CustomData = JSON.parse(n.CustomData.replace(/\n/g,"\\n").replace(/\r/g,"\\r"))
+      //     }
 
-      //   if (item.PrintData.ectype === 'jdpop') {
-      //     jdList.push(item)
-      //   } else {
-      //     otherList.push(item)
-      //   }
+      //     if (n.hasOwnProperty('StdTemplate')) {
+      //       jdNewList.push(n);
+      //       return
+      //     } else if (!n.PrintData) {
+      //       return
+      //     }
+
+      //     n.PrintData = JSON.parse(n.PrintData.replace(/\n/g,"\\n").replace(/\r/g,"\\r"))
+      //     n.PrintData.data = {...form, ...n.PrintData.data}
+
+      //     if (n.PrintData.ectype === 'jdpop') {
+      //       jdList.push(n)
+      //     } else {
+      //       otherList.push(n)
+      //     }
+      //   })
       // })
 
-      // if (jdList.length === 0 && otherList.length === 0) {
+      // if (jdList.length === 0 && otherList.length === 0 && jdNewList.length === 0) {
       //   notification.warning({
       //     top: 92,
       //     message: '鏃犳墦鍗版暟鎹紒',
@@ -433,79 +454,88 @@
       //   return
       // }
 
-      // let execPrint = (list, linkUrl) => {
-      //   let printdata = {}
+      // let execPrint = (list, linkUrl, type) => {
+      //   let printdata = {};
+      //   let printerList = [];
 
-      //   list.forEach(res => {
-      //     let _printer = defaultPrinter
+      //   if (type === 'jd') {
+      //     printerList = list.map(m => {
+      //       let _printer = defaultPrinter;
 
-      //     if (res.printType && printers[res.printType]) {
-      //       _printer = printers[res.printType]
-      //     }
-
-      //     printdata[_printer] = printdata[_printer] || []
-
-      //     printdata[_printer].push(res)
-      //   })
-
-      //   let printerList = []
-
-      //   Object.keys(printdata).forEach(printer => {
-      //     let _documents = []
-      //     printdata[printer].forEach(item => {
-      //       let _cell = {
-      //         documentID: getuuid(),
-      //         contents: []
+      //       if (m.printType && printers[m.printType]) {
+      //         _printer = printers[m.printType];
       //       }
 
-      //       if (item.PrintData) {
-      //         _cell.contents.push(item.PrintData)
+      //       return {
+      //         orderType: "print", 
+      //         parameters: {
+      //           printName: _printer === 'lackprinter' ? '' : _printer,
+      //           tempUrl: m.StdTemplate,
+      //           customTempUrl: m.CusTemplate,
+      //           customData: [m.CustomData],
+      //           printData: [m.PrintData]
+      //         } 
       //       }
-      //       if (item.CustomData) {
-      //         _cell.contents.push(item.CustomData)
+      //     })
+      //   } else {
+      //     list.forEach(res => {
+      //       let _printer = defaultPrinter
+
+      //       if (res.printType && printers[res.printType]) {
+      //         _printer = printers[res.printType]
       //       }
 
-      //       for (let i = 0; i < item.printCount; i++) {
+      //       printdata[_printer] = printdata[_printer] || []
+
+      //       printdata[_printer].push(res)
+      //     })
+
+      //     Object.keys(printdata).forEach(printer => {
+      //       let _documents = []
+      //       printdata[printer].forEach(item => {
+      //         let _cell = {
+      //           documentID: getuuid(),
+      //           contents: []
+      //         }
+
+      //         if (item.PrintData) {
+      //           _cell.contents.push(item.PrintData)
+      //         }
+      //         if (item.CustomData) {
+      //           _cell.contents.push(item.CustomData)
+      //         }
+
       //         _documents.push(_cell)
-      //       }
+      //       })
+      //       printerList.push({
+      //         cmd: 'print',
+      //         requestID: '',
+      //         version: '',
+      //         task: {
+      //           taskID: getuuid(),
+      //           preview: false,
+      //           printer: printer === 'lackprinter' ? '' : printer,
+      //           documents: _documents
+      //         }
+      //       })
       //     })
-      //     printerList.push({
-      //       cmd: 'print',
-      //       requestID: '',
-      //       version: '',
-      //       task: {
-      //         taskID: getuuid(),
-      //         preview: false,
-      //         printer: printer,
-      //         documents: _documents
-      //       }
-      //     })
-      //   })
-
-      //   let lackItems = printerList.filter(cell => cell.task.printer === 'lackprinter')[0]
+      //   }
 
       //   let socket = new WebSocket('ws://' + linkUrl)
 
       //   // 鎵撳紑Socket
       //   socket.onopen = () =>{
-      //     if (lackItems) {
-      //       let request  = {
-      //         requestID: '',
-      //         version: '',
-      //         cmd: 'getPrinters'
-      //       }
-      //       socket.send(JSON.stringify(request))
-      //     } else {
-      //       printerList.forEach(cell => {
-      //         socket.send(JSON.stringify(cell).replace(/\\r/g,"\r").replace(/\\n/g,"\n"))
-      //       })
+      //       printerList.forEach((cell, i) => {
+      //         setTimeout(() => {
+      //           socket.send(JSON.stringify(cell).replace(/\\r/g,"\r").replace(/\\n/g,"\n"))
+      //       }, 1000 * i)
+      //       });
 
       //       notification.success({
       //         top: 92,
       //         message: '鎵撳嵃璇锋眰宸插彂鍑恒��',
       //         duration: 2
       //       })
-      //     }
       //   }
       //   // 鐩戝惉娑堟伅
       //   socket.onmessage = (event) => {
@@ -524,20 +554,7 @@
       //       }
       //     }
 
-      //     if (data && data.cmd === 'getPrinters' && data.status) {
-      //       printerList.forEach(cell => {
-      //         if (cell.task.printer === 'lackprinter') {
-      //           cell.task.printer = data.defaultPrinter
-      //         }
-      //         socket.send(JSON.stringify(cell).replace(/\\r/g,"\r").replace(/\\n/g,"\n"))
-      //       })
-
-      //       notification.success({
-      //         top: 92,
-      //         message: '鎵撳嵃璇锋眰宸插彂鍑恒��',
-      //         duration: 2
-      //       })
-      //     } else if (data && data.message && !data.status) {
+      //     if (data && data.message && !data.status) {
       //       notification.warning({
       //         top: 92,
       //         message: data.message,
@@ -555,6 +572,9 @@
       //   }
       // }
 
+      // if (jdNewList.length > 0) {
+      //   execPrint(jdNewList, '127.0.0.1:9113', 'jd');
+      // }
       // if (jdList.length > 0) {
       //   execPrint(jdList, '127.0.0.1:13529')
       // }
@@ -566,8 +586,8 @@
 
       try {
         // eslint-disable-next-line
-        let evalfunc = eval('(true && function (data, form, printer, notification) {' + btn.verify.printFunc + '})')
-        evalfunc(printlist, formdata, btn.verify, notification)
+        let evalfunc = eval('(true && function (data, form, printer, notification, Api, systemType) {' + btn.verify.printFunc + '})')
+        evalfunc(printlist, formdata, btn.verify, notification, Api, window.GLOB.systemType)
       } catch (error) {
         console.warn(error)
         notification.warning({
@@ -1209,8 +1229,9 @@
    */
   execSuccess = (res) => {
     const { btn } = this.props
+    const { autoMatic } = this.state
 
-    if (res && res.ErrCode === 'S') { // 鎵ц鎴愬姛
+    if ((res && res.ErrCode === 'S') || autoMatic) { // 鎵ц鎴愬姛
       notification.success({
         top: 92,
         message: res.ErrMesg || this.state.dict['main.action.confirm.success'],
@@ -1223,13 +1244,18 @@
     } else if (res && res.ErrCode === '-1') { // 瀹屾垚鍚庝笉鎻愮ず
 
     }
-    
+
     this.setState({
       loading: false
     })
 
+    if (autoMatic) {
+      MKEmitter.emit('autoExecOver', btn.uuid, 'success')
+      return
+    }
+
     if (btn.execSuccess !== 'never') {
-      MKEmitter.emit('refreshByButtonResult', btn.$menuId, btn.execSuccess, btn)
+      MKEmitter.emit('refreshByButtonResult', btn.$menuId, btn.execSuccess, btn, '', this.state.selines)
     }
   }
 
@@ -1241,12 +1267,13 @@
    */
   execError = (res) => {
     const { btn } = this.props
+    const { btnconfig, autoMatic } = this.state
 
-    if (res.ErrCode === 'E') {
+    if (res.ErrCode === 'E' && !autoMatic) {
       Modal.error({
         title: res.message || res.ErrMesg,
       })
-    } else if (res.ErrCode === 'N') {
+    } else if (res.ErrCode === 'N' || autoMatic) {
       notification.error({
         top: 92,
         message: res.message || res.ErrMesg,
@@ -1267,8 +1294,17 @@
       loading: false
     })
 
+    if (autoMatic) {
+      MKEmitter.emit('autoExecOver', btn.uuid, 'error')
+      return
+    }
+
+    if (btnconfig && btnconfig.setting && btnconfig.setting.errFocus) {
+      MKEmitter.emit('mkFC', 'focus', btnconfig.setting.errFocus)
+    }
+
     if (btn.execError !== 'never') {
-      MKEmitter.emit('refreshByButtonResult', btn.$menuId, btn.execError, btn)
+      MKEmitter.emit('refreshByButtonResult', btn.$menuId, btn.execError, btn, '', this.state.selines)
     }
   }
 
@@ -1311,10 +1347,10 @@
    */
   improveAction = () => {
     const { btn } = this.props
-    const { btnconfig } = this.state
+    const { btnconfig, autoMatic } = this.state
 
     if (btnconfig) {
-      if (btnconfig.setting.display === 'prompt') { // 濡傛灉琛ㄥ崟浠ユ槸鍚︽灞曠ず
+      if (!autoMatic && (btnconfig.setting.display === 'prompt' || btnconfig.setting.display === 'exec')) { // 濡傛灉琛ㄥ崟浠ユ槸鍚︽灞曠ず
         this.modelconfirm()
       } else {
         this.setState({
@@ -1343,14 +1379,14 @@
             message: res.message,
             duration: 5
           })
-          this.updateStatus('over')
+          this.setState({ loading: false })
         } else if (!_LongParam || (btn.execMode === 'pop' && _LongParam.type !== 'Modal')) {
           notification.warning({
             top: 92,
             message: '鏈幏鍙栧埌鎸夐挳閰嶇疆淇℃伅锛�',
             duration: 5
           })
-          this.updateStatus('over')
+          this.setState({ loading: false })
         } else {
           _LongParam = updateForm(_LongParam)
           _LongParam = this.handleModelConfig(_LongParam)
@@ -1358,7 +1394,7 @@
           this.setState({
             btnconfig: _LongParam
           }, () => {
-            if (_LongParam.setting.display === 'prompt') { // 濡傛灉琛ㄥ崟浠ユ槸鍚︽灞曠ず
+            if (!autoMatic && (_LongParam.setting.display === 'prompt' || _LongParam.setting.display === 'exec')) { // 濡傛灉琛ㄥ崟浠ユ槸鍚︽灞曠ず
               this.modelconfirm()
             } else {
               this.setState({
@@ -1375,11 +1411,17 @@
    * @description 妯℃�佹锛堣〃鍗曪級锛岀‘璁�
    */
   handleOk = () => {
+    const { btnconfig, autoMatic } = this.state
+
+    if (!this.formRef) return
+
     this.formRef.handleConfirm().then(res => {
-      this.setState({
-        visible: false
-      })
-      this.triggerPrint(this.state.tabledata, res)
+      if (btnconfig.setting.finish !== 'unclose' || autoMatic) {
+        this.setState({
+          visible: false
+        })
+      }
+      this.triggerPrint(this.state.selines, res)
     })
   }
 
@@ -1388,14 +1430,14 @@
    */
   handleCancel = () => {
     this.setState({
+      loading: false,
       visible: false
     })
-    this.updateStatus('over')
   }
 
   modelconfirm = () => {
     const { BData } = this.props
-    const { btnconfig, tabledata } = this.state
+    const { btnconfig, selines } = this.state
     let _this = this
     let result = []
     
@@ -1410,8 +1452,8 @@
 
       if (item.type === 'linkMain' && BData && BData.hasOwnProperty(item.field)) {
         _initval = BData[item.field]
-      } else if (_readin && tabledata[0] && tabledata[0].hasOwnProperty(item.field)) {
-        _initval = tabledata[0][item.field]
+      } else if (_readin && selines[0] && selines[0].hasOwnProperty(item.field)) {
+        _initval = selines[0][item.field]
       } else if (item.type === 'date' && _initval) {
         _initval = moment().subtract(_initval, 'days').format('YYYY-MM-DD')
       } else if (item.type === 'datemonth' && _initval) {
@@ -1425,6 +1467,24 @@
         _fieldlen = item.fieldlength || 512
       } else if (item.type === 'number') {
         _fieldlen = item.decimal ? item.decimal : 0
+      } else if (item.type === 'rate') {
+        item.rateCount = item.rateCount || 5
+        let allowHalf = item.allowHalf === 'true'
+
+        if (allowHalf) {
+          _initval = parseFloat(_initval)
+          if (_initval % 0.5 !== 0) {
+            _initval = parseInt(_initval)
+          }
+        } else {
+          _initval = parseInt(_initval)
+        }
+
+        if (isNaN(_initval) || _initval < 0) {
+          _initval = 0
+        } else if (_initval > item.rateCount) {
+          _initval = item.rateCount
+        }
       }
 
       if (_initval === undefined) {
@@ -1434,22 +1494,26 @@
       result.push({
         key: item.field,
         readonly: item.readonly === 'true',
-        readin: _readin,
+        readin: item.readin !== 'false' && item.readin !== 'top',
         fieldlen: _fieldlen,
         type: item.type,
         value: _initval
       })
     })
 
-    confirm({
-      title: this.state.dict['main.action.confirm.tip'],
-      onOk() {
-        _this.triggerPrint(tabledata, result)
-      },
-      onCancel() {
-        _this.updateStatus('over')
-      }
-    })
+    if (btnconfig.setting.display === 'exec') {
+      this.execSubmit(selines, () => {}, result)
+    } else {
+      confirm({
+        title: this.state.dict['main.action.confirm.tip'],
+        onOk() {
+          _this.triggerPrint(selines, result)
+        },
+        onCancel() {
+          _this.setState({ loading: false })
+        }
+      })
+    }
   }
 
   /**
@@ -1496,7 +1560,7 @@
           menuType={this.props.menuType}
           action={btnconfig}
           inputSubmit={this.handleOk}
-          data={this.state.tabledata[0]}
+          data={this.state.selines[0]}
           BData={this.props.BData}
           wrappedComponentRef={(inst) => this.formRef = inst}
         />
@@ -1506,7 +1570,9 @@
 
   render() {
     const { btn, show } = this.props
-    const { loadingNumber, loading, disabled } = this.state
+    const { loadingNumber, loading, disabled, hidden } = this.state
+
+    if (hidden) return null
 
     if (show === 'actionList') {
       return <div style={{display: 'inline-block'}} onClick={(e) => e.stopPropagation()}>
@@ -1527,7 +1593,7 @@
         label = btn.label
         icon = btn.icon || ''
       } else if (show === 'link') {
-        label = <span>{btn.label}{btn.icon ? <Icon style={{marginLeft: '8px'}} type={btn.icon}/> : ''}</span>
+        label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon}/> : ''}</span>
         icon = ''
       } else if (show === 'icon') {
         icon = btn.icon || ''
@@ -1554,7 +1620,6 @@
 
 const mapStateToProps = (state) => {
   return {
-    tabviews: state.tabviews,
     menuType: state.editLevel
   }
 }

--
Gitblit v1.8.0