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, Spin, Icon, Col, Card, Tabs, Row, Input } from 'antd'
|
import moment from 'moment'
|
import Preview from './preview'
|
import TransferForm from '@/components/transferform'
|
import Utils from '@/utils/utils.js'
|
import DragElement from '../menuelement'
|
import asyncLoadComponent from '@/utils/asyncLoadComponent'
|
import { sysTemps } from '@/utils/option.js'
|
import Api from '@/api'
|
import zhCN from '@/locales/zh-CN/header.js'
|
import enUS from '@/locales/en-US/header.js'
|
import nortable from '@/assets/img/normaltable.jpg'
|
import './index.scss'
|
|
const ComTableConfig = asyncLoadComponent(() => import('@/templates/comtableconfig'))
|
const FormTabConfig = asyncLoadComponent(() => import('@/templates/formtabconfig'))
|
const ModalConfig = asyncLoadComponent(() => import('@/templates/modalconfig'))
|
const SubTable = asyncLoadComponent(() => import('@/templates/subtableconfig'))
|
|
const { confirm } = Modal
|
const { TabPane } = Tabs
|
const { Search } = Input
|
|
const illust = { // 模板图片,用于已使用模板
|
CommonTable: nortable
|
}
|
|
class EditMenu extends Component {
|
static propTpyes = {
|
reload: PropTypes.func, // 菜单修改后刷新
|
menulist: PropTypes.any, // 三级菜单列表
|
exitEdit: PropTypes.func, // 退出编辑状态
|
supMenu: PropTypes.object, // 对应的上级菜单
|
supMenuList: PropTypes.array // 上级菜单列表,用于三级菜单切换上级菜单
|
}
|
|
state = {
|
dict: (!localStorage.getItem('lang') || localStorage.getItem('lang') === 'zh-CN') ? zhCN : enUS,
|
thawmenulist: null, // 已冻结的二级菜单
|
type: '', // 操作类型,新建或编辑菜单
|
thawMvisible: false, // 解除冻结模态框
|
confirmLoading: false, // 提交中。。。
|
selectTemp: '', // 选择模板
|
usedTemplates: null, // 已使用模板列表
|
tempSearchKey: '', // 菜单名称过滤值
|
loading: false, // 编辑菜单或使用已使用模板时,获取配置信息
|
preview: null, // 图片预览url
|
pretemplate: null, // 预览模板
|
btnParam: null, // 编辑按钮的配置信息
|
menulist: null, // 编辑中的菜单
|
sysTemplates: sysTemps, // 系统模板
|
tabview: '', // 选择模板窗口(template)、基础表格配置(CommonTable)、表单(Modal)、子表(SubTable)
|
editMenu: null, // 编辑菜单
|
editAction: null, // 编辑按钮
|
editTab: null, // 编辑标签
|
tabConfig: null, // 标签配置信息
|
editSubTab: null, // 编辑子标签(标签中的标签)
|
subTabConfig: null, // 子标签配置信息
|
subConfig: null, // 子配置信息
|
btnTab: null, // 打开新标签或当前页面刷新的按钮
|
btnTabConfig: null // 打开新标签按钮配置
|
}
|
|
/**
|
* @description 菜单顺序改变时,保存中间状态
|
*/
|
handlePreviewList = (List) => {
|
this.setState({
|
menulist: List
|
})
|
}
|
|
/**
|
* @description 菜单编辑:修改、删除
|
* 1、菜单修改或删除时,先查看菜单顺序是否改变
|
* 2、菜单删除
|
* 3、菜单编辑,查询菜单配置信息,信息正确,进入对应编辑页面
|
*/
|
handleMenu = (menu) => {
|
const _this = this
|
|
if (!is(fromJS(this.props.menulist), fromJS(this.state.menulist))) {
|
notification.warning({
|
top: 92,
|
message: this.state.dict['header.menu.presave'],
|
duration: 10
|
})
|
} else if (menu.type === 'close') {
|
confirm({
|
title: this.state.dict['header.menu.close'].replace('@M', menu.card.text),
|
content: '',
|
okText: this.state.dict['header.confirm'],
|
cancelText: this.state.dict['header.cancel'],
|
onOk() {
|
let param = {
|
func: 'sPC_MainMenu_Del',
|
MenuID: menu.card.MenuID
|
}
|
return Api.getSystemConfig(param).then(res => {
|
if (res.status) {
|
_this.props.reload()
|
} else {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 10
|
})
|
}
|
})
|
},
|
onCancel() {}
|
})
|
} else if (menu.type === 'edit') {
|
let _menu = menu.card
|
let param = {
|
func: 'sPC_Get_LongParam',
|
MenuID: _menu.MenuID
|
}
|
|
this.setState({
|
loading: true
|
})
|
|
Api.getSystemConfig(param).then(res => {
|
if (res.status) {
|
let _LongParam = res.LongParam && window.decodeURIComponent(window.atob(res.LongParam))
|
try {
|
_LongParam = JSON.parse(_LongParam)
|
} catch (e) {
|
_LongParam = ''
|
}
|
|
_menu.LongParam = _LongParam
|
_menu.ParentID = this.props.supMenu.MenuID
|
|
// 检测模板是否存在
|
let _Template = this.state.sysTemplates.filter(temp => temp.type === _menu.PageParam.Template)
|
|
this.setState({
|
type: 'edit',
|
editMenu: _menu,
|
loading: false,
|
tabview: _Template.length > 0 ? _menu.PageParam.Template : 'template'
|
}, () => {
|
document.getElementById('root').style.overflowY = 'hidden'
|
})
|
|
// 模板不存在时错误提示
|
if (_Template.length === 0) {
|
notification.warning({
|
top: 92,
|
message: this.state.dict['header.menu.template.empty'],
|
duration: 10
|
})
|
}
|
} else {
|
this.setState({
|
loading: false
|
})
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 10
|
})
|
}
|
})
|
}
|
}
|
|
handleSubBtn = (type) => {
|
// 操作按钮:添加、解除冻结、确认及关闭
|
if (type === 'add') { // 点击添加时,展开模板
|
if (!is(fromJS(this.props.menulist), fromJS(this.state.menulist))) {
|
notification.warning({
|
top: 92,
|
message: this.state.dict['header.menu.presave'],
|
duration: 10
|
})
|
return
|
}
|
this.setState({
|
tabview: 'template',
|
editMenu: {
|
MenuID: Utils.getuuid(),
|
MenuName: '',
|
MenuNo: '',
|
type: '',
|
PageParam: '',
|
LongParam: '',
|
isSubtable: '', // 是否为主子表
|
ParentID: this.props.supMenu.MenuID
|
}
|
})
|
document.getElementById('root').style.overflowY = 'hidden'
|
} else if (type === 'thaw') {
|
if (!is(fromJS(this.props.menulist), fromJS(this.state.menulist))) {
|
notification.warning({
|
top: 92,
|
message: this.state.dict['header.menu.presave'],
|
duration: 10
|
})
|
return
|
}
|
this.setState({
|
thawMvisible: true
|
})
|
Api.getSystemConfig({
|
func: 'sPC_Get_FrozenMenu',
|
ParentID: this.props.supMenu.MenuID,
|
TYPE: 30
|
}).then(res => {
|
if (res.status) {
|
this.setState({
|
thawmenulist: res.data.map(menu => {
|
return {
|
key: menu.MenuID,
|
title: menu.MenuName
|
}
|
})
|
})
|
} else {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 10
|
})
|
}
|
})
|
} else if (type === 'confirm') {
|
if (!is(fromJS(this.props.menulist), fromJS(this.state.menulist))) {
|
let _this = this
|
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') + '.000' // 时间戳
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp) // md5密钥
|
|
confirm({
|
title: this.state.dict['header.menu.resetorder'],
|
content: '',
|
okText: this.state.dict['header.confirm'],
|
cancelText: this.state.dict['header.cancel'],
|
onOk() {
|
return Api.getSystemConfig(param).then(res => {
|
if (res.status) {
|
_this.props.reload()
|
} else {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 10
|
})
|
}
|
})
|
},
|
onCancel() {}
|
})
|
} else {
|
this.props.exitEdit()
|
}
|
} else if (type === 'close') {
|
this.props.exitEdit()
|
}
|
}
|
|
thawMemuSubmit = () => {
|
// 三级菜单解除冻结
|
if (this.refs.trawmenu.state.targetKeys.length === 0) {
|
notification.warning({
|
top: 92,
|
message: this.state.dict['header.menu.thawmenu.select'],
|
duration: 10
|
})
|
} else {
|
this.setState({
|
confirmLoading: true
|
})
|
let defers = this.refs.trawmenu.state.targetKeys.map(item => {
|
return new Promise((resolve) => {
|
Api.getSystemConfig({
|
func: 'sPC_MainMenu_ReDel',
|
MenuID: item
|
}).then(res => {
|
if (res.status) {
|
resolve('')
|
} else {
|
resolve(res.message)
|
}
|
})
|
})
|
})
|
Promise.all(defers).then(res => {
|
let msg = res.filter(Boolean)[0]
|
if (msg) {
|
notification.error({
|
top: 92,
|
message: msg,
|
duration: 15
|
})
|
} else {
|
this.setState({
|
confirmLoading: false,
|
thawMvisible: false,
|
thawmenulist: null
|
})
|
this.props.reload()
|
}
|
})
|
}
|
}
|
|
thawMemuCancel = () => {
|
// 解除冻结-取消
|
this.setState({
|
thawMvisible: false,
|
thawmenulist: null
|
})
|
}
|
|
previewPicture = (template) => {
|
// 图片预览
|
this.setState({
|
preview: template.url,
|
pretemplate: template
|
})
|
}
|
|
cancelPrePicture = () => {
|
// 关闭图片预览
|
this.setState({
|
preview: null
|
})
|
}
|
|
/**
|
* @description 使用模板
|
* 1、使用系统模板时,使用系统配置
|
* 2、使用已有菜单模板时,获取菜单配置信息,标记为user(复制菜单按钮)
|
*/
|
useTemplate = (template, useType) => {
|
const { editMenu } = this.state
|
|
new Promise(resolve => {
|
if (useType === 'sys') {
|
resolve(true)
|
} else {
|
let param = {
|
func: 'sPC_Get_LongParam',
|
MenuID: template.uuid
|
}
|
this.setState({
|
loading: true
|
})
|
|
Api.getSystemConfig(param).then(result => {
|
if (!result.status) {
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 10
|
})
|
resolve(false)
|
} else {
|
let _config = ''
|
if (result.LongParam) {
|
try {
|
_config = window.decodeURIComponent(window.atob(result.LongParam))
|
_config = JSON.parse(_config)
|
_config.type = 'user'
|
} catch (e) {
|
_config = ''
|
}
|
}
|
|
if (_config) {
|
template.baseconfig = _config
|
resolve(true)
|
} else {
|
notification.warning({
|
top: 92,
|
message: '模板信息获取失败!',
|
duration: 10
|
})
|
resolve(false)
|
}
|
}
|
})
|
}
|
}).then(res => {
|
if (!res) { // 错误时,重置状态
|
this.setState({
|
loading: false
|
})
|
return
|
}
|
|
// 选择模板:添加菜单时(不存在menuId)
|
let _PageParam = {
|
OpenType: editMenu.PageParam ? editMenu.PageParam.OpenType : 'newtab',
|
Template: template.type
|
}
|
|
this.setState({
|
loading: false,
|
tabview: template.type,
|
editMenu: {
|
...editMenu,
|
type: template.type,
|
PageParam: _PageParam,
|
LongParam: template.baseconfig,
|
isSubtable: template.isSubtable
|
}
|
})
|
document.getElementById('root').style.overflowY = 'hidden'
|
})
|
}
|
|
getUsedTemplate = () => {
|
Api.getSystemConfig({func: 'sPC_Get_UserTemp', TypeCharTwo: 'menu'}).then(res => {
|
this.setState({
|
usedTemplates: res.UserTemp.map(temp => {
|
|
return {
|
uuid: temp.MenuID,
|
title: temp.MenuName,
|
type: temp.Template,
|
url: illust[temp.Template]
|
}
|
})
|
})
|
})
|
}
|
|
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'
|
}
|
})
|
}
|
|
UNSAFE_componentWillMount () {
|
this.getUsedTemplate()
|
|
this.setState({
|
menulist: this.props.menulist
|
})
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
if (!is(fromJS(this.props.menulist), fromJS(nextProps.menulist))) {
|
this.setState({
|
menulist: nextProps.menulist
|
})
|
}
|
}
|
|
render () {
|
return (
|
<div className="third-edit-box">
|
{!this.state.tabview ?
|
<div className="mask">
|
<div className="tipcard card1">
|
拖动三级菜单可调整顺序,顺序调整后,请点击确定按钮保存。
|
</div>
|
<div className="tipcard card2">
|
点击编辑图标会根据菜单模板,进入相应的模板编辑页面,点击删除图标可删除菜单。
|
</div>
|
<div className="tipcard card3">
|
点击解除冻结按钮,可还原已删除的三级菜单。
|
</div>
|
<div className="tipcard card4">
|
点击添加图标,会显示系统模板和已使用模板,选择已使用模板时,会复制已添加菜单的配置信息。
|
</div>
|
<div className="tipcard card5">
|
<p>编辑状态中,菜单之外区域会锁定,查看系统数据请点击。</p>
|
<div>
|
<a target="blank" href="#/main" >新页面</a>
|
</div>
|
</div>
|
</div> : null
|
}
|
<div className="cus-submenu-title">
|
<Icon type={this.props.supMenu.PageParam.Icon} />
|
<span>{this.props.supMenu.text}</span>
|
</div>
|
<DndProvider backend={HTML5Backend}>
|
<DragElement
|
list={this.state.menulist}
|
handlePreviewList={this.handlePreviewList}
|
handleMenu={this.handleMenu}
|
/>
|
</DndProvider>
|
<div className="menu-add" onClick={() => {this.handleSubBtn('add')}}>
|
<Icon type="plus" />
|
</div>
|
<div className="menu-btn">
|
<Button type="primary" onClick={() => {this.handleSubBtn('thaw')}}>{this.state.dict['header.thawmenu']}</Button>
|
<Button type="primary" onClick={() => {this.handleSubBtn('confirm')}}>{this.state.dict['header.confirm']}</Button>
|
<Button onClick={() => {this.handleSubBtn('close')}}>{this.state.dict['header.close']}</Button>
|
</div>
|
{this.state.tabview === 'template' &&
|
<div className="editboard">
|
<div className="workplace">
|
<Button className="top-btn" onClick={this.exittabview}>{this.state.dict['header.cancel']}</Button>
|
{this.state.tabview === 'template' && <Tabs defaultActiveKey="1">
|
<TabPane tab="系统模板" key="1">
|
<Row>
|
{this.state.sysTemplates.map((template, index) => {
|
return (
|
<Col key={`${index}`} span={8}>
|
<Card
|
title={template.title}>
|
<img onClick={() => {this.previewPicture(template)}} src={template.url} alt=""/>
|
<div className="card-operation">
|
<Button type="primary" onClick={() => {this.useTemplate(template, 'sys')}}>使用模板</Button>
|
</div>
|
</Card>
|
</Col>
|
)
|
})}
|
</Row>
|
</TabPane>
|
<TabPane tab="已使用模板" key="2">
|
<Row>
|
<Col span={8}>
|
<Search placeholder="请输入菜单名称" defaultValue={this.state.tempSearchKey} onSearch={value => {this.setState({tempSearchKey: value})}} enterButton />
|
</Col>
|
</Row>
|
<Row>
|
{this.state.usedTemplates && this.state.usedTemplates.map((template, index) => {
|
if (template.title.toLowerCase().indexOf(this.state.tempSearchKey.toLowerCase()) >= 0) {
|
return (
|
<Col key={template.type + index} span={8}>
|
<Card
|
title={template.title}>
|
<img onClick={() => {this.previewPicture(template)}} src={template.url} alt=""/>
|
<div className="card-operation">
|
<Button type="primary" onClick={() => {this.useTemplate(template, 'user')}}>使用模板</Button>
|
</div>
|
</Card>
|
</Col>
|
)
|
} else {
|
return ''
|
}
|
})}
|
</Row>
|
</TabPane>
|
</Tabs>}
|
</div>
|
</div>}
|
{this.state.tabview === 'CommonTable' &&
|
<ComTableConfig
|
menu={this.state.editMenu}
|
supMenuList={this.props.supMenuList}
|
reloadmenu={() => {this.props.reload()}}
|
handleView={this.handleView}
|
/>
|
}
|
{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}
|
/>
|
}
|
{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}
|
/>
|
}
|
{this.state.tabview === 'FormTab' &&
|
<FormTabConfig
|
menu={this.state.editMenu}
|
btnTab={this.state.btnTab}
|
config={this.state.subConfig}
|
handleView={this.handleView}
|
/>
|
}
|
{/* 图片预览 */}
|
<Preview cancel={this.cancelPrePicture} preview={this.state.preview} template={this.state.pretemplate} confirm={this.useTemplate}/>
|
{/* 解冻菜单模态框 */}
|
<Modal
|
title={this.state.dict['header.thawmenu']}
|
okText={this.state.dict['header.confirm']}
|
cancelText={this.state.dict['header.cancel']}
|
visible={this.state.thawMvisible}
|
onOk={this.thawMemuSubmit}
|
confirmLoading={this.state.confirmLoading}
|
onCancel={this.thawMemuCancel}
|
>
|
{!this.state.thawmenulist && <Spin style={{marginLeft: 'calc(50% - 22px)', marginTop: '70px', marginBottom: '70px'}} size="large" />}
|
{this.state.thawmenulist && <TransferForm ref="trawmenu" dict={this.state.dict} menulist={this.state.thawmenulist}/>}
|
</Modal>
|
{this.state.loading && <Spin className="loading-thdmenu" size="large" />}
|
</div>
|
)
|
}
|
}
|
|
export default EditMenu
|