king
2024-09-10 4d6b9f8a2d7b316633e43b489eae9ada949c07b5
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
import React, {Component} from 'react'
import { fromJS } from 'immutable'
import { Tabs, BackTop, notification, Modal } from 'antd'
import { RedoOutlined, CloseOutlined, 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 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 BaseTable = asyncComponent(() => import('@/tabviews/basetable'))
const TreePage = asyncComponent(() => import('@/tabviews/treepage'))
const Iframe = asyncComponent(() => import('@/tabviews/iframe'))
const RoleManage = asyncComponent(() => import('@/tabviews/rolemanage'))
 
if (sessionStorage.getItem('lang') === 'zh-CN') {
  moment.locale('zh-cn')
}
 
class TabViews extends Component {
  state = {
    activeId: '',
    tabviews: [],
    visible: false
  }
 
  reloading = false
 
  UNSAFE_componentWillMount () {
    if (!window.GLOB.mkHS) {
      this.setState({
        activeId: 'home_page_id',
        tabviews: [{
          MenuID: 'home_page_id',
          MenuName: window.GLOB.dict['home'] || '首页',
          type: 'Home'
        }]
      })
    }
  }
 
  componentDidMount () {
    MKEmitter.addListener('modifyTabs', this.modifyTabs)
    MKEmitter.addListener('closeTabView', this.closeTabView)
    MKEmitter.addListener('reloadTabs', this.reloadTabs)
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('modifyTabs', this.modifyTabs)
    MKEmitter.removeListener('reloadTabs', this.reloadTabs)
    MKEmitter.removeListener('closeTabView', this.closeTabView)
  }
 
  // 关闭tab页,重新选择显示页
  closeTabView = (id) => {
    let tabs = fromJS(this.state.tabviews).toJS()
    let index = -1
 
    tabs = tabs.filter((tab, i) => {
      if (tab.MenuID === id) {
        index = i
        return false
      } else {
        return true
      }
    })
    
    if (index > -1) {
      let activeId = ''
      if (id !== this.state.activeId) {
        activeId = this.state.activeId
      } else if (index > 0) {
        activeId = tabs[index - 1].MenuID || ''
      } else if (tabs[index]) {
        activeId = tabs[index].MenuID || ''
      }
 
      this.setState({
        activeId,
        tabviews: tabs
      })
 
      let node = document.getElementById('root').parentNode.parentNode
      if (node) {
        node.scrollTop = 0
      }
    }
  }
 
  refreshTabview = (e, menu) => {
    e.stopPropagation()
    window.GLOB.CacheMap = new Map()
 
    if (window.GLOB.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
              })
            }
  
            window.GLOB.mkActions = _permAction
          }
 
          resolve()
        })
      })
  
      // 获取主菜单参数
      let menudefer = new Promise(resolve => {
        Api.getAppVersion(menu.MenuID).then(() => {
          resolve()
        }, () => {
          resolve()
        })
      })
      
      Promise.all([roledefer, menudefer]).then(() => {
        MKEmitter.emit('reloadMenuView', menu.MenuID)
      })
    } else {
      MKEmitter.emit('reloadMenuView', menu.MenuID)
    }
  }
 
  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) {
          let _tabIds = this.state.tabviews.map(item => item.MenuID)
          list.forEach((m, i) => {
            if (_tabIds.includes(m)) {
              setTimeout(() => {
                MKEmitter.emit('reloadMenuView', m)
              }, i * 20)
            }
          })
        }
      }, 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)
    })
  }
 
  modifyTabs = (tab, fixed) => {
    const { tabviews, activeId } = this.state
 
    if (fixed) {
      let _tabs = tabviews.filter(item => item.MenuID !== tab.MenuID)
      let index = _tabs.findIndex(item => item.MenuID === activeId)
 
      this.setState({
        tabviews: _tabs
      }, () => {
        if (index > -1) {
          _tabs.splice(index + 1, 0, tab)
        } else {
          _tabs.push(tab)
        }
        this.setState({
          tabviews: _tabs,
          activeId: tab.MenuID
        })
      })
    } else {
      if (tabviews.findIndex(item => item.MenuID === tab.MenuID) > -1) {
        let _tabs = tabviews.filter(item => item.MenuID !== tab.MenuID)
        this.setState({
          tabviews: _tabs
        }, () => {
          this.setState({
            tabviews: [..._tabs, tab],
            activeId: tab.MenuID
          })
        })
      } else {
        this.setState({
          tabviews: [...tabviews, tab],
          activeId: tab.MenuID
        })
      }
    }
 
    let node = document.getElementById('root').parentNode.parentNode
    if (node) {
      node.scrollTop = 0
    }
  }
 
  changeTab = (e, menu) => {
    e.stopPropagation()
    // 窗口切换
    
    this.setState({
      activeId: menu.MenuID || ''
    }, () => {
      MKEmitter.emit('resetActiveMenu', menu.MenuID || '')
    })
  }
 
  changeTemp = (MenuID, Template) => {
    let tabs = fromJS(this.state.tabviews).toJS()
 
    this.setState({
      tabviews: tabs.map(item => {
        if (item.MenuID === MenuID) {
          item.type = Template
        }
        return item
      })
    })
  }
 
  selectcomponent = (view) => {
    // 根据tab页中菜单信息,选择所需的组件
    if (view.type === 'Home') {
      return (<Home MenuID={view.MenuID} MenuName={view.MenuName}/>)
    } else 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 === '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 === 'RolePermission') {
      return (<RoleManage MenuID={view.MenuID}/>)
    } else if (view.type === 'iframe') {
      return (<Iframe MenuID={view.MenuID} title={view.MenuName} url={view.src}/>)
    } else {
      return (<NotFount />)
    }
  }
 
  render () {
    const { tabviews, activeId, visible } = this.state
 
    return (
      <section id="mk-tabgroup-wrap" className="mk-tabview-wrap">
        <div className="content-header">
          {tabviews.length > 0 &&
            <Tabs activeKey={activeId}>
              {tabviews.map(view => {
                return (
                  <Tabs.TabPane
                    tab={
                      <span className="tab-control">
                        <RedoOutlined onClick={(e) => {this.refreshTabview(e, view)}}/>
                        <span className="tab-name" onClick={(e) => {this.changeTab(e, view)}}>
                          {view.MenuName}
                        </span>
                        {view.type !== 'Home' ?
                          <CloseOutlined onClick={(e) => {e.stopPropagation();this.closeTabView(view.MenuID)}}/> : 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>
                  </Tabs.TabPane>
                )
              })}
            </Tabs>
          }
        </div>
        <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>
    )
  }
}
 
export default TabViews