import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { notification, Switch} from 'antd'
|
|
import Api from '@/api'
|
import zhCN from '@/locales/zh-CN/main.js'
|
import enUS from '@/locales/en-US/main.js'
|
import options from '@/store/options.js'
|
import { buttonConfig, tabConfig, refCodes } from './config'
|
|
import asyncSpinComponent from '@/utils/asyncSpinComponent'
|
import SubAction from './actionList'
|
|
import './index.scss'
|
|
const SubTable = asyncSpinComponent(() => import('@/tabviews/zshare/normalTable'))
|
|
class VerupSubTabViewTable extends Component {
|
static propTpyes = {
|
menuType: PropTypes.any, // 菜单类型,普通菜单或HS
|
Tab: PropTypes.object, // 标签信息
|
BID: PropTypes.string, // 上级数据ID
|
BData: PropTypes.any, // 上级数据
|
MenuID: PropTypes.string, // 菜单Id
|
SupMenuID: PropTypes.string, // 上级菜单Id
|
ContainerId: PropTypes.any, // 三级菜单Container(html) ID
|
handleMainTable: PropTypes.func, // 刷新主表
|
refreshtabs:PropTypes.any
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
config: null, // 页面配置信息,包括按钮、搜索、显示列、标签等
|
actions: null, // 按钮集
|
columns: null, // 显示列
|
setting: null, // 页面全局设置:数据源、按钮及显示列固定、主键等
|
data: null, // 列表数据集
|
selectedData: [], // 已选表格数据
|
resetTable: false, // 表格重置,值在true与false之间切换,切换时表格重置
|
loading: false, // 列表数据加载中
|
visible: false, // 弹框显示隐藏控制
|
pickup: false, // 子表数据隐藏显示切换
|
}
|
|
/**
|
* @description 上级菜单id变化时,刷新数据
|
*/
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
if (this.state.config && this.props.Tab.supMenu && !is(fromJS(this.props.BID), fromJS(nextProps.BID))) {
|
this.setState({
|
pageIndex: 1,
|
selectedData: [],
|
resetTable: !this.state.resetTable,
|
}, () => {
|
this.loadmaindata(nextProps.BID, 'refresh')
|
})
|
} else if (this.state.config && nextProps.refreshtabs && nextProps.refreshtabs.includes(this.props.Tab.uuid)) {
|
|
this.reloadtable()
|
}
|
}
|
|
/**
|
* @description 获取页面配置信息
|
*/
|
async loadconfig () {
|
const { Tab, BID } = this.props
|
|
let config = tabConfig[this.props.MenuID]
|
|
let _arrField = [] // 字段集
|
let _columns = [] // 显示列
|
let _hideCol = [] // 隐藏及合并列中字段的uuid集
|
let colMap = new Map()
|
|
// 1、筛选字段集,2、过滤隐藏列及合并列中的字段uuid
|
config.columns.forEach(col => {
|
if (col.field) {
|
_arrField.push(col.field)
|
|
}
|
if (col.type === 'colspan' && col.sublist) { // 筛选隐藏列
|
_hideCol = _hideCol.concat(col.sublist)
|
} else if (col.Hide === 'true') {
|
_hideCol.push(col.uuid)
|
}
|
colMap.set(col.uuid, col)
|
})
|
|
// 生成显示列,处理合并列中的字段
|
config.columns.forEach(col => {
|
if (_hideCol.includes(col.uuid)) return
|
|
if (col.type === 'colspan' && col.sublist) {
|
let _col = JSON.parse(JSON.stringify(col))
|
let subColumn = []
|
_col.sublist.forEach(sub => {
|
if (colMap.has(sub)) {
|
subColumn.push(colMap.get(sub))
|
}
|
})
|
_col.subColumn = subColumn
|
_columns.push(_col)
|
} else {
|
_columns.push(col)
|
}
|
})
|
|
config.setting.tabType = 'sub'
|
|
this.setState({
|
config: config,
|
setting: config.setting,
|
actions: config.action.map(item => {
|
if (buttonConfig[item.uuid]) {
|
item = {...buttonConfig[item.uuid], ...item}
|
}
|
return item
|
}),
|
columns: _columns
|
}, () => {
|
if (config.setting.onload !== 'false' && (!Tab.supMenu || BID)) { // 初始化可加载
|
this.setState({
|
loading: true
|
})
|
this.loadmaindata()
|
}
|
})
|
}
|
|
/**
|
* @description 子表数据加载
|
*/
|
async loadmaindata (bid, type) {
|
let _BID = this.props.BID
|
|
if (type === 'refresh') {
|
_BID = bid
|
if (!bid) { // 主表ID不存在时,不查询子表
|
this.setState({
|
data: [],
|
loading: false
|
})
|
return
|
}
|
}
|
|
let param = this.getCustomParam(_BID)
|
let result = await Api.getLocalConfig(param)
|
|
if (result.status) {
|
let str = result.secret_param
|
|
try {
|
if (str) {
|
str = str.substring(1)
|
str = window.atob(str)
|
refCodes.forEach(item => {
|
str = str.replace(new RegExp(item.md5str, 'g'), item.char)
|
})
|
str = JSON.parse(window.decodeURIComponent(window.atob(str)))
|
}
|
} catch {
|
str = ''
|
}
|
|
if (!str) {
|
this.setState({
|
loading: false,
|
pickup: false,
|
data: []
|
})
|
notification.error({
|
top: 92,
|
message: '密钥组数据丢失或格式错误,请恢复默认设置,重新添加!',
|
duration: 10
|
})
|
return
|
}
|
|
let _data = str.map((item, index) => {
|
item.key = index
|
return item
|
})
|
|
this.setState({
|
data: _data,
|
pickup: false,
|
loading: false
|
})
|
} else {
|
this.setState({
|
loading: false
|
})
|
notification.error({
|
top: 92,
|
message: result.message,
|
duration: 10
|
})
|
}
|
}
|
|
/**
|
* @description 获取用户自定义存储过程传参
|
*/
|
getCustomParam = (BID) => {
|
const { setting } = this.state
|
|
let param = {
|
BID: BID
|
}
|
|
if (setting.interType === 'inner') {
|
param.func = setting.innerFunc
|
} else {
|
if (setting.sysInterface === 'true' && options.cloudServiceApi) {
|
param.rduri = options.cloudServiceApi
|
} else if (setting.sysInterface !== 'true') {
|
param.rduri = setting.interface
|
}
|
|
if (setting.outerFunc) {
|
param.func = setting.outerFunc
|
}
|
}
|
|
return param
|
}
|
|
/**
|
* @description 表格条件改变时重置数据(分页或排序)
|
*/
|
refreshbytable = (pagination, filters, sorter) => {
|
let { data } = this.state
|
|
if (!data || data.length === 0 || !sorter.columnKey) return
|
|
if (sorter.order === 'asc' || sorter.order === 'ascend') {
|
data.sort((a, b) => {
|
return a[sorter.columnKey].localeCompare(b[sorter.columnKey])
|
})
|
} else if (sorter.order === 'desc' || sorter.order === 'descend') {
|
data.sort((a, b) => {
|
return b[sorter.columnKey].localeCompare(a[sorter.columnKey])
|
})
|
}
|
|
this.setState({data: data, selectedData: []})
|
}
|
|
/**
|
* @description 表格刷新
|
*/
|
reloadtable = () => {
|
this.setState({
|
loading: true,
|
selectedData: [],
|
resetTable: !this.state.resetTable
|
}, () => {
|
this.loadmaindata()
|
})
|
}
|
|
/**
|
* @description 页面刷新,重新获取配置
|
*/
|
reloadview = () => {
|
this.setState({
|
config: null, actions: null, columns: null, setting: null, data: null, loading: false, selectedData: []
|
}, () => {
|
this.loadconfig()
|
})
|
}
|
|
/**
|
* @description 按钮操作完成后(成功或失败),页面刷新,重置页码及选择项
|
*/
|
refreshbyaction = (position) => {
|
if (position === 'grid') {
|
this.reloadtable()
|
} else if (position === 'view') {
|
this.reloadview()
|
}
|
}
|
|
/**
|
* @description 表格选择项切换
|
*/
|
changeSelectedData = (selectedData) => {
|
this.setState({selectedData})
|
}
|
|
/**
|
* @description 数据展开合并切换
|
*/
|
pickupChange = () => {
|
const { pickup } = this.state
|
|
this.setState({
|
pickup: !pickup
|
})
|
}
|
|
UNSAFE_componentWillMount() {
|
// 组件加载时,获取菜单数据
|
this.loadconfig()
|
}
|
|
shouldComponentUpdate (nextProps, nextState) { // handleMainTable 函数判断时不相等
|
return !is(fromJS({...this.props, handleMainTable: ''}), fromJS({...nextProps, handleMainTable: ''})) || !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
}
|
|
render() {
|
const { setting, actions, columns, pickup, config, selectedData } = this.state
|
|
return (
|
<div className="secretKey-subtable" id={'subtable' + this.props.MenuID}>
|
<div className="sub-action">
|
<SubAction
|
setting={setting}
|
actions={actions}
|
Tab={this.props.Tab}
|
BID={this.props.BID}
|
datalist={this.state.data}
|
dict={this.state.dict}
|
selectedData={selectedData}
|
MenuID={this.props.SupMenuID}
|
refreshdata={this.refreshbyaction}
|
ContainerId={this.props.ContainerId}
|
/>
|
</div>
|
<div className="subtable-box">
|
{this.state.data && this.state.data.length > 0 ?
|
<Switch title="收起" className="subtable-pickup" checkedChildren="开" unCheckedChildren="关" defaultChecked={pickup} onChange={this.pickupChange} /> : null
|
}
|
<SubTable
|
tableId={this.props.Tab.uuid}
|
pickup={pickup}
|
config={config}
|
setting={setting}
|
columns={columns}
|
dict={this.state.dict}
|
data={this.state.data}
|
MenuID={this.props.MenuID}
|
loading={this.state.loading}
|
refreshdata={this.refreshbytable}
|
buttonTrigger={() => {}}
|
handleTableId={() => {}}
|
chgSelectData={this.changeSelectedData}
|
/>
|
</div>
|
</div>
|
)
|
}
|
}
|
|
export default VerupSubTabViewTable
|