From afb39f8ab004b2607bb718edab02e99c7a010114 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期三, 22 三月 2023 14:36:44 +0800 Subject: [PATCH] 2023-03-22 --- src/tabviews/custom/components/chart/custom-chart/index.jsx | 191 ++++++++++++++++++++++------------------------- 1 files changed, 89 insertions(+), 102 deletions(-) diff --git a/src/tabviews/custom/components/chart/custom-chart/index.jsx b/src/tabviews/custom/components/chart/custom-chart/index.jsx index f84f163..fb81260 100644 --- a/src/tabviews/custom/components/chart/custom-chart/index.jsx +++ b/src/tabviews/custom/components/chart/custom-chart/index.jsx @@ -5,22 +5,20 @@ import DataSet from '@antv/data-set' import * as echarts from 'echarts' import { Spin, Empty, notification } from 'antd' -import moment from 'moment' import Api from '@/api' import Utils from '@/utils/utils.js' import UtilsDM from '@/utils/utils-datamanage.js' import MKEmitter from '@/utils/events.js' +import TimerTask from '@/utils/timer-task.js' import NormalHeader from '@/tabviews/custom/components/share/normalheader' import './index.scss' class CustomChart extends Component { static propTpyes = { - BID: PropTypes.any, // 鐖剁骇Id data: PropTypes.array, // 缁熶竴鏌ヨ鏁版嵁 config: PropTypes.object, // 缁勪欢閰嶇疆淇℃伅 mainSearch: PropTypes.any, // 澶栧眰鎼滅储鏉′欢 - menuType: PropTypes.any, // 鑿滃崟绫诲瀷 } state = { @@ -30,30 +28,42 @@ loading: false, // 鏁版嵁鍔犺浇鐘舵�� sync: false, // 鏄惁缁熶竴璇锋眰鏁版嵁 plot: null, // 鍥捐〃璁剧疆 - data: null, // 鏁版嵁 search: null, // 鎼滅储鏉′欢 } + data = [] + UNSAFE_componentWillMount () { - const { config, data, initdata, BID } = this.props + const { config, data, initdata } = this.props let _config = fromJS(config).toJS() - let _data = null let _sync = config.setting.sync === 'true' - if (config.setting.sync === 'true' && data) { - _data = data[config.dataName] || [] + 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 (_sync && data) { + this.data = data[config.dataName] || [] _sync = false - } else if (config.setting.sync === 'true' && initdata) { - _data = initdata || [] + } else if (_sync && initdata) { + this.data = initdata || [] _sync = false } - _config.style.height = config.plot.height || 400 + _config.plot.height = Utils.getHeight(_config.plot.height) + _config.style.height = 'auto' this.setState({ config: _config, - data: _data, - empty: !_data || _data.length === 0, + empty: this.data.length === 0, BID: BID || '', arr_field: _config.columns.map(col => col.field).join(','), plot: _config.plot, @@ -64,10 +74,12 @@ setTimeout(() => { this.loadData() }, _config.setting.delay || 0) - } else if (config.setting.sync === 'true' && _data) { - this.handleData() } }) + + if (this.data.length > 0) { + this.handleData() + } } /** @@ -82,10 +94,13 @@ _data = nextProps.data[config.dataName] || [] } - this.setState({sync: false, data: _data, empty: _data.length === 0}, () => { + if (!is(fromJS(this.data), fromJS(_data))) { + this.data = _data this.handleData() - }) - } else if (config.setting.syncRefresh && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { + } + + this.setState({sync: false, empty: _data.length === 0}) + } else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { this.setState({}, () => { this.loadData() }) @@ -97,18 +112,37 @@ } componentDidMount () { + const { config, sync } = this.state + MKEmitter.addListener('reloadData', this.reloadData) MKEmitter.addListener('resetSelectLine', this.resetParentParam) MKEmitter.addListener('queryModuleParam', this.queryModuleParam) MKEmitter.addListener('refreshByButtonResult', this.refreshByButtonResult) - this.handleTimer() + + if (config.timer) { + this.timer = new TimerTask() + this.timer.init(config.uuid, config.timer, config.timerRepeats, () => { + this.loadData(true) + }) + } + + if (config.$cache && (config.setting.sync !== 'true' || sync)) { + Api.getLCacheConfig(config.uuid).then(res => { + if (!res || this.data.length > 0) return + + if (!is(fromJS(this.data), fromJS(res))) { + this.data = res + this.handleData() + } + this.setState({empty: false}) + }) + } } /** * @description 缁勪欢閿�姣侊紝娓呴櫎state鏇存柊锛屾竻闄ゅ揩鎹烽敭璁剧疆 */ componentWillUnmount () { - clearTimeout(this.timer) this.setState = () => { return } @@ -116,56 +150,8 @@ MKEmitter.removeListener('resetSelectLine', this.resetParentParam) MKEmitter.removeListener('queryModuleParam', this.queryModuleParam) MKEmitter.removeListener('refreshByButtonResult', this.refreshByButtonResult) - } - handleTimer = () => { - const { config } = this.state - - if (!config.timer) return - - const _change = { '5s': 5000, '15s': 15000, '30s': 30000, '1min': 60000, '5min': 300000, '10min': 600000, '15min': 900000, '30min': 1800000, '1hour': 3600000 } - - let timer = _change[config.timer] - - if (!timer) return - - let _param = { - func: 's_get_timers_role', - LText: `select '${window.GLOB.appkey || ''}','${config.uuid}'`, - timer_type: config.timer, - component_id: config.uuid - } - - _param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') // 鏃堕棿鎴� - _param.LText = Utils.formatOptions(_param.LText) // 鍏抽敭瀛楃鏇挎崲锛宐ase64鍔犲瘑 - _param.secretkey = Utils.encrypt(_param.LText, _param.timestamp) // md5瀵嗛挜 - - Api.getSystemConfig(_param).then(result => { - if (!result.status) { - notification.warning({ - top: 92, - message: result.message, - duration: 5 - }) - return - } else if (result.run_type) { - this.setState({timer}) - this.timer = setTimeout(() => { - this.timerTask() - }, timer) - } - }) - } - - timerTask = () => { - const { timer } = this.state - if (!timer) return - - this.loadData(true) - - this.timer = setTimeout(() => { - this.timerTask() - }, timer) + this.timer && this.timer.stop() } /** @@ -179,16 +165,13 @@ if (config.uuid !== menuId) return - this.loadData() // 鏁版嵁鍒锋柊 - - if (btn.syncComponentId && btn.syncComponentId !== config.uuid && btn.syncComponentId !== config.setting.supModule) { - MKEmitter.emit('reloadData', btn.syncComponentId) // 鍚岀骇鏍囩鍒锋柊 + if ((position === 'mainline' || position === 'popclose') && config.setting.supModule && BID) { // 鍒锋柊婧愮粍浠舵椂锛岄檮甯﹀埛鏂颁笂绾ц涓庡綋鍓嶇粍浠� + MKEmitter.emit('reloadData', config.setting.supModule, BID) + } else { + this.loadData() } - if (position === 'mainline' && config.setting.supModule) { // 涓昏〃琛屽埛鏂� - MKEmitter.emit('reloadData', config.setting.supModule, (BID || 'empty')) - } else if (position === 'popclose') { // 鏍囩鍏抽棴鍒锋柊 - config.setting.supModule && MKEmitter.emit('reloadData', config.setting.supModule, (BID || 'empty')) + if (position === 'popclose') { // 鎵ц鍚姩寮圭獥鐨勬寜閽墍閫夋嫨鐨勫埛鏂伴」 btn.$tabId && MKEmitter.emit('refreshPopButton', btn.$tabId) } } @@ -215,7 +198,7 @@ /** * @description 瀵煎嚭Excel鏃讹紝鑾峰彇椤甸潰鎼滅储鎺掑簭绛夊弬鏁� */ - queryModuleParam = (menuId, btnId) => { + queryModuleParam = (menuId, callback) => { const { mainSearch } = this.props const { arr_field, config, search } = this.state @@ -231,7 +214,7 @@ }) } - MKEmitter.emit('returnModuleParam', config.uuid, btnId, { + callback({ arr_field: arr_field, orderBy: config.setting.order || '', search: searches, @@ -243,15 +226,18 @@ * @description 鏁版嵁鍔犺浇 */ async loadData (hastimer) { - const { mainSearch, menuType } = this.props + const { mainSearch } = this.props const { config, arr_field, BID, search } = this.state if (config.setting.supModule && !BID) { // BID 涓嶅瓨鍦ㄦ椂锛屼笉鍋氭煡璇� this.setState({ - data: [] - }, () => { - this.handleData() + empty: true }) + + if (!is(fromJS(this.data), fromJS([]))) { + this.data = [] + this.handleData() + } return } @@ -277,29 +263,28 @@ } let _orderBy = config.setting.order || '' - let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, '', '', BID, menuType) + let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, '', '', BID) let result = await Api.genericInterface(param) if (result.status) { - let reset = true - - if (hastimer && is(fromJS(result.data), fromJS(this.state.data))) { - reset = false + if (config.$cache && config.setting.onload !== 'false') { + Api.writeCacheConfig(config.uuid, result.data || '') } this.setState({ - data: result.data, loading: false, - empty: result.data.length === 0 - }, () => { - if (!reset) return - this.handleData() + empty: !result.data || result.data.length === 0 }) + + if (!is(fromJS(this.data), fromJS(result.data || []))) { + this.data = result.data || [] + this.handleData() + } } else { this.setState({ - loading: false, - timer: null + loading: false }) + this.timer && this.timer.stop() notification.error({ top: 92, message: result.message, @@ -320,14 +305,14 @@ setTimeout(() => { this.viewrender() - }, 150) + }, 100) } /** * @description 鍥捐〃娓叉煋鍒嗙粍 */ viewrender = () => { - const { config, data } = this.state + const { config } = this.state if (!config.plot.script) return @@ -335,7 +320,7 @@ try { // eslint-disable-next-line let func = new Function('Chart', 'DataSet', 'wrap', 'data', 'config', config.plot.script) - func(Chart, DataSet, this.wrap, data, config) + func(Chart, DataSet, this.wrap, this.data, config) } catch (e) { console.warn(e) @@ -349,7 +334,7 @@ try { // eslint-disable-next-line let func = new Function('echarts', 'DataSet', 'wrap', 'data', 'config', config.plot.script) - func(echarts, DataSet, this.wrap, data, config) + func(echarts, DataSet, this.wrap, this.data, config) } catch (e) { console.warn(e) @@ -373,15 +358,17 @@ const { config, loading, empty, BID } = this.state return ( - <div className="custom-chart-plot-box" style={config.style}> + <div className="custom-chart-plot-box" id={'anchor' + config.uuid} style={config.style}> {loading ? <div className="loading-mask"> <div className="ant-spin-blur"></div> <Spin /> </div> : null } - <NormalHeader config={config} BID={BID} menuType={this.props.menuType} refresh={this.refreshSearch} /> - <div className={'canvas' + (empty ? ' empty' : '')} ref={ref => this.wrap = ref}></div> + <NormalHeader config={config} BID={BID} refresh={this.refreshSearch}/> + <div className="canvas-wrap" style={{height: (config.plot.height + 30) + 'px'}}> + <div className={'canvas' + (empty ? ' empty' : '')} style={{height: config.plot.height + 'px'}} ref={ref => this.wrap = ref}></div> + </div> {empty ? <Empty description={false}/> : null} </div> ) -- Gitblit v1.8.0