import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Spin, notification, Modal } from 'antd'
|
|
import Api from '@/api'
|
import UtilsDM from '@/utils/utils-datamanage.js'
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
class SandBoxComponent extends Component {
|
static propTpyes = {
|
config: PropTypes.object
|
}
|
|
state = {
|
BID: '', // 上级ID
|
config: null, // 图表配置信息
|
loading: false, // 数据加载状态
|
data: [], // 数据
|
html: '',
|
result: {}
|
}
|
|
loaded = false
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
let _config = fromJS(config).toJS()
|
let _data = []
|
let BID = ''
|
let BData = ''
|
|
if (_config.setting.supModule) {
|
BData = window.GLOB.CacheData.get(_config.setting.supModule)
|
} else {
|
BData = window.GLOB.CacheData.get(_config.$pageId)
|
}
|
if (BData) {
|
BID = BData.$BID || ''
|
}
|
|
if (_config.wrap.datatype !== 'static') {
|
_config.setting.onload = _config.setting.sync === 'true' ? 'false' : 'true'
|
|
if (_config.setting.sync === 'true' && window.GLOB.SyncData.has(_config.dataName)) {
|
_data = window.GLOB.SyncData.get(_config.dataName) || []
|
|
if (_config.$cache) {
|
Api.writeCacheConfig(_config.uuid, fromJS(_data).toJS())
|
}
|
|
_config.setting.sync = 'false'
|
|
this.loaded = true
|
|
window.GLOB.SyncData.delete(_config.dataName)
|
}
|
}
|
|
if (_config.css) {
|
let node = document.getElementById(_config.uuid)
|
node && node.remove()
|
|
let ele = document.createElement('style')
|
ele.id = _config.uuid
|
ele.innerHTML = _config.css
|
document.getElementsByTagName('head')[0].appendChild(ele)
|
}
|
|
this.setState({
|
data: _data,
|
BID: BID || '',
|
config: _config,
|
arr_field: _config.columns.map(col => col.field).join(',')
|
})
|
}
|
|
componentDidMount () {
|
const { config } = this.state
|
|
MKEmitter.addListener('reloadData', this.reloadData)
|
|
if (config.setting.useMSearch) {
|
MKEmitter.addListener('searchRefresh', this.searchRefresh)
|
}
|
|
if (config.setting.sync === 'true') {
|
MKEmitter.addListener('transferSyncData', this.transferSyncData)
|
}
|
|
this.initExec()
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('reloadData', this.reloadData)
|
MKEmitter.removeListener('searchRefresh', this.searchRefresh)
|
MKEmitter.removeListener('transferSyncData', this.transferSyncData)
|
}
|
|
initExec = () => {
|
const { config } = this.state
|
|
if (config.wrap.datatype !== 'static') {
|
if (config.$cache) {
|
if (config.$time) {
|
if (!this.loaded) {
|
Api.getLCacheConfig(config.uuid, config.$time).then(res => {
|
if (!res && config.setting.onload === 'true') {
|
setTimeout(() => {
|
this.loadData()
|
}, config.setting.delay || 0)
|
}
|
|
if (!res || this.loaded) return
|
|
this.setState({data: res}, () => {
|
this.renderView()
|
})
|
})
|
} else {
|
this.renderView()
|
}
|
} else {
|
if (!this.loaded) {
|
Api.getLCacheConfig(config.uuid, 0).then(res => {
|
if (!res || this.loaded) return
|
|
this.setState({data: res}, () => {
|
this.renderView()
|
})
|
})
|
}
|
|
if (config.setting.onload === 'true') {
|
setTimeout(() => {
|
this.loadData()
|
}, config.setting.delay || 0)
|
} else if (this.loaded) {
|
this.renderView()
|
}
|
}
|
} else if (config.setting.onload === 'true') {
|
setTimeout(() => {
|
this.loadData()
|
}, config.setting.delay || 0)
|
} else if (this.loaded) {
|
this.renderView()
|
}
|
} else {
|
this.renderView()
|
}
|
}
|
|
transferSyncData = (syncId) => {
|
const { config } = this.state
|
|
if (config.$syncId !== syncId) return
|
|
let _data = window.GLOB.SyncData.get(config.dataName) || []
|
|
if (config.$cache) {
|
Api.writeCacheConfig(config.uuid, fromJS(_data).toJS())
|
}
|
|
if (!is(fromJS(this.state.data), fromJS(_data))) {
|
setTimeout(() => {
|
this.renderView()
|
}, 10)
|
}
|
|
this.setState({data: _data})
|
|
this.loaded = true
|
|
window.GLOB.SyncData.delete(config.dataName)
|
|
MKEmitter.removeListener('transferSyncData', this.transferSyncData)
|
}
|
|
searchRefresh = (searchId) => {
|
const { config } = this.state
|
|
if (config.$searchId !== searchId) return
|
|
this.setState({}, () => {
|
this.loadData()
|
})
|
}
|
|
reloadData = (menuId) => {
|
const { config } = this.state
|
|
if (menuId !== config.uuid) return
|
|
this.loadData()
|
}
|
|
async loadData () {
|
const { config, arr_field, BID } = this.state
|
|
if (config.wrap.datatype === 'static') {
|
this.setState({
|
data: [],
|
loading: false
|
})
|
return
|
} else if (config.setting.supModule && !BID) { // BID 不存在时,不做查询
|
this.setState({
|
data: [],
|
loading: false
|
})
|
this.loaded = true
|
return
|
}
|
|
let searches = []
|
if (config.setting.useMSearch) { // 主表搜索条件
|
searches = window.GLOB.SearchBox.get(config.$searchId) || []
|
}
|
|
if (config.$s_req && searches.filter(item => item.required && item.value === '').length > 0) {
|
return
|
}
|
|
this.setState({
|
loading: true
|
})
|
|
let _orderBy = config.setting.order || ''
|
let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, 1, 1, BID)
|
|
let result = await Api.genericInterface(param)
|
if (result.status) {
|
let _data = result.data || []
|
|
this.loaded = true
|
if (config.$cache && config.setting.onload !== 'false') {
|
Api.writeCacheConfig(config.uuid, result.data || [])
|
}
|
|
if (!is(fromJS(this.state.data), fromJS(_data))) {
|
setTimeout(() => {
|
this.renderView()
|
}, 10)
|
}
|
|
let _result = {...result}
|
delete _result.data
|
|
this.setState({
|
data: _data,
|
result: _result,
|
loading: false
|
})
|
|
if (result.message) {
|
if (result.ErrCode === 'Y') {
|
Modal.success({
|
title: result.message
|
})
|
} else if (result.ErrCode === 'S') {
|
notification.success({
|
top: 92,
|
message: result.message,
|
duration: 2
|
})
|
}
|
}
|
} else {
|
this.setState({
|
loading: false
|
})
|
|
if (!result.message) return
|
if (result.ErrCode === 'N') {
|
Modal.error({
|
title: result.message
|
})
|
} else if (result.ErrCode !== '-2') {
|
notification.error({
|
top: 92,
|
message: result.message,
|
duration: 10
|
})
|
}
|
}
|
}
|
|
renderView = () => {
|
const { data, result } = this.state
|
const { html, js, wrap, columns } = this.state.config
|
|
let _html = html
|
if (_html && wrap.datatype !== 'static') {
|
if (/@[\u4E00-\u9FA50-9a-zA-Z_]+@/ig.test(_html)) {
|
let _data = data[0] || {}
|
|
columns.forEach(col => {
|
if (col.field) {
|
let val = _data[col.field] !== undefined ? _data[col.field] : ''
|
let reg = new RegExp('@' + col.field + '@', 'ig')
|
|
_html = _html.replace(reg, val)
|
}
|
})
|
}
|
}
|
|
this.setState({html: _html}, () => {
|
if (js) {
|
try {
|
// eslint-disable-next-line
|
let evalfunc = eval('(true && function (data, result) {' + js + '})')
|
evalfunc(data, result)
|
} catch (e) {
|
console.warn(e)
|
}
|
}
|
})
|
}
|
|
render() {
|
const { config, loading, html } = this.state
|
|
return (
|
<div className="custom-sand-box" id={'anchor' + config.uuid} style={{...config.style}}>
|
{loading ?
|
<div className="loading-mask">
|
<div className="ant-spin-blur"></div>
|
<Spin />
|
</div> : null
|
}
|
<div dangerouslySetInnerHTML={{ __html: html }}></div>
|
</div>
|
)
|
}
|
}
|
|
export default SandBoxComponent
|