king
2024-08-25 ae170a9d58b4f91a225eada1dc83ed4a116b8d50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { DndProvider } from 'react-dnd'
import HTML5Backend from 'react-dnd-html5-backend'
import { Button, Modal, Collapse, notification, Switch } from 'antd'
import { SettingOutlined, LeftOutlined, SearchOutlined, PlusOutlined } from '@ant-design/icons'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
import { getSearchForm } from '@/templates/zshare/formconfig'
import SourceElement from '@/templates/modalconfig/dragelement/source'
import SettingForm from './settingform'
import asyncComponent from '@/utils/asyncComponent'
import { SearchItems } from './source'
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
const { Panel } = Collapse
const { confirm } = Modal
const SearchForm = asyncComponent(() => import('@/templates/sharecomponent/searchcomponent/searchform'))
// const PasteComponent = asyncComponent(() => import('./pastecomponent'))
const DragElement = asyncComponent(() => import('./searchdragelement'))
const GDragElement = asyncComponent(() => import('./groupdragelement'))
const GroupForm = asyncComponent(() => import('./groupform'))
 
class ComModalConfig extends Component {
  static propTpyes = {
    btn: PropTypes.object,
    handleSave: PropTypes.func,
    handleBack: PropTypes.func
  }
 
  state = {
    config: null,          // 页面配置,包括模板类型、模态框设置、添加表名、表单列表
    visible: false,        // 表单编辑模态框,显示控制
    formlist: null,        // 表单编辑模态框,可编辑字段
    card: null,            // 编辑元素
    settingVisible: false, // 全局配置模态框
    originConfig: null,    // 原始菜单
    sqlVerifing: false,    // sql验证
    showField: false,      // 显示表单字段值
    group: null,
    editGroup: null,
    saving: false
  }
 
  /**
   * @description 数据预处理
   */
  UNSAFE_componentWillMount () {
    const { config } = this.props
 
    this.setState({
      group: fromJS(config).toJS(),
      config: fromJS(config).toJS(),
      originConfig: fromJS(config).toJS()
    })
  }
 
  componentDidMount () {
    MKEmitter.addListener('completeSave', this.completeSave)
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('completeSave', this.completeSave)
  }
 
  completeSave = () => {
    this.setState({saving: false})
  }
 
  /**
   * @description 表单变化
   */
  handleList = (list, newcard) => {
    let _group = {...this.state.group, fields: list}
 
    this.setState({
      group: _group
    }, () => {
      if (newcard) {
        this.handleForm(newcard)
      }
    })
    this.resetConfig(_group)
  }
 
  /**
   * @description 分组变化
   */
  handleGroupList = (list) => {
    let _group = {...this.state.group, groups: list}
 
    this.setState({
      group: _group
    })
    this.resetConfig(_group)
  }
 
  /**
   * @description 表单变化
   */
  handleList = (list, newcard) => {
    let _group = {...this.state.group, fields: list}
 
    this.setState({
      group: _group
    }, () => {
      if (newcard) {
        this.handleForm(newcard)
      }
    })
    this.resetConfig(_group)
  }
 
  resetConfig = (group) => {
    let _config = fromJS(this.state.config).toJS()
    if (group.floor === 1) {
      _config = group
    } else {
      _config.groups = _config.groups.map(item => {
        if (item.uuid === group.uuid) return group
        return item
      })
    }
 
    this.setState({
      config: _config
    })
  }
 
  /**
   * @description 表单编辑
   */
  handleForm = (_card) => {
    const { group } = this.state
    let card = fromJS(_card).toJS()
    let linkableFields = []
 
    group.fields.forEach(item => {
      if (item.uuid === card.uuid) return
      if (!['select', 'link', 'checkcard'].includes(item.type)) return
      if (item.type === 'checkcard' && item.multiple === 'true') return
      
      linkableFields.push({
        value: item.field,
        text: item.label
      })
    })
 
    this.setState({
      visible: true,
      card: card,
      formlist: getSearchForm(card, linkableFields)
    })
  }
 
  /**
   * @description 编辑后提交
   * 1、获取编辑后的表单信息
   * 2、去除可能存在的示例表单
   * 3、通过loading刷新
   */
  handleSubmit = () => {
    const { config } = this.state
    this.formRef.handleConfirm().then(res => {
      let _group = fromJS(this.state.group).toJS()
      
      _group.fields = _group.fields.map(item => {
        if (item.uuid === res.uuid) {
          return res
        } else {
          return item
        }
      })
 
      let fieldrepet = false // 字段重复
      let lowerField = res.field.toLowerCase()
 
      if (config.setting.field) {
        let m = config.setting.field.toLowerCase().split(',')
        if (m.includes(lowerField)) {
          fieldrepet = true
        }
      }
      config.fields.forEach(item => {
        if (item.uuid === res.uuid) return
        if (res.type === 'date' && item.type === 'date') return
        if (item.field.toLowerCase() === lowerField) {
          fieldrepet = true
        }
      })
 
      config.groups.forEach(group => {
        if (group.setting.field) {
          let m = group.setting.field.toLowerCase().split(',')
          if (m.includes(lowerField)) {
            fieldrepet = true
          }
        }
        group.fields.forEach(item => {
          if (item.uuid === res.uuid) return
          if (res.type === 'date' && item.type === 'date') return
          if (item.field.toLowerCase() === lowerField) {
            fieldrepet = true
          }
        })
      })
 
      if (fieldrepet) {
        notification.warning({
          top: 92,
          message: '字段已存在!',
          duration: 10
        })
        return
      }
 
      if (['checkcard'].includes(res.type) && res.resourceType === '1' && res.database !== 'sso' && /\s/.test(res.dataSource)) {
        this.setState({
          sqlVerifing: true
        })
 
        let sql = `declare @mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20)
        ${res.dataSource}`
        sql = sql.replace(/@\$|\$@/ig, '')
        
        Api.sDebug(sql).then(result => {
          if (result.status || result.ErrCode === '-2') {
            this.setState({
              sqlVerifing: false,
              group: _group,
              card: null,
              visible: false
            })
            this.resetConfig(_group)
          } else {
            this.setState({sqlVerifing: false})
            
            Modal.error({
              title: result.message
            })
          }
        })
      } else {
        this.setState({
          group: _group,
          card: null,
          visible: false
        })
        this.resetConfig(_group)
      }
    })
  }
 
  /**
   * @description 表单删除并刷新
   */
  closeForm = (card) => {
    let that = this
 
    confirm({
      content: `确定删除${card.label ? `<<${card.label}>>` : ''}吗?`,
      onOk() {
        let _group = fromJS(that.state.group).toJS()
        _group.fields = _group.fields.filter(item => item.uuid !== card.uuid)
 
        that.setState({
          group: _group,
        })
        that.resetConfig(_group)
      },
      onCancel() {}
    })
  }
 
  submitConfig = () => {
    const { config } = this.state
 
    this.setState({originConfig: fromJS(config).toJS(), saving: true})
    this.props.handleSave(config)
 
    setTimeout(() => {
      MKEmitter.emit('triggerMenuSave')
    }, 100)
  }
 
  cancelConfig = () => {
    const { config, originConfig } = this.state
 
    if (!is(fromJS(config), fromJS(originConfig))) {
      let that = this
      confirm({
        content: '配置信息未保存,确定返回吗?',
        onOk() {
          that.props.handleBack()
        },
        onCancel() {}
      })
    } else {
      this.props.handleBack()
    }
  }
 
  /**
   * @description 分组设置模态框
   */
  changeSetting = () => {
    this.setState({
      settingVisible: true
    })
  }
 
  /**
   * @description 保存分组设置
   */
  settingSave = () => {
    const { config } = this.state
    this.settingRef.handleConfirm().then(res => {
      let _group = {...this.state.group, setting: res}
      let lowers = _group.setting.field ? _group.setting.field.toLowerCase() : ''
 
      if (lowers) {
        let fieldrepet = false // 字段重复
        lowers = lowers.split(',')
        let length = lowers.length
 
        if (_group.floor !== 1 && config.setting.field) {
          let m = config.setting.field.toLowerCase().split(',')
          if (Array.from(new Set([...m, ...lowers])).length < m.length + length) {
            fieldrepet = true
          }
        }
 
        config.fields.forEach(item => {
          if (lowers.includes(item.field.toLowerCase())) {
            fieldrepet = true
          }
        })
 
        config.groups.forEach(group => {
          if (_group.uuid === group.uuid) return
          if (group.setting.field) {
            let m = group.setting.field.toLowerCase().split(',')
            if (Array.from(new Set([...m, ...lowers])).length < m.length + length) {
              fieldrepet = true
            }
          }
          group.fields.forEach(item => {
            if (lowers.includes(item.field.toLowerCase())) {
              fieldrepet = true
            }
          })
        })
 
        if (fieldrepet) {
          notification.warning({
            top: 92,
            message: '字段已存在!',
            duration: 10
          })
          return
        }
      }
 
      this.setState({
        group: _group,
        settingVisible: false
      })
      this.resetConfig(_group)
    })
  }
 
  editModalCancel = () => {
    const { group, card } = this.state
 
    if (card.focus) {
      let _fields = group.fields.filter(item => item.uuid !== card.uuid)
      let _group = {...group, fields: _fields}
 
      this.setState({
        card: null,
        group: _group,
        visible: false
      })
      this.resetConfig(_group)
    } else {
      this.setState({
        card: null,
        visible: false
      })
    }
  }
 
  plusGroup = () => {
    let config = fromJS(this.state.config).toJS()
    let _g = {
      uuid: Utils.getuuid(),
      wrap: { name: 'name', icon: '' },
      setting: { type: 'title', field: '', title: '', focus: 'true', btn: 'hidden', backgroundColor: sessionStorage.getItem('sysBgColor') },
      fields: []
    }
 
    config.groups.push(_g)
 
    this.setState({config, group: config, editGroup: _g, gVisible: true})
  }
 
  handleGroupForm = (_g) => {
    this.setState({editGroup: _g, gVisible: true})
  }
 
   /**
   * @description 保存分组设置
   */
  groupSave = () => {
    this.gRef.handleConfirm().then(res => {
      let _g = {...this.state.editGroup, wrap: res}
 
      let _group = fromJS(this.state.group).toJS()
      _group.groups = _group.groups.map(item => {
        if (item.uuid === _g.uuid) return _g
        return item
      })
 
      this.setState({
        editGroup: null,
        group: _group,
        gVisible: false
      })
      this.resetConfig(_group)
    })
  }
 
  handleGroup = (g) => {
    this.setState({
      group: g
    })
  }
 
  closeGroup = (g) => {
    const that = this
    let _group = fromJS(this.state.group).toJS()
    _group.groups = _group.groups.filter(item => item.uuid !== g.uuid)
 
    confirm({
      content: `确定删除分组《${g.wrap.name}》吗?`,
      onOk() {
        that.setState({ group: _group })
        that.resetConfig(_group)
      },
      onCancel() {}
    })
  }
 
  returnUp = () => {
    const { config } = this.state
 
    this.setState({
      group: config
    })
  }
 
  render () {
    const { group, editGroup, saving } = this.state
 
    return (
      <div className="mob-search-board">
        <DndProvider backend={HTML5Backend}>
          <div className="tools">
            <Collapse accordion defaultActiveKey="1" bordered={false}>
              <Panel header="搜索条件" key="1">
                <div className="search-element">
                  {SearchItems.map((item, index) => {
                    return (<SourceElement key={index} content={item}/>)
                  })}
                </div>
              </Panel>
            </Collapse>
          </div>
          <div className="modal-control">
            <Button type="primary" loading={saving} onClick={this.submitConfig}>保存</Button>
            <Button onClick={this.cancelConfig}>关闭</Button>
            {!group.floor ? <Button onClick={this.returnUp}>返回上级</Button> : null}
            {/* <PasteComponent insert={this.insert} /> */}
            <Switch checkedChildren="开" unCheckedChildren="关" defaultChecked={this.state.showField} onChange={(val) => this.setState({showField: val})} />
          </div>
          <div className="setting">
            <div className="mob-shell" style={{width: window.GLOB.shellWidth, height: window.GLOB.shellHeight}}>
              <div className="mob-shell-inner" style={{backgroundColor: group.setting.backgroundColor || '#ffffff'}}>
                <SettingOutlined className="setting-group" onClick={this.changeSetting}/>
                {group.setting.type === 'title' ? <div className="am-navbar">
                  <LeftOutlined />
                  <div className="am-navbar-title">{group.setting.title}</div>
                </div> : <div className="am-navbar">
                  {/* <LeftOutlined /> */}
                  <div className="search-bar"><SearchOutlined style={{marginRight: '10px'}}/>{group.setting.label || ''}</div>
                  <Button >取消</Button>
                </div>}
                {group.floor === 1 ? <PlusOutlined className="plus-group" onClick={this.plusGroup} /> : null}
                <div style={{minHeight: 'calc(100% - 100px)'}}>
                  {group.floor === 1 && group.groups.length > 0 ? <GDragElement
                    list={group.groups}
                    handleList={this.handleGroupList}
                    handleForm={this.handleGroupForm}
                    handleGroup={this.handleGroup}
                    closeForm={this.closeGroup}
                  /> : null}
                  <DragElement
                    list={group.fields}
                    showField={this.state.showField}
                    handleList={this.handleList}
                    handleForm={this.handleForm}
                    closeForm={this.closeForm}
                  />
                </div>
                {group.setting.btn !== 'hidden' ? <div className="search-btn">
                  <Button className="reset">重置</Button>
                  <Button className="submit" type="primary">确定</Button>
                </div> : null}
              </div>
            </div>
          </div>
        </DndProvider>
        <Modal
          title="编辑"
          visible={this.state.visible}
          width={950}
          onCancel={this.editModalCancel}
          onOk={this.handleSubmit}
          confirmLoading={this.state.sqlVerifing}
          destroyOnClose
        >
          <SearchForm
            card={this.state.card}
            formlist={this.state.formlist}
            inputSubmit={this.handleSubmit}
            wrappedComponentRef={(inst) => this.formRef = inst}
          />
        </Modal>
        <Modal
          title="编辑"
          visible={this.state.settingVisible}
          width={850}
          maskClosable={false}
          onOk={this.settingSave}
          onCancel={() => { this.setState({ settingVisible: false }) }}
          destroyOnClose
        >
          <SettingForm
            config={group}
            inputSubmit={this.settingSave}
            wrappedComponentRef={(inst) => this.settingRef = inst}
          />
        </Modal>
        <Modal
          title="编辑"
          visible={this.state.gVisible}
          width={600}
          maskClosable={false}
          onOk={this.groupSave}
          onCancel={() => { this.setState({ gVisible: false }) }}
          destroyOnClose
        >
          <GroupForm
            config={editGroup}
            inputSubmit={this.groupSave}
            wrappedComponentRef={(inst) => this.gRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
export default ComModalConfig