import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Form, Row, Col, Input, Button, Select, DatePicker, notification } from 'antd'
|
import moment from 'moment'
|
|
import Api from '@/api'
|
import options from '@/store/options.js'
|
import DateGroup from '@/tabviews/zshare/dategroup'
|
import Utils from '@/utils/utils.js'
|
import './index.scss'
|
|
const {MonthPicker, WeekPicker, RangePicker} = DatePicker
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
BID: PropTypes.any, // 父级Id,用于查询下拉选择项
|
menuType: PropTypes.any, // 菜单权限,是否为HS
|
searchlist: PropTypes.array, // 搜索条件列表
|
dict: PropTypes.object // 字典项
|
}
|
|
state = {
|
match: null, // 搜索条件匹配规则
|
style: null, // 搜索条件类型
|
label: null, // 提示文字
|
required: null, // 是否必填
|
searchlist: null, // 搜索项
|
groups: null, // 组合搜索项
|
formId: Utils.getuuid() // 搜索表单Id
|
}
|
|
UNSAFE_componentWillMount () {
|
let searchlist = fromJS(this.props.searchlist).toJS()
|
let match = {}
|
let label = {}
|
let style = {}
|
let required = {}
|
let _list = []
|
let fieldMap = new Map()
|
let mainItems = [] // 云端或单点数据
|
let localItems = [] // 本地数据
|
let deForms = [] // 测试系统,单个请求
|
|
searchlist.forEach(item => {
|
if (fieldMap.has(item.field)) {
|
item.field = item.field + '@tail@'
|
}
|
fieldMap.set(item.field, true)
|
|
match[item.field] = item.match
|
label[item.field] = item.label
|
style[item.field] = item.type
|
required[item.field] = item.required === 'true'
|
|
if (['select', 'link', 'multiselect'].includes(item.type)) {
|
if (item.setAll === 'true' && item.type !== 'multiselect') {
|
item.options.unshift({
|
key: Utils.getuuid(),
|
Value: '',
|
Text: this.props.dict['main.all']
|
})
|
}
|
|
// 数据源查询语句
|
if (item.resourceType === '1' && item.dataSource) {
|
let _option = Utils.getSelectQueryOptions(item)
|
|
if (sessionStorage.getItem('dataM') === 'true') { // 数据权限
|
_option.sql = _option.sql.replace(/\$@/ig, '/*')
|
_option.sql = _option.sql.replace(/@\$/ig, '*/')
|
} else {
|
_option.sql = _option.sql.replace(/@\$|\$@/ig, '')
|
}
|
|
// 测试系统单个请求
|
if (this.props.menuType !== 'HS' && options.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))}' as LText`)
|
} else {
|
localItems.push(`select '${item.field}' as obj_name,'${_option.field}' as arr_field,'${window.btoa(window.encodeURIComponent(_option.sql))}' as LText`)
|
}
|
}
|
}
|
item.oriOptions = fromJS(item.options).toJS()
|
}
|
|
_list.push(item)
|
})
|
|
let _groups = []
|
_list = _list.map(item => {
|
if (item.type === 'link') {
|
let supItem = _list.filter(form => form.field === item.linkField)[0]
|
|
if (!supItem) {
|
notification.warning({
|
top: 92,
|
message: '未查询到搜索条件《' + item.label + '》关联字段!',
|
duration: 5
|
})
|
item.supInitVal = ''
|
} else {
|
item.supInitVal = supItem.initval
|
item.options = item.oriOptions.filter(option => option.ParentID === supItem.initval)
|
}
|
} else if (item.type === 'group' && item.Hide !== 'true') {
|
_groups.push(fromJS(item).toJS())
|
}
|
|
return item
|
})
|
|
this.setState({
|
match: match,
|
label: label,
|
style: style,
|
required: required,
|
searchlist: _list,
|
groups: _groups
|
}, () => {
|
if (this.props.menuType !== 'HS' && options.sysType === 'local' && !window.GLOB.systemType) {
|
this.improveSimpleSearch(deForms)
|
} else {
|
this.improveSearch(mainItems, localItems)
|
}
|
})
|
}
|
|
// 查询下拉菜单
|
improveSearch = (mainItems, localItems) => {
|
const { menuType, BID } = this.props
|
let deffers = []
|
|
if (menuType !== 'HS' && window.GLOB.systemType !== 'production') {
|
localItems = [...localItems, ...mainItems]
|
mainItems = []
|
}
|
|
// 本地请求
|
let param = {
|
func: 'sPC_Get_SelectedList',
|
LText: localItems.join(' union all '),
|
obj_name: '',
|
arr_field: '',
|
table_type: 'Y'
|
}
|
|
if (BID) {
|
param.BID = BID
|
}
|
|
if (param.LText) {
|
param.LText = Utils.formatOptions(param.LText)
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
if (menuType === 'HS') { // 云端数据验证
|
param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
|
}
|
|
deffers.push(
|
new Promise(resolve => {
|
Api.getSystemCacheConfig(param).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
}
|
resolve(res)
|
})
|
})
|
)
|
}
|
|
// 系统请求
|
let mainparam = {
|
func: 'sPC_Get_SelectedList',
|
LText: mainItems.join(' union all '),
|
obj_name: '',
|
arr_field: '',
|
table_type: 'Y'
|
}
|
|
if (BID) {
|
mainparam.BID = BID
|
}
|
|
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 (menuType === 'HS') { // 云端数据验证
|
mainparam.open_key = Utils.encryptOpenKey(mainparam.secretkey, mainparam.timestamp)
|
if (options.cloudServiceApi) {
|
mainparam.rduri = options.cloudServiceApi
|
mainparam.userid = sessionStorage.getItem('CloudUserID') || ''
|
mainparam.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
|
}
|
} else if (window.GLOB.mainSystemApi) {
|
mainparam.rduri = window.GLOB.mainSystemApi
|
}
|
|
deffers.push(
|
new Promise(resolve => {
|
Api.getSystemCacheConfig(mainparam).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
}
|
resolve(res)
|
})
|
})
|
)
|
}
|
|
Promise.all(deffers).then(response => {
|
let result = {...response[0], ...(response[1] || {})}
|
|
delete result.ErrCode
|
delete result.ErrMesg
|
delete result.message
|
delete result.status
|
|
let _searchlist = this.state.searchlist.map(item => {
|
if (['select', 'link', 'multiselect'].includes(item.type) && result[item.field] && result[item.field].length > 0) {
|
let options = result[item.field].map(cell => {
|
let _item = {
|
key: Utils.getuuid(),
|
Value: cell[item.valueField],
|
Text: cell[item.valueText]
|
}
|
|
if (item.type === 'link') {
|
_item.ParentID = cell[item.linkField]
|
}
|
|
return _item
|
})
|
|
item.oriOptions = [...item.oriOptions, ...options]
|
}
|
return item
|
})
|
|
this.setState({
|
searchlist: _searchlist.map(item => {
|
if (item.type === 'link') {
|
if (item.supInitVal) {
|
item.options = item.oriOptions.filter(option => option.ParentID === item.supInitVal)
|
} else {
|
item.options = item.oriOptions
|
}
|
} else if (item.type === 'select' || item.type === 'multiselect') {
|
item.options = item.oriOptions
|
}
|
|
return item
|
})
|
})
|
})
|
}
|
|
// 测试系统单个请求下拉选项
|
improveSimpleSearch = (deForms) => {
|
if (deForms.length === 0) return
|
|
let deffers = deForms.map(item => {
|
let param = {
|
func: 'sPC_Get_SelectedList',
|
LText: item.data_sql,
|
obj_name: item.field,
|
arr_field: item.arr_field
|
}
|
|
if (this.props.BID) {
|
param.BID = this.props.BID
|
}
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
return (
|
new Promise(resolve => {
|
Api.getSystemCacheConfig(param).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
}
|
resolve(res)
|
})
|
})
|
)
|
})
|
|
Promise.all(deffers).then(response => {
|
let result = {}
|
|
response.forEach(res => {
|
result = {...result, ...res}
|
})
|
|
delete result.ErrCode
|
delete result.ErrMesg
|
delete result.message
|
delete result.status
|
|
let _searchlist = this.state.searchlist.map(item => {
|
if (['select', 'link', 'multiselect'].includes(item.type) && result[item.field] && result[item.field].length > 0) {
|
let options = result[item.field].map(cell => {
|
let _item = {
|
key: Utils.getuuid(),
|
Value: cell[item.valueField],
|
Text: cell[item.valueText]
|
}
|
|
if (item.type === 'link') {
|
_item.ParentID = cell[item.linkField]
|
}
|
|
return _item
|
})
|
|
item.oriOptions = [...item.oriOptions, ...options]
|
}
|
return item
|
})
|
|
this.setState({
|
searchlist: _searchlist.map(item => {
|
if (item.type === 'link') {
|
if (item.supInitVal) {
|
item.options = item.oriOptions.filter(option => option.ParentID === item.supInitVal)
|
} else {
|
item.options = item.oriOptions
|
}
|
} else if (item.type === 'select' || item.type === 'multiselect') {
|
item.options = item.oriOptions
|
}
|
|
return item
|
})
|
})
|
})
|
}
|
|
resetform = (formlist, supfields, index, fieldsvalue) => {
|
index++
|
let subfields = []
|
|
supfields.forEach(supfield => {
|
formlist = formlist.map(item => {
|
if (item.type === 'link' && item.linkField === supfield.field) {
|
item.options = item.oriOptions.filter(option => option.ParentID === supfield.initval)
|
item.initval = item.options[0] ? item.options[0].Value : ''
|
|
if (this.props.form.getFieldValue(item.field) !== undefined) {
|
fieldsvalue[item.field] = item.initval
|
}
|
|
subfields.push(item)
|
}
|
return item
|
})
|
})
|
|
if (subfields.length === 0 || index > 6) {
|
return formlist
|
} else {
|
return this.resetform(formlist, subfields, index, fieldsvalue)
|
}
|
}
|
|
selectChange = (_field, value) => {
|
let formlist = fromJS(this.state.searchlist).toJS()
|
|
let subfields = []
|
let fieldsvalue = {}
|
formlist = formlist.map(item => {
|
if (item.type === 'link' && item.linkField === _field.field) {
|
item.options = item.oriOptions.filter(option => option.ParentID === value)
|
item.initval = item.options[0] ? item.options[0].Value : ''
|
|
if (this.props.form.getFieldValue(item.field) !== undefined) {
|
fieldsvalue[item.field] = item.initval
|
}
|
|
subfields.push(item)
|
}
|
return item
|
})
|
|
if (subfields.length === 0) {
|
this.searchChange()
|
return
|
}
|
|
formlist = this.resetform(formlist, subfields, 0, fieldsvalue)
|
|
if (Object.keys(fieldsvalue).length > 0) {
|
this.props.form.setFieldsValue(fieldsvalue)
|
}
|
|
this.setState({
|
searchlist: formlist
|
}, () => {
|
this.searchChange()
|
})
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
|
this.state.searchlist.forEach((item, index) => {
|
if (item.Hide === 'true') return
|
|
if (item.type === 'text') { // 文本搜索
|
fields.push(
|
<Col span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: item.initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
}
|
]
|
})(<Input placeholder="" autoComplete="off" />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select') { // 下拉搜索
|
fields.push(
|
<Col span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: item.initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Select
|
showSearch
|
onChange={(value) => {this.selectChange(item, value)}}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
getPopupContainer={() => document.getElementById(this.state.formId)}
|
>
|
{item.options.map((option, i) =>
|
<Select.Option id={`${i}`} title={option.Text} key={`${i}`} value={option.Value}>{option.Text}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'multiselect') { // 下拉多选
|
let _initval = item.initval ? item.initval.split(',').filter(Boolean) : []
|
fields.push(
|
<Col span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: _initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Select
|
showSearch
|
mode="multiple"
|
onChange={this.searchChange}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
getPopupContainer={() => document.getElementById(this.state.formId)}
|
>
|
{item.options.map((option, i) =>
|
<Select.Option id={`${i}`} title={option.Text} key={`${i}`} value={option.Value}>{option.Text}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'date') { // 时间搜索
|
fields.push(
|
<Col span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: item.initval ? moment().subtract(item.initval, 'days') : null,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<DatePicker onChange={this.searchChange} getCalendarContainer={() => document.getElementById(this.state.formId)} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'datemonth') {
|
fields.push(
|
<Col span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: item.initval ? moment().subtract(item.initval, 'month') : null,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<MonthPicker onChange={this.searchChange} getCalendarContainer={() => document.getElementById(this.state.formId)} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'dateweek') {
|
fields.push(
|
<Col span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: item.initval ? moment().subtract(item.initval * 7, 'days') : null,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<WeekPicker onChange={this.searchChange} getCalendarContainer={() => document.getElementById(this.state.formId)} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'daterange') {
|
let _defaultValue = [null, null]
|
|
if (item.initval) {
|
try {
|
let _initval = JSON.parse(item.initval)
|
_defaultValue = [moment().subtract(_initval[0], 'days'), moment().subtract(_initval[1], 'days')]
|
} catch {
|
_defaultValue = [null, null]
|
}
|
}
|
|
fields.push(
|
<Col className="daterange" span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field,
|
{
|
initialValue: _defaultValue,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<RangePicker
|
placeholder={['开始日期', '结束日期']}
|
renderExtraFooter={() => 'extra footer'}
|
onChange={this.searchChange}
|
getCalendarContainer={() => document.getElementById(this.state.formId)}
|
/>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'group') {
|
fields.push(
|
<Col span={item.ratio || 6} key={index}>
|
<Form.Item label={item.label} className={item.required === 'true' ? 'group-required' : ''}>
|
<DateGroup ref={item.uuid} position={index} card={item} onGroupChange={this.searchChange} />
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
|
fields.push(
|
<Col span={6} style={{ whiteSpace: 'nowrap' }} key="actions">
|
<Form.Item label={' '} colon={false} style={{ minHeight: '40px' }}>
|
<Button type="primary" htmlType="submit">
|
{this.props.dict['main.search']}
|
</Button>
|
<Button style={{ marginLeft: 8 }} onClick={this.handleReset}>
|
{this.props.dict['main.reset']}
|
</Button>
|
</Form.Item>
|
</Col>
|
)
|
|
return fields
|
}
|
|
addHideFieldValue = (values) => {
|
const { searchlist } = this.state
|
let hideValue = {}
|
searchlist.forEach(item => {
|
if (item.Hide === 'true') {
|
let value = ''
|
|
if (item.type === 'multiselect') { // 下拉多选
|
value = item.initval ? item.initval.split(',').filter(Boolean) : []
|
} else if (item.type === 'date') { // 时间搜索
|
value = item.initval ? moment().subtract(item.initval, 'days') : ''
|
} else if (item.type === 'datemonth') {
|
value = item.initval ? moment().subtract(item.initval, 'month') : ''
|
} else if (item.type === 'dateweek') {
|
value = item.initval ? moment().subtract(item.initval * 7, 'days') : ''
|
} else if (item.type === 'daterange') {
|
if (item.initval) {
|
try {
|
let _initval = JSON.parse(item.initval)
|
value = [moment().subtract(_initval[0], 'days'), moment().subtract(_initval[1], 'days')]
|
} catch {
|
value = ''
|
}
|
}
|
} else if (item.type !== 'group') {
|
value = item.initval
|
}
|
|
hideValue[item.field] = value
|
}
|
})
|
|
return {...hideValue, ...values}
|
}
|
|
handleSearch = (e) => {
|
// 回车或点击搜索
|
e.preventDefault()
|
this.props.form.validateFields((err, values) => {
|
if (!err) {
|
values = this.addHideFieldValue(values)
|
let searches = this.getFieldsValues(values)
|
this.props.refreshdata(searches)
|
}
|
})
|
}
|
|
searchChange = () => {
|
this.setState({}, () => {
|
this.props.form.validateFields((err, values) => {
|
if (!err) {
|
values = this.addHideFieldValue(values)
|
let searches = this.getFieldsValues(values)
|
this.props.refreshdata(searches)
|
}
|
})
|
})
|
}
|
|
/**
|
* @description 搜索条件重置
|
*/
|
handleReset = () => {
|
const { groups } = this.state
|
|
if (groups.length > 0) {
|
groups.forEach(item => {
|
this.refs[item.uuid].reset()
|
})
|
}
|
|
let searchlist = this.state.searchlist.map(item => {
|
item.initval = item.oriInitval
|
return item
|
})
|
|
this.setState({searchlist}, () => {
|
this.props.form.resetFields()
|
this.props.form.validateFields((err, values) => {
|
if (!err) {
|
// 异步获取更新后的时间组
|
this.setState({}, () => {
|
values = this.addHideFieldValue(values)
|
let searches = this.getFieldsValues(values)
|
this.props.refreshdata(searches)
|
})
|
}
|
})
|
})
|
}
|
|
getFieldsValues = (values) => {
|
const { groups } = this.state
|
// 获取搜索条件值
|
let search = []
|
Object.keys(values).forEach(key => {
|
let _value = ''
|
if (this.state.style[key] === 'daterange') {
|
if (values[key].length > 0 && values[key][0] && values[key][1]) {
|
_value = [moment(values[key][0]).format('YYYY-MM-DD'), moment(values[key][1]).format('YYYY-MM-DD')]
|
}
|
} else if (this.state.style[key] === 'dateweek') {
|
if (values[key]) {
|
_value = [moment(values[key]).startOf('week').format('YYYY-MM-DD'), moment(values[key]).endOf('week').format('YYYY-MM-DD')]
|
}
|
} else if (this.state.style[key] === 'date') {
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM-DD')
|
}
|
} else if (this.state.style[key] === 'datemonth') {
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM')
|
}
|
} else if (this.state.style[key] === 'multiselect') {
|
_value = values[key] || []
|
|
} else {
|
_value = (values[key] || values[key] === 0) ? values[key] : ''
|
|
_value = _value.replace(/(^\s*|\s*$)/ig, '')
|
}
|
|
search.push({
|
type: this.state.style[key],
|
key: key.replace(/@tail@$/, ''),
|
value: _value,
|
label: this.state.label[key],
|
match: this.state.match[key],
|
required: this.state.required[key]
|
})
|
})
|
|
if (groups.length > 0) {
|
groups.forEach(item => {
|
let items = this.refs[item.uuid].getSearchItems()
|
search.push(...items)
|
})
|
}
|
|
return search
|
}
|
|
render() {
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 8 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 16 }
|
}
|
}
|
|
return (
|
<Form {...formItemLayout} className="ant-advanced-search-form top-search" id={this.state.formId} onSubmit={this.handleSearch}>
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|