king
2020-03-26 a24beb36feaa46f39cbb26ce5277e84f91241ce8
src/tabviews/zshare/actionList/index.jsx
@@ -1,6 +1,7 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import moment from 'moment'
import { is, fromJS } from 'immutable'
import { Button, Affix, Modal, notification, Spin, message } from 'antd'
import * as XLSX from 'xlsx'
import MutilForm from '@/tabviews/zshare/mutilform'
@@ -30,7 +31,8 @@
    triggerPopview: PropTypes.func,   // 弹窗标签页触发
    getexceloutparam: PropTypes.func, // 获取excel导出数据
    gettableselected: PropTypes.func, // 获取表格中数据
    permRoles: PropTypes.any          // 用户权限列表
    permRoles: PropTypes.any,         // 用户权限列表
    triggerBtn: PropTypes.any
  }
  state = {
@@ -41,7 +43,19 @@
    execAction: null,
    loadingUuid: '',
    btnloading: false,
    configMap: {}
    configMap: {},
    running: false
  }
  /**
   * @description 上级菜单id变化时,刷新数据
   */
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.triggerBtn && !is(fromJS(this.props.triggerBtn), fromJS(nextProps.triggerBtn)) && nextProps.triggerBtn.parentId === this.props.MenuID) {
      if (!this.state.loadingUuid && !this.state.visible && !this.state.running) {
        this.actionTrigger(nextProps.triggerBtn.button, nextProps.triggerBtn.data)
      }
    }
  }
  refreshdata = (item, type) => {
@@ -97,6 +111,9 @@
    }
    if (item.OpenType === 'prompt') {
      this.setState({
        running: true
      })
      confirm({
        title: this.props.dict['main.action.confirm.tip'],
        onOk() {
@@ -104,7 +121,11 @@
            _this.execSubmit(item, data, resolve)
          })
        },
        onCancel() {}
        onCancel() {
          _this.setState({
            running: false
          })
        }
      })
    } else if (item.OpenType === 'exec') {
      this.setState({loadingUuid: item.uuid})
@@ -196,12 +217,19 @@
            this.improveAction(item)
          })
        } else if (item.execMode === 'prompt') {
          this.setState({
            running: true
          })
          confirm({
            title: this.props.dict['main.action.confirm.tip'],
            onOk() {
              _this.triggerPrint(item, data)
            },
            onCancel() {}
            onCancel() {
              _this.setState({
                running: false
              })
            }
          })
        } else {
          this.triggerPrint(item, data)
@@ -239,64 +267,54 @@
    }
    let printlist = []
    let templates = [btn.verify.Template || '']
    let templates = []
    let printCount = 1
    if (formdata.printCount && typeof(formdata.printCount) === 'number' && formdata.printCount >= 1) {
      printCount = formdata.printCount
      delete formdata.printCount
    }
    this.setState({loadingUuid: btn.uuid})
    new Promise(resolve => {
      if (btn.intertype === 'inner' && !btn.innerFunc) {
        formdata.TemplateID = btn.verify.Template
        let printcell = {}
        printcell.printType = formdata.printType || ''
        printcell.printCount = printCount
        printcell.templateID = btn.verify.Template || ''
        if (btn.Ot === 'notRequired') {
          for (let i = 0; i < printCount; i++) {
            printlist.push(JSON.parse(JSON.stringify(formdata)))
          }
          printcell.data = [formdata]
        } else {
          data.forEach(cell => {
            let _cell = {...cell, ...formdata}
            for (let i = 0; i < printCount; i++) {
              printlist.push(JSON.parse(JSON.stringify(_cell)))
            }
          printcell.data = data.map(cell => {
            return {...cell, ...formdata}
          })
        }
        if (btn.verify.printMode === 'custom') {
          this.execCustomPrint(btn, printlist)
        } else {
          resolve('getTemp')
        }
        templates.push(printcell.templateID)
        printlist.push(printcell)
        resolve(true)
      } else {
        this.getprintdata(btn, data, formdata, formlist).then(result => {
          if (result.next === 'getTemp') {
            result.list.forEach(item => {
              let _cell = item.data
              if (_cell.TemplateID) {
                templates.push(_cell.TemplateID)
              } else {
                _cell.TemplateID = btn.verify.Template
          if (result.next) {
            printlist = result.list.map(cell => {
              cell.templateID = cell.templateID || btn.verify.Template
              cell.printType = cell.printType || formdata.printType || ''
              let _printCount = printCount
              if (cell.printCount && typeof(cell.printCount) === 'number' && cell.printCount >= 1) {
                _printCount = cell.printCount
              }
              let _count = printCount
              if (item.count && typeof(item.count) === 'number' && item.count >= 1) {
                _count = item.count
              }
              for (let i = 0; i < _count; i++) {
                printlist.push({...formdata, ..._cell})
              }
              cell.printCount = _printCount
              templates.push(cell.templateID)
              return cell
            })
          } else if (result.next === 'customPrint') {
            this.execCustomPrint(btn, result.list)
          }
          
          resolve(result.next)
@@ -304,8 +322,13 @@
      }
    }).then(res => {
      // 获取打印模板 getTemp
      if (!res) return
      if (res !== 'getTemp') return false
      if (!res) return false
      if (btn.verify.printMode === 'custom') {
        this.execCustomPrint(btn, printlist, formdata)
        return false
      }
      templates = Array.from(new Set(templates)) // 去重
@@ -355,119 +378,33 @@
      })
      if (!errorMsg) {
        this.execPrint(btn, printlist, _temps)
        this.execPrint(btn, printlist, _temps, formdata)
      } else {
        this.execError(errorMsg, btn)
      }
    })
  }
  execCustomPrint = (item, printlist) => {
  execCustomPrint = (item, printlist, formdata) => {
    this.setState({loadingUuid: ''})
    if (!socket || socket.readyState !== 1 || socket.url !== 'ws://' + item.verify.linkUrl) {
      socket = new WebSocket('ws://' + item.verify.linkUrl)
    } else {
      if (!item.printer) {
        let request  = {
          requestID: '',
          version: '',
          cmd: 'getPrinters'
        }
        socket.send(JSON.stringify(request))
      } else {
        this.setState({loadingUuid: ''})
        try {
          // eslint-disable-next-line
          let func = new Function('socket', 'data', 'printer', 'notification', item.verify.printFunc)
          func(socket, printlist, item.printer, notification)
        } catch (e) {
          console.warn(e)
          notification.warning({
            top: 92,
            message: '自定义函数执行错误!',
            duration: 10
          })
        }
      }
    }
    // 打开Socket
    socket.onopen = () =>{
      if (!item.printer) {
        let request  = {
          requestID: '',
          version: '',
          cmd: 'getPrinters'
        }
        socket.send(JSON.stringify(request))
      } else {
        this.setState({loadingUuid: ''})
        try {
          // eslint-disable-next-line
          let func = new Function('socket', 'data', 'printer', 'notification', item.verify.printFunc)
          func(socket, printlist, item.printer, notification)
        } catch (e) {
          console.warn(e)
          notification.warning({
            top: 92,
            message: '自定义函数执行错误!',
            duration: 10
          })
        }
      }
    }
    // 监听消息
    socket.onmessage = (event) => {
      let data = ''
      if (event.data) {
        try {
          data = JSON.parse(event.data)
        } catch {
          data = ''
          notification.warning({
            top: 92,
            message: event.data,
            duration: 10
          })
          this.setState({loadingUuid: ''})
        }
      }
      if (data && data.cmd === 'getPrinters' && data.status) {
        this.setState({loadingUuid: ''})
        try {
          // eslint-disable-next-line
          let func = new Function('socket', 'data', 'printer', 'notification', item.verify.printFunc)
          func(socket, printlist, data.defaultPrinter, notification)
        } catch (e) {
          console.warn(e)
          notification.warning({
            top: 92,
            message: '自定义函数执行错误!',
            duration: 10
          })
        }
      } else if (data && data.message && !data.status) {
        notification.warning({
          top: 92,
          message: data.message,
          duration: 10
        })
        this.setState({loadingUuid: ''})
      }
    }
    socket.onerror = () => {
    try {
      // eslint-disable-next-line
      let func = new Function('data', 'form', 'printer', 'notification', item.verify.printFunc)
      func(printlist, formdata, item.verify, notification)
    } catch (e) {
      console.warn(e)
      notification.warning({
        top: 92,
        message: '无法连接到:' + item.verify.linkUrl,
        message: '自定义函数执行错误!',
        duration: 10
      })
      this.setState({loadingUuid: ''})
    }
  }
  /**
   * @description 获取打印数据
   */
  getprintdata = (btn, data, formdata, formlist) => {
    const { setting } = this.props
@@ -531,23 +468,15 @@
          Promise.all(deffers).then(result => {
            let errorMsg = ''
            result.forEach(res => {
              if (res.status) {
                if (btn.verify.printMode === 'custom') {
                  _list.push(res)
                } else {
                  res.data.forEach(_item => {
                    _list.push({data: _item, count: res.printCount})
                  })
                }
              } else {
              if (!res.status) {
                errorMsg = res
              }
            })
            if (!errorMsg) {
              resolve({next: btn.verify.printMode === 'custom' ? 'customPrint' : 'getTemp', list: _list})
              resolve({next: true, list: result})
            } else {
              this.execError(errorMsg, btn)
              resolve({next: false, list: _list})
              resolve({next: false, list: []})
            }
          })
        } else {
@@ -588,7 +517,7 @@
          } else {
            this.execError(res, btn)
            resolve(false)
            _resolve({next: false, list: _list})
            _resolve({next: false, list: []})
          }
        })
      } else {
@@ -632,45 +561,34 @@
        return Api.genericInterface(_callbackparam)
      } else if (response.status) {
        if (btn.verify.printMode === 'custom') {
          _list.push(response)
        } else {
          response.data.forEach(_item => {
            _list.push({data: _item, count: response.printCount})
          })
        }
        _list.push(response)
        // 一次请求成功,进行下一项请求
        if (params.length === 0) {
          _resolve({next: btn.verify.printMode === 'custom' ? 'customPrint' : 'getTemp', list: _list})
          _resolve({next: true, list: _list})
        } else {
          this.printOuterLoopRequest(params, btn, _list, _resolve)
        }
      } else {
        this.execError(response, btn)
        _resolve({next: false, list: _list})
        _resolve({next: false, list: []})
      }
    }).then(response => {
      if (!response) return
      if (response.status) {
        if (btn.verify.printMode === 'custom') {
          _list.push(response)
        } else {
          response.data.forEach(_item => {
            _list.push({data: _item, count: response.printCount})
          })
        }
        _list.push(response)
        // 一次请求成功,进行下一项请求
        if (params.length === 0) {
          _resolve({next: btn.verify.printMode === 'custom' ? 'customPrint' : 'getTemp', list: _list})
          _resolve({next: true, list: _list})
        } else {
          this.printOuterLoopRequest(params, btn, _list, _resolve)
        }
      } else {
        this.execError(response, btn)
        _resolve({next: false, list: _list})
        _resolve({next: false, list: []})
      }
    })
  }
@@ -683,22 +601,16 @@
    Api.genericInterface(param).then(res => {
      if (res.status) {
        if (btn.verify.printMode === 'custom') {
          _list.push(res)
        } else {
          res.data.forEach(_item => {
            _list.push({data: _item, count: res.printCount})
          })
        }
        _list.push(res)
        if (params.length === 0) {
          _resolve({next: btn.verify.printMode === 'custom' ? 'customPrint' : 'getTemp', list: _list})
          _resolve({next: true, list: _list})
        } else {
          this.printInnerLoopRequest(params, btn, _list, _resolve)
        }
      } else {
        this.execError(res, btn)
        _resolve({next: false, list: _list})
        _resolve({next: false, list: []})
      }
    })
  }
@@ -825,65 +737,104 @@
    }
  }
  execPrint = (item, list, template) => {
    let _documents = []
  execPrint = (item, list, template, formdata) => {
    let _errors = []
    Object.keys(template).forEach(key => {
      let _data = list.filter(cell => cell.TemplateID === key)
      if (_data.length > 0) {
        let _fields = Array.from(new Set(template[key].fields))
        let _nonEFields = Array.from(new Set(template[key].nonEFields))
        let lacks = []
        let emptys = []
        _data.forEach(d => {
          _fields.forEach(f => {
            if (!d.hasOwnProperty(f)) {
              lacks.push(f)
            } else if (_nonEFields.includes(f) && !d[f] && d[f] !== 0) {
              emptys.push(f)
            }
          })
        })
        let _cell = {
          documentID: Utils.getuuid(),
          contents: [
            {
              data: _data,
              templateURL: JSON.stringify(template[key].config)
            }
          ]
    let defaultPrinter = item.verify.defaultPrinter || 'lackprinter'
    let printers = {}
    if (item.verify.printerTypeList && item.verify.printerTypeList.length > 0) {
      item.verify.printerTypeList.forEach(cell => {
        if (cell.printer) {
          printers[cell.Value] = cell.printer
        }
      })
    }
        if (lacks.length > 0 || emptys.length > 0) {
          lacks = Array.from(new Set(lacks))
          emptys = Array.from(new Set(emptys))
    let printdata = {}
          _errors.push({
            title: template[key].config.Title,
            lacks: lacks,
            emptys: emptys
          })
        }
    list.forEach(res => {
      let _printer = defaultPrinter
        _documents.push(_cell)
      if (res.printType && printers[res.printType]) {
        _printer = printers[res.printType]
      }
      printdata[_printer] = printdata[_printer] || []
      printdata[_printer].push(res)
    })
    let printdata = {
      cmd: 'print',
      requestID: '',
      version: '',
      task: {
        taskID: Utils.getuuid(),
        preview: false,
        printer: item.printer,
        documents: _documents
    let printerList = []
    Object.keys(printdata).forEach(printer => {
      let _documents = []
      Object.keys(template).forEach(key => {
        let _datalist = printdata[printer].filter(cell => cell.templateID === key)
        if (_datalist.length > 0) {
          let _data = []
          _datalist.forEach(res => {
            res.data.forEach(_cell => {
              for (let i = 0; i < res.printCount; i++) {
                _data.push({...formdata, ..._cell})
              }
            })
          })
          let _fields = Array.from(new Set(template[key].fields))
          let _nonEFields = Array.from(new Set(template[key].nonEFields))
          let lacks = []
          let emptys = []
          _data.forEach(d => {
            _fields.forEach(f => {
              if (!d.hasOwnProperty(f)) {
                lacks.push(f)
              } else if (_nonEFields.includes(f) && !d[f] && d[f] !== 0) {
                emptys.push(f)
              }
            })
          })
          let _cell = {
            documentID: Utils.getuuid(),
            contents: [
              {
                data: _data,
                templateURL: JSON.stringify(template[key].config)
              }
            ]
          }
          if (lacks.length > 0 || emptys.length > 0) {
            lacks = Array.from(new Set(lacks))
            emptys = Array.from(new Set(emptys))
            _errors.push({
              title: template[key].config.Title,
              lacks: lacks,
              emptys: emptys
            })
          }
          _documents.push(_cell)
        }
      })
      if (_documents.length > 0) {
        printerList.push({
          cmd: 'print',
          requestID: Utils.getuuid(),
          version: Utils.getuuid(),
          task: {
            taskID: Utils.getuuid(),
            preview: false,
            printer: printer,
            documents: _documents
          }
        })
      }
    }
    })
    if (list.length === 0) {
      notification.warning({
@@ -896,6 +847,7 @@
    } else if (_errors.length > 0) {
      let lackerror = []
      let emptyerror = []
      _errors.forEach(err => {
        if (err.lacks.length > 0) {
          lackerror.push(`数据中未获取到模板(${err.title})${err.lacks.join('、')} 字段`)
@@ -923,10 +875,12 @@
      return
    }
    let lackItems = printerList.filter(cell => cell.task.printer === 'lackprinter')[0]
    if (!socket || socket.readyState !== 1 || socket.url !== 'ws://' + item.verify.linkUrl) {
      socket = new WebSocket('ws://' + item.verify.linkUrl)
    } else {
      if (!item.printer) {
      if (lackItems) {
        let request  = {
          requestID: '',
          version: '',
@@ -934,18 +888,21 @@
        }
        socket.send(JSON.stringify(request))
      } else {
        printerList.forEach(cell => {
          socket.send(JSON.stringify(cell))
        })
        this.setState({loadingUuid: ''})
        notification.success({
          top: 92,
          message: '打印请求已发出。',
          duration: 2
        })
        socket.send(JSON.stringify(printdata))
      }
    }
    // 打开Socket
    socket.onopen = () =>{
      if (!item.printer) {
      if (lackItems) {
        let request  = {
          requestID: '',
          version: '',
@@ -953,13 +910,16 @@
        }
        socket.send(JSON.stringify(request))
      } else {
        printerList.forEach(cell => {
          socket.send(JSON.stringify(cell))
        })
        this.setState({loadingUuid: ''})
        notification.success({
          top: 92,
          message: '打印请求已发出。',
          duration: 2
        })
        socket.send(JSON.stringify(printdata))
      }
    }
    // 监听消息
@@ -981,14 +941,19 @@
      }
      if (data && data.cmd === 'getPrinters' && data.status) {
        printdata.task.printer = data.defaultPrinter
        printerList.forEach(cell => {
          if (cell.task.printer === 'lackprinter') {
            cell.task.printer = data.defaultPrinter
          }
          socket.send(JSON.stringify(cell))
        })
        this.setState({loadingUuid: ''})
        notification.success({
          top: 92,
          message: '打印请求已发出。',
          duration: 2
        })
        socket.send(JSON.stringify(printdata))
      } else if (data && data.message && !data.status) {
        notification.warning({
          top: 92,
@@ -1014,6 +979,7 @@
   */
  changeUser = (btn, data) => {
    const { setting } = this.props
    let _this = this
    let param = {
      BID: this.props.BID,
@@ -1024,6 +990,9 @@
    param[setting.primaryKey] = data[0][setting.primaryKey]
    this.setState({
      running: true
    })
    confirm({
      title: this.props.dict['main.action.confirm.tip'],
      onOk() {
@@ -1036,6 +1005,7 @@
              sessionStorage.setItem('SessionUid', Utils.getuuid())
              sessionStorage.setItem('LoginUID', res.LoginUID)
              sessionStorage.setItem('User_Name', res.UserName)
              sessionStorage.setItem('Full_Name', res.FullName)
              
              window.location.reload()
            } else {
@@ -1048,7 +1018,11 @@
          })
        })
      },
      onCancel() {}
      onCancel() {
        _this.setState({
          running: false
        })
      }
    })
  }
@@ -1080,7 +1054,6 @@
        btn.Ot === 'notRequired' ||
        btn.Ot === 'requiredSgl' ||
        (btn.Ot === 'requiredOnce' && btn.OpenType !== 'pop')
        // (btn.OpenType === 'pop' && !btn.innerFunc && btn.sql && btn.sqlType === 'insert')
      ) {
        // 创建凭证时,需要选择行时
@@ -1135,12 +1108,14 @@
          } else if (btn.sql && btn.sqlType === 'insert') { // 系统函数添加时,生成uuid
            primaryId = ''
            if (formdata && setting.primaryKey) { // 表单中存在主键字段,主键值以表单中的值为准
              let _form = formdata.filter(_form => _form.key === setting.primaryKey)[0]
              if (_form) {
                primaryId = _form.value
              }
            }
            param.ID = primaryId || Utils.getguid()
            param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting, formdata, param, data[0], logcolumns)) // 数据源
            param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
@@ -1499,7 +1474,8 @@
    }
    this.setState({
      loadingUuid: ''
      loadingUuid: '',
      running: false
    })
    this.refreshdata(btn, 'success')
@@ -1534,7 +1510,8 @@
    }
    
    this.setState({
      loadingUuid: ''
      loadingUuid: '',
      running: false
    })
    
    this.refreshdata(btn, 'error')
@@ -1544,6 +1521,9 @@
   * @description 按钮配置信息错误提示
   */
  actionSettingError = () => {
    this.setState({
      running: false
    })
    notification.warning({
      top: 92,
      message: this.props.dict['main.action.settingerror'],
@@ -2416,6 +2396,9 @@
        value: _initval
      }
    })
    this.setState({
      running: true
    })
    confirm({
      title: this.props.dict['main.action.confirm.tip'],
@@ -2424,7 +2407,11 @@
          _this.execSubmit(execAction, tabledata, resolve, result)
        })
      },
      onCancel() {}
      onCancel() {
        _this.setState({
          running: false
        })
      }
    })
  }