import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { notification, Spin, Row, Col } from 'antd'
|
|
import Api from '@/api'
|
import zhCN from '@/locales/zh-CN/main.js'
|
import enUS from '@/locales/en-US/main.js'
|
import Utils from '@/utils/utils.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import MKEmitter from '@/utils/events.js'
|
import NotFount from '@/components/404'
|
import './index.scss'
|
|
// 通用组件
|
const AntvTabs = asyncComponent(() => import('@/tabviews/custom/components/tabs/antv-tabs'))
|
const MkBaseTable = asyncComponent(() => import('@/tabviews/custom/components/table/base-table'))
|
const SettingComponent = asyncComponent(() => import('@/tabviews/zshare/settingcomponent'))
|
const TableNodes = asyncComponent(() => import('@/tabviews/zshare/tablenodes'))
|
const AutoMatic = asyncComponent(() => import('@/tabviews/zshare/automatic'))
|
const DebugTable = asyncComponent(() => import('@/tabviews/debugtable'))
|
|
class BasePage extends Component {
|
static propTpyes = {
|
param: PropTypes.any, // 其他页面传递的参数
|
Tab: PropTypes.string, // 弹窗标签
|
MenuID: PropTypes.string, // 菜单Id
|
MenuName: PropTypes.string, // 菜单名称
|
changeTemp: PropTypes.func
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
ContainerId: Utils.getuuid(), // 菜单外层html Id
|
BID: '', // 页面跳转时携带ID
|
loadingview: true, // 页面加载中
|
viewlost: false, // 页面丢失:1、未获取到配置-页面丢失;2、页面未启用
|
lostmsg: '', // 页面丢失时的提示信息
|
config: null, // 页面配置信息,包括组件等
|
mainSearch: null, // 主搜索
|
userConfig: null, // 用户自定义设置
|
data: null, // 列表数据集
|
loading: false, // 列表数据加载中
|
visible: false, // 标签页控制
|
shortcuts: null, // 快捷键
|
autoMatic: null
|
}
|
|
/**
|
* @description 获取页面配置信息
|
*/
|
async loadconfig () {
|
const { MenuID, MenuName } = this.props
|
|
let _param = {
|
func: 'sPC_Get_LongParam',
|
MenuID: MenuID
|
}
|
|
let result = await Api.getCacheConfig(_param)
|
|
if (result.status) {
|
let config = ''
|
let shortcuts = []
|
|
try { // 配置信息解析
|
config = window.decodeURIComponent(window.atob(result.LongParam))
|
config = config.replace(/@mywebsite@\//ig, window.GLOB.baseurl)
|
config = JSON.parse(config)
|
config.MenuID = MenuID
|
config.MenuName = MenuName || config.MenuName
|
} catch (e) {
|
console.warn('Parse Failure')
|
config = ''
|
}
|
|
// 页面配置解析错误时提示
|
if (!config) {
|
this.setState({
|
viewlost: true,
|
loadingview: false
|
})
|
return
|
}
|
|
// 页面未启用时,显示未启用页面
|
if (!config.enabled) {
|
this.setState({
|
viewlost: true,
|
loadingview: false,
|
lostmsg: this.state.dict['main.view.unenabled']
|
})
|
return
|
}
|
|
// 模板错误
|
if (config.Template !== 'BaseTable') {
|
if (config.Template === 'CustomPage' || config.Template === 'CommonTable') {
|
this.props.changeTemp(MenuID, config.Template)
|
} else {
|
this.setState({
|
viewlost: true,
|
loadingview: false,
|
lostmsg: '菜单信息错误,可能原因:1、当前用户无权限;2、打开此菜单的按钮需要更新。'
|
})
|
}
|
return
|
}
|
|
// HS不使用自定义设置
|
if (result.LongParamUser && !window.GLOB.mkHS) {
|
try { // 配置信息解析
|
let userConfig = JSON.parse(window.decodeURIComponent(window.atob(result.LongParamUser)))
|
if (userConfig) {
|
shortcuts = userConfig.action
|
userConfig.printers.forEach(item => {
|
window.GLOB.UserCacheMap.set(item.parentId + item.uuid, item)
|
})
|
}
|
} catch (e) {
|
console.warn('Parse Failure')
|
}
|
}
|
|
// 权限过滤
|
let roleId = sessionStorage.getItem('role_id') || '' // 角色ID
|
let skip = window.GLOB.mkHS
|
let param = this.props.param || {} // url参数
|
|
window.GLOB.CacheData.set(MenuID, param)
|
|
let userName = sessionStorage.getItem('User_Name') || ''
|
let fullName = sessionStorage.getItem('Full_Name') || ''
|
|
if (sessionStorage.getItem('isEditState') === 'true') {
|
userName = sessionStorage.getItem('CloudUserName') || ''
|
fullName = sessionStorage.getItem('CloudFullName') || ''
|
}
|
|
let regs = [
|
{ reg: /@userName@/ig, value: `'${userName}'` },
|
{ reg: /@fullName@/ig, value: `'${fullName}'` }
|
]
|
|
if (window.GLOB.externalDatabase !== null) {
|
regs.push({
|
reg: /@db@/ig,
|
value: window.GLOB.externalDatabase
|
})
|
}
|
if (config.urlFields) {
|
config.urlFields.forEach(field => {
|
let val = `'${param[field] || ''}'`
|
regs.push({
|
reg: new RegExp('@' + field + '@', 'ig'),
|
value: val
|
})
|
})
|
}
|
|
config.components = this.filterComponent(config.components, roleId, window.GLOB.mkActions, skip, param, MenuID, config.MenuName)
|
|
let autoMatic = null
|
if (config.autoMatic && config.autoMatic.enable === 'true') {
|
config.components[0].action.forEach(item => {
|
if (item.uuid === config.autoMatic.action) {
|
autoMatic = config.autoMatic
|
autoMatic.OpenType = item.execMode || item.OpenType
|
config.components[0].wrap.selected = 'false'
|
config.components[0].MenuID = config.components[0].uuid
|
config.components[0].autoMatic = true
|
item.autoMatic = true
|
}
|
})
|
}
|
|
// 获取主搜索条件
|
let mainSearch = []
|
config.components.forEach(component => {
|
if (component.type === 'tabs') return
|
|
component.search = component.search.map(item => {
|
item.oriInitval = item.initval
|
if (['text', 'select', 'link'].includes(item.type) && param.$searchkey === item.field) {
|
item.initval = param.$searchval
|
}
|
|
return item
|
})
|
|
mainSearch = Utils.initMainSearch(component.search)
|
})
|
|
let BID = param.$BID || ''
|
|
config.components = this.formatSetting(config.components, regs)
|
|
this.setState({
|
BID: BID,
|
shortcuts: shortcuts.length > 0 ? shortcuts : null,
|
config,
|
autoMatic,
|
mainSearch
|
}, () => {
|
setTimeout(() => { // 延时加载状态
|
this.setState({
|
loadingview: false
|
})
|
}, 1000)
|
|
if (!this.props.Tab) {
|
this.setShortcut()
|
}
|
})
|
} else {
|
this.setState({
|
loadingview: false,
|
viewlost: true
|
})
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 5
|
})
|
}
|
}
|
|
setShortcut = () => {
|
const { shortcuts } = this.state
|
|
document.onkeydown = (event) => {
|
let e = event || window.event
|
let keyCode = e.keyCode || e.which || e.charCode
|
let preKey = ''
|
|
if (e.ctrlKey) {
|
preKey = 'ctrl'
|
} else if (e.shiftKey) {
|
preKey = 'shift'
|
} else if (e.altKey) {
|
preKey = 'alt'
|
}
|
|
if (!preKey || !keyCode) return
|
|
let _shortcut = `${preKey}+${keyCode}`
|
|
if (window.GLOB.breakpoint && _shortcut === 'ctrl+67') {
|
window.debugger = false
|
window.GLOB.breakpoint = false
|
sessionStorage.removeItem('breakpoint')
|
|
MKEmitter.emit('debugChange')
|
}
|
|
if (!shortcuts) return
|
|
shortcuts.some(item => {
|
if (item.$shortcut === _shortcut) {
|
MKEmitter.emit('triggerBtnId', item.uuid)
|
return true
|
}
|
return false
|
})
|
}
|
}
|
|
filterComponent = (components, roleId, permAction, skip, urlparam, pageId, MenuName) => {
|
return components.filter(item => {
|
item.$pageId = pageId
|
|
item.$menuname = (MenuName || '') + '-' + (item.name || '主表')
|
|
if (item.type === 'tabs') {
|
item.subtabs = item.subtabs.filter(tab => {
|
if (!skip && !permAction[tab.components[0].uuid] && tab.permission === 'true') {
|
return false
|
} else if (tab.hide === 'true') {
|
return false
|
}
|
return true
|
})
|
|
if (item.subtabs.length === 0) return false
|
|
item.subtabs = item.subtabs.map(tab => {
|
tab.components[0].name = tab.label
|
if (tab.permission !== 'true') { // 权限未开启不做权限控制
|
skip = true
|
}
|
tab.components = this.filterComponent(tab.components, roleId, permAction, skip, urlparam, pageId, MenuName)
|
return tab
|
})
|
|
return true
|
} else {
|
item.name = (MenuName || '')
|
}
|
|
// 搜索条件初始化
|
if (item.search && item.search.length > 0) {
|
item.search = Utils.initSearchVal(item.search)
|
}
|
|
if (item.setting.supModule) {
|
let pid = item.setting.supModule.pop()
|
if (pid && pid !== 'empty') {
|
item.setting.supModule = pid
|
} else {
|
item.setting.supModule = ''
|
}
|
}
|
|
let statFields = []
|
let getCols = (cols) => {
|
return cols.filter(col => {
|
if (col.blacklist && col.blacklist.filter(v => roleId.indexOf(v) > -1).length > 0) {
|
return false
|
} else if (col.Hide === 'true') {
|
return false
|
} else if (col.type === 'action') {
|
col.type = 'custom'
|
}
|
|
if (col.type === 'number') {
|
if (col.sum === 'true') {
|
statFields.push(col)
|
}
|
if (typeof(col.decimal) === 'number') {
|
col.round = Math.pow(10, col.decimal)
|
if (col.format === 'percent') {
|
col.decimal = col.decimal > 2 ? col.decimal - 2 : 0
|
}
|
}
|
} else if (col.type === 'colspan') {
|
col.subcols = getCols(col.subcols || [])
|
if (col.subcols.length === 0) {
|
return false
|
}
|
} else if (col.type === 'custom') {
|
col.elements = col.elements.filter(cell => {
|
if (cell.eleType === 'button') {
|
if (cell.hidden === 'true') return false
|
|
cell.logLabel = item.$menuname + '-' + cell.label
|
cell.Ot = cell.Ot || 'requiredSgl'
|
cell.ContainerId = this.state.ContainerId
|
cell.syncComponentId = cell.syncComponent ? (cell.syncComponent.pop() || '') : ''
|
cell.$menuId = item.uuid
|
cell.$MenuID = this.props.MenuID
|
cell.$view = 'popview'
|
|
if (cell.syncComponentId) {
|
if (cell.syncComponentId === item.setting.supModule) {
|
cell.syncComponentId = ''
|
if (cell.execSuccess === 'line' || cell.execSuccess === 'grid') {
|
cell.execSuccess = 'mainline'
|
}
|
} else if (cell.syncComponentId === 'multiComponent') {
|
let ids = cell.syncComponents.map(m => {
|
return m.syncComId.pop() || ''
|
})
|
|
if (item.setting.supModule && ids.includes(item.setting.supModule)) {
|
if (cell.execSuccess === 'line' || cell.execSuccess === 'grid') {
|
cell.execSuccess = 'mainline'
|
}
|
ids = ids.filter(id => id !== item.setting.supModule)
|
}
|
|
if (ids.length === 0) {
|
cell.syncComponentId = ''
|
} else {
|
cell.syncComponentIds = ids
|
}
|
}
|
}
|
|
if (cell.OpenType === 'funcbutton' && cell.funcType === 'print' && cell.verify) { // 打印机设置
|
cell = this.getPrinter(cell, item.uuid)
|
}
|
|
if (cell.controlField) {
|
if (/,/ig.test(cell.controlVal)) {
|
cell.controlVals = cell.controlVal.split(',')
|
} else {
|
cell.controlVals = [(cell.controlVal || '')]
|
}
|
}
|
|
return skip || permAction[cell.uuid]
|
} else if (['text', 'number', 'formula'].includes(cell.eleType)) {
|
if (!cell.height) {
|
cell.innerHeight = 'auto'
|
}
|
if (cell.eleType === 'number' && typeof(cell.decimal) === 'number') {
|
cell.round = Math.pow(10, cell.decimal)
|
if (cell.format === 'percent') {
|
cell.decimal = cell.decimal > 2 ? cell.decimal - 2 : 0
|
}
|
}
|
}
|
return true
|
})
|
|
if (col.elements.length === 0) {
|
return false
|
}
|
}
|
|
if (col.linkmenu && col.linkmenu.length > 0) {
|
let menu_id = col.linkmenu.pop()
|
col.linkThdMenu = window.GLOB.mkThdMenus.filter(m => m.MenuID === menu_id)[0] || ''
|
} else {
|
col.linkThdMenu = ''
|
}
|
|
return true
|
})
|
}
|
|
item.cols = getCols(item.cols)
|
item.statFields = Array.from(new Set(statFields))
|
|
// 权限过滤
|
if (item.action && item.action.length > 0) {
|
item.action = item.action.filter(cell => {
|
if (cell.hidden === 'true') return false
|
|
cell.logLabel = item.$menuname + '-' + cell.label
|
cell.ContainerId = this.state.ContainerId
|
cell.syncComponentId = cell.syncComponent ? (cell.syncComponent.pop() || '') : ''
|
cell.$menuId = item.uuid
|
cell.$MenuID = this.props.MenuID
|
cell.$view = 'popview'
|
cell.$toolbtn = true
|
|
if (cell.syncComponentId) {
|
if (cell.syncComponentId === item.setting.supModule) {
|
cell.syncComponentId = ''
|
if (cell.execSuccess === 'line' || cell.execSuccess === 'grid') {
|
cell.execSuccess = 'mainline'
|
}
|
} else if (cell.syncComponentId === 'multiComponent') {
|
let ids = cell.syncComponents.map(m => {
|
return m.syncComId.pop() || ''
|
})
|
|
if (item.setting.supModule && ids.includes(item.setting.supModule)) {
|
if (cell.execSuccess === 'line' || cell.execSuccess === 'grid') {
|
cell.execSuccess = 'mainline'
|
}
|
ids = ids.filter(id => id !== item.setting.supModule)
|
}
|
|
if (ids.length === 0) {
|
cell.syncComponentId = ''
|
} else {
|
cell.syncComponentIds = ids
|
}
|
}
|
}
|
|
if (cell.OpenType === 'funcbutton' && cell.funcType === 'print' && cell.verify) { // 打印机设置
|
cell = this.getPrinter(cell, item.uuid)
|
}
|
|
if (cell.controlField) {
|
if (/,/ig.test(cell.controlVal)) {
|
cell.controlVals = cell.controlVal.split(',')
|
} else {
|
cell.controlVals = [(cell.controlVal || '')]
|
}
|
}
|
|
return skip || permAction[cell.uuid]
|
})
|
}
|
|
return true
|
})
|
}
|
|
getPrinter = (item, parentId) => {
|
let _item = window.GLOB.UserCacheMap.get(parentId + item.uuid)
|
|
if (_item) {
|
item.printer = _item.printer || ''
|
item.verify.defaultPrinter = _item.printer || ''
|
if (item.verify.printerTypeList && _item.printerList) {
|
item.verify.printerTypeList = item.verify.printerTypeList.map(cell => {
|
cell.printer = _item.printerList[cell.Value] || ''
|
|
return cell
|
})
|
}
|
}
|
|
return item
|
}
|
|
// 格式化默认设置
|
formatSetting = (components, regs) => {
|
let delay = 20
|
return components.map(component => {
|
if (component.type === 'tabs') {
|
component.subtabs = component.subtabs.map(tab => {
|
tab.components = this.formatSetting(tab.components, regs)
|
return tab
|
})
|
return component
|
}
|
|
component.setting.useMSearch = component.setting.useMSearch === 'true'
|
|
if (component.setting.interType !== 'system') { // 不使用系统函数时
|
component.setting.sync = 'false'
|
component.setting.laypage = component.setting.laypage === 'true'
|
return component
|
}
|
|
let _customScript = ''
|
component.scripts && component.scripts.forEach(script => {
|
if (script.status !== 'false') {
|
_customScript += `
|
${script.sql}
|
`
|
}
|
})
|
delete component.scripts
|
component.setting.$name = component.$menuname || ''
|
component.setting.execute = component.setting.execute !== 'false' // 默认sql是否执行,转为boolean 统一格式
|
component.setting.laypage = component.setting.laypage === 'true' // 是否分页,转为boolean 统一格式
|
|
if (!component.setting.execute) {
|
component.setting.dataresource = ''
|
}
|
if (/\s/.test(component.setting.dataresource)) {
|
component.setting.dataresource = '(' + component.setting.dataresource + ') tb'
|
}
|
|
if (sessionStorage.getItem('dataM') === 'true') { // 数据权限
|
component.setting.dataresource = component.setting.dataresource.replace(/\$@/ig, '/*').replace(/@datam@/ig, '\'Y\'')
|
component.setting.dataresource = component.setting.dataresource.replace(/@\$/ig, '*/')
|
_customScript = _customScript.replace(/\$@/ig, '/*').replace(/@datam@/ig, '\'Y\'')
|
_customScript = _customScript.replace(/@\$/ig, '*/')
|
} else {
|
component.setting.dataresource = component.setting.dataresource.replace(/@\$|\$@/ig, '').replace(/@datam@/ig, '\'\'')
|
_customScript = _customScript.replace(/@\$|\$@/ig, '').replace(/@datam@/ig, '\'\'')
|
}
|
|
regs.forEach(cell => {
|
component.setting.dataresource = component.setting.dataresource.replace(cell.reg, cell.value)
|
_customScript = _customScript.replace(cell.reg, cell.value)
|
})
|
|
component.setting.customScript = _customScript // 整理后自定义脚本
|
|
component.setting.delay = delay
|
delay += 20
|
|
return component
|
})
|
}
|
|
reloadMenuView = (menuId) => {
|
const { MenuID } = this.props
|
|
if (MenuID !== menuId) return
|
|
this.reloadview()
|
}
|
|
resetActiveMenu = (menuId) => {
|
const { MenuID, Tab } = this.props
|
|
if (MenuID !== menuId || Tab) return
|
|
this.setShortcut()
|
}
|
|
UNSAFE_componentWillMount () {
|
// 组件加载时,获取菜单数据
|
this.loadconfig()
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('debugChange', this.debugChange)
|
MKEmitter.addListener('reloadMenuView', this.reloadMenuView)
|
MKEmitter.addListener('resetActiveMenu', this.resetActiveMenu)
|
}
|
|
/**
|
* @description 组件销毁,清除state更新,清除快捷键设置
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('debugChange', this.debugChange)
|
MKEmitter.removeListener('reloadMenuView', this.reloadMenuView)
|
MKEmitter.removeListener('resetActiveMenu', this.resetActiveMenu)
|
|
window.GLOB.CacheData.delete(this.props.MenuID)
|
if (this.state.config) {
|
this.deleteCache(this.state.config.components)
|
}
|
}
|
|
debugChange = () => {
|
this.setState({visible: !this.state.visible})
|
}
|
|
deleteCache = (components) => {
|
components.forEach(item => {
|
if (item.type === 'tabs') {
|
item.subtabs.forEach(tab => {
|
this.deleteCache(tab.components)
|
})
|
} else {
|
window.GLOB.CacheData.delete(item.uuid)
|
}
|
})
|
}
|
|
reloadview = () => {
|
window.GLOB.CacheData.delete(this.props.MenuID)
|
if (this.state.config) {
|
this.deleteCache(this.state.config.components)
|
}
|
|
this.setState({
|
BID: '', // 页面跳转时携带ID
|
loadingview: true, // 页面加载中
|
viewlost: false, // 页面丢失:1、未获取到配置-页面丢失;2、页面未启用
|
config: null, // 页面配置信息,包括组件等
|
loading: false, // 列表数据加载中
|
shortcuts: null,
|
data: ''
|
}, () => {
|
this.loadconfig()
|
})
|
}
|
|
resetSearch = (search) => {
|
this.setState({mainSearch: null}, () => {
|
this.setState({mainSearch: search})
|
})
|
}
|
|
getComponents = () => {
|
const { config, data, mainSearch } = this.state
|
|
if (!config || !config.components) return
|
|
return config.components.map(item => {
|
if (item.type === 'tabs') {
|
return (
|
<Col span={item.width} key={item.uuid}>
|
<AntvTabs config={item} mainSearch={mainSearch} />
|
</Col>
|
)
|
} else {
|
return (
|
<Col span={item.width} key={item.uuid}>
|
<MkBaseTable config={item} data={data}/>
|
</Col>
|
)
|
}
|
})
|
}
|
|
render() {
|
const { loadingview, viewlost, config, loading, shortcuts, autoMatic } = this.state
|
|
return (
|
<div className={'custom-table-wrap ' + (loadingview || loading ? 'loading' : '')} id={this.state.ContainerId} style={config ? config.style : null}>
|
{(loadingview || loading) ? <Spin className="view-spin" size="large" /> : null}
|
<Row className="component-wrap">{this.getComponents()}</Row>
|
{config && window.GLOB.breakpoint ? <DebugTable /> : null}
|
{!window.GLOB.mkHS && config && autoMatic ? <AutoMatic autoMatic={autoMatic} tabId={config.MenuID} config={config.components[0]} /> : null}
|
{!window.GLOB.mkHS && window.GLOB.systemType !== 'production' ? <TableNodes config={config} /> : null}
|
{!window.GLOB.mkHS && config ? <SettingComponent config={config} dict={this.state.dict} shortcuts={shortcuts || []}/> : null}
|
{viewlost ? <NotFount msg={this.state.lostmsg} /> : null}
|
</div>
|
)
|
}
|
}
|
|
export default BasePage
|