king
2020-08-11 ae6ce73e7abde1121a788f884e54ccd6cfad0460
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import {Tabs, Icon, Button, ConfigProvider, message, BackTop} from 'antd'
import enUS from 'antd/es/locale/en_US'
import zhCN from 'antd/es/locale/zh_CN'
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 CommonTable = asyncComponent(() => import('@/tabviews/commontable'))
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 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/'
}
 
class Header extends Component {
  static propTpyes = {
    collapse: PropTypes.bool,
    tabviews: PropTypes.array // 标签页数组
  }
 
  state = {
    tabviews: null, // 标签集
    iFrameHeight: 0,
    dict: localStorage.getItem('lang') !== 'en-US' ? mzhCN : menUS,
    locale: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS
  }
 
  handleTabview = (e, menu) => {
    e.stopPropagation()
    // 关闭tab页,重新选择显示页
    let tabs = JSON.parse(JSON.stringify(this.state.tabviews))
    tabs = tabs.filter(tab => {
      return tab.MenuID !== menu.MenuID
    })
    
    if (menu.selected) {
      tabs[0] && (tabs[0].selected = true)
    }
 
    this.props.modifyTabview(tabs)
  }
 
  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()
    // 窗口切换
    let tabs = JSON.parse(JSON.stringify(this.state.tabviews))
    tabs = tabs.map(tab => {
      tab.selected = false
 
      if (tab.MenuID === menu.MenuID) {
        tab.selected = true
      }
 
      return tab
    })
 
    this.setState({
      tabviews: tabs
    })
 
    this.props.modifyTabview(tabs)
  }
 
  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 === 'TreePage') {
      return (<TreePage 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 (<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 (<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 (<Iframe key={view.MenuID} title={view.MenuName} MenuName={view.MenuName} url={service + view.LinkUrl}/>)
    } else {
      return (<NotFount key={view.MenuID} />)
    }
  }
 
  resetWindow = (view) => {
    // 窗口在iframe与普通页面切换时,修改左侧菜单栏样式
    if (!view) return
    let _isiframe = this.props.isiframe
    if (view && view.type === 'iframe') {
      _isiframe = true
    } else {
      _isiframe = false
    }
 
    if (_isiframe !== this.props.isiframe) {
      this.props.toggleIsiframe(_isiframe)
    }
  }
 
  copyMenuNo = (e) => {
    e.stopPropagation()
    let oInput = document.createElement('input')
    oInput.value = e.target.dataset.menuno || ''
    document.body.appendChild(oInput)
    oInput.select()
    document.execCommand('Copy')
    oInput.className = 'oInput'
    oInput.style.display = 'none'
    message.success(this.state.dict['main.copy.success'])
  }
 
  UNSAFE_componentWillMount () {
    if (!sessionStorage.getItem('lang') || sessionStorage.getItem('lang') === 'zh-CN') {
      moment.locale('zh-cn')
    } else {
      moment.locale('en')
    }
    this.setState({
      tabviews: this.props.tabviews
    })
  }
 
  componentDidMount () {
    let home = {
      MenuID: '1576117946681plembmkk9akkv8sn0vtdfdsfaf',
      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.state.tabviews), fromJS(nextProps.tabviews))) {
      // 保存修改标签集
      this.setState({
        tabviews: nextProps.tabviews
      })
 
      let node = document.getElementById('root').parentNode.parentNode
      if (node) {
        node.scrollTop = 0
      }
    }
  }
 
  render () {
    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">
            {tabviews && tabviews.length > 0 &&
              <Tabs activeKey={selectedTabId}>
                {tabviews.map(view => {
                  return (
                    <Tabs.TabPane
                      tab={
                        <span className="tab-control">
                          {['CommonTable', 'FormTab', 'TreePage'].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>
                          {view.type !== 'Home' ?
                            <Icon type="close" onClick={(e) => {this.handleTabview(e, view)}}/> : null
                          }
                        </span>
                      }
                      key={view.MenuID}
                    >
                      {this.selectcomponent(view)}
                      {options.sysType !== 'cloud' && !['CommonTable', 'TreePage', 'ManageTable'].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>
        </ConfigProvider>
      </section>
    )
  }
}
 
const mapStateToProps = (state) => {
  return {
    tabviews: state.tabviews,
    collapse: state.collapse,
    isiframe: state.isiframe
  }
}
 
const mapDispatchToProps = (dispatch) => {
  return {
    modifyTabview: (tabviews) => dispatch(modifyTabview(tabviews)),
    refreshTabView: (refreshTab) => dispatch(refreshTabView(refreshTab)),
    toggleIsiframe: (isiframe) => dispatch(toggleIsiframe(isiframe))
  }
}
 
export default connect(mapStateToProps, mapDispatchToProps)(Header)