king
2024-11-06 e2fd227b24a2d188ef209dc47fabcc8390f2a94b
src/components/breadview/index.jsx
@@ -1,49 +1,66 @@
import React, {Component} from 'react'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import { BackTop, Breadcrumb, Icon, notification} from 'antd'
import { BackTop, Breadcrumb, notification, Modal } from 'antd'
import { HomeOutlined, RightOutlined, RedoOutlined, LoadingOutlined } from '@ant-design/icons'
import moment from 'moment'
import 'moment/locale/zh-cn'
import asyncComponent from '@/utils/asyncLoadComponent'
import NotFount from '@/components/404'
import options from '@/store/options.js'
import mzhCN from '@/locales/zh-CN/main.js'
import menUS from '@/locales/en-US/main.js'
import MKEmitter from '@/utils/events.js'
import { initActionPermission } from '@/store/action'
import Api from '@/api'
import './index.scss'
const Home = asyncComponent(() => import('@/tabviews/home'))
const CustomPage = asyncComponent(() => import('@/tabviews/custom'))
const CommonTable = asyncComponent(() => import('@/tabviews/commontable'))
const CalendarPage = asyncComponent(() => import('@/tabviews/calendar'))
const BaseTable = asyncComponent(() => import('@/tabviews/basetable'))
const TreePage = asyncComponent(() => import('@/tabviews/treepage'))
const Iframe = asyncComponent(() => import('@/tabviews/iframe'))
const RoleManage = asyncComponent(() => import('@/tabviews/rolemanage'))
const FormTab = asyncComponent(() => import('@/tabviews/formtab'))
let service = ''
if (process.env.NODE_ENV === 'production') {
  service = document.location.origin + '/' + window.GLOB.service + 'zh-CN/'
} else {
  service = window.GLOB.location + '/' + window.GLOB.service + 'zh-CN/'
if (sessionStorage.getItem('lang') === 'zh-CN') {
  moment.locale('zh-cn')
}
class BreadView extends Component {
  state = {
    tabview: null, // 标签
    dict: sessionStorage.getItem('lang') !== 'en-US' ? mzhCN : menUS,
    hasNavBar: window.GLOB.navBar === 'linkage_navigation'
    tabview: null,
    hasNavBar: window.GLOB.navBar === 'linkage_navigation',
    visible: false
  }
  reloading = false
  UNSAFE_componentWillMount () {
    let home = {
      MenuID: 'home_page_id',
      MenuName: window.GLOB.dict['home'] || '首页',
      type: 'Home'
    }
    this.setState({tabview: home})
  }
  componentDidMount () {
    MKEmitter.addListener('modifyTabs', this.modifyTabs)
    MKEmitter.addListener('reloadTabs', this.reloadTabs)
  }
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('reloadTabs', this.reloadTabs)
    MKEmitter.removeListener('modifyTabs', this.modifyTabs)
  }
  refreshTabview = () => {
    const { tabview } = this.state
    window.GLOB.CacheMap = new Map()
    if (options.sysType === 'local' && window.GLOB.systemType !== 'production') {
    if (window.GLOB.sysType === 'local' && window.GLOB.systemType !== 'production') {
      let roledefer = new Promise(resolve => {
        Api.getSystemConfig({
          func: 's_Get_TrdMenu_Role',
@@ -67,7 +84,7 @@
              })
            }
  
            this.props.initActionPermission(_permAction)
            window.GLOB.mkActions = _permAction
          }
          resolve()
@@ -91,96 +108,117 @@
    }
  }
  selectcomponent = (view) => {
    // 根据tab页中菜单信息,选择所需的组件
    if (view.type === 'Home') {
      return (<Home MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'CommonTable' || view.type === 'ManageTable') {
      return (<CommonTable MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID} param={view.param}/>)
    } else if (view.type === 'CustomPage') {
      return (<CustomPage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID} param={view.param}/>)
    } else if (view.type === 'TreePage') {
      return (<TreePage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID} param={view.param}/>)
    } else if (view.type === 'CalendarPage') {
      return (<CalendarPage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID} param={view.param}/>)
    } else if (view.type === 'RolePermission') {
      return (<RoleManage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'FormTab') {
      return (<FormTab MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID} param={view.param}/>)
    } else if (view.type === 'iframe') {
      return (<Iframe key={view.MenuID} MenuID={view.MenuID} MenuNo={view.MenuNo} title={view.MenuName} MenuName={view.MenuName} url={service + view.LinkUrl}/>)
    } else {
      return (<NotFount key={view.MenuID} />)
    }
  changeTemp = (MenuID, Template) => {
    this.setState({
      tabview: {...this.state.tabview, type: Template}
    })
  }
  UNSAFE_componentWillMount () {
    if (!sessionStorage.getItem('lang') || sessionStorage.getItem('lang') === 'zh-CN') {
      moment.locale('zh-cn')
  selectcomponent = (view) => {
    // 根据tab页中菜单信息,选择所需的组件
    if (view.type === 'BaseTable') {
      return (<BaseTable MenuID={view.MenuID} MenuName={view.MenuName} param={view.param} changeTemp={this.changeTemp}/>)
    } else if (view.type === 'CustomPage') {
      return (<CustomPage MenuID={view.MenuID} MenuName={view.MenuName} param={view.param} changeTemp={this.changeTemp}/>)
    } else if (view.type === 'Home') {
      return (<Home MenuID={view.MenuID} MenuName={view.MenuName}/>)
    } else if (view.type === 'RolePermission') {
      return (<RoleManage MenuID={view.MenuID}/>)
    } else if (view.type === 'CommonTable') {
      return (<CommonTable MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} param={view.param} changeTemp={this.changeTemp}/>)
    } else if (view.type === 'TreePage') {
      return (<TreePage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} param={view.param}/>)
    } else if (view.type === 'iframe') {
      return (<Iframe MenuID={view.MenuID} title={view.MenuName} url={view.src}/>)
    } else {
      moment.locale('en')
      return (<NotFount />)
    }
    let home = {
      MenuID: 'home_page_id',
      MenuName: '首页',
      selected: true,
      type: 'Home'
    }
    this.setState({tabview: home})
  }
  gotoHome = () => {
    let home = {
      MenuID: 'home_page_id',
      MenuName: '首页',
      selected: true,
      MenuName: window.GLOB.dict['home'] || '首页',
      type: 'Home'
    }
    this.setState({tabview: home})
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (nextProps.tabviews && !is(fromJS(this.state.tabviews), fromJS(nextProps.tabviews))) {
      // 保存修改标签集
      this.setState({
        tabview: nextProps.tabviews[nextProps.tabviews.length - 1]
      })
  modifyTabs = (tab) => {
    this.setState({
      tabview: tab
    })
      let node = document.getElementById('root').parentNode.parentNode
      if (node) {
        node.scrollTop = 0
      }
    let node = document.getElementById('root').parentNode.parentNode
    if (node) {
      node.scrollTop = 0
    }
  }
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  reloadTabs = () => {
    if (this.reloading) return
    let time = new Date().getTime()
    let oldTime = sessionStorage.getItem('mk_reloadTabs')
    if (oldTime && time - oldTime < 180000) return
    sessionStorage.setItem('mk_reloadTabs', time)
    this.reloading = true
    this.setState({visible: true})
    Api.getAppVersion(true).then((list) => {
      let _time = new Date().getTime()
      let delay = _time - time
      delay = delay < 3000 ? 3000 - delay : 0
      setTimeout(() => {
        this.setState({visible: false})
        this.reloading = false
        Modal.success({
          title: '更新成功。',
        })
        if (list && list.length && list.includes(this.state.tabview.MenuID)) {
          MKEmitter.emit('reloadMenuView', this.state.tabview.MenuID)
        }
      }, delay)
    }, (message) => {
      let _time = new Date().getTime()
      let delay = _time - time
      delay = delay < 3000 ? 3000 - delay : 0
      setTimeout(() => {
        this.setState({visible: false})
        this.reloading = false
        Modal.error({
          title: message || '系统配置更新失败!',
        })
      }, delay)
    })
  }
  render () {
    const { tabview, hasNavBar } = this.state
    const { tabview, hasNavBar, visible } = this.state
    return (
      <section id="mk-breadview-wrap" className="mk-breadview-wrap">
      <section id="mk-tabgroup-wrap" className="mk-breadview-wrap">
        {hasNavBar && tabview ? <Breadcrumb separator="">
          <Breadcrumb.Item>
            <Icon type="home" onClick={this.gotoHome} />
            <HomeOutlined onClick={this.gotoHome} />
          </Breadcrumb.Item>
          {tabview.ParentNames && tabview.ParentNames[0] ?
            <Breadcrumb.Item>{tabview.ParentNames[0]}</Breadcrumb.Item> : null}
          {tabview.ParentNames && tabview.ParentNames[0] ?
              <Breadcrumb.Separator children={<Icon type="right" />} /> : null}
              <Breadcrumb.Separator children={<RightOutlined />} /> : null}
          {tabview.ParentNames && tabview.ParentNames[1] ?
            <Breadcrumb.Item>{tabview.ParentNames[1]}</Breadcrumb.Item> : null}
          {tabview.ParentNames && tabview.ParentNames[1] ?
              <Breadcrumb.Separator children={<Icon type="right" />} /> : null}
          <Breadcrumb.Item><Icon type="redo" onClick={this.refreshTabview}/>{tabview.MenuName}</Breadcrumb.Item>
              <Breadcrumb.Separator children={<RightOutlined />} /> : null}
          <Breadcrumb.Item><RedoOutlined onClick={this.refreshTabview}/>{tabview.MenuName}</Breadcrumb.Item>
        </Breadcrumb> : null}
        {tabview ? this.selectcomponent(tabview) : null}
        <BackTop>
@@ -190,21 +228,24 @@
            </div>
          </div>
        </BackTop>
        <Modal
          visible={visible}
          width={400}
          closable={false}
          centered={true}
          footer={null}
          destroyOnClose
        >
          <div className="mk-menus-update">
            <div className="tip">
              系统更新中,请稍后
            </div>
            <LoadingOutlined />
          </div>
        </Modal>
      </section>
    )
  }
}
const mapStateToProps = (state) => {
  return {
    tabviews: state.tabviews
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    initActionPermission: (permAction) => dispatch(initActionPermission(permAction)),
  }
}
export default connect(mapStateToProps, mapDispatchToProps)(BreadView)
export default BreadView