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
|