king
2023-11-14 169f6cd29a0d3add4fe2cf35c3579b45fa83c0d4
src/utils/utils.js
@@ -1,3 +1,4 @@
import React from 'react'
import moment from 'moment'
import md5 from 'md5'
@@ -112,46 +113,6 @@
    let _str = salt + timestamp + secretkey
    return md5(_str)
  }
  /**
   * @description sql语法验证
   * @return {String}  sql    sql语句
   * @return {String}  type   验证类型
   */
  static verifySql (sql, type) {
    if (!sql) return ''
    let chars = [
      {key: 'create', reg: /(^|\s|\(|\))create\s/ig},
      {key: 'insert', reg: /(^|\s|\(|\))insert\s/ig},
      {key: 'delete', reg: /(^|\s|\(|\))delete\s/ig},
      {key: 'update', reg: /(^|\s|\(|\))update\s/ig},
      {key: 'set', reg: /(^|\s|\(|\))set\s/ig},
      {key: 'drop', reg: /(^|\s|\(|\))drop\s/ig},
      {key: 'alter', reg: /(^|\s|\(|\))alter\s/ig},
      {key: 'truncate', reg: /(^|\s|\(|\))truncate\s/ig},
      {key: 'if', reg: /(^|\s|\(|\))if\s/ig},
      {key: 'exec', reg: /(^|\s|\(|\))exec(\s|\()/ig},
      {key: 'OBJECT', reg: /(^|\s|\(|\))object(\s|\()/ig},
      {key: 'sys.', reg: /(^|\s|\(|\))sys\./ig},
      {key: 'kill', reg: /(^|\s|\(|\))kill\s/ig}
    ]
    if (type === 'customscript') {
      chars = chars.filter(char => !['create', 'insert', 'delete', 'update', 'set', 'drop', 'if', 'exec'].includes(char.key))
    }
    let error = ''
    sql = sql.replace(/sys\.fn_/ig, '') // 跳过sys.fn_验证
    chars.forEach(char => {
      if (!error && char.reg.test(sql)) {
        error = char.key
      }
    })
    return error
  }
  /**
@@ -407,6 +368,10 @@
      }
      if (item.type === 'text' || item.type === 'select') {
        if (/@username@|@fullName@/ig.test(item.initval)) {
          item.initval = item.initval.replace(/@username@/ig, sessionStorage.getItem('User_Name') || '').replace(/@fullName@/ig, sessionStorage.getItem('Full_Name') || '')
          item.oriInitval = item.initval
        }
        if (/,/.test(item.field)) {
          item.field.split(',').forEach(field => {
            keys.push(field.toLowerCase())
@@ -504,17 +469,21 @@
          newsearches[item.key] = _val
        }
      } else if (item.type === 'datemonth') {
        // 月-过滤条件,从月开始至结束
        let _startval = ''
        let _endval = ''
        if (item.value) {
          _startval = moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000'
          _endval = moment(item.value, 'YYYY-MM').endOf('month').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000'
        if (item.match === '=') {
          newsearches[item.key] = item.value
        } else {
          // 月-过滤条件,从月开始至结束
          let _startval = ''
          let _endval = ''
          if (item.value) {
            _startval = moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000'
            _endval = moment(item.value, 'YYYY-MM').endOf('month').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000'
          }
          newsearches[item.key] = _startval
          newsearches[item.key + '1'] = _endval
        }
        newsearches[item.key] = _startval
        newsearches[item.key + '1'] = _endval
      } else if (item.type === 'dateweek') {
        let _startval = ''
        let _endval = ''
@@ -618,10 +587,14 @@
        searchText.push('(' + item.key + ' ' + _match + ' \'' + _val + timetail + '\')')
      } else if (item.type === 'datemonth') { // 月-过滤条件,从月开始至结束,结束时间为月末加一天的0点,方式为<
        let _startval = moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000'
        let _endval = moment(item.value, 'YYYY-MM').endOf('month').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000'
        searchText.push('(' + item.key + ' >= \'' + _startval + '\' AND ' + item.key + ' < \'' + _endval + '\')')
        if (item.match === '=') {
          searchText.push('(' + item.key + ' = \'' + item.value + '\')')
        } else {
          let _startval = moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000'
          let _endval = moment(item.value, 'YYYY-MM').endOf('month').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000'
          searchText.push('(' + item.key + ' >= \'' + _startval + '\' AND ' + item.key + ' < \'' + _endval + '\')')
        }
      } else if (item.type === 'dateweek') { // 周-过滤条件
        let _startval = moment(item.value, 'YYYY-MM-DD' ).startOf('week').format('YYYY-MM-DD') + ' 00:00:00.000'
        let _endval = moment(item.value, 'YYYY-MM-DD').endOf('week').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000'
@@ -714,17 +687,21 @@
        options.push(item)
      } else if (item.type === 'datemonth') {
        let _startval = item.value ? moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000' : '1970-01-01 00:00:00.000'
        let _endval = item.value ? moment(item.value, 'YYYY-MM').endOf('month').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000' : '2050-01-01 00:00:00.000'
        let copy = JSON.parse(JSON.stringify(item))
        copy.key = copy.key + '1'
        copy.value = _endval
        item.value = _startval
        options.push(item)
        options.push(copy)
        if (item.match === '=') {
          options.push(item)
        } else {
          let _startval = item.value ? moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000' : '1970-01-01 00:00:00.000'
          let _endval = item.value ? moment(item.value, 'YYYY-MM').endOf('month').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000' : '2050-01-01 00:00:00.000'
          let copy = JSON.parse(JSON.stringify(item))
          copy.key = copy.key + '1'
          copy.value = _endval
          item.value = _startval
          options.push(item)
          options.push(copy)
        }
      } else if (item.type === 'dateweek') {
        let _startval = item.value ? moment(item.value, 'YYYY-MM-DD').startOf('week').format('YYYY-MM-DD') + ' 00:00:00.000' : '1970-01-01 00:00:00.000'
        let _endval = item.value ? moment(item.value, 'YYYY-MM-DD').endOf('week').add(1, 'days').format('YYYY-MM-DD') + ' 00:00:00.000' : '2050-01-01 00:00:00.000'
@@ -974,7 +951,11 @@
          })
        }
      } else if (/^Decimal/ig.test(col.type) || /^int/ig.test(col.type)) {
        if (!val && val !== 0) {
        if (col.required === 'false') {
          if (!val || isNaN(val)) {
            val = 0
          }
        } else if (!val && val !== 0) {
          errors.push(_position + '内容不可为空')
        } else if (isNaN(val)) {                                  // 检验是否为数值
          errors.push(_position + '内容应为数值')
@@ -1166,6 +1147,7 @@
    sql = sql.replace(/\n\s{6}/ig, '\n')
    if (window.GLOB.debugger === true) {
      console.info('%c' + item.logLabel, 'color: blue')
      console.info(sql)
    }
  } else {
@@ -1372,6 +1354,7 @@
    sql = sql.replace(/\n\s{6}/ig, '\n')
    if (window.GLOB.debugger === true) {
      console.info('%c' + btn.logLabel, 'color: blue')
      console.info(sql)
    }
  } else {
@@ -1401,7 +1384,7 @@
 * @return {Array}   columns   显示列
 * @return {Boolean} retmsg    是否需要数据返回
 */
export function getSysDefaultSql (btn, setting, formdata, param, data, columns, retmsg = false, moduleParams, getOptions) {
export function getSysDefaultSql (btn, setting, formdata, param, data, columns, retmsg = false, moduleParams) {
  let primaryId = param.ID
  let BID = param.BID || ''
  let verify = btn.verify || {}
@@ -1557,8 +1540,6 @@
    }
    columns.forEach(col => {
      if (col.field === 'works_flow_param') return
      if (col.type === 'colspan' || col.type === 'old_colspan') {
        col.subcols.forEach(cell => {
          setField(cell)
@@ -1578,7 +1559,7 @@
      Declare @tbid nvarchar(50),@ErrorCode nvarchar(50),@retmsg nvarchar(4000),@BillCode nvarchar(50),@BVoucher nvarchar(50),@FIBVoucherDate nvarchar(50), @FiYear nvarchar(50),@ModularDetailCode nvarchar(50), @UserName nvarchar(50),@FullName nvarchar(50),@RoleID nvarchar(512),@mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20),@mk_nation nvarchar(50),@mk_province nvarchar(50),@mk_city nvarchar(50),@mk_district nvarchar(50),@mk_address nvarchar(100),@mk_deleted int,@bid nvarchar(50)${_declarefields}
    `
  let userName = sessionStorage.getItem('User_Name') || ''
  let userName = sessionStorage.getItem('User_Name') || ''
  let fullName = sessionStorage.getItem('Full_Name') || ''
  let RoleID = sessionStorage.getItem('role_id') || ''
  let departmentcode = sessionStorage.getItem('departmentcode') || ''
@@ -1653,47 +1634,9 @@
  }
  // 失效验证,添加数据时不用
  if (btn.sqlType !== 'insert' && btn.Ot !== 'notRequired' && verify.invalid === 'true' && setting.dataresource) {
    let datasource = setting.dataresource
    let customScript = setting.customScript || ''
    let search = moduleParams ? moduleParams.search : null
    let orderBy = moduleParams ? moduleParams.orderBy : setting.order
    if (/\s/.test(datasource) && !/tb$/.test(datasource)) { // 拼接别名
      datasource = '(' + datasource + ') tb'
    }
    if (getOptions && (setting.queryType === 'statistics' || customScript)) {
      let allSearch = getOptions(search)
      let regoptions = allSearch.map(item => {
        return {
          reg: new RegExp('@' + item.key + '@', 'ig'),
          value: `'${item.value}'`
        }
      })
      regoptions.push({
        reg: new RegExp('@userName@', 'ig'),
        value: `'${userName}'`
      }, {
        reg: new RegExp('@fullName@', 'ig'),
        value: `'${fullName}'`
      }, {
        reg: new RegExp('@orderBy@', 'ig'),
        value: orderBy
      }, {
        reg: new RegExp('@pageSize@', 'ig'),
        value: 999999
      }, {
        reg: new RegExp('@pageIndex@', 'ig'),
        value: 1
      })
      regoptions.forEach(item => {
        datasource = datasource.replace(item.reg, item.value)
        customScript = customScript.replace(item.reg, item.value)
      })
    }
  if (verify.invalid === 'true' && moduleParams && moduleParams.dataresource) {
    let datasource = moduleParams.dataresource
    let customScript = moduleParams.customScript || ''
    if (customScript) {
      _sql += `
@@ -1903,7 +1846,7 @@
  let hasvoucher = false
  // 凭证-显示列中选取,必须选行
  if (verify.voucher && verify.voucher.enabled && data) {
  if (verify.voucher && verify.voucher.enabled) {
    let _voucher = verify.voucher
    hasvoucher = true
@@ -1911,7 +1854,7 @@
    _sql += `
      /* 创建凭证 */
      exec s_BVoucher_Create
        @Bill ='${_data[_voucher.linkField.toLowerCase()]}',
        @Bill ='${_data[_voucher.linkField.toLowerCase()] || ''}',
        @BVoucherType ='${_voucher.BVoucherType}',
        @VoucherTypeOne ='${_voucher.VoucherTypeOne}',
        @VoucherTypeTwo ='${_voucher.VoucherTypeTwo}',
@@ -2113,6 +2056,7 @@
    let status = 0
    let statusName = ''
    let detailId = ''
    let error = ''
    if (verify.flowType === 'start') {
      target = flow.cells.filter(cell => cell.mknode === 'start')[0]
@@ -2121,17 +2065,112 @@
        detailId = target.id
        status = target.mkdata.status
        statusName = target.mkdata.statusName
      } else {
        error = '工作流无开始节点'
      }
    } else if (_data.works_flow_param) {
      node = JSON.parse(window.decodeURIComponent(window.atob(_data.works_flow_param)))
      try {
        node = JSON.parse(window.decodeURIComponent(window.atob(_data.works_flow_param)))
      } catch (e) {
        node = null
      }
      if (node) {
        let lines = flow.cells.filter(cell => cell.shape === 'edge' && cell.source.cell === node.id)
        if (verify.flowType === 'reject') {
          line = lines.filter(cell => cell.mkdata.flowType === 'reject')[0]
          line = lines.filter(cell => cell.mkdata.flowType === 'reject' || cell.mknode === 'startEdge')[0]
        } else {
          line = lines.filter(cell => cell.mkdata.flowType !== 'reject')[0]
          let endEdge = null
          lines = lines.filter(cell => {
            if (cell.mknode === 'endEdge') {
              endEdge = cell
              return false
            }
            return cell.mkdata.flowType !== 'reject' && cell.mknode !== 'startEdge'
          })
          if (lines.length === 0) {
            if (!endEdge) {
              error = '无可执行的流程分支'
            } else {
              line = endEdge
            }
          } else {
            let branchKey = verify.flowBranch ? verify.flowBranch.toLowerCase() : ''
            if (!branchKey) {
              lines.forEach(line => {
                if (line.mkdata.execCondition === 'open') {
                  error = '按钮未设置流程控制字段。'
                }
              })
              if (!error) {
                line = lines[0]
              }
            } else if (!_data.hasOwnProperty(branchKey)) {
              error = '行信息中无流程控制字段。'
            } else {
              if (endEdge) {
                line = endEdge
              }
              let equalLine = null
              let gtOrLtLine = []
              let unEqualLine = null
              let branchVal = _data[branchKey]
              if (branchVal && typeof(branchVal) === 'string' && !isNaN(branchVal)) {
                branchVal = +branchVal
              }
              lines.forEach(item => {
                if (item.mkdata.execCondition !== 'open') {
                  line = item
                } else {
                  if (item.mkdata.match === '=') {
                    if (item.mkdata.matchVal === branchVal + '') {
                      equalLine = item
                    }
                  } else if (item.mkdata.match === '!=') {
                    if (item.mkdata.matchVal !== branchVal + '') {
                      unEqualLine = item
                    }
                  } else {
                    if (item.mkdata.match === '<') {
                      if (item.mkdata.matchVal < branchVal) {
                        gtOrLtLine.push({...item, dist: Math.abs(item.mkdata.matchVal - branchVal)})
                      }
                    } else if (item.mkdata.match === '>') {
                      if (item.mkdata.matchVal > branchVal) {
                        gtOrLtLine.push({...item, dist: Math.abs(item.mkdata.matchVal - branchVal)})
                      }
                    } else if (item.mkdata.match === '<=') {
                      if (item.mkdata.matchVal <= branchVal) {
                        gtOrLtLine.push({...item, dist: Math.abs(item.mkdata.matchVal - branchVal)})
                      }
                    } else if (item.mkdata.match === '>=') {
                      if (item.mkdata.matchVal >= branchVal) {
                        gtOrLtLine.push({...item, dist: Math.abs(item.mkdata.matchVal - branchVal)})
                      }
                    }
                  }
                }
              })
              if (equalLine) {
                line = equalLine
              } else if (gtOrLtLine.length > 0) {
                gtOrLtLine.sort((a, b) => a.dist - b.dist)
                line = gtOrLtLine[0]
              } else if (unEqualLine) {
                line = unEqualLine
              }
            }
          }
        }
      } else {
        error = '行信息中工作流参数无法解析'
      }
      
      if (line) {
@@ -2139,7 +2178,11 @@
        status = line.mkdata.status
        statusName = line.mkdata.statusName
        target = flow.cells.filter(cell => cell.id === line.target.cell)[0]
      } else if (!error) {
        error = '工作流中无对应流程'
      }
    } else {
      error = '行信息中无工作流参数'
    }
    if (verify.flowSql === 'true' && target) {
@@ -2156,7 +2199,7 @@
      } else {
        _sql += `
      /* 工作流默认sql */
      update s_my_works_flow set status=@status@,statusname=@statusname@,modifydate=getdate(),modifyuserid=@userid@,modifyuser=@username,modifystaff=@fullname
      update s_my_works_flow set status=@status@,statusname=@statusname@,works_flow_param=@works_flow_param@,works_flow_detail_id=@works_flow_detail_id@,modifydate=getdate(),modifyuserid=@userid@,modifyuser=@username,modifystaff=@fullname
      where works_flow_id=@ID@ and works_flow_code=@works_flow_code@ and deleted=0
      insert into s_my_works_flow_log (works_flow_id,works_flow_code,works_flow_name,works_flow_param,status,statusname,works_flow_detail_id,work_group,work_grade,upid)
      select @ID@,@works_flow_code@,@works_flow_name@ ,@works_flow_param@,@status@,@statusname@,@works_flow_detail_id@,@work_group@,@work_grade@,@time_id@
@@ -2180,20 +2223,12 @@
      `
      }
    } else if (verify.flowSql === 'true') {
      target = flow.cells.filter(cell => cell.mknode === 'start')[0]
      if (target) {
        detailId = target.id
      }
      status = 0
      statusName = '异常'
      _sql += `
      /* 工作流异常sql */
      update s_my_works_flow set status=@status@,statusname=@statusname@,modifydate=getdate(),modifyuserid=@userid@,modifyuser=@username,modifystaff=@fullname
      where works_flow_id=@ID@ and works_flow_code=@works_flow_code@ and deleted=0
      insert into s_my_works_flow_log (works_flow_id,works_flow_code,works_flow_name,works_flow_param,status,statusname,works_flow_detail_id,work_group,work_grade,upid)
      select @ID@,@works_flow_code@,@works_flow_name@ ,@works_flow_param@,@status@,@statusname@,@works_flow_detail_id@,@work_group@,@work_grade@,@time_id@
      select @ErrorCode='E',@retmsg='${error || '工作流执行异常'}' goto aaa
      `
    }
@@ -2222,7 +2257,8 @@
    _sql = _sql.replace(/@works_flow_code@/ig, `'${flow.flow_code}'`)
    _sql = _sql.replace(/@works_flow_name@/ig, `'${flow.flow_name}'`)
    if (target) {
      let msg = {...target.mkdata, id: target.id}
      let label = target.attrs && target.attrs.text && target.attrs.text.text ? target.attrs.text.text : ''
      let msg = {...target.mkdata, label: label, id: target.id}
      _sql = _sql.replace(/@works_flow_param@/ig, `'${window.btoa(window.encodeURIComponent(JSON.stringify(msg)))}'`)
    } else {
      _sql = _sql.replace(/@works_flow_param@/ig, `''`)
@@ -2272,6 +2308,10 @@
  _sql = _sql.replace(/@Appkey@/ig, `'${window.GLOB.appkey || ''}'`)
  _sql = _sql.replace(/@typename@/ig, `'admin'`)
  if (window.GLOB.externalDatabase !== null) {
    _sql = _sql.replace(/@db@/ig, window.GLOB.externalDatabase)
  }
  if (sessionStorage.getItem('dataM') === 'true') { // 数据权限
    _sql = _sql.replace(/\$@/ig, '/*').replace(/@\$/ig, '*/').replace(/@datam@/ig, `'Y'`)
  } else {
@@ -2279,7 +2319,7 @@
  }
  if (window.GLOB.debugger === true) {
    // _sql = _sql.replace(/\n\s{8}/ig, '\n')
    console.info('%c' + btn.logLabel, 'color: blue')
    console.info(_sql)
  }
@@ -2296,12 +2336,8 @@
/**
 * @description 获取标记信息
 */
export function getMark (marks, record, style = {}) {
  let icon = null
  let innerStyle = null
  let position = null
  let signType = ''
  style = JSON.parse(JSON.stringify(style))
export function getMark (marks, record, style) {
  let res = {signType: ''}
  marks.some(mark => {
    let originVal = record[mark.field[0]]
@@ -2337,42 +2373,45 @@
    if (type === 'font') {
      style.color = mark.color
      innerStyle = {color: mark.color}
      signType = ' sign-font'
      res.innerStyle = {color: mark.color}
      res.signType = ' sign-font'
    } else if (type === 'background') {
      style.backgroundColor = mark.color
      if (mark.fontColor) {
        style.color = mark.fontColor
        innerStyle = {color: mark.fontColor}
        res.innerStyle = {color: mark.fontColor}
      }
    } else if (type === 'underline') {
      style.textDecoration = 'underline'
      style.color = mark.color
      innerStyle = {color: mark.color, textDecoration: 'underline'}
      res.innerStyle = {color: mark.color, textDecoration: 'underline'}
    } else if (type === 'line-through') {
      style.textDecoration = 'line-through'
      style.color = mark.color
      innerStyle = {color: mark.color, textDecoration: 'line-through'}
      res.innerStyle = {color: mark.color, textDecoration: 'line-through'}
    } else if (type.indexOf('icon') > -1) {
      icon = mark.signType[mark.signType.length - 1]
      innerStyle = {color: mark.color}
      res.icon = mark.signType[mark.signType.length - 1]
      res.innerStyle = {color: mark.color}
      if (type === 'iconfront' || mark.signType[1] === 'front') {
        position = 'front'
        res.position = 'front'
      } else {
        position = 'back'
        res.position = 'back'
      }
    } else if (type === 'indent') {
      style.color = mark.color
      res.space = Array(mark.signType[1] + 1).join('&nbsp;')
    } else if (type === 'pointfront') {
      res.position = 'front'
      res.point = <span style={{display: 'inline-block', marginRight: '3px', width: '8px', height: '8px', borderRadius: '14px', background: mark.color, verticalAlign: 'middle'}}></span>
    } else if (type === 'pointback') {
      res.position = 'back'
      res.point = <span style={{display: 'inline-block', marginLeft: '3px', width: '8px', height: '8px', borderRadius: '14px', background: mark.color, verticalAlign: 'middle'}}></span>
    }
    return true
  })
  return {
    style,
    icon,
    innerStyle,
    position,
    signType
  }
  return res
}
/**