king
2024-02-04 0bbaa727cdfc65622e33e91c4bf694c83f184535
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
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 { notification, Modal, Button } from 'antd'
import moment from 'moment'
 
import Utils from '@/utils/utils.js'
import DragElement from '../menuelement'
import MenuForm from '../menuform'
import Api from '@/api'
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
const { confirm } = Modal
 
class EditMenu extends Component {
  static propTpyes = {
    menulist: PropTypes.any,      // 二级菜单列表
    menuTree: PropTypes.array,    // 一级菜单列表
    supMenu: PropTypes.object,    // 二级菜单所对应的一级菜单
    exitEdit: PropTypes.func      // 退出编辑
  }
 
  state = {
    menulist: null,        // 菜单列表
    visible: null,         // 模态框是否可见
    formlist: null,        // 表单信息
    editMenu: null,        // 编辑菜单
    loading: false,        // 提交中。。。
    change: false
  }
 
  handlePreviewList = (List) => {
    this.setState({
      menulist: List,
      change: !is(fromJS(List), fromJS(this.props.menulist))
    })
  }
 
  handleMenu = (menu) => {
    // 菜单编辑:修改、删除,如菜单顺序已改变,提示保存菜单顺序
    if (this.state.change) {
      notification.warning({
        top: 92,
        message: '菜单顺序已调整,请保存!',
        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.getCloudConfig(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') {
      this.setState({
        visible: true,
        editMenu: menu.card,
        formlist: [
          { // 父级菜单
            type: 'select',
            key: 'parentId',
            label: '上级菜单',
            initVal: this.props.supMenu.MenuID,
            required: true,
            options: this.props.menuTree
          },
          { // 菜单名称
            type: 'text',
            key: 'menuName',
            label: '菜单名称',
            initVal: menu.card.MenuName,
            required: true,
            readonly: false
          },
          { // 菜单图标
            type: 'icon',
            key: 'icon',
            label: '图标',
            initVal: menu.card.PageParam.Icon || 'folder',
            required: true
          }
        ]
      })
    }
  }
 
  handleSubBtn = (type) => {
    const that = this
    if (type === 'confirm') { // 保存调整后的顺序
      let param  = {
        func: 'sPC_Menu_SortUpt',
        exec_type: 'x',
        LText: this.state.menulist.map((item, index) => {
          return 'select \'' + item.MenuID + '\' as Menuid,' + (index + 1) * 10 + ' as sort'
        })
      }
 
      param.LText = param.LText.join(' union ')
      param.LText = Utils.formatOptions(param.LText, 'x')
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      param.secretkey = Utils.encrypt('', param.timestamp)
 
      confirm({
        title: '确认调整菜单顺序吗?',
        content: '',
        onOk() {
          return Api.getCloudConfig(param).then(res => {
            if (res.status) {
              that.setState({ change: false })
              MKEmitter.emit('mkUpdateMenuList')
            } else {
              notification.warning({
                top: 92,
                message: res.message,
                duration: 5
              })
            }
          })
        },
        onCancel() {}
      })
    } else if (type === 'close') { // 退出编辑
      if (this.state.change) {
        confirm({
          title: '菜单顺序已调整,放弃保存吗?',
          content: '',
          onOk() {
            that.props.exitEdit()
          },
          onCancel() {}
        })
      } else {
        this.props.exitEdit()
      }
    }
  }
 
  memuHandleSubmit = () => {
    this.menuFormRef.handleConfirm().then(values => {
      let param = {
        func: 'sPC_SndMenu_Upt',
        ParentID: values.parentId,
        MenuID: this.state.editMenu.MenuID,
        MenuName: values.menuName,
        PageParam: JSON.stringify({
          Icon: values.icon
        })
      }
      this.setState({
        loading: true
      })
      Api.getCloudConfig(param).then(res => {
        if (res.status) {
          this.setState({
            loading: false,
            visible: false
          })
          MKEmitter.emit('mkUpdateMenuList')
        } else {
          this.setState({
            loading: false
          })
          notification.warning({
            top: 92,
            message: res.message,
            duration: 5
          })
        }
      })
    }, () => {})
  }
 
  memuHandleCancel = () => { // 取消操作,关闭模态框
    this.setState({
      visible: false,
      formlist: null,
      editMenu: null
    })
  }
 
  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, loading } = this.state
 
    return (
      <>
        <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>
        <Modal
          title="修改菜单"
          visible={this.state.visible}
          onOk={this.memuHandleSubmit}
          confirmLoading={loading}
          onCancel={this.memuHandleCancel}
          destroyOnClose
        >
          {this.state.formlist ?
            <MenuForm
              inputSubmit={this.memuHandleSubmit}
              formlist={this.state.formlist}
              wrappedComponentRef={(inst) => this.menuFormRef = inst}
            /> : null}
        </Modal>
      </>
    )
  }
}
 
export default EditMenu