king
2024-02-19 1e4a7720c748bc0206b02b30f4a2e0b3dafb54f3
src/tabviews/zshare/topSearch/index.jsx
@@ -22,11 +22,12 @@
const MKSelect = asyncComponent(() => import('./mkSelect'))
const DateGroup = asyncComponent(() => import('./dategroup'))
const MKDatePicker = asyncComponent(() => import('./mkDatePicker'))
const MKNumber = asyncComponent(() => import('./mkNumber'))
class MainSearch extends Component {
  static propTpyes = {
    BID: PropTypes.any,          // 父级Id,用于查询下拉选择项
    config: PropTypes.object,    // 组件配置信息
    BID: PropTypes.any,
    config: PropTypes.object
  }
  state = {
@@ -41,7 +42,7 @@
  sign = ''
  UNSAFE_componentWillMount () {
    const { config } = this.props
    const { config, BID } = this.props
    let _searchlist = []
    let fieldMap = new Map()
@@ -114,21 +115,44 @@
      if (item.advanced && !forbid) {
        _setting.showAdv = true
        if (!['group', 'check', 'switch'].includes(item.type)) {
          item.signValue = true
        }
      } else {
        item.advanced = false
      }
      if (item.advanced && item.initval) {
        let val = item.initval
        if (item.precision === 'hour') {
          if (/,/ig.test(val)) {
            val = val.split(',').map(m => m + ':00').join(',')
          } else {
            val = val + ':00'
      if (item.checkShift && !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 (['text', 'date', 'datemonth', 'dateweek', 'daterange'].includes(item.type)) {
          advanceValues.push({field: item.field, type: item.type, label: item.label, value: val})
        if (d) {
          if (item.type === 'daterange') {
            if (item.$initval === 'week') {
              item.initval = [moment(d).startOf('week').format(item.format), moment(d).endOf('week').format(item.format)].join(',')
            } else if (item.$initval === 'month') {
              item.initval = [moment(d).startOf('month').format(item.format), moment(d).endOf('month').format(item.format)].join(',')
            } else if (item.$initval === 'lastMonth') {
              item.initval = [moment(d).subtract(1, 'months').startOf('month').format(item.format), moment(d).subtract(1, 'months').endOf('month').format(item.format)].join(',')
            } else {
              try {
                let _initval = JSON.parse(item.$initval)
                let _vals = [moment(d).subtract(_initval[0], 'days').format(item.format), moment(d).subtract(_initval[1], 'days').format(item.format)]
                item.initval = _vals.join(',')
              } catch (e) {
                item.initval = ''
              }
            }
          } else {
            item.initval = moment(d).subtract(item.$initval, 'month').format('YYYY-MM')
          }
        }
      }
@@ -155,22 +179,34 @@
        if (item.resourceType === '1' && item.dataSource) {
          let _option = Utils.getSelectQueryOptions(item)
          if (window.GLOB.debugger === true) {
            console.info(_option.sql)
          let exec = true
          if (item.checkBid) {
            item.sql = _option.sql
            item.arr_field = _option.field
            exec = !!BID
          }
          // 测试系统单个请求
          if (!window.GLOB.mkHS && window.GLOB.sysType === 'local' && !window.GLOB.systemType) {
            deForms.push({
              ...item,
              arr_field: _option.field,
              data_sql: Utils.formatOptions(_option.sql)
            })
          } else { // 合并请求,区分本地及系统
            if (item.database === 'sso') {
              mainItems.push(`select '${item.field}' as obj_name,'${_option.field}' as arr_field,'${window.btoa(window.encodeURIComponent(_option.sql.replace(/%/ig, ' mpercent ')))}' as LText`)
            } else {
              localItems.push(`select '${item.field}' as obj_name,'${_option.field}' as arr_field,'${window.btoa(window.encodeURIComponent(_option.sql.replace(/%/ig, ' mpercent ')))}' as LText`)
          if (exec) {
            _option.sql = _option.sql.replace(/@BID@/ig, `'${BID || ''}'`)
            if (window.GLOB.debugger === true) {
              console.info(_option.sql)
            }
            // 测试系统单个请求
            if (!window.GLOB.mkHS && window.GLOB.sysType === 'local' && !window.GLOB.systemType) {
              deForms.push({
                ...item,
                arr_field: _option.field,
                data_sql: Utils.formatOptions(_option.sql, window.GLOB.execType)
              })
            } else { // 合并请求,区分本地及系统
              if (item.database === 'sso') {
                mainItems.push(`select '${item.field}' as obj_name,'${_option.field}' as arr_field,'${window.btoa(window.encodeURIComponent(_option.sql.replace(/%/ig, ' mpercent ')))}' as LText`)
              } else {
                localItems.push(`select '${item.field}' as obj_name,'${_option.field}' as arr_field,'${window.btoa(window.encodeURIComponent(_option.sql.replace(/%/ig, ' mpercent ')))}' as LText`)
              }
            }
          }
        }
@@ -179,6 +215,29 @@
        if (item.type === 'checkcard' && item.multiple === 'dropdown' && item.resourceType === '0') {
          this.resetCheckcard(item)
        }
      }
      if (item.signValue && item.initval) {
        let val = item.initval
        if (item.precision === 'hour') {
          if (/,/ig.test(val)) {
            val = val.split(',').map(m => m + ':00').join(',')
          } else {
            val = val + ':00'
          }
        }
        let text = val
        if (item.type === 'select' || item.type === 'link' || item.type === 'radio') {
          item.oriOptions.forEach(cell => {
            if (cell.Value === val) {
              text = cell.Text
            }
          })
        }
        advanceValues.push({field: item.field, type: item.type, label: item.label, value: val, text})
      }
      fieldMap.set(item.field, item)
@@ -219,11 +278,84 @@
      searchlist: _list
    }, () => {
      if (!window.GLOB.mkHS && window.GLOB.sysType === 'local' && window.GLOB.systemType !== 'production') {
        this.improveSimpleSearch(deForms)
        this.improveSimpleSearch(deForms, false)
      } else if (mainItems.length > 0 || localItems.length > 0) {
        this.improveSearch(mainItems, localItems)
      }
    })
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    const { config, BID } = this.props
    if (config.checkBid && nextProps.BID !== BID) {
      this.resetOptions(nextProps.BID)
    }
  }
  resetOptions = (BID) => {
    let deForms = []
    let searchlist = fromJS(this.state.searchlist).toJS().map(item => {
      if (item.checkBid) {
        let sql = item.sql.replace(/@BID@/ig, `'${BID || ''}'`)
        if (window.GLOB.debugger === true) {
          console.info(sql)
        }
        deForms.push({
          ...item,
          arr_field: item.arr_field,
          data_sql: Utils.formatOptions(sql, window.GLOB.execType)
        })
      } else if (item.checkShift) {
        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.type === 'daterange') {
            if (item.$initval === 'week') {
              item.initval = [moment(d).startOf('week').format(item.format), moment(d).endOf('week').format(item.format)].join(',')
            } else if (item.$initval === 'month') {
              item.initval = [moment(d).startOf('month').format(item.format), moment(d).endOf('month').format(item.format)].join(',')
            } else if (item.$initval === 'lastMonth') {
              item.initval = [moment(d).subtract(1, 'months').startOf('month').format(item.format), moment(d).subtract(1, 'months').endOf('month').format(item.format)].join(',')
            } else {
              try {
                let _initval = JSON.parse(item.$initval)
                let _vals = [moment(d).subtract(_initval[0], 'days').format(item.format), moment(d).subtract(_initval[1], 'days').format(item.format)]
                item.initval = _vals.join(',')
              } catch (e) {
                item.initval = ''
              }
            }
          } else {
            item.initval = moment(d).subtract(item.$initval, 'month').format('YYYY-MM')
          }
        }
      }
      return item
    })
    if (deForms.length > 0) {
      this.improveSimpleSearch(deForms, true, searchlist)
    } else {
      this.setState({
        searchlist: searchlist
      })
      setTimeout(() => {
        this.handleSubmit()
      }, 20)
    }
  }
  resetCheckcard = (item) => {
@@ -277,9 +409,13 @@
    }
    
    if (param.LText) {
      param.LText = Utils.formatOptions(param.LText)
      if (window.GLOB.execType === 'x') {
        param.exec_type = 'x'
      }
      param.LText = Utils.formatOptions(param.LText, param.exec_type)
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      param.secretkey = Utils.encrypt(param.LText, param.timestamp)
      param.secretkey = Utils.encrypt(window.GLOB.execType === 'x' ? '' : param.LText, param.timestamp)
      if (window.GLOB.mkHS) { // 云端数据验证
        param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
@@ -315,19 +451,19 @@
    }
    if (mainparam.LText) {
      mainparam.LText = Utils.formatOptions(mainparam.LText)
      mainparam.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      mainparam.secretkey = Utils.encrypt(mainparam.LText, mainparam.timestamp)
      if (window.GLOB.execType === 'x') {
        mainparam.exec_type = 'x'
      }
      mainparam.LText = Utils.formatOptions(mainparam.LText, mainparam.exec_type)
      mainparam.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      mainparam.secretkey = Utils.encrypt(window.GLOB.execType === 'x' ? '' : mainparam.LText, mainparam.timestamp)
      if (window.GLOB.mainSystemApi) {
        mainparam.rduri = window.GLOB.mainSystemApi
      }
      if (window.GLOB.mkHS) { // 云端数据验证
        mainparam.open_key = Utils.encryptOpenKey(mainparam.secretkey, mainparam.timestamp)
        if (window.GLOB.cloudServiceApi) {
          mainparam.rduri = window.GLOB.cloudServiceApi
          mainparam.userid = sessionStorage.getItem('CloudUserID') || ''
          mainparam.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
        }
      } else if (window.GLOB.mainSystemApi) {
        mainparam.rduri = window.GLOB.mainSystemApi
      }
      deffers.push(
@@ -354,12 +490,12 @@
      delete result.message
      delete result.status
      this.resetSearch(result)
      this.resetSearch(result, false)
    })
  }
  // 测试系统单个请求下拉选项
  improveSimpleSearch = (deForms) => {
  improveSimpleSearch = (deForms, trigger, searchlist) => {
    if (deForms.length === 0) return
    let deffers = deForms.map((item, index) => {
@@ -374,8 +510,12 @@
        param.BID = this.props.BID
      }
      if (window.GLOB.execType === 'x') {
        param.exec_type = 'x'
      }
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      param.secretkey = Utils.encrypt(param.LText, param.timestamp)
      param.secretkey = Utils.encrypt(window.GLOB.execType === 'x' ? '' : param.LText, param.timestamp)
      if (item.database === 'sso' && window.GLOB.mainSystemApi) {
        param.rduri = window.GLOB.mainSystemApi
@@ -411,12 +551,13 @@
      delete result.message
      delete result.status
      this.resetSearch(result)
      this.resetSearch(result, trigger, searchlist)
    })
  }
  resetSearch = (result) => {
    let _searchlist = fromJS(this.state.searchlist).toJS().map(item => {
  resetSearch = (result, submit, searchlist) => {
    let trigger = submit
    let _searchlist = fromJS(searchlist || this.state.searchlist).toJS().map(item => {
      if (['select', 'link', 'multiselect', 'checkcard', 'radio'].includes(item.type) && result[item.field] && result[item.field].length > 0) {
        let options = []
        let map = new Map()
@@ -481,12 +622,27 @@
        item.options = item.oriOptions
      }
      if (item.$first) {
        if (item.options.length > 0) {
          item.initval = item.options[0].Value
          trigger = true
        } else {
          item.initval = ''
        }
      }
      return item
    })
    this.setState({
      searchlist: _searchlist
    })
    if (trigger) {
      setTimeout(() => {
        this.handleSubmit()
      }, 20)
    }
  }
  recordChange = (val, defer, item) => {
@@ -545,7 +701,7 @@
      } else if (item.type === 'select' || item.type === 'link' || item.type === 'multiselect') {
        content = (<MKSelect config={item} onChange={(val, defer) => this.recordChange(val, defer, item)} />)
      } else if (item.type === 'date' || item.type === 'datemonth' || item.type === 'dateweek' || item.type === 'daterange') {
        content = (<MKDatePicker config={item} onChange={(val) => this.recordChange(val, false, item)} />)
        content = (<MKDatePicker config={item} onChange={(val, defer) => this.recordChange(val, defer, item)} />)
      } else if (item.type === 'group') {
        field = item.datefield
        content = <DateGroup position={index} config={item} onChange={(val, type) => this.dateGroupChange(val, type, item)} />
@@ -557,6 +713,8 @@
        content = <MKCheck config={item} onChange={(val) => this.recordChange(val, false, item)} />
      } else if (item.type === 'switch') {
        content = <MKSwitch config={item} onChange={(val) => this.recordChange(val, false, item)} />
      } else if (item.type === 'range') {
        content = <MKNumber config={item} onInputSubmit={this.handleSubmit} />
      }
      if (content) {
@@ -621,7 +779,7 @@
      if (visible) {
        let advanceValues = []
        this.state.searchlist.forEach(item => {
          if (!item.advanced) return
          if (!item.signValue) return
    
          let val = this.record[item.field]
          if (val || val === 0) {
@@ -632,9 +790,18 @@
                val = val + ':00'
              }
            }
            if (['text', 'date', 'datemonth', 'dateweek', 'daterange'].includes(item.type)) {
              advanceValues.push({field: item.field, type: item.type, label: item.label, value: val})
            let text = val
            if (item.type === 'select' || item.type === 'link' || item.type === 'radio') {
              item.oriOptions.forEach(cell => {
                if (cell.Value === val) {
                  text = cell.Text
                }
              })
            }
            advanceValues.push({field: item.field, type: item.type, label: item.label, value: val, text})
          }
        })
        this.setState({advanceValues})
@@ -652,6 +819,8 @@
  }
  handleSubmit = () => {
    const { config } = this.props
    this.setState({}, () => {
      this.props.form.validateFields((err, values) => {
        if (err) return
@@ -689,11 +858,20 @@
          this.sign = ''
        }, 2000)
        
        if (config.wrap && config.wrap.cacheSearch === 'true') {
          let _values = {}
          searches.forEach(item => {
            _values[item.key] = item.value
          })
          window.GLOB.SearchBox.set(config.$searchId + 'cache', _values)
        }
        if (this.props.refreshdata) {
          this.props.refreshdata(searches)
        } else {
          window.GLOB.SearchBox.set(this.props.config.$searchId, searches)
          MKEmitter.emit('searchRefresh', this.props.config.$searchId)
          window.GLOB.SearchBox.set(config.$searchId, searches)
          MKEmitter.emit('searchRefresh', config.$searchId)
        }
      })
    })
@@ -710,7 +888,7 @@
    let searchlist = this.state.searchlist.map(item => {
      item.initval = item.oriInitval
      if (setting.resetContrl === 'clear' && ['text', 'date', 'datemonth', 'dateweek', 'daterange'].includes(item.type)) {
      if (setting.resetContrl === 'clear' && ['text', 'date', 'datemonth', 'dateweek', 'daterange', 'range'].includes(item.type)) {
        item.initval = ''
      }
@@ -720,7 +898,7 @@
      } else {
        record[item.field] = item.initval
      }
      if (item.advanced && item.initval) {
      if (item.signValue && item.initval) {
        let val = item.initval
        if (item.precision === 'hour') {
          if (/,/ig.test(val)) {
@@ -729,9 +907,18 @@
            val = val + ':00'
          }
        }
        if (['text', 'date', 'datemonth', 'dateweek', 'daterange'].includes(item.type)) {
          advanceValues.push({field: item.field, type: item.type, label: item.label, value: val})
        let text = val
        if (item.type === 'select' || item.type === 'link' || item.type === 'radio') {
          item.oriOptions.forEach(cell => {
            if (cell.Value === val) {
              text = cell.Text
            }
          })
        }
        advanceValues.push({field: item.field, type: item.type, label: item.label, value: val, text})
      }
      return item
@@ -835,7 +1022,7 @@
    let advanceValues = []
    this.state.searchlist.forEach(item => {
      if (!item.advanced) return
      if (!item.signValue) return
      let val = this.record[item.field]
      if (val || val === 0) {
@@ -846,9 +1033,18 @@
            val = val + ':00'
          }
        }
        if (['text', 'date', 'datemonth', 'dateweek', 'daterange'].includes(item.type)) {
          advanceValues.push({field: item.field, type: item.type, label: item.label, value: val})
        let text = val
        if (item.type === 'select' || item.type === 'link' || item.type === 'radio') {
          item.oriOptions.forEach(cell => {
            if (cell.Value === val) {
              text = cell.Text
            }
          })
        }
        advanceValues.push({field: item.field, type: item.type, label: item.label, value: val, text})
      }
    })
@@ -886,7 +1082,7 @@
                return (
                  <div key={index}>
                    <span>{item.label}: </span>
                    <span className="advance-value">{item.value}</span>
                    <span className="advance-value">{item.text}</span>
                    <CloseOutlined onClick={() => this.closeAdvanceForm(item)} />
                  </div>)
              })}