king
2020-10-26 1b89aa4493d1c9768447f2f480d594cdb8077fdc
src/components/tabview/index.jsx
@@ -2,27 +2,39 @@
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import {Tabs, Icon, Button, ConfigProvider, message} from 'antd'
import {modifyTabview, toggleIsiframe} from '@/store/action'
// import asyncComponent from '@/utils/asyncComponent'
import asyncComponent from '@/utils/asyncLoadComponent'
import NotFount from '@/components/404'
import enUS from 'antd/es/locale/en_US'
import zhCN from 'antd/es/locale/zh_CN'
import mzhCN from '@/locales/zh-CN/main.js'
import menUS from '@/locales/en-US/main.js'
import {Tabs, Icon, Button, message, BackTop} from 'antd'
import moment from 'moment'
import 'moment/locale/zh-cn'
import {modifyTabview, toggleIsiframe, refreshTabView} from '@/store/action'
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 './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 TreePage = asyncComponent(() => import('@/tabviews/treepage'))
const VerupTable = asyncComponent(() => import('@/tabviews/verupmanage'))
const ScriptTable = asyncComponent(() => import('@/tabviews/scriptmanage'))
const TabManage = asyncComponent(() => import('@/tabviews/tabmanage'))
const Iframe = asyncComponent(() => import('@/tabviews/iframe'))
const DataManage = asyncComponent(() => import('@/tabviews/datamanage'))
const RoleManage = asyncComponent(() => import('@/tabviews/rolemanage'))
const TabForm = asyncComponent(() => import('@/tabviews/tabform'))
const FormTab = asyncComponent(() => import('@/tabviews/formtab'))
let Comps = {}
let service = window.GLOB.service ? (/\/$/.test(window.GLOB.service) ? window.GLOB.service : window.GLOB.service + '/') : ''
let service = ''
if (process.env.NODE_ENV === 'production') {
  service = document.location.origin + '/' + service + 'zh-CN/'
  service = document.location.origin + '/' + window.GLOB.service + 'zh-CN/'
} else {
  service = 'http://qingqiumarket.cn/' + service + 'zh-CN/'
  service = window.GLOB.location + window.GLOB.service + 'zh-CN/'
}
class Header extends Component {
@@ -32,72 +44,92 @@
  }
  state = {
    selectedTabId: '', // 当前选中tab页面
    tabviews: null, // 标签集
    iFrameHeight: 0,
    dict: (!sessionStorage.getItem('lang') || sessionStorage.getItem('lang') === 'zh-CN') ? mzhCN : menUS,
    locale: (!sessionStorage.getItem('lang') || sessionStorage.getItem('lang') === 'zh-CN') ? zhCN : enUS
    dict: localStorage.getItem('lang') !== 'en-US' ? mzhCN : menUS
  }
  handleTabview (menu) {
  handleTabview = (e, menu) => {
    e.stopPropagation()
    // 关闭tab页,重新选择显示页
    let tabs = JSON.parse(JSON.stringify(this.props.tabviews))
    let tabs = fromJS(this.state.tabviews).toJS()
    tabs = tabs.filter(tab => {
      if (tab.MenuID === this.state.selectedTabId) {
        tab.selected = true
      } else {
        tab.selected = false
      }
      if (menu.type === 'TabForm' || menu.type === 'iframe') {
        return tab.MenuID !== menu.MenuID
      } else {
        return tab.MenuNo !== menu.MenuNo
      }
      return tab.MenuID !== menu.MenuID
    })
    if (menu.MenuID === this.state.selectedTabId) {
    if (menu.selected) {
      tabs[0] && (tabs[0].selected = true)
    }
    console.log(tabs)
    this.props.modifyTabview(tabs)
  }
  changeTab (menu) {
  refreshTabview = (e, menu) => {
    e.stopPropagation()
    window.GLOB.CacheMap = new Map()
    let _menu = {
      MenuID: menu.MenuID,
      position: 'view'
    }
    this.props.refreshTabView(_menu)
  }
  changeTab = (e, menu) => {
    e.stopPropagation()
    // 窗口切换
    this.setState({
      selectedTabId: menu.MenuID
    })
    let tabs = JSON.parse(JSON.stringify(this.props.tabviews))
    let tabs = fromJS(this.state.tabviews).toJS()
    tabs = tabs.map(tab => {
      tab.selected = false
      if (tab.MenuID === menu.MenuID) {
        tab.selected = true
      } else {
        tab.selected = false
      }
      return tab
    })
    this.setState({
      tabviews: tabs
    })
    this.props.modifyTabview(tabs)
    this.resetWindow(menu)
  }
  selectcomponent (view) {
  selectcomponent = (view) => {
    // 根据tab页中菜单信息,选择所需的组件
    if (view.type === 'Home') {
      return (<Comps.Home MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'CommonTable') {
      return (<Comps.CommonTable MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
      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 === 'VerupTable') {
      return (<VerupTable MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'ScriptTable') {
      return (<ScriptTable MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'TabManage') {
      return (<TabManage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'DataManage') {
      return (<Comps.DataManage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'RoleManage') {
      return (<Comps.RoleManage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
      return (<DataManage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'RolePermission') {
      return (<RoleManage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'TabForm') {
      return (<Comps.TabForm MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID} param={view.param}/>)
      return (<TabForm MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID} param={view.param}/>)
    } 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 (<Comps.Iframe key={view.MenuID} title={view.MenuName} MenuName={view.MenuName} url={service + view.LinkUrl}/>)
      return (<Iframe key={view.MenuID} title={view.MenuName} MenuName={view.MenuName} url={service + view.LinkUrl}/>)
    } else {
      return (<NotFount key={view.MenuID} />)
    }
  }
  resetWindow (view) {
  resetWindow = (view) => {
    // 窗口在iframe与普通页面切换时,修改左侧菜单栏样式
    if (!view) return
    let _isiframe = this.props.isiframe
@@ -130,88 +162,97 @@
    } else {
      moment.locale('en')
    }
    this.setState({
      tabviews: this.props.tabviews
    })
  }
  componentDidMount () {
    let home = {
      MenuID: '1576117946681plembmkk9akkv8sn0vtdfdsfaf',
      MenuID: 'home_page_id',
      MenuName: '首页',
      MenuNo: 'MESOrderDetailMwe',
      PageParam: {},
      id: 1,
      selected: true,
      src: '',
      text: '首页',
      type: 'Home'
    }
    this.props.modifyTabview([home])
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (nextProps.tabviews && !is(fromJS(this.props.tabviews), fromJS(nextProps.tabviews))) {
      // tab窗口页增加或删除
      if (nextProps.tabviews.length > this.props.tabviews.length) {
        // 查看新tab页需要组件是否加载
        let MenuIDs = this.props.tabviews.map(tab => {return tab.MenuID})
        let newtab = nextProps.tabviews.filter(tab => !MenuIDs.includes(tab.MenuID))[0]
        if (!Comps.Home && newtab.type === 'Home') {
          Comps.Home = asyncComponent(() => import('@/tabviews/home'))
        } else if (!Comps.CommonTable && newtab.type === 'CommonTable') {
          Comps.CommonTable = asyncComponent(() => import('@/tabviews/commontable'))
        } else if (!Comps.Iframe && newtab.type === 'iframe') {
          Comps.Iframe = asyncComponent(() => import('@/tabviews/iframe'))
        } else if (!Comps.DataManage && newtab.type === 'DataManage') {
          Comps.DataManage = asyncComponent(() => import('@/tabviews/datamanage'))
        } else if (!Comps.RoleManage && newtab.type === 'RoleManage') {
          Comps.RoleManage = asyncComponent(() => import('@/tabviews/rolemanage'))
        } else if (!Comps.TabForm && newtab.type === 'TabForm') {
          Comps.TabForm = asyncComponent(() => import('@/tabviews/tabform'))
        }
      }
      // 设置选中窗口
      let view = nextProps.tabviews.filter(tab => tab.selected)[0]
    if (nextProps.tabviews && !is(fromJS(this.state.tabviews), fromJS(nextProps.tabviews))) {
      // 保存修改标签集
      this.setState({
        selectedTabId: view ? view.MenuID : ''
        tabviews: nextProps.tabviews
      })
      this.resetWindow(view)
      let node = document.getElementById('root').parentNode.parentNode
      if (node) {
        node.scrollTop = 0
      }
    }
  }
  shouldComponentUpdate(nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
  render () {
    const { menuType } = this.props
    const { tabviews } = this.state
    let view = tabviews.filter(tab => tab.selected)[0]
    this.resetWindow(view)
    let selectedTabId = view ? view.MenuID : ''
    return (
      <section className={'flex-container content-box' + (this.props.collapse ? ' collapsed' : '')}>
        <ConfigProvider locale={this.state.locale}>
          <div className="content-header">
            {this.props.tabviews && this.props.tabviews.length > 0 &&
              <Tabs activeKey={this.state.selectedTabId}>
                {this.props.tabviews.map(view => {
                  return (
                    <Tabs.TabPane
                      className="test"
                      tab={
                        <span className="tab-control">
                          <span className="tab-name" onClick={() => {this.changeTab(view)}}>
                            {view.MenuName}
                          </span>
                          {view.type !== 'Home' ? <Icon type="close" onClick={() => {this.handleTabview(view)}}/> : null}
        <div className="content-header">
          {tabviews && tabviews.length > 0 &&
            <Tabs activeKey={selectedTabId}>
              {tabviews.map(view => {
                return (
                  <Tabs.TabPane
                    tab={
                      <span className="tab-control">
                        {['CommonTable', 'FormTab', 'TreePage', 'CalendarPage', 'CustomPage'].includes(view.type) ?
                          <Icon type="redo" onClick={(e) => {this.refreshTabview(e, view)}}/> : null
                        }
                        <span className="tab-name" onClick={(e) => {this.changeTab(e, view)}}>
                          {view.MenuName}
                        </span>
                      }
                      key={view.MenuID}
                    >
                      {this.selectcomponent(view)}
                      <Button className={'main-copy ' + (view.type === 'iframe' ? 'ifr-copy' : '')} icon="copy" data-menuno={view.MenuNo} onClick={this.copyMenuNo} shape="circle" />
                    </Tabs.TabPane>
                  )
                })}
              </Tabs>
            }
          </div>
        </ConfigProvider>
                        {view.type !== 'Home' ?
                          <Icon type="close" onClick={(e) => {this.handleTabview(e, view)}}/> : null
                        }
                      </span>
                    }
                    key={view.MenuID}
                  >
                    {this.selectcomponent(view)}
                    {options.sysType !== 'cloud' && menuType !== 'HS' && !['CommonTable', 'TreePage', 'ManageTable', 'CalendarPage', 'Home'].includes(view.type) ?
                      <Button
                        icon="copy"
                        shape="circle"
                        className={'main-copy ' + (view.type === 'iframe' ? 'ifr-copy' : '')}
                        data-menuno={view.MenuNo}
                        onClick={this.copyMenuNo}
                      /> : null
                    }
                    <BackTop>
                      <div className="ant-back-top">
                        <div className="ant-back-top-content">
                          <div className="ant-back-top-icon"></div>
                        </div>
                      </div>
                    </BackTop>
                  </Tabs.TabPane>
                )
              })}
            </Tabs>
          }
        </div>
      </section>
    )
  }
@@ -219,6 +260,7 @@
const mapStateToProps = (state) => {
  return {
    menuType: state.editLevel,
    tabviews: state.tabviews,
    collapse: state.collapse,
    isiframe: state.isiframe
@@ -228,6 +270,7 @@
const mapDispatchToProps = (dispatch) => {
  return {
    modifyTabview: (tabviews) => dispatch(modifyTabview(tabviews)),
    refreshTabView: (refreshTab) => dispatch(refreshTabView(refreshTab)),
    toggleIsiframe: (isiframe) => dispatch(toggleIsiframe(isiframe))
  }
}