king
2023-08-11 4adb8b8868aeed1f5f3b89ae269a7724c6b451ad
src/menu/components/share/usercomponent/index.jsx
@@ -1,28 +1,25 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Icon, Modal, notification } from 'antd'
import { Modal, notification } from 'antd'
import { UserOutlined } from '@ant-design/icons'
import html2canvas from 'html2canvas'
import Api from '@/api'
import Utils from '@/utils/utils.js'
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import UserForm from './settingform'
import MKEmitter from '@/utils/events.js'
import './index.scss'
class DataSource extends Component {
class UserComponent extends Component {
  static propTpyes = {
    btnlog: PropTypes.array,
    handlelog: PropTypes.func
    config: PropTypes.object
  }
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    visible: false,
    loading: false,
    name: ''
    name: '',
  }
  shouldComponentUpdate (nextProps, nextState) {
@@ -31,7 +28,7 @@
  trigger = () => {
    const { config } = this.props
    this.setState({
      visible: true,
      loading: false,
@@ -39,53 +36,159 @@
    })
  }
  getUserComponent = () => {
    let config = fromJS(this.props.config).toJS()
    let _config = {}
    _config.wrap = config.wrap || {}
    _config.type = config.type || ''
    _config.subtype = config.subtype || ''
    _config.style = config.style || {}
    _config.subcards = config.subcards || []
    _config.headerStyle = config.headerStyle || {}
    _config.action = config.action || []
    _config.search = config.search || []
    _config.cols = config.cols || []
    _config.plot = config.plot || {}
    _config.html = config.html || ''
    _config.css = config.css || ''
    _config.js = config.js || ''
    _config.width = _config.wrap.width || _config.plot.width || config.width || 24
    _config.subcards = _config.subcards.map(card => {
      if (card.elements) {
        card.elements = card.elements.map(item => this.resetElement(item))
      }
      if (card.backElements) {
        card.backElements = card.backElements.map(item => this.resetElement(item))
      }
      return card
    })
    _config.action = _config.action.map(item => {
      item.verify = null
      return item
    })
    _config.search = _config.search.map(item => {
      if (item.resourceType === '1') {
        item.resourceType = '0'
        item.dataSource = ''
        item.valueText = ''
        item.valueText = ''
        item.options = ''
      }
      item.blacklist = []
      return item
    })
    let loopCol = (cols) => {
      return cols.map(col => {
        col.uuid = Utils.getuuid()
        col.marks = null
        if (col.type === 'colspan' && col.subcols) {
          col.subcols = loopCol(col.subcols)
        } else if (col.type === 'custom' && col.elements) {
          col.elements = col.elements.map(cell => {
            if (cell.eleType === 'button') {
              cell.verify = null
            } else {
              cell = this.resetElement(cell)
            }
            return cell
          })
        }
        return col
      })
    }
    _config.cols = loopCol(_config.cols)
    return _config
  }
  resetElement = (item) => {
    item.marks = null
    item.verify = null
    if (item.datatype === 'dynamic') {
      if (item.eleType ===  'icon') {
        item.tooltip = item.field
      } else if (item.eleType === 'slider') {
        item.value = 50
      } else {
        item.value = item.field
      }
      item.datatype = 'static'
      item.field = ''
    }
    if (item.link === 'dynamic') {
      item.link = 'static'
    }
    return item
  }
  submit = () => {
    const { config } = this.props
    this.verifyRef.handleConfirm().then(res => {
      this.setState({loading: true})
      document.getElementsByClassName('menu-view')[0].classList.add('saving')
      html2canvas(document.getElementById(config.uuid)).then(canvas => {
        let img = canvas.toDataURL('image/png') // 获取生成的图片
        Api.fileuploadbase64(img, 'cloud').then(result => {
          if (result.status) {
            Api.getSystemConfig({
              func: 's_custom_components_adduptdel',
              c_id: config.uuid,
              images: Utils.getcloudurl(result.Images),
              c_name: res.name,
              long_param: window.btoa(window.encodeURIComponent(JSON.stringify(config))),
              del_type: ''
            }).then(response => {
              if (response.status) {
                this.setState({loading: false, visible: false})
                notification.success({
                  top: 92,
                  message: '保存成功',
                  duration: 2
                })
                document.getElementsByClassName('menu-view')[0].classList.remove('saving')
                MKEmitter.emit('updateCustomComponent')
              } else {
                this.setState({loading: false})
                notification.warning({
                  top: 92,
                  message: response.message,
                  duration: 5
                })
              }
            })
          } else {
            this.setState({loading: false})
            notification.warning({
              top: 92,
              message: result.ErrMesg,
              duration: 5
            })
      this.setState({loading: true})
      setTimeout(() => {
        // let template = this.getUserComponent()
        let template = fromJS(config).toJS()
        html2canvas(document.getElementById(config.uuid)).then(canvas => {
          let param = {
            Base64Img: canvas.toDataURL('image/png') // 获取生成的图片
          }
          if (window.GLOB.cloudServiceApi) {
            param.rduri = window.GLOB.cloudServiceApi
            param.userid = sessionStorage.getItem('CloudUserID') || ''
            param.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
          }
          Api.fileuploadbase64(param).then(result => {
            if (result.status) {
              Api.getCloudConfig({
                func: 's_custom_components_adduptdel',
                c_id: config.uuid,
                images: Utils.getcloudurl(result.Images),
                typename: sessionStorage.getItem('appType') || '',
                c_name: res.name,
                long_param: window.btoa(window.encodeURIComponent(JSON.stringify(template))),
                del_type: ''
              }).then(response => {
                if (response.status) {
                  this.setState({loading: false, visible: false})
                  notification.success({
                    top: 92,
                    message: '保存成功',
                    duration: 2
                  })
                  document.getElementsByClassName('menu-view')[0].classList.remove('saving')
                  MKEmitter.emit('updateCustomComponent')
                } else {
                  this.setState({loading: false})
                  notification.warning({
                    top: 92,
                    message: response.message,
                    duration: 5
                  })
                }
              })
            } else {
              this.setState({loading: false})
              notification.warning({
                top: 92,
                message: result.ErrMesg,
                duration: 5
              })
            }
          })
        })
      })
      }, 300)
    })
  }
@@ -95,13 +198,12 @@
  }
  render () {
    const { visible, dict, loading, name } = this.state
    const { visible, loading, name } = this.state
    return (
      <div className="user-component-wrap">
        <Icon type="user" title="生成自定义组件" onClick={this.trigger} />
        <UserOutlined title="生成自定义组件" onClick={this.trigger} />
        <Modal
          wrapClassName="popview-modal"
          title="自定义组件"
          visible={visible}
          width={500}
@@ -111,11 +213,11 @@
          onCancel={this.cancel}
          destroyOnClose
        >
          <UserForm dict={dict} name={name} inputSubmit={this.submit} wrappedComponentRef={(inst) => this.verifyRef = inst}/>
          <UserForm name={name} inputSubmit={this.submit} wrappedComponentRef={(inst) => this.verifyRef = inst}/>
        </Modal>
      </div>
    )
  }
}
export default DataSource
export default UserComponent