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))
|