king
2021-09-16 d7ec8fbd65cd7225ce8d405a0ee0a1f166f44d7b
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import {Tabs, Icon, BackTop, notification} from 'antd'
import moment from 'moment'
import 'moment/locale/zh-cn'
 
import { modifyTabview, toggleIsiframe, initActionPermission } 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 MKEmitter from '@/utils/events.js'
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 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 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/'
}
 
class TabViews extends Component {
  static propTpyes = {
    collapse: PropTypes.bool,
    tabviews: PropTypes.array // 标签页数组
  }
 
  state = {
    activeId: '',
    // tabviews: [{
    //   MenuID: 'home_page_id',
    //   MenuName: '首页',
    //   selected: true,
    //   type: 'Home'
    // }], // 标签集
    tabviews: null, // 标签集
    iFrameHeight: 0,
    dict: sessionStorage.getItem('lang') !== 'en-US' ? mzhCN : menUS,
  }
 
  // 关闭tab页,重新选择显示页
  handleTabview = (e, menu, index) => {
    e.stopPropagation()
    let tabs = fromJS(this.state.tabviews).toJS()
    tabs = tabs.filter(tab => {
      return tab.MenuID !== menu.MenuID
    })
    
    if (menu.selected && tabs[index - 1]) {
      tabs[index - 1].selected = true
      this.setState({
        activeId: tabs[index - 1].MenuID || ''
      })
 
      if (this.props.isiframe) {
        this.props.toggleIsiframe(false)
      }
    }
 
    this.props.modifyTabview(tabs)
  }
 
  // 关闭tab页,重新选择显示页
  closeTabView = (id) => {
    let tabs = fromJS(this.state.tabviews).toJS()
    let index = 0
    tabs = tabs.filter((tab, i) => {
      if (tab.MenuID === id) {
        index = i
        return false
      } else {
        return true
      }
    })
    
    if (tabs[index - 1]) {
      tabs[index - 1].selected = true
      this.setState({
        activeId: tabs[index - 1].MenuID || ''
      })
 
      if (this.props.isiframe) {
        this.props.toggleIsiframe(false)
      }
    }
 
    this.props.modifyTabview(tabs)
  }
 
  refreshTabview = (e, menu) => {
    e.stopPropagation()
    window.GLOB.CacheMap = new Map()
 
    if (options.sysType === 'local' && window.GLOB.systemType !== 'production') {
      let roledefer = new Promise(resolve => {
        Api.getSystemConfig({
          func: 's_Get_TrdMenu_Role',
          edition_type: 'A',
          pro_sys: ''
        }).then(result => {
          if (!result) return
          if (!result.status) {
            notification.error({
              top: 92,
              message: result.message,
              duration: 10
            })
          } else {
            let _permAction = {loaded: true} // 按钮权限
    
            if (result.UserRoles_Menu) {
              result.UserRoles_Menu.forEach(menu => {
                if (!menu.MenuID) return
                _permAction[menu.MenuID] = true
              })
            }
  
            this.props.initActionPermission(_permAction)
          }
 
          resolve()
        })
      })
  
      // 获取主菜单参数
      let menudefer = new Promise(resolve => {
        Api.getAppVersion().then(() => {
          resolve()
        }, () => {
          resolve()
        })
      })
      
      Promise.all([roledefer, menudefer]).then(() => {
        MKEmitter.emit('reloadMenuView', menu.MenuID)
      })
    } else {
      MKEmitter.emit('reloadMenuView', menu.MenuID)
    }
  }
 
  // modifyTabs = (tab, type) => {
 
  // }
 
  changeTab = (e, menu) => {
    e.stopPropagation()
    // 窗口切换
    let tabs = fromJS(this.state.tabviews).toJS()
    tabs = tabs.map(tab => {
      tab.selected = false
 
      if (tab.MenuID === menu.MenuID) {
        tab.selected = true
      }
 
      return tab
    })
 
    if (menu) {
      let _isiframe = this.props.isiframe
      if (menu.type === 'iframe') {
        _isiframe = true
      } else {
        _isiframe = false
      }
 
      if (_isiframe !== this.props.isiframe) {
        this.props.toggleIsiframe(_isiframe)
      }
    }
    
    this.setState({
      tabviews: tabs,
      activeId: menu.MenuID || ''
    }, () => {
      if (menu.MenuID) {
        MKEmitter.emit('resetActiveMenu', menu.MenuID)
      }
    })
 
    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 === '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 === 'RolePermission') {
      return (<RoleManage MenuNo={view.MenuNo} MenuID={view.MenuID} MenuName={view.MenuName} key={view.MenuID}/>)
    } else if (view.type === 'appRolePermission') {
      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} />)
    }
  }
 
  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 () {
    if (sessionStorage.getItem('isEditState') !== 'true') {
      let home = {
        MenuID: 'home_page_id',
        MenuName: '首页',
        selected: true,
        type: 'Home'
      }
      this.props.modifyTabview([home])
    }
    // MKEmitter.addListener('modifyTabs', this.modifyTabs)
    MKEmitter.addListener('closeTabView', this.closeTabView)
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (nextProps.tabviews && !is(fromJS(this.state.tabviews), fromJS(nextProps.tabviews))) {
      let view = nextProps.tabviews.filter(tab => tab.selected)[0]
 
      // 窗口在iframe与普通页面切换时,修改左侧菜单栏样式
      let activeId = ''
      if (view) {
        let _isiframe = this.props.isiframe
        if (view.type === 'iframe') {
          _isiframe = true
        } else {
          _isiframe = false
        }
  
        if (_isiframe !== this.props.isiframe) {
          this.props.toggleIsiframe(_isiframe)
        }
 
        activeId = view.MenuID
      }
 
      // 保存修改标签集
      this.setState({
        tabviews: nextProps.tabviews,
        activeId
      })
 
      let node = document.getElementById('root').parentNode.parentNode
      if (node) {
        node.scrollTop = 0
      }
    }
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    // MKEmitter.removeListener('modifyTabs', this.modifyTabs)
    MKEmitter.removeListener('closeTabView', this.closeTabView)
  }
 
  render () {
    const { tabviews, activeId } = this.state
 
    return (
      <section id="mk-tabview-wrap" className={'mk-tabview-wrap' + (this.props.collapse ? ' collapsed' : '')}>
        <div className="content-header">
          {tabviews && tabviews.length > 0 &&
            <Tabs activeKey={activeId}>
              {tabviews.map((view, index) => {
                return (
                  <Tabs.TabPane
                    tab={
                      <span className="tab-control">
                        <Icon type="redo" onClick={(e) => {this.refreshTabview(e, view)}}/>
                        <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, index)}}/> : null
                        }
                      </span>
                    }
                    key={view.MenuID}
                  >
                    {this.selectcomponent(view)}
                    <BackTop>
                      <div className="ant-back-top">
                        <div className="ant-back-top-content">
                          <div className="ant-back-top-icon"></div>
                        </div>
                      </div>
                    </BackTop>
                    {/* {options.sysType === 'local' && window.GLOB.systemType !== 'production' ? <div className="mk-water-mark">测试系统</div> : null} */}
                  </Tabs.TabPane>
                )
              })}
            </Tabs>
          }
        </div>
      </section>
    )
  }
}
 
const mapStateToProps = (state) => {
  return {
    tabviews: state.tabviews,
    collapse: state.collapse,
    isiframe: state.isiframe
  }
}
 
const mapDispatchToProps = (dispatch) => {
  return {
    modifyTabview: (tabviews) => dispatch(modifyTabview(tabviews)),
    initActionPermission: (permAction) => dispatch(initActionPermission(permAction)),
    toggleIsiframe: (isiframe) => dispatch(toggleIsiframe(isiframe))
  }
}
 
export default connect(mapStateToProps, mapDispatchToProps)(TabViews)