king
2022-12-09 ea471bc4b911b8f8ee4177a9eaafe900935451bf
src/tabviews/zshare/actionList/printbutton/index.jsx
@@ -183,11 +183,7 @@
      // 数据选择类型校验
      this.actionSettingError()
      return
    } else if (
      !btn.verify ||
      (btn.verify.printMode === 'normal' && (!btn.verify.Template || !btn.verify.linkUrl)) ||
      (btn.verify.printMode === 'custom' && (!btn.verify.printFunc || !btn.verify.linkUrl))
    ) {
    } else if (!btn.verify || !btn.verify.printMode) {
      notification.warning({
        top: 92,
        message: '请完善打印验证信息!',
@@ -232,10 +228,20 @@
    }
  }
  triggerPrint = (data, formlist = []) => {
    const { btn } = this.props
    if (btn.verify.printMode === 'RFID') {
      this.triggerRFIDPrint(data, formlist)
    } else {
      this.triggerNormalPrint(data, formlist)
    }
  }
  /**
   * @description 触发打印
   */
  triggerPrint = (data, formlist = []) => {
  triggerNormalPrint = (data, formlist) => {
    const { btn } = this.props
    let formdata = {}
    let baseCount = 1
@@ -378,7 +384,7 @@
            param.rduri = window.GLOB.mainSystemApi
          }
    
          Api.genericInterface(param).then(result => {
          Api.getSystemCacheConfig(param).then(result => {
            result.tempId = tempId
            resolve(result)
          })
@@ -448,6 +454,109 @@
        }
      } else {
        this.execError(errorMsg)
      }
    })
  }
  /**
   * @description 触发RFID打印
   */
  triggerRFIDPrint = (data, formlist) => {
    const { btn } = this.props
    let formdata = {}
    let list = []
    formlist.forEach(_data => {
      formdata[_data.key] = _data.value
    })
    new Promise(resolve => {
      if (btn.intertype === 'system') { // 使用系统时,直接从表格或表单中选取数据
        if (btn.Ot === 'notRequired') {
          if (formlist.length > 0) {
            list = [formdata]
          }
        } else {
          data.forEach(cell => {
            let _cell = {...cell, ...formdata}
            list.push(_cell)
          })
        }
        resolve(true)
      } else {
        this.getprintdata(btn, data, formdata, formlist).then(result => {
          if (result.next) {
            result.list.forEach(cell => {
              // 系统打印数据,校验data字段
              if (!cell.data || cell.data.length === 0) return
              cell.data.forEach(m => {
                let _cell = {...m, ...formdata}
                list.push(_cell)
              })
            })
          }
          resolve(result.next)
        })
      }
    }).then(res => {
      // 获取打印模板 getTemp
      if (!res) return false
      if (list.length === 0) {
        notification.warning({
          top: 92,
          message: '未获取到打印数据!',
          duration: 5
        })
        return false
      }
      let param = {
        func: 's_PrintTemplateMGetData',
        Type: 'Y',
        // ID: tempId, // 添加模板时,保存及查询使用模板参数
        PrintTempNO: btn.verify.Template
      }
      if (window.GLOB.mainSystemApi) { // 从单点登录服务器取打印配置信息
        param.rduri = window.GLOB.mainSystemApi
      }
      return Api.getSystemCacheConfig(param)
    }).then(result => {
      if (!result) {
        this.setState({ loading: false })
        return
      }
      if (!result.ConfigParam) {
        notification.warning({
          top: 92,
          message: '未获取到打印模板信息!',
          duration: 5
        })
        this.setState({ loading: false })
      } else {
        let configParam = ''
        try {
          configParam = JSON.parse(window.decodeURIComponent(window.atob(result.ConfigParam)))
        } catch (e) {
          configParam = ''
        }
        if (!configParam) {
          notification.warning({
            top: 92,
            message: '打印模板解析错误!',
            duration: 5
          })
          this.setState({ loading: false })
        } else {
          this.execRfidPrint(list, configParam)
        }
      }
    })
  }
@@ -1087,48 +1196,152 @@
    }
  }
  // execRfidPrint = () => {
  //   let type = 'HF'
  //   let printparamsJsonArray = []
  execRfidPrint = (list, template) => {
    const { btn } = this.props
  //   let mm = 12
  //   if (type === 'HF') {
  //     printparamsJsonArray.push({PTK_OpenUSBPort: 255}); // 打开打印机USB端口
  //     printparamsJsonArray.push({PTK_ClearBuffer: ''}); // 清空缓存
  //     printparamsJsonArray.push({PTK_SetDarkness: 10}); // 设置打印黑度 取值范围 0-20
  //     printparamsJsonArray.push({PTK_SetPrintSpeed: 4}); // 设置打印速度
  //     printparamsJsonArray.push({PTK_SetDirection: 'B'}); // 设置打印方向
  //     printparamsJsonArray.push({PTK_SetLabelHeight: 25*mm + ","+ 10*mm +","+ 0 +","+ false}); // 设置标签高度、间隙及偏移
  //     printparamsJsonArray.push({PTK_SetLabelWidth: 60*mm}); // 设置标签宽度
  //     printparamsJsonArray.push({PTK_SetRFID: 0+","+0+","+0+","+0+","+0}); // UHF RFID打印设置
  //     printparamsJsonArray.push({PTK_RWRFIDLabel: 1+","+0+","+0+","+4+","+1+',EEBA99192945C746'});
  //     printparamsJsonArray.push({PTK_DrawText_TrueType: "50,10,36,0,微软雅黑,1,700,0,0,0,北京明科有限公司"});
  //     // printparamsJsonArray.push({PTK_DrawText_TrueType: x+","+y+","+FHeight+","+FWidth+","+FType+","+Fspin+","+FWeight+","+FItalic+","+FUnline+","+FStrikeOut+","+data});
  //     // printparamsJsonArray.push({PTK_EncodeRFIDPC:'mkA040'});
  //     printparamsJsonArray.push({PTK_PrintLabel:1+","+1});
  //     printparamsJsonArray.push({PTK_CloseUSBPort: ""}); // 关闭USB通讯端口
  //   }
    list = list.filter(item => !!item[btn.verify.valueField])
  //   let data = {
  //     reqParam: '1',
  //     printparams: JSON.stringify(printparamsJsonArray)
  //     // printparams: JSON.stringify([{PTK_OpenUSBPort: 255},{PTK_ErrorReport_USBInterrupt: 'status:'},{PTK_GetPrinterDPI: 'DPI:'},{PTK_CloseUSBPort: ''}])
  //     // printparams: JSON.stringify([{PTK_OpenUSBPort: 255},{PTK_RFIDCalibrate: ''},{PTK_CloseUSBPort: ''}])
  //   }
    if (list.length === 0) {
      notification.warning({
        top: 92,
        message: '未获取到打印数据!',
        duration: 5
      })
      this.setState({ loading: false })
      return
    }
  //   Api.postekPrint(data).then(result => {
    let width = template.width
    let height = template.height
    let gap = template.gap || 0
    let mm = 12
  //   })
  // }
    if (btn.verify.DPI === '203') {
      mm = 8
    } else if (btn.verify.DPI === '600') {
      mm = 24
    }
    let items = []
    template.elements.forEach(element => {
      if (!['text', 'barcode', 'qrcode'].includes(element.type)) return
      let _field = element.field
      if (_field === 'other_field') {
        _field = element.cusfield || ''
      }
      let item = {
        type: element.type,
        value: element.value || '',
        field: _field
      }
      if (item.type === 'text') {
        let size = Math.floor(4 * mm * (element.fontSize || 12) / 12)
        let fontWeight = 400
        if (element.fontWeight === 'bold') {
          fontWeight = 600
        } else if (element.fontWeight === 'bolder') {
          fontWeight = 800
        } else if (element.fontWeight === 'lighter') {
          fontWeight = 300
        }
        item.draw = `${element.left * mm},${element.top * mm},${size},0,${element.fontFamily || '微软雅黑'},${element.align === 'center' ? 5 : 1},${fontWeight},0,0,0,`
      } else if (item.type === 'barcode') {
        let codeType = 1
        if (element.barcodeType === 'EAN13') {
          codeType = 'E30'
        }
        let narrowWidth = element.narrowWidth || 2
        let horizontal = element.narrowWidth || 2
        let vertical = Math.ceil(element.barcodeHeight * mm)
        item.draw = `${element.left * mm},${element.top * mm},0,${codeType},${narrowWidth},${horizontal},${vertical},${element.barcodeLabel === 'true' ? 'B' : 'N'},`
      } else if (item.type === 'qrcode') {
        let r = Math.ceil(element.qrcodeWidth / 2)
        item.draw = `${element.left * mm},${element.top * mm},0,0,0,${r},0,0,8,`
      }
      items.push(item)
    })
    let ip = '192.168.1.2'
    let params = list.map(cell => {
      let array = []
      let value = cell[btn.verify.valueField]
      if (btn.verify.linkType !== 'USB') {
        array.push({PTK_Connect_Timer: `${ip},${btn.verify.port},5`}); // 打开打印机网络端口
      } else {
        array.push({PTK_OpenUSBPort: btn.verify.port}); // 打开打印机USB端口
      }
      array.push({PTK_ClearBuffer: ''}); // 清空缓存
      array.push({PTK_SetDirection: 'B'}); // 设置打印方向
      array.push({PTK_SetLabelHeight: height * mm + ',' + gap * mm + ',' + 0 + ',' + false}); // 设置标签高度、间隙及偏移
      array.push({PTK_SetLabelWidth: width * mm}); // 设置标签宽度
      array.push({PTK_SetRFID: '0,0,0,0,0'}); // UHF RFID打印设置
      array.push({PTK_RWRFIDLabel: '1,0,0,4,1,' + value});
      items.forEach(m => {
        let val = ''
        if (m.field) {
          val = cell[m.field]
        } else {
          val = m.value
        }
        if (!val && val !== 0) return
        if (m.type === 'text') {
          array.push({PTK_DrawText_TrueType: m.draw + val});
        } else if (m.type === 'barcode') {
          array.push({PTK_DrawBarcode: m.draw + val});
        } else if (m.type === 'qrcode') {
          array.push({PTK_DrawBar2D_QR: m.draw + val});
        }
      })
      array.push({PTK_PrintLabel: '1,1'});
      array.push({PTK_CloseUSBPort: ''}); // 关闭USB通讯端口
      return {
        reqParam: '1',
        printparams: JSON.stringify(array)
      }
    })
    this.loopRFIDPrint(params)
  }
  loopRFIDPrint = (params) => {
    let param = params.shift()
    Api.postekPrint(param).then(res => {
      if (res.retval === '0') {
        if (params.length === 0) {
          this.execSuccess({ ErrCode: 'S', message: '', ErrMesg: '打印请求已发出。', status: true })
        } else {
          setTimeout(() => {
            this.loopRFIDPrint(params)
          }, 200)
        }
      } else {
        if (res.retval === '30021') {
          res.msg = res.msg.replace(/PTK_ClearBuffer[\s\S]*/, '')
        }
        this.execError({ErrCode: 'N', message: res.msg})
      }
    })
  }
  execPrint = (list, template, formdata) => {
    const { btn } = this.props
    let _errors = []
    // if (btn) {
    //   this.execRfidPrint()
    //   return
    // }
    let defaultPrinter = btn.verify.defaultPrinter || 'lackprinter'
    let printers = {}