import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Modal, Button, notification } from 'antd'
|
import moment from 'moment'
|
import md5 from 'md5'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
import { getTables } from '@/utils/utils-custom.js'
|
// import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
class Transfer extends Component {
|
static propTpyes = {
|
config: PropTypes.object,
|
updateConfig: PropTypes.func
|
}
|
|
state = {}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
trigger = () => {
|
const { config } = this.props
|
const _this = this
|
|
if (!config.enabled) {
|
notification.warning({
|
top: 92,
|
message: '未启用菜单不可升级!',
|
duration: 5
|
})
|
return
|
}
|
|
confirm({
|
title: '确定切换为自定义页面吗?',
|
content: '',
|
onOk() {
|
return new Promise(resolve => {
|
_this.execUpdate(resolve)
|
})
|
},
|
onCancel() {}
|
})
|
}
|
|
execUpdate = (_resolve) => {
|
let _config = fromJS(this.props.config).toJS()
|
_config.permission = 'true'
|
_config.Template = 'CustomPage'
|
_config.version = 2.0
|
_config.enabled = false
|
|
delete _config.autoMatic
|
|
let useMSearch = false
|
_config.components.forEach((item, i) => {
|
if (item.type === 'tabs') {
|
item.name = '标签组' + i
|
item.width = 24
|
item.subtype = 'tabs'
|
item.setting = {autoSwitch: 'false', blacklist: [], name: item.name, position: 'top', supModule: [], tabStyle: 'line', width: 24}
|
|
item.subtabs.forEach(tab => {
|
tab.components[0].subtype = 'normaltable'
|
tab.components[0].width = 24
|
tab.components[0].wrap.width = 24
|
tab.components[0].wrap.name = tab.components[0].name
|
if (tab.components[0].setting.useMSearch === 'true') {
|
useMSearch = true
|
}
|
|
tab.components[0].action.forEach(btn => {
|
if (btn.OpenType === 'popview' && btn.config) {
|
btn.config.Template = 'CustomPage'
|
if (btn.config.components[0]) {
|
btn.config.components[0].subtype = 'normaltable'
|
}
|
}
|
})
|
tab.components[0].cols.forEach(col => {
|
if (col.type !== 'action') return
|
col.elements.forEach(btn => {
|
if (btn.OpenType === 'popview' && btn.config) {
|
btn.config.Template = 'CustomPage'
|
if (btn.config.components[0]) {
|
btn.config.components[0].subtype = 'normaltable'
|
}
|
}
|
})
|
})
|
})
|
} else {
|
item.subtype = 'normaltable'
|
item.width = 24
|
item.wrap.width = 24
|
item.wrap.name = item.name
|
item.action.forEach(btn => {
|
if (btn.OpenType === 'popview' && btn.config) {
|
btn.config.Template = 'CustomPage'
|
if (btn.config.components[0]) {
|
btn.config.components[0].subtype = 'normaltable'
|
}
|
}
|
})
|
item.cols.forEach(col => {
|
if (col.type !== 'action') return
|
col.elements.forEach(btn => {
|
if (btn.OpenType === 'popview' && btn.config) {
|
btn.config.Template = 'CustomPage'
|
if (btn.config.components[0]) {
|
btn.config.components[0].subtype = 'normaltable'
|
}
|
}
|
})
|
})
|
}
|
})
|
|
if (useMSearch) {
|
let cell = {
|
uuid: Utils.getuuid(),
|
type: 'search',
|
width: 24,
|
subtype: 'mainsearch',
|
name: '搜索',
|
search: _config.components[0].search || [],
|
style: {borderBottomColor: '#f0f0f0', borderBottomWidth: '1px'},
|
wrap: {float: 'left', blacklist: [], name: '搜索', width: 24}
|
}
|
|
cell.wrap.advanceType = _config.components[0].wrap.advanceType || 'modal'
|
cell.wrap.advanceWidth = _config.components[0].wrap.advanceWidth || 1000
|
cell.wrap.searchLwidth = _config.components[0].wrap.searchLwidth || 33.3
|
cell.wrap.searchRatio = _config.components[0].wrap.searchRatio || 6
|
cell.wrap.show = _config.components[0].wrap.show || 'true'
|
|
if (cell.wrap.advanceType === 'drawer') {
|
cell.wrap.drawerPlacement = _config.components[0].wrap.drawerPlacement || 'left'
|
}
|
|
_config.components[0].search = []
|
_config.components[0].setting.useMSearch = 'true'
|
|
_config.components.unshift(cell)
|
_config.components[0].$tables = getTables(_config.components[0])
|
_config.components[1].$tables = getTables(_config.components[1])
|
}
|
|
let tbs = []
|
_config.components.forEach(item => {
|
if (item.type === 'tabs') {
|
item.subtabs.forEach(tab => {
|
if (tab.components[0].$tables) {
|
tbs.push(...tab.components[0].$tables)
|
}
|
})
|
} else {
|
if (item.$tables) {
|
tbs.push(...item.$tables)
|
}
|
}
|
})
|
let arr = []
|
tbs = tbs.filter(tb => {
|
let _tb = tb.toLowerCase()
|
|
if (arr.includes(_tb)) return false
|
arr.push(_tb)
|
|
return true
|
})
|
tbs.sort()
|
if (tbs.length && sessionStorage.getItem('mk_tb_names')) {
|
let names = sessionStorage.getItem('mk_tb_names')
|
tbs = tbs.filter(tb => names.indexOf(',' + tb.toLowerCase() + ',') > -1)
|
}
|
tbs = tbs.map(tb => `'${tb}'`).join(';')
|
|
let key = md5(_config.uuid + tbs.toLowerCase())
|
|
let urlparam = {
|
FstId: _config.fstMenuId,
|
ParentId: _config.parentId,
|
MenuId: _config.uuid,
|
MenuName: _config.MenuName,
|
MenuNo: _config.MenuNo,
|
EasyCode: _config.easyCode,
|
type: 'admin',
|
OpenType: _config.OpenType,
|
PageParam: {Template: 'CustomPage', OpenType: 'newtab', hidden: _config.hidden},
|
MenuType: 'custom'
|
}
|
|
let url = window.btoa(window.encodeURIComponent(JSON.stringify(urlparam)))
|
_config.tbkey = key
|
|
let param = {
|
func: 'sPC_TrdMenu_AddUpt',
|
FstID: _config.fstMenuId || '',
|
SndID: _config.parentId,
|
ParentID: _config.parentId,
|
MenuID: _config.uuid,
|
MenuNo: _config.MenuNo || '',
|
EasyCode: _config.easyCode || '',
|
Template: 'CustomPage',
|
MenuName: _config.MenuName || '',
|
PageParam: JSON.stringify({Template: 'CustomPage', OpenType: _config.OpenType || 'newtab', hidden: _config.hidden || 'false'}),
|
open_edition: _config.open_edition,
|
debug_md5: key,
|
debug_url: url,
|
debug_list: window.btoa(tbs),
|
LongParam: window.btoa(window.encodeURIComponent(JSON.stringify(_config)))
|
}
|
|
param.LText = Utils.formatOptions(param.LText)
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
Api.getSystemConfig(param).then(res => {
|
_resolve()
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return
|
}
|
|
localStorage.setItem('menuUpdate', new Date().getTime())
|
notification.success({
|
top: 92,
|
message: '切换成功。',
|
duration: 2
|
})
|
|
delete urlparam.type
|
|
let _param = window.btoa(window.encodeURIComponent(JSON.stringify(urlparam)))
|
|
setTimeout(() => {
|
window.history.replaceState(null, null, window.location.href.split('#')[0] + `#/menudesign/${_param}`)
|
window.location.reload()
|
}, 2000)
|
})
|
}
|
|
render() {
|
return (
|
<div className="mk-replace-field" style={{display: 'inline-block'}}>
|
<Button className="mk-border-green" icon="retweet" onClick={this.trigger}>模板切换</Button>
|
</div>
|
)
|
}
|
}
|
|
export default Transfer
|