From a29d9d644a2a30e9ef4afcc6d728c20c218dc359 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期四, 15 六月 2023 14:25:28 +0800 Subject: [PATCH] 2023-06-15 --- src/tabviews/custom/components/code/sand-box/index.jsx | 99 +++++++++++++++++++++++++++++++++---------------- 1 files changed, 66 insertions(+), 33 deletions(-) diff --git a/src/tabviews/custom/components/code/sand-box/index.jsx b/src/tabviews/custom/components/code/sand-box/index.jsx index 2456f8b..dc1a995 100644 --- a/src/tabviews/custom/components/code/sand-box/index.jsx +++ b/src/tabviews/custom/components/code/sand-box/index.jsx @@ -20,15 +20,18 @@ config: null, // 鍥捐〃閰嶇疆淇℃伅 loading: false, // 鏁版嵁鍔犺浇鐘舵�� sync: false, // 鏄惁缁熶竴璇锋眰鏁版嵁 - data: {}, // 鏁版嵁 + data: [], // 鏁版嵁 html: '', + result: {} } + + loaded = false UNSAFE_componentWillMount () { const { data, initdata } = this.props let _config = fromJS(this.props.config).toJS() - let _data = {} + let _data = [] let _sync = false let BID = '' @@ -47,14 +50,17 @@ _sync = _config.setting.sync === 'true' if (_sync && data) { - _data = data[_config.dataName] || {} + _data = data[_config.dataName] || [] _sync = false + this.loaded = true } else if (_sync && initdata) { _data = initdata || {} _sync = false + this.loaded = true } } else { _data = {} + this.loaded = true } if (_config.css) { @@ -84,7 +90,19 @@ } componentDidMount () { + const { config } = this.state + MKEmitter.addListener('reloadData', this.reloadData) + + if (config.$cache && !this.loaded) { + Api.getLCacheConfig(config.uuid).then(res => { + if (!res || this.loaded) return + + this.setState({data: res}, () => { + this.renderView() + }) + }) + } } shouldComponentUpdate (nextProps, nextState) { @@ -105,14 +123,20 @@ const { sync, config } = this.state if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) { - let _data = {} + let _data = [] if (nextProps.data && nextProps.data[config.dataName]) { _data = nextProps.data[config.dataName] } - this.setState({sync: false, data: _data}, () => { - this.renderView() - }) + this.loaded = true + + if (!is(fromJS(this.state.data), fromJS(_data))) { + setTimeout(() => { + this.renderView() + }, 10) + } + + this.setState({sync: false, data: _data}) } else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { this.setState({}, () => { this.loadData() @@ -134,15 +158,16 @@ if (config.wrap.datatype === 'static') { this.setState({ - data: {}, + data: [], loading: false }) return } else if (config.setting.supModule && !BID) { // BID 涓嶅瓨鍦ㄦ椂锛屼笉鍋氭煡璇� this.setState({ - data: {}, + data: [], loading: false }) + this.loaded = true return } @@ -162,13 +187,26 @@ let result = await Api.genericInterface(param) if (result.status) { - let _data = result.data || {} + 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 - }, () => { - this.renderView() }) } else { this.setState({ @@ -183,36 +221,31 @@ } renderView = () => { - const { data } = this.state + const { data, result } = this.state const { html, js, wrap, columns } = this.state.config let _html = html - if (_html && wrap.datatype !== 'static' && wrap.compileMode !== 'custom') { - let _data = data - if (Array.isArray(_data)) { - _data = _data[0] || {} - } - columns.forEach(col => { - if (col.field) { - let val = (_data[col.field] || _data[col.field] === 0) ? _data[col.field] : '' - let reg = new RegExp('@' + col.field + '@', 'ig') + if (_html && wrap.datatype !== 'static') { + if (/@[\u4E00-\u9FA50-9a-zA-Z_]+@/ig.test(_html)) { + let _data = data[0] || {} - _html = _html.replace(reg, val) - } - }) + 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 { - if (wrap.compileMode !== 'custom') { - // eslint-disable-next-line no-eval - eval(js) - } else { - // eslint-disable-next-line - let evalfunc = eval('(true && function (data) {' + js + '})') - evalfunc(data) - } + // eslint-disable-next-line + let evalfunc = eval('(true && function (data, result) {' + js + '})') + evalfunc(data, result) } catch (e) { console.warn(e) } -- Gitblit v1.8.0