From ba9e189dd33c8d48f7f4ac36bcefeef9afb426f8 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期一, 17 七月 2023 23:40:49 +0800 Subject: [PATCH] 2023-07-17 --- src/tabviews/custom/components/code/sand-box/index.jsx | 133 +++++++++++++++++++++++++++++++------------- 1 files changed, 94 insertions(+), 39 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..6bdaf07 100644 --- a/src/tabviews/custom/components/code/sand-box/index.jsx +++ b/src/tabviews/custom/components/code/sand-box/index.jsx @@ -1,7 +1,7 @@ import React, {Component} from 'react' import PropTypes from 'prop-types' import { is, fromJS } from 'immutable' -import { Spin, notification } from 'antd' +import { Spin, notification, Modal } from 'antd' import Api from '@/api' import UtilsDM from '@/utils/utils-datamanage.js' @@ -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,57 +187,87 @@ 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() }) + + 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 }) - notification.error({ - top: 92, - message: result.message, - duration: 10 - }) + + 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 } = 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