king
2022-07-27 fbf25a99e0bc643be89b0f9dd0efed7867f1490e
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { connect } from 'react-redux'
import { DndProvider } from 'react-dnd'
import { withRouter } from 'react-router-dom'
import HTML5Backend from 'react-dnd-html5-backend'
import { notification, Modal, Button } from 'antd'
import moment from 'moment'
 
import Api from '@/api'
import MKEmitter from '@/utils/events.js'
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import MkIcon from '@/components/mk-icon'
import Utils from '@/utils/utils.js'
import MenuUtils from '@/utils/utils-custom.js'
import DragElement from '../menuelement'
import asyncLoadComponent from '@/utils/asyncLoadComponent'
import './index.scss'
 
const MenuForm = asyncLoadComponent(() => import('../thdmenuform'))
const ComTableConfig = asyncLoadComponent(() => import('@/templates/comtableconfig'))
const TreePageConfig = asyncLoadComponent(() => import('@/templates/treepageconfig'))
const CalendarPageConfig = asyncLoadComponent(() => import('@/templates/calendarconfig'))
const FormTabConfig = asyncLoadComponent(() => import('@/templates/formtabconfig'))
const ModalConfig = asyncLoadComponent(() => import('@/templates/modalconfig'))
const SubTable = asyncLoadComponent(() => import('@/templates/subtableconfig'))
 
const { confirm } = Modal
 
class EditMenu extends Component {
  static propTpyes = {
    menulist: PropTypes.any,     // 三级菜单列表
    exitEdit: PropTypes.func,    // 退出编辑状态
    supMenu: PropTypes.object,   // 对应的上级菜单
    supMenuList: PropTypes.array // 上级菜单列表,用于三级菜单切换上级菜单
  }
 
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    confirmLoading: false,  // 提交中。。。
    loading: false,         // 编辑菜单或使用已使用模板时,获取配置信息
    btnParam: null,         // 编辑按钮的配置信息
    menulist: null,         // 编辑中的菜单
    tabview: '',            // 选择模板窗口(template)、基础表格配置(CommonTable)、表单(Modal)、子表(SubTable)
    editMenu: null,         // 编辑菜单
    editAction: null,       // 编辑按钮
    editTab: null,          // 编辑标签
    tabConfig: null,        // 标签配置信息
    editSubTab: null,       // 编辑子标签(标签中的标签)
    subTabConfig: null,     // 子标签配置信息
    subConfig: null,        // 子配置信息
    btnTab: null,           // 打开新标签或当前页面刷新的按钮
    btnTabConfig: null,     // 打开新标签按钮配置
    handleMVisible: false,  // 添加或修改菜单模态框(角色权限分配等)
    sysMenu: null,          // 添加或编辑菜单(角色权限分配等)
    change: false
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  /**
   * @description 菜单顺序改变时,保存中间状态
   */
  handlePreviewList = (List) => {
    this.setState({
      menulist: List,
      change: !is(fromJS(List), fromJS(this.props.menulist))
    })
  }
 
  /**
   * @description 菜单编辑:修改、删除
   * 1、菜单修改或删除时,先查看菜单顺序是否改变
   * 2、菜单删除
   * 3、菜单编辑,查询菜单配置信息,信息正确,进入对应编辑页面
   */
  handleMenu = (menu) => {
    if (this.state.change) {
      notification.warning({
        top: 92,
        message: this.state.dict['model.menu.presave'],
        duration: 5
      })
      return
    }
    
    if (menu.type === 'close') {
      confirm({
        title: `确定删除菜单《${menu.card.MenuName}》吗?`,
        content: '',
        onOk() {
          let param = {
            func: 'sPC_MainMenu_Del',
            MenuID: menu.card.MenuID
          }
          return Api.getSystemConfig(param).then(res => {
            if (res.status) {
              MKEmitter.emit('mkUpdateMenuList')
            } else {
              notification.warning({
                top: 92,
                message: res.message,
                duration: 5
              })
            }
          })
        },
        onCancel() {}
      })
    } else if (menu.type === 'edit') {
      let _menu = fromJS(menu.card).toJS()
 
      if (['RolePermission', 'NewPage'].includes(_menu.PageParam.Template)) { // 单页面修改
        _menu.Template = _menu.PageParam.Template
        _menu.url = _menu.PageParam.url || ''
 
        _menu.fstMenuId = _menu.FstId
        _menu.supMenuList = this.props.supMenuList
        _menu.fstMenuList = this.props.menuTree
  
        this.setState({
          handleMVisible: true,
          sysMenu: _menu
        })
 
        return
      }
 
      // let _param = ''
      // if (card.type === 'CustomPage') {
      //   _param = {
      //     MenuType: 'custom',
      //     MenuId: card.MenuID,
      //     ParentId: card.ParentId,
      //     MenuName: card.MenuName,
      //     MenuNo: card.MenuNo
      //   }
      //   _param = window.btoa(window.encodeURIComponent(JSON.stringify(_param)))
      // }
      // window.open(`#/menudesign/${_param}`)}
 
      // let param = {
      //   func: 'sPC_Get_LongParam',
      //   MenuID: _menu.MenuID
      // }
 
      // this.setState({
      //   loading: true
      // })
 
      // Api.getSystemConfig(param).then(res => {
      //   if (res.status) {
      //     let _LongParam = ''
      //     if (res.LongParam) {
      //       try {
      //         _LongParam = JSON.parse(window.decodeURIComponent(window.atob(res.LongParam)))
      //       } catch (e) {
      //         console.warn('Parse Failure')
      //         _LongParam = ''
      //       }
      //     }
 
      //     _menu.LongParam = _LongParam
      //     _menu.fstMenuId = _menu.FstId
      //     _menu.supMenuList = this.props.supMenuList
      //     _menu.fstMenuList = this.props.menuTree
      //     _menu.open_edition = res.open_edition || ''
 
          
      //     this.setState({
      //       editMenu: _menu,
      //       loading: false,
      //       tabview: _menu.PageParam.Template
      //     }, () => {
      //       document.getElementById('root').style.overflowY = 'hidden'
      //     })
      //   } else {
      //     this.setState({
      //       loading: false
      //     })
      //     notification.warning({
      //       top: 92,
      //       message: res.message,
      //       duration: 5
      //     })
      //   }
      // })
    }
  }
 
  handleSubBtn = (type) => {
    // 操作按钮:添加、解除冻结、确认及关闭
    if (type === 'confirm') {
      let param  = {
        func: 'sPC_Menu_SortUpt',
        LText: this.state.menulist.map((item, index) => {
          return 'select \'' + item.MenuID + '\' as Menuid,' + (index + 1) * 10 + ' as sort'
        })
      }
 
      param.LText = param.LText.join(' union ') // sql拼接
      param.LText = Utils.formatOptions(param.LText) // 关键字符替换,base64加密
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') // 时间戳
      param.secretkey = Utils.encrypt(param.LText, param.timestamp) // md5密钥
 
      confirm({
        title: '确认调整菜单顺序吗?',
        content: '',
        onOk() {
          return Api.getSystemConfig(param).then(res => {
            if (res.status) {
              MKEmitter.emit('mkUpdateMenuList')
            } else {
              notification.warning({
                top: 92,
                message: res.message,
                duration: 5
              })
            }
          })
        },
        onCancel() {}
      })
    } else if (type === 'close') {
      if (this.state.change) {
        let _this = this
 
        confirm({
          title: '菜单顺序已调整,放弃保存吗?',
          content: '',
          onOk() {
            _this.props.exitEdit()
          },
          onCancel() {}
        })
      } else {
        this.props.exitEdit()
      }
    }
  }
 
  exittabview = () => {
    this.setState({tabview: ''})
    document.getElementById('root').style.overflowY = 'unset'
  }
 
  handleView = (param) => {
    this.setState({
      tabview: ''
    }, () => {
      if (param) {
        this.setState(param)
        document.getElementById('root').style.overflowY = 'hidden'
      } else {
        document.getElementById('root').style.overflowY = 'unset'
      }
    })
  }
 
  /**
   * @description 三级菜单添加或修改
   */
  memuSubmit = () => {
    const { sysMenu } = this.state
    let sysTemplates = fromJS(this.state.sysTemplates).toJS()
 
    this.menuFormRef.handleConfirm().then(res => {
      let PageParam = {
        Template: sysMenu.Template,
        OpenType: 'newtab'
      }
 
      if (sysMenu.Template === 'NewPage') {
        PageParam.OpenType = 'NewPage'
        PageParam.url = res.url
      }
 
      let param = {
        func: 'sPC_TrdMenu_AddUpt',
        FstID: res.fstMenuId,
        SndID: res.ParentID,
        ParentID: res.ParentID,
        MenuID: sysMenu.MenuID,
        MenuNo: res.MenuNo,
        Template: sysMenu.Template,
        MenuName: res.MenuName,
        PageParam: JSON.stringify(PageParam),
        LongParam: '',
        LText: '',
        LTexttb: ''
      }
 
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      param.secretkey = Utils.encrypt(param.LText, param.timestamp)
 
      this.setState({
        confirmLoading: true
      })
 
      Api.getSystemConfig(param).then(response => {
        if (response.status) {
          this.setState({
            sysTemplates: sysTemplates,
            confirmLoading: false,
            handleMVisible: false,
            sysMenu: '',
            tabview: ''
          })
 
          MKEmitter.emit('mkUpdateMenuList')
          document.getElementById('root').style.overflowY = 'unset'
        } else {
          this.setState({
            confirmLoading: false
          })
          notification.warning({
            top: 92,
            message: response.message,
            duration: 5
          })
        }
      })
    })
  }
 
  copyMenu = (param, MenuId) => {
    Api.getSystemConfig({
      func: 'sPC_Get_LongParam',
      MenuID: MenuId
    }).then(result => {
      if (result.status) {
        let config = null
  
        try {
          config = result.LongParam ? JSON.parse(window.decodeURIComponent(window.atob(result.LongParam))) : null
        } catch (e) {
          console.warn('Parse Failure')
          config = null
        }
  
        if (config) {
          config.uuid = param.MenuID
          config.MenuID = param.MenuID
          config.parentId = param.ParentID
          config.MenuName = param.MenuName
          config.MenuNo = param.MenuNo
          config.easyCode = ''
          config.components = MenuUtils.resetConfig(config.components)
          config.enabled = false
 
          param.LongParam = window.btoa(window.encodeURIComponent(JSON.stringify(config)))
        }
 
        Api.getSystemConfig(param).then(response => {
          if (response.status) {
            this.setState({
              confirmLoading: false,
              handleMVisible: false,
              sysMenu: '',
              tabview: ''
            })
  
            MKEmitter.emit('mkUpdateMenuList')
            document.getElementById('root').style.overflowY = 'unset'
          } else {
            this.setState({
              confirmLoading: false
            })
            notification.warning({
              top: 92,
              message: response.message,
              duration: 5
            })
          }
        })
      } else {
        this.setState({
          confirmLoading: false
        })
        notification.warning({
          top: 92,
          message: result.message,
          duration: 5
        })
      }
    })
  }
 
  UNSAFE_componentWillMount () {
    this.setState({
      menulist: this.props.menulist
    })
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (!is(fromJS(this.props.menulist), fromJS(nextProps.menulist))) {
      this.setState({
        menulist: nextProps.menulist,
        change: false
      })
    }
  }
 
  render () {
    const { change } = this.state
 
    return (
      <div className="third-edit-box">
        <div className="cus-submenu-title">
          <MkIcon type={this.props.supMenu.PageParam.Icon} />
          <span>{this.props.supMenu.MenuName}</span>
        </div>
        <DndProvider backend={HTML5Backend}>
          <DragElement
            list={this.state.menulist}
            handlePreviewList={this.handlePreviewList}
            handleMenu={this.handleMenu}
          />
        </DndProvider>
        <div className="menu-btn">
          <Button type="primary" className="mk-save-menu" disabled={!change} onClick={() => {this.handleSubBtn('confirm')}}>保存</Button>
          <Button onClick={() => {this.handleSubBtn('close')}}>关闭</Button>
        </div>
        
        {this.state.tabview === 'TreePage' ?
          <TreePageConfig
            menu={this.state.editMenu}
            reloadmenu={() => {MKEmitter.emit('mkUpdateMenuList')}}
            handleView={this.handleView}
          /> : null
        }
        {this.state.tabview === 'CalendarPage' ?
          <CalendarPageConfig
            menu={this.state.editMenu}
            reloadmenu={() => {MKEmitter.emit('mkUpdateMenuList')}}
            handleView={this.handleView}
          /> : null
        }
        {this.state.tabview === 'CommonTable' ?
          <ComTableConfig
            menu={this.state.editMenu}
            reloadmenu={() => {MKEmitter.emit('mkUpdateMenuList')}}
            handleView={this.handleView}
          /> : null
        }
        {this.state.tabview === 'Modal' ?
          <ModalConfig
            menu={this.state.editMenu}
            editTab={this.state.editTab}
            tabConfig={this.state.tabConfig}
            editSubTab={this.state.editSubTab}
            subTabConfig={this.state.subTabConfig}
            btnTab={this.state.btnTab}
            btnTabConfig={this.state.btnTabConfig}
            editAction={this.state.editAction}
            subConfig={this.state.subConfig}
            handleView={this.handleView}
          /> : null
        }
        {this.state.tabview === 'SubTable' ?
          <SubTable
            menu={this.state.editMenu}
            editTab={this.state.editTab}
            editSubTab={this.state.editSubTab}
            tabConfig={this.state.tabConfig}
            btnTab={this.state.btnTab}
            btnTabConfig={this.state.btnTabConfig}
            config={this.state.subConfig}
            handleView={this.handleView}
          /> : null
        }
        {this.state.tabview === 'FormTab' ?
          <FormTabConfig
            menu={this.state.editMenu}
            btnTab={this.state.btnTab}
            config={this.state.subConfig}
            handleView={this.handleView}
          /> : null
        }
        {/* 添加系统菜单 */}
        <Modal
          title="修改菜单"
          visible={this.state.handleMVisible}
          width={600}
          onOk={this.memuSubmit}
          confirmLoading={this.state.loading}
          onCancel={() => {this.setState({handleMVisible: false})}}
          destroyOnClose
        >
          <MenuForm
            menu={this.state.sysMenu}
            inputSubmit={this.memuSubmit}
            wrappedComponentRef={(inst) => this.menuFormRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
const mapStateToProps = (state) => {
  return {
    mainMenu: state.mainMenu,
    menuTree: state.menuTree
  }
}
 
const mapDispatchToProps = () => {
  return {}
}
 
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(EditMenu))