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 |  160 ++++++++++++++++++++++-------------------------------
 1 files changed, 66 insertions(+), 94 deletions(-)

diff --git a/src/tabviews/custom/components/chart/custom-chart/index.jsx b/src/tabviews/custom/components/chart/custom-chart/index.jsx
index be4bb7a..fb81260 100644
--- a/src/tabviews/custom/components/chart/custom-chart/index.jsx
+++ b/src/tabviews/custom/components/chart/custom-chart/index.jsx
@@ -5,12 +5,12 @@
 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'
 
@@ -28,14 +28,14 @@
     loading: false,            // 鏁版嵁鍔犺浇鐘舵��
     sync: false,               // 鏄惁缁熶竴璇锋眰鏁版嵁
     plot: null,                // 鍥捐〃璁剧疆
-    data: null,                // 鏁版嵁
     search: null,              // 鎼滅储鏉′欢
   }
+
+  data = []
 
   UNSAFE_componentWillMount () {
     const { config, data, initdata } = this.props
     let _config = fromJS(config).toJS()
-    let _data = null
     let _sync = config.setting.sync === 'true'
 
     let BID = ''
@@ -50,20 +50,20 @@
       BID = BData.$BID || ''
     }
 
-    if (config.setting.sync === 'true' && data) {
-      _data = data[config.dataName] || []
+    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,
@@ -74,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()
+    }
   }
 
   /**
@@ -92,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()
       })
@@ -107,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
     }
@@ -126,64 +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) {
-        let repeats = config.timerRepeats || 0
-        this.setState({timer, repeats})
-        this.timer = setTimeout(() => {
-          this.timerTask(repeats)
-        }, timer)
-      }
-    })
-  }
-
-  timerTask = (times) => {
-    const { timer, repeats } = this.state
-    if (!timer) return
-    
-    this.loadData(true)
-
-    if (repeats) {
-      times = times - 1
-      if (times <= 0) {
-        clearTimeout(this.timer)
-        return
-      }
-    }
-    this.timer = setTimeout(() => {
-      this.timerTask(times)
-    }, timer)
+    this.timer && this.timer.stop()
   }
 
   /**
@@ -263,10 +231,13 @@
 
     if (config.setting.supModule && !BID) { // BID 涓嶅瓨鍦ㄦ椂锛屼笉鍋氭煡璇�
       this.setState({
-        data: []
-      }, () => {
-        this.handleData()
+        empty: true
       })
+
+      if (!is(fromJS(this.data), fromJS([]))) {
+        this.data = []
+        this.handleData()
+      }
       return
     }
 
@@ -296,25 +267,24 @@
 
     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,
@@ -335,14 +305,14 @@
     
     setTimeout(() => {
       this.viewrender()
-    }, 150)
+    }, 100)
   }
 
   /**
    * @description 鍥捐〃娓叉煋鍒嗙粍
    */
   viewrender = () => {
-    const { config, data } = this.state
+    const { config } = this.state
 
     if (!config.plot.script) return
 
@@ -350,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)
   
@@ -364,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)
   
@@ -395,8 +365,10 @@
             <Spin />
           </div> : null
         }
-        <NormalHeader config={config} BID={BID} 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