import React, { Component } from 'react'
|
import { is, fromJS } from 'immutable'
|
import { Modal, notification, Spin, Select } from 'antd'
|
import { SwapOutlined } from '@ant-design/icons'
|
import moment from 'moment'
|
import md5 from 'md5'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
import MenuUtils, { setLangTrans } from '@/utils/utils-custom.js'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
class TransMenu extends Component {
|
state = {
|
visible: false,
|
loading: false,
|
saveing: false,
|
dicts: [],
|
menus: [],
|
values: '',
|
kei_no: '',
|
zhApp: null
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
trigger = () => {
|
const { supApp, app } = this.props
|
|
let zhApp = supApp.sublist.filter(item => item.typename === app.typename && item.lang === 'zh-CN')[0]
|
|
this.setState({
|
loading: true,
|
visible: true,
|
saveing: false,
|
values: '',
|
kei_no: supApp.kei_no,
|
zhApp
|
})
|
|
this.getDicts()
|
this.getMenus(zhApp, supApp.kei_no)
|
}
|
|
getDicts = () => {
|
// let sql = `select id,lang,lang_name,mother_tongue,translation,use_type,case when use_type='menu' then '菜单' when use_type='button' then '按钮' when use_type='title' then '标题' when use_type='list' then '选项' else '文本' end as use_type_text from s_app_lang_translation where appkey=@appkey@ and deleted=0 and translation != ''`
|
let sql = `select mother_tongue as reg,translation as value,use_type as type,case when use_type='menu' then '菜单' when use_type='button' then '按钮' when use_type='title' then '标题' when use_type='list' then '选项' else '文本' end as use_type_text from s_app_lang_translation where appkey=@appkey@ and deleted=0 and translation != '' and lang='${sessionStorage.getItem('lang') || ''}'`
|
|
let param = {
|
func: 'sPC_Get_SelectedList',
|
LText: Utils.formatOptions(sql, 'x'),
|
obj_name: 'data',
|
arr_field: 'reg,value,type',
|
exec_type: 'x'
|
}
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
|
|
Api.getCloudConfig(param).then(result => {
|
if (!result.status) {
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 5
|
})
|
return
|
}
|
|
this.setState({dicts: result.data || []})
|
})
|
}
|
|
getMenus = (zhApp, kei_no) => {
|
if (this.state.menus.length > 0) {
|
this.setState({loading: false})
|
return
|
}
|
|
let param = {
|
func: 's_get_app_menus',
|
TypeCharOne: kei_no,
|
typename: zhApp.typename,
|
LText: `select '${window.GLOB.appkey}'`,
|
timestamp: moment().format('YYYY-MM-DD HH:mm:ss'),
|
lang: 'zh-CN'
|
}
|
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
Api.getCloudConfig(param).then(result => {
|
if (result.status) {
|
let menus = []
|
result.menus.forEach(item => {
|
if (zhApp.userbind === item.MenuID) return
|
|
menus.push(item)
|
})
|
|
this.setState({menus: menus, loading: false})
|
} else {
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 5
|
})
|
}
|
})
|
}
|
|
submit = () => {
|
const { values } = this.state
|
|
if (!values) {
|
notification.warning({
|
top: 92,
|
message: '请选择菜单!',
|
duration: 5
|
})
|
return
|
}
|
|
this.setState({saveing: true})
|
|
this.transThdMenu()
|
}
|
|
transThdMenu = () => {
|
const { app } = this.props
|
const { zhApp, dicts, values, kei_no } = this.state
|
|
let _param = {
|
func: 'sPC_Get_LongParam',
|
TypeCharOne: kei_no,
|
typename: zhApp.typename,
|
MenuID: values,
|
lang: 'zh-CN'
|
}
|
|
Api.getCloudConfig(_param).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
this.setState({saveing: false})
|
return
|
}
|
|
let config = null
|
if (res.LongParam) {
|
try {
|
config = JSON.parse(window.decodeURIComponent(window.atob(res.LongParam)))
|
} catch (e) {
|
console.warn('Parse Failure')
|
config = ''
|
}
|
}
|
|
if (!config) {
|
notification.warning({
|
top: 92,
|
message: '未获取到菜单配置信息',
|
duration: 5
|
})
|
this.setState({saveing: false})
|
}
|
|
let btnDict = {}
|
let titDict = {}
|
let lisDict = {}
|
let menuDict = {}
|
let regs = []
|
|
dicts.forEach(item => {
|
if (item.type === 'button') {
|
btnDict[item.reg] = item.value
|
} else if (item.type === 'title') {
|
titDict[item.reg] = item.value
|
} else if (item.type === 'list') {
|
lisDict[item.reg] = item.value
|
} else if (item.type === 'text') {
|
regs.push({reg: new RegExp(item.reg, 'g'), value: item.value, sort: item.reg.length})
|
} else if (item.type === 'menu') {
|
menuDict[item.reg] = item.value
|
}
|
})
|
|
regs.sort((a, b) => b.sort - a.sort)
|
|
let tail = md5(window.GLOB.appkey + kei_no + zhApp.typename + app.lang).toLowerCase()
|
|
tail = tail.slice(-8)
|
|
let menus_rolelist = ''
|
if (config.type === 'navbar') {
|
config.menus.forEach(item => {
|
if (item.linkMenuId) {
|
item.linkMenuId = item.linkMenuId.slice(0, 24) + tail
|
} else {
|
item.MenuID = item.MenuID.slice(0, 24) + tail
|
}
|
if (item.name && menuDict[item.name]) {
|
item.name = menuDict[item.name]
|
}
|
if (item.sublist) {
|
item.sublist.forEach(sec => {
|
if (sec.linkMenuId) {
|
sec.linkMenuId = sec.linkMenuId.slice(0, 24) + tail
|
} else {
|
sec.MenuID = sec.MenuID.slice(0, 24) + tail
|
}
|
if (sec.name && menuDict[sec.name]) {
|
sec.name = menuDict[sec.name]
|
}
|
if (sec.sublist) {
|
sec.sublist.forEach(thd => {
|
if (thd.linkMenuId) {
|
thd.linkMenuId = thd.linkMenuId.slice(0, 24) + tail
|
} else {
|
thd.MenuID = thd.MenuID.slice(0, 24) + tail
|
}
|
if (thd.name && menuDict[thd.name]) {
|
thd.name = menuDict[thd.name]
|
}
|
})
|
}
|
})
|
}
|
})
|
if (config.columns) {
|
config.columns.forEach(cell => {
|
if (cell.label && titDict[cell.label]) {
|
cell.label = titDict[cell.label]
|
}
|
})
|
}
|
|
config.MenuNo = 'navbar_' + app.lang
|
if (config.name === menuDict[config.name]) {
|
config.name = menuDict[config.name]
|
} else {
|
config.name = config.name + '_' + app.lang
|
}
|
|
menus_rolelist = {
|
type: 'navbar',
|
version: '1.0',
|
key: config.uuid,
|
title: config.name,
|
children: []
|
}
|
|
if (config.wrap.permission === 'true') {
|
menus_rolelist.children = config.menus.map(fst => {
|
if (fst.property === 'classify' && fst.sublist.length > 0) {
|
return {
|
key: fst.MenuID,
|
title: fst.name,
|
children: fst.sublist.map(scd => {
|
if (scd.property === 'classify' && scd.sublist.length > 0) {
|
return {
|
key: scd.MenuID,
|
title: scd.name,
|
children: scd.sublist.map(thd => {
|
return { key: thd.MenuID, title: thd.name }
|
})
|
}
|
} else {
|
return { key: scd.MenuID, title: scd.name }
|
}
|
})
|
}
|
} else {
|
return { key: fst.MenuID, title: fst.name }
|
}
|
})
|
} else {
|
menus_rolelist.pass = true
|
}
|
menus_rolelist = window.btoa(window.encodeURIComponent(JSON.stringify(menus_rolelist)))
|
} else {
|
setLangTrans(config, btnDict, titDict, lisDict, menuDict, regs, tail)
|
|
if (config.components) {
|
let commonId = Utils.getuuid()
|
if (config.interfaces && config.interfaces.length > 0) {
|
config.interfaces = config.interfaces.map(inter => {
|
inter.uuid = md5(commonId + inter.uuid)
|
return inter
|
})
|
}
|
config.components = MenuUtils.resetConfig(config.components, commonId, false)
|
config.tables = config.tables || []
|
config.style = config.style || {}
|
}
|
|
config.enabled = false
|
config.Template = 'webPage'
|
if (config.MenuNo) {
|
config.MenuNo = config.MenuNo + '_' + app.lang
|
}
|
if (config.MenuName === menuDict[config.MenuName]) {
|
config.MenuName = menuDict[config.MenuName]
|
} else {
|
config.MenuName = config.MenuName + '_' + app.lang
|
}
|
}
|
|
delete config.tbkey
|
|
config.uuid = values.slice(0, 24) + tail
|
config.MenuID = config.uuid
|
config.open_edition = ''
|
|
let param = {
|
func: 'sPC_TrdMenu_AddUpt',
|
FstID: 'mk_app',
|
SndID: 'mk_app',
|
ParentID: 'mk_app',
|
MenuID: config.uuid,
|
MenuNo: config.MenuNo,
|
EasyCode: '',
|
TypeCharOne: kei_no,
|
Typename: app.typename,
|
Template: config.Template || config.type,
|
MenuName: config.MenuName || config.name || '',
|
PageParam: JSON.stringify({Template: config.Template || config.type}),
|
lang: app.lang
|
}
|
|
if (menus_rolelist) {
|
param.menus_rolelist = menus_rolelist
|
}
|
|
Api.getCloudConfig({
|
func: 'sPC_Get_LongParam',
|
TypeCharOne: kei_no,
|
typename: app.typename,
|
MenuID: config.MenuID,
|
lang: app.lang
|
}).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
this.setState({saveing: false})
|
return
|
}
|
|
config.open_edition = res.open_edition || ''
|
param.open_edition = config.open_edition
|
param.LongParam = window.btoa(window.encodeURIComponent(JSON.stringify(config)))
|
|
if (res.LongParam) {
|
const that = this
|
confirm({
|
title: '菜单已存在,确定重新生成吗?',
|
content: '',
|
onOk() {
|
that.setMenu(param, config)
|
},
|
onCancel() {}
|
})
|
} else {
|
this.setMenu(param, config)
|
}
|
})
|
})
|
}
|
|
setMenu = (param, config) => {
|
const { app } = this.props
|
const { kei_no } = this.state
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
|
Api.getCloudConfig(param).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
this.setState({saveing: false})
|
return
|
}
|
|
if (config.type === 'navbar') {
|
Api.getCloudConfig({
|
func: 's_get_keyids',
|
bid: app.ID
|
}).then(res => {
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
this.setState({saveing: false})
|
return
|
}
|
|
let appViewList = res.data || []
|
appViewList = appViewList.filter(item => item.keys_id !== config.uuid)
|
appViewList.unshift({
|
appkey: window.GLOB.appkey || '',
|
bid: app.ID,
|
kei_no: kei_no || '',
|
keys_id: config.uuid,
|
keys_type: 'navbar',
|
remark: config.name
|
})
|
|
let param = {
|
func: 's_kei_link_keyids_addupt',
|
BID: app.ID,
|
exec_type: 'x',
|
LText: ''
|
}
|
|
param.LText = appViewList.map(item => `select '${item.keys_id}','${item.keys_type}','${item.kei_no}','${item.appkey}','${item.bid}','${sessionStorage.getItem('CloudUserID')}','${item.remark}'`)
|
param.LText = param.LText.join(' union all ')
|
param.LText = Utils.formatOptions(param.LText, 'x')
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
|
Api.getCloudConfig(param).then(result => {
|
if (!result.status) {
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 5
|
})
|
this.setState({saveing: false})
|
return
|
}
|
|
notification.success({
|
top: 92,
|
message: '添加成功。',
|
duration: 5
|
})
|
|
this.setState({
|
loading: false,
|
visible: false
|
})
|
})
|
})
|
} else {
|
notification.success({
|
top: 92,
|
message: '添加成功。',
|
duration: 5
|
})
|
|
this.setState({
|
loading: false,
|
visible: false
|
})
|
}
|
})
|
}
|
|
cancel = () => {
|
this.setState({
|
loading: false,
|
visible: false
|
})
|
}
|
|
render () {
|
const { supApp, app } = this.props
|
const { visible, loading, saveing, menus } = this.state
|
|
if (supApp.sublist.findIndex(item => item.typename === app.typename && item.lang === 'zh-CN') === -1) return null
|
|
return (
|
<>
|
<SwapOutlined onClick={this.trigger}/>
|
<Modal
|
wrapClassName="sync-menu-modal"
|
title="菜单转换"
|
visible={visible}
|
width={600}
|
closable={false}
|
maskClosable={false}
|
onOk={this.submit}
|
onCancel={this.cancel}
|
confirmLoading={saveing}
|
destroyOnClose
|
>
|
{loading ? <Spin /> : <div>
|
<Select
|
showSearch
|
placeholder="请选择菜单"
|
dropdownMatchSelectWidth={false}
|
onChange={(val) => this.setState({values: val})}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
>
|
{menus.map((item, i) => (<Select.Option key={i} value={item.MenuID}>{`${item.MenuName}(${item.MenuNo})`}</Select.Option>))}
|
</Select>
|
</div>}
|
</Modal>
|
</>
|
)
|
}
|
}
|
|
export default TransMenu
|