king
2024-04-03 13fcf5880bd38bafe92f0b4a3ede7fd7d5e2448b
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, BID } = this.props
    const { config } = this.props
    let _searchlist = []
    let fieldMap = new Map()
@@ -53,6 +54,7 @@
    let record = {}
    let forbid = false // header中不设置高级搜索
    let _setting = {showAdv: false, show: false, style: null}
    let BID = this.props.BID
    
    if (config.wrap) {
      _setting.show = config.wrap.show !== 'false'
@@ -65,6 +67,15 @@
      _setting.wrapperCol = {style: {width: (100 - _setting.labelwidth) + '%'}}
      _setting.borderRadius = config.wrap.borderRadius
      _setting.resetContrl = config.wrap.resetContrl || 'init'
      if (config.wrap.searchBtn === 'show') {
        _setting.showBtn = true
      }
      if (config.wrap.supModule) {
        let BData = window.GLOB.CacheData.get(config.wrap.supModule)
        BID = BData ? (BData.$BID || '') : ''
      }
    }
    
    if (config.type === 'search') {
@@ -121,6 +132,40 @@
        item.advanced = false
      }
      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 (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')
          }
        }
      }
      if (item.type === 'group') {
        record[item.field] = item.initType
        record[item.datefield] = item.initval
@@ -144,24 +189,34 @@
        if (item.resourceType === '1' && item.dataSource) {
          let _option = Utils.getSelectQueryOptions(item)
          _option.sql = _option.sql.replace(/@BID@/ig, `'${BID || ''}'`)
          let exec = true
          if (item.checkBid) {
            item.sql = _option.sql
            item.arr_field = _option.field
          if (window.GLOB.debugger === true) {
            console.info(_option.sql)
            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`)
              }
            }
          }
        }
@@ -233,11 +288,107 @@
      searchlist: _list
    }, () => {
      if (!window.GLOB.mkHS && window.GLOB.sysType === 'local' && window.GLOB.systemType !== 'production') {
        this.improveSimpleSearch(deForms)
        this.improveSimpleSearch(deForms, false, null, BID)
      } else if (mainItems.length > 0 || localItems.length > 0) {
        this.improveSearch(mainItems, localItems)
        this.improveSearch(mainItems, localItems, BID)
      }
    })
  }
  componentDidMount () {
    const { config } = this.props
    if (config.type === 'search' && config.wrap.supModule) {
      MKEmitter.addListener('resetSelectLine', this.resetParentParam)
    }
  }
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    const { config, BID } = this.props
    if (config.checkBid && config.type !== 'search' && nextProps.BID !== BID) {
      this.resetOptions(nextProps.BID)
    }
  }
  resetParentParam = (MenuID, id) => {
    const { config } = this.props
    if (config.wrap.supModule !== MenuID) return
    this.resetOptions(id)
  }
  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, BID)
    } else {
      this.setState({
        searchlist: searchlist
      })
      setTimeout(() => {
        this.handleSubmit()
      }, 20)
    }
  }
  resetCheckcard = (item) => {
@@ -273,8 +424,7 @@
  }
  // 查询下拉菜单
  improveSearch = (mainItems, localItems) => {
    const { BID } = this.props
  improveSearch = (mainItems, localItems, BID) => {
    let deffers = []
    // 本地请求
@@ -291,9 +441,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)
@@ -329,9 +483,13 @@
    }
    if (mainparam.LText) {
      mainparam.LText = Utils.formatOptions(mainparam.LText)
      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(mainparam.LText, mainparam.timestamp)
      mainparam.secretkey = Utils.encrypt(window.GLOB.execType === 'x' ? '' : mainparam.LText, mainparam.timestamp)
      if (window.GLOB.mainSystemApi) {
        mainparam.rduri = window.GLOB.mainSystemApi
@@ -364,12 +522,12 @@
      delete result.message
      delete result.status
      this.resetSearch(result)
      this.resetSearch(result, false)
    })
  }
  // 测试系统单个请求下拉选项
  improveSimpleSearch = (deForms) => {
  improveSimpleSearch = (deForms, trigger, searchlist, BID) => {
    if (deForms.length === 0) return
    let deffers = deForms.map((item, index) => {
@@ -380,12 +538,16 @@
        arr_field: item.arr_field
      }
      if (this.props.BID) {
        param.BID = this.props.BID
      if (BID) {
        param.BID = 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
@@ -421,13 +583,13 @@
      delete result.message
      delete result.status
      this.resetSearch(result)
      this.resetSearch(result, trigger, searchlist)
    })
  }
  resetSearch = (result) => {
    let trigger = false
    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()
@@ -439,11 +601,11 @@
          }
          if (item.linkField) {
            _item.ParentID = cell[item.linkField]
            _item.ParentID = cell[item.linkField] + ''
          }
          if (item.type !== 'checkcard') {
            _item.Value = cell[item.valueField]
            _item.Value = cell[item.valueField] + ''
            _item.Text = cell[item.valueText] + ''
            
            if (map.has(_item.ParentID + _item.Value)) return
@@ -458,7 +620,7 @@
            
            map.set(_item.ParentID + _item.Value, 0)
          } else {
            _item.$value = cell[item.cardValField]
            _item.$value = cell[item.cardValField] + ''
            _item = {..._item, ...cell}
            if (item.urlField) {
@@ -492,9 +654,13 @@
        item.options = item.oriOptions
      }
      if (item.$first && item.options.length > 0) {
        item.initval = item.options[0].Value
        trigger = true
      if (item.$first) {
        if (item.options.length > 0) {
          item.initval = item.options[0].Value
          trigger = true
        } else {
          item.initval = ''
        }
      }
      return item
@@ -507,7 +673,7 @@
    if (trigger) {
      setTimeout(() => {
        this.handleSubmit()
      }, 10)
      }, 20)
    }
  }
@@ -567,7 +733,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)} />
@@ -579,6 +745,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) {
@@ -622,6 +790,16 @@
            {setting.showAdv ? <Button className={visible ? 'visible' : ''} type="link" onClick={this.handleAdvance}>
              高级{setting.advanceType === 'pulldown' ? <DownOutlined /> : null}
            </Button> : null}
          </Form.Item>
        </Col>
      )
    } else if (setting.showBtn) {
      fields.push(
        <Col className="mk-search-col search-button" key="actions">
          <Form.Item>
            <Button type="primary" onClick={this.handleSubmit}>
              搜索
            </Button>
          </Form.Item>
        </Col>
      )
@@ -752,7 +930,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 = ''
      }