king
2024-03-26 7da23c8a73681703f42c4c5a4c907dc17acf8b8f
src/utils/utils.js
@@ -1,6 +1,7 @@
import React from 'react'
import moment from 'moment'
import md5 from 'md5'
import CryptoJS from 'crypto-js'
const formatKeys = [
  { key: 'select', value: ' msltk ' },
@@ -119,30 +120,45 @@
   * @description sql加密
   * @return {String}   value
   */
  static formatOptions (value) {
  static formatOptions (value, exec_type = '') {
    if (!value) return ''
    let salt = 'minKe' // 盐值
    value = value.replace(/\n/ig, ' \n ')
    // 替换关键字
    formatKeys.forEach(item => {
      let reg = new RegExp('(^|\\s)' + item.key + '(\\s|$)', 'ig')
      value = value.replace(reg, item.value)
    })
    // 替换%符(数据库中解析后sql报错)
    value = value.replace(/%/ig, ' mpercent ')
    // 外联数据库替换
    if (window.GLOB.externalDatabase !== null) {
      value = value.replace(/@db@/ig, window.GLOB.externalDatabase)
    }
    // encode编码(中文字符超出base64加密范围)
    value = window.btoa(window.encodeURIComponent(value))
    // 插入字符
    let index = Math.floor(value.length / 2)
    value = value.slice(0, index) + salt + value.slice(index)
    // base64加密
    value = window.btoa(value)
    // 替换%符(数据库中解析后sql报错)
    value = value.replace(/%/ig, ' mpercent ')
    if (exec_type === 'x') {
      let key = CryptoJS.enc.Utf8.parse('D1185ED7B32568C9')
      let iv = CryptoJS.enc.Utf8.parse('')
      let srcs = CryptoJS.enc.Utf8.parse(value)
      let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
      value = encrypted.toString()
      // let wordArray = CryptoJS.enc.Base64.parse(value)
      // let decryptedWordArray = CryptoJS.AES.decrypt({ ciphertext: wordArray }, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
      // let decryptedText = CryptoJS.enc.Utf8.stringify(decryptedWordArray)
    } else {
      // 替换关键字
      formatKeys.forEach(item => {
        let reg = new RegExp('(^|\\s)' + item.key + '(\\s|$)', 'ig')
        value = value.replace(reg, item.value)
      })
      // encode编码(中文字符超出base64加密范围)
      value = window.btoa(window.encodeURIComponent(value))
      // 插入字符
      let salt = 'minKe' // 盐值
      let index = Math.floor(value.length / 2)
      value = value.slice(0, index) + salt + value.slice(index)
      // base64加密
      value = window.btoa(value)
    }
    return value
  }
@@ -171,59 +187,6 @@
  // }
  /**
   * @description sPC_TableData_InUpDe sql加密
   * @return {String}  value
   */
  static sPCInUpDeFormatOptions (value) {
    if (!value) return {LText: '', LText1: '', LText2: ''}
    let salt = 'minKe' // 盐值
    value = value.replace(/\n/ig, ' \n ')
    // 替换关键字
    formatKeys.forEach(item => {
      let reg = new RegExp('(^|\\s)' + item.key + '(\\s|$)', 'ig')
      value = value.replace(reg, item.value)
    })
    // 1、替换%符(数据库中解析后sql报错)
    value = value.replace(/%/ig, ' mpercent ')
    // 外联数据库替换
    if (window.GLOB.externalDatabase !== null) {
      value = value.replace(/@db@/ig, window.GLOB.externalDatabase)
    }
    let encodesql = (val) => {
      if (!val) return ''
      let _value = window.btoa(window.encodeURIComponent(val))
      // 插入字符
      let index = Math.floor(_value.length / 2)
      _value = _value.slice(0, index) + salt + _value.slice(index)
      // base64加密
      return window.btoa(_value)
    }
    let len = value.length
    // 注:LText 与 LText1 顺序颠倒
    if (len > 1000) {
      let limit = Math.floor(len / 3)
      return {
        LText1: encodesql(value.substring(0, limit)),
        LText: encodesql(value.substring(limit, limit * 2)),
        LText2: encodesql(value.substring(limit * 2))
      }
    } else {
      return {
        LText1: '',
        LText: encodesql(value),
        LText2: ''
      }
    }
  }
  /**
   * @description 初始化搜索条件初始值
   * @param {Array}   searches     搜索条件
   */
@@ -239,6 +202,13 @@
    let values = null
    if (config.wrap && config.wrap.cacheSearch === 'true') {
      values = window.GLOB.SearchBox.get(config.$searchId + 'cache')
    }
    let supModule = ''
    if (config.setting && config.setting.supModule) {
      supModule = config.setting.supModule
    } else if (config.wrap && config.wrap.supModule) {
      supModule = config.wrap.supModule
    }
    config.search = config.search.map(item => {
@@ -260,10 +230,46 @@
          format = 'YYYY-MM-DD HH:mm:ss'
        }
        item.format = format
        item.initval = item.initval ? moment().subtract(item.initval, 'days').format(format) : ''
      } else if (item.type === 'datemonth') {
        item.initval = item.initval ? moment().subtract(item.initval, 'month').format('YYYY-MM') : ''
        item.format = 'YYYY-MM'
        if (item.initval) {
          if (!item.dateShift) {
            item.initval = moment().subtract(item.initval, 'month').format('YYYY-MM')
          } else {
            item.$initval = item.initval
            item.$supId = config.$pageId
            if (supModule) {
              item.$supId = supModule
              config.checkBid = true
              item.checkShift = true
              if (config.setting) {
                config.setting.checkBid = true
              }
            }
            item.initval = ''
            let d = ''
            if (window.GLOB.CacheData.has(item.$supId)) {
              d = window.GLOB.CacheData.get(item.$supId)
              d = d[item.dateShift] || ''
              if (d) {
                d = moment(d).format('YYYY-MM-DD')
                d = d === 'Invalid date' ? '' : d
              }
            }
            if (d) {
              item.initval = moment(d).subtract(item.initval, 'month').format('YYYY-MM')
            }
          }
        }
      } else if (item.type === 'dateweek') {
        item.format = 'YYYY-MM-DD'
        item.initval = item.initval ? moment().subtract(item.initval * 7, 'days').format('YYYY-MM-DD') : ''
      } else if (item.type === 'daterange') {
        let format = 'YYYY-MM-DD'
@@ -276,19 +282,68 @@
        } else if (item.precision === 'second') {
          format = 'YYYY-MM-DD HH:mm:ss'
        }
        if (item.initval === 'week') {
          item.initval = [moment().startOf('week').format(format), moment().endOf('week').format(format)].join(',')
        } else if (item.initval === 'month') {
          item.initval = [moment().startOf('month').format(format), moment().endOf('month').format(format)].join(',')
        } else if (item.initval === 'lastMonth') {
          item.initval = [moment().subtract(1, 'months').startOf('month').format(format), moment().subtract(1, 'months').endOf('month').format(format)].join(',')
        } else if (item.initval) {
          try {
            let _initval = JSON.parse(item.initval)
            let _vals = [moment().subtract(_initval[0], 'days').format(format), moment().subtract(_initval[1], 'days').format(format)]
            item.initval = _vals.join(',')
          } catch (e) {
        item.format = format
        if (item.initval) {
          if (!item.dateShift) {
            if (item.initval === 'week') {
              item.initval = [moment().startOf('week').format(format), moment().endOf('week').format(format)].join(',')
            } else if (item.initval === 'month') {
              item.initval = [moment().startOf('month').format(format), moment().endOf('month').format(format)].join(',')
            } else if (item.initval === 'lastMonth') {
              item.initval = [moment().subtract(1, 'months').startOf('month').format(format), moment().subtract(1, 'months').endOf('month').format(format)].join(',')
            } else if (item.initval) {
              try {
                let _initval = JSON.parse(item.initval)
                let _vals = [moment().subtract(_initval[0], 'days').format(format), moment().subtract(_initval[1], 'days').format(format)]
                item.initval = _vals.join(',')
              } catch (e) {
                item.initval = ''
              }
            }
          } else {
            item.$initval = item.initval
            item.$supId = config.$pageId
            if (supModule) {
              item.$supId = supModule
              config.checkBid = true
              item.checkShift = true
              if (config.setting) {
                config.setting.checkBid = true
              }
            }
            item.initval = ''
            let d = ''
            if (window.GLOB.CacheData.has(item.$supId)) {
              d = window.GLOB.CacheData.get(item.$supId)
              d = d[item.dateShift] || ''
              if (d) {
                d = moment(d).format('YYYY-MM-DD')
                d = d === 'Invalid date' ? '' : d
              }
            }
            if (d) {
              if (item.$initval === 'week') {
                item.initval = [moment(d).startOf('week').format(format), moment(d).endOf('week').format(format)].join(',')
              } else if (item.$initval === 'month') {
                item.initval = [moment(d).startOf('month').format(format), moment(d).endOf('month').format(format)].join(',')
              } else if (item.$initval === 'lastMonth') {
                item.initval = [moment(d).subtract(1, 'months').startOf('month').format(format), moment(d).subtract(1, 'months').endOf('month').format(format)].join(',')
              } else {
                try {
                  let _initval = JSON.parse(item.$initval)
                  let _vals = [moment(d).subtract(_initval[0], 'days').format(format), moment(d).subtract(_initval[1], 'days').format(format)]
                  item.initval = _vals.join(',')
                } catch (e) {
                  item.initval = ''
                }
              }
            }
          }
        }
      } else if (item.type === 'group') {
@@ -348,9 +403,18 @@
          item.initval = ''
          item.initType = ''
        }
      } else if ((item.type === 'select' || item.type === 'link') && item.initval === '$first' && item.resourceType === '1') {
        item.initval = ''
        item.$first = true
      } else if ((item.type === 'select' || item.type === 'link') && item.resourceType === '1') {
        if (/@BID@/ig.test(item.dataSource) && supModule) {
          config.checkBid = true
          item.checkBid = true
          if (config.setting) {
            config.setting.checkBid = true
          }
        }
        if (item.initval === '$first') {
          item.initval = ''
          item.$first = true
        }
      }
      
      item.oriInitval = item.initval
@@ -487,6 +551,11 @@
          newsearches[item.key] = _startval
          newsearches[item.key + '1'] = _endval
        }
      } else if (item.type === 'range') {
        let val = item.value.split(',')
        newsearches[item.key] = val[0] || -999999
        newsearches[item.key + '1'] = val[1] || 999999
      } else if (item.type === 'dateweek') {
        let _startval = ''
        let _endval = ''
@@ -634,7 +703,7 @@
      } else if (item.type === 'range') {
        let val = item.value.split(',')
        searchText.push('(' + item.key + ' >= \'' + val[0] + '\' AND ' + item.key + ' < \'' + val[1] + '\')')
        searchText.push('(' + item.key + ' >= ' + (val[0] || -999999) + ' AND ' + item.key + ' < ' + (val[1] || 999999) + ')')
      } else {
        searchText.push('(' + item.key + ' ' + item.match + ' \'' + item.value + '\')')
      }
@@ -670,9 +739,9 @@
      if (item.type === 'date') {
        if (!item.value) {
          if (['>=', '>'].includes(item.match)) {
            item.value = '1970-01-01 00:00:00.000'
            item.value = '1900-01-01 00:00:00.000'
          } else if (['<=', '<'].includes(item.match)) {
            item.value = '2050-01-01 00:00:00.000'
            item.value = '3000-01-01 00:00:00.000'
          }
        } else if (search.precision === 'day') {
          if (['>=', '>'].includes(item.match)) {
@@ -693,8 +762,8 @@
        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 _startval = item.value ? moment(item.value, 'YYYY-MM').startOf('month').format('YYYY-MM-DD') + ' 00:00:00.000' : '1900-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' : '3000-01-01 00:00:00.000'
  
          let copy = JSON.parse(JSON.stringify(item))
          copy.key = copy.key + '1'
@@ -705,9 +774,20 @@
          options.push(item)
          options.push(copy)
        }
      } else if (item.type === 'range') {
        let val = item.value.split(',')
        let copy = JSON.parse(JSON.stringify(item))
        copy.key = copy.key + '1'
        copy.value = val[1] || 999999
        item.value = val[0] || -999999
        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'
        let _startval = item.value ? moment(item.value, 'YYYY-MM-DD').startOf('week').format('YYYY-MM-DD') + ' 00:00:00.000' : '1900-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' : '3000-01-01 00:00:00.000'
        let copy = JSON.parse(JSON.stringify(item))
        copy.key = copy.key + '1'
@@ -718,8 +798,8 @@
        options.push(item)
        options.push(copy)
      } else if (item.type === 'daterange') {
        let _startval = '1970-01-01 00:00:00.000'
        let _endval = '2050-01-01 00:00:00.000'
        let _startval = '1900-01-01 00:00:00.000'
        let _endval = '3000-01-01 00:00:00.000'
        if (item.value) {
          let val = item.value.split(',')
@@ -1089,7 +1169,7 @@
      end
      
      ${unique.verifyType.indexOf('temp') === -1 ? `Set @tbid=''
      Select top 1 @tbid=${_afields.join('+\' \'+')} from  #${sheet} a Inner join ${sheet} b on ${_fields_}
      Select top 1 @tbid=${_afields.join('+\' \'+')} from  #${sheet} a Inner join ${database}${sheet} b on ${_fields_}
      
      If @tbid!=''
      Begin
@@ -1118,10 +1198,19 @@
    let _insert = ''
    if (btn.default !== 'false') {
      let _fields = []
      btn.columns.forEach(col => {
        if (col.import === 'false' || col.import === 'init') return
        _fields.push(col.Column)
      })
      _fields = _fields.join(',')
      _insert = `
      /* 默认sql */
      Insert into ${database}${sheet} (${fields},createuserid,createuser,createstaff,bid)
      Select ${fields},'${sessionStorage.getItem('UserID') || ''}',@username,@fullname,'${BID}' From #${sheet}
      Insert into ${database}${sheet} (${_fields},createuserid,createuser,createstaff,bid)
      Select ${_fields},'${sessionStorage.getItem('UserID') || ''}',@username,@fullname,'${BID}' From #${sheet}
      `
    }
@@ -1300,7 +1389,7 @@
      end
      
      ${unique.verifyType.indexOf('temp') === -1 ? `Set @tbid=''
      Select top 1 @tbid=${_afields.join('+\' \'+')} from  #${sheet} a Inner join ${sheet} b on ${_fields_}
      Select top 1 @tbid=${_afields.join('+\' \'+')} from  #${sheet} a Inner join ${database}${sheet} b on ${_fields_}
      
      If @tbid!=''
      Begin
@@ -1394,7 +1483,6 @@
  let datavars = {}                 // 声明的变量,表单及显示列
  let _actionType = null
  let _callbacksql = ''
  let foreignKey = setting.foreignKey ? setting.foreignKey.toLowerCase() : ''
  if (verify.default !== 'false') { // 判断是否使用默认sql
    _actionType = btn.sqlType
@@ -1561,6 +1649,14 @@
  _sql = `/* 系统生成 */
      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}
    `
  if (btn.$process && verify.workFlow === 'true' && window.GLOB.UserCacheMap.has(btn.$flowId)) {
    _sql += `
      /* 工作流变量定义与赋值 */
      Declare @works_flow_code nvarchar(50),@works_flow_name nvarchar(50),@works_flow_param nvarchar(4000),@works_flow_detail_id nvarchar(50),@status int,@statusname nvarchar(50), @work_group nvarchar(50),@work_grade int, @start_type nvarchar(50),@check_type nvarchar(50),@notice_type nvarchar(50),@check_userids nvarchar(512),@notice_userids nvarchar(512)
      select @works_flow_code=@works_flow_code@,@works_flow_name=@works_flow_name@,@works_flow_param=@works_flow_param@,@works_flow_detail_id=@works_flow_detail_id@,@status=@status@,@statusname=@statusname@,@work_group=@work_group@,@work_grade=@work_grade@, @start_type=@start_type@,@check_type=@check_type@,@notice_type=@notice_type@,@check_userids=@check_userids@,@notice_userids=@notice_userids@
      `
  }
  let userName = sessionStorage.getItem('User_Name') || '' 
  let fullName = sessionStorage.getItem('Full_Name') || ''
@@ -1777,19 +1873,13 @@
      item.field.split(',').forEach((_field, index) => {
        let _key = _field.toLowerCase()
        let _val = datavars[_key] !== undefined ? datavars[_key] : ''
        let _fval = `'${_val}'`
        if (_key === 'bid' && !datavars.bid) { // 表单中没有bid则使用系统bid变量
          _fval = '@BID@'
        }
        if (_key === 'bid' && foreignKey) {
          arr.push(foreignKey)
          _fieldValue.push(`${foreignKey}=${_fval}`)
        } else {
          arr.push(_key)
          _fieldValue.push(`${_key}=${_fval}`)
        arr.push(_key)
        if (_key === 'bid' && !_val) { // 表单中没有bid则使用系统bid变量
          _val = BID
        }
        
        _fieldValue.push(`${_key}='${_val}'`)
        _value.push(`${_labels[index] || ''}:${_val || ''}`)
      })
@@ -1905,14 +1995,7 @@
      values.push('@fullname')
    }
    if (!keys.includes('bid')) {
      if (foreignKey && !keys.includes(foreignKey)) {
        keys.push(foreignKey)
      } else {
        keys.push('bid')
      }
      values.push('@BID@')
    } else if (foreignKey && !keys.includes(foreignKey)) {
      keys.push(foreignKey)
      keys.push('bid')
      values.push('@BID@')
    }
@@ -2251,6 +2334,9 @@
        _sql = _sql.replace(/@check_userids@/ig, `''`)
        _sql = _sql.replace(/@notice_userids@/ig, `''`)
      }
    } else {
      _sql = _sql.replace(/@check_userids@/ig, `''`)
      _sql = _sql.replace(/@notice_userids@/ig, `''`)
    }
    _sql = _sql.replace(/@start_type@/ig, `'开始'`)
@@ -2291,7 +2377,7 @@
      z_debug_end: select @ErrorCode='E',@retmsg='debug_end' goto aaa`
  }
  if (retmsg) {
  if (retmsg || btn.returnValue === 'true') {
    _sql += `
      aaa: if @ErrorCode!=''
      insert into tmp_err_retmsg (ID, ErrorCode, retmsg, CreateUserID) select @time_id@,@ErrorCode, @retmsg,@UserID@`