king
2024-10-17 044a4ec4ebe85f8c00e889fe3914de17a6ea1f28
src/components/header/index.jsx
@@ -1,51 +1,108 @@
import React, {Component} from 'react'
import { withRouter } from 'react-router-dom'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import { Dropdown, Menu, Modal, notification, Switch, Input } from 'antd'
import { SearchOutlined, DownOutlined, MenuFoldOutlined, MenuUnfoldOutlined } from '@ant-design/icons'
import {
  toggleCollapse,
  modifyMainMenu,
  logout
} from '@/store/action'
import asyncComponent from '@/utils/asyncSpinComponent'
import asyncSpinComponent from '@/utils/asyncSpinComponent'
import asyncComponent from '@/utils/asyncComponent'
import Api from '@/api'
import MKEmitter from '@/utils/events.js'
import options from '@/store/options.js'
import Utils from '@/utils/utils.js'
import avatar from '@/assets/img/avatar.jpg'
import wxicon from '@/assets/img/wx-icon.png'
import './index.scss'
const { confirm } = Modal
const { Search } = Input
const Resetpwd = asyncComponent(() => import('@/components/resetPassword'))
const LoginForm = asyncComponent(() => import('./loginform'))
const QrCode = asyncComponent(() => import('@/components/qrcode'))
const LoginForm = asyncSpinComponent(() => import('./loginform'))
class Header extends Component {
  static propTpyes = {
    collapse: PropTypes.bool
  }
  state = {
    menulist: null, // 一级菜单
    userName: '',
    fullName: '',
    logourl: window.GLOB.mainlogo,
    wxVisible: false,
    loginVisible: false,
    loginLoading: false,
    avatar: Utils.getrealurl(sessionStorage.getItem('avatar')),
    systems: [],
    searchkey: '',
    thdMenuList: [],
    debug: sessionStorage.getItem('debug') === 'true'
    debug: sessionStorage.getItem('debug') === 'true' && window.GLOB.memberLevel > 0,
    collapse: sessionStorage.getItem('collapse') === 'true',
    lang: sessionStorage.getItem('lang') || '',
    dict: window.GLOB.dict
  }
  UNSAFE_componentWillMount () {
    window.GLOB.mainMenu = null
    // 组件加载时,获取菜单数据
    this.getRolesMenu()
    let fullName = sessionStorage.getItem('Full_Name') || ''
    let userName = sessionStorage.getItem('User_Name') || ''
    if (fullName.toLowerCase() === userName.toLowerCase()) {
      userName = ''
    }
    this.setState({fullName, userName})
  }
  componentDidMount () {
    // 获取系统的版本信息,延时查询
    setTimeout(() => {
      if (!window.GLOB.$error) {
        Api.getAppVersion()
      }
    }, 1000)
    // sessionStorage 跨页面共享
    window.addEventListener('storage', (e) => {
      if (e.key === 'getSessionStorage' && e.newValue === window.GLOB.appkey) {
        localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage))
      } else if (e.key === 'getSysPermission' && e.newValue === window.GLOB.appkey) {
        localStorage.setItem('sysPermissions', JSON.stringify({mkThdMenus: this.state.thdMenuList, mkActions: window.GLOB.mkActions}))
      } else if (e.key === 'menuUpdate') {
        let vals = e.newValue.split(',')
        let menuId = vals[1]
        let position = vals[2] || ''
        if (position === 'menu') {
          MKEmitter.emit('reloadMenuView', menuId)
        } else if (menuId) {
          Api.getAppVersion(menuId).then(() => {
            MKEmitter.emit('reloadMenuView', menuId)
          })
        }
      }
    })
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
  handleCollapse = () => {
    // 展开、收起左侧菜单栏
    this.props.toggleCollapse(!this.props.collapse)
    localStorage.setItem('collapse', !this.props.collapse)
    const { collapse } = this.state
    sessionStorage.setItem('collapse', !collapse)
    this.setState({collapse: !collapse})
    MKEmitter.emit('toggleCollapse', !collapse)
  }
  changePassword = () => {
@@ -53,24 +110,25 @@
    MKEmitter.emit('resetpassword', () => {
      setTimeout(() => {
        sessionStorage.clear()
        this.props.logout()
        this.props.history.replace('/login')
        window.location.reload()
      }, 2000)
    })
  }
  logout = () => {
    const { dict } = this.state
    // 退出登录
    let _this = this
    let that = this
    confirm({
      title: '您确定要退出吗?',
      title: dict['logout_query'] || '您确定要退出吗?',
      content: '',
      okText: '确定',
      cancelText: '取消',
      okText: dict['ok'] || '确定',
      cancelText: dict['cancel'] || '取消',
      onOk() {
        sessionStorage.clear()
        _this.props.logout()
        _this.props.history.replace('/login')
        that.props.history.replace('/login')
        window.location.reload()
      },
      onCancel() {}
    })
@@ -78,16 +136,19 @@
  changeMenu (value) {
    // 主菜单切换
    if (value.OpenType === 'outpage') {
      window.open(value.linkUrl)
    if (value.OpenType === 'newpage') {
      window.open(value.src)
    } else if (value.OpenType === 'newtab') {
      MKEmitter.emit('modifyTabs', value)
    } else if (value.OpenType === 'menu') {
      this.props.modifyMainMenu(value)
      window.GLOB.mainMenu = value
      MKEmitter.emit('mainMenuChange')
    }
  }
  getRolesMenu () {
    // 获取主菜单参数
    let _param = {func: 's_get_pc_menus', systemType: options.sysType}
    let _param = {func: 's_get_pc_menus', systemType: window.GLOB.sysType}
    _param.pro_sys = window.GLOB.systemType === 'production' ? 'Y' : ''
    
    Api.getSystemConfig(_param).then(result => {
@@ -103,10 +164,10 @@
      const { menulist, thdMenuList } = this.getMenulist(result)
      let systems = []
      if ((options.sysType === 'local' || options.sysType === 'SSO') && result.sys_list) {
      if ((window.GLOB.sysType === 'local' || window.GLOB.sysType === 'SSO') && result.sys_list) {
        systems = result.sys_list
        if (options.sysType === 'local' && window.GLOB.systemType !== 'production' && systems.length > 10) {
        if (window.GLOB.sysType === 'local' && window.GLOB.systemType !== 'production' && systems.length > 10) {
          systems.length = 10
        }
      }
@@ -127,16 +188,23 @@
        sessionStorage.removeItem('ThirdMenu')
      }
      this.props.modifyMainMenu(mainMenu)
      window.GLOB.mainMenu = mainMenu
      window.GLOB.mkThdMenus = [...thdMenuList, {MenuID: 'home_page_id', EasyCode: '', MenuName: 'home', type: 'CustomPage'}]
      thdMenuList.forEach(item => {
        window.GLOB.mkThdMenus.set(item.MenuID, item)
      })
      if (_menu) { // 延时打开,防止标签组未完成加载
        setTimeout(() => {
          MKEmitter.emit('modifyTabs', _menu)
        }, 200)
      window.GLOB.mkThdMenus.set('home_page_id', {MenuID: 'home_page_id', EasyCode: '', MenuName: 'home', type: 'CustomPage'})
      MKEmitter.emit('mainMenuChange')
      if (_menu) {
        this.openTab(_menu, 0)
      }
      MKEmitter.emit('mkMenuLoaded')
      setTimeout(() => {
        this.getwork()
      }, 100)
    })
    
    // 获取角色权限, edition_type 接口版本控制 ''、'Y'、'A'
@@ -155,7 +223,7 @@
              _permAction[menu.MenuID] = true
            })
          }
        } else {
        } else if (!/PRIMARY KEY/ig.test(result.message)) {
          notification.error({
            top: 92,
            message: result.message,
@@ -163,10 +231,47 @@
          })
        }
        MKEmitter.emit('mkActionLoaded')
        window.GLOB.mkActions = _permAction
      })
    }, 50)
  }
  getwork = () => {
    if (sessionStorage.getItem('work_grade')) return
    let param = {func: 's_get_local_my_worker_v1'}
    if (window.GLOB.forcedUpdate) {
      param.s_version_up = 'true'
    }
    Api.genericInterface(param).then(result => {
      sessionStorage.setItem('work_grade', result.work_grade || 0)
      sessionStorage.setItem('work_group', result.work_group || '')
      if (!result.status && sessionStorage.getItem('debug') === 'true') {
        notification.error({
          top: 92,
          message: /s_get_local_my_worker_v1/.test(result.message) ? '职员信息获取失败,请联系管理员。' : result.message,
          duration: 10
        })
      }
    })
  }
  openTab = (menu, times) => {
    if (times > 50) return
    times++
    let tabgroup = document.getElementById('mk-tabgroup-wrap')
    if (window.GLOB.mkActions.loaded && tabgroup) {
      MKEmitter.emit('modifyTabs', menu)
    } else {
      setTimeout(() => {
        this.openTab(menu, times)
      }, 200)
    }
  }
  getMenulist = (result) => {
@@ -186,8 +291,13 @@
          let PageParam = JSON.parse(fst.PageParam)
          if (PageParam.OpenType === 'outpage' && PageParam.linkUrl) {
            fstItem.OpenType = 'outpage'
            fstItem.linkUrl = PageParam.linkUrl
            fstItem.OpenType = 'newpage'
            fstItem.src = window.GLOB.systemType === 'production' && PageParam.linkProUrl ? PageParam.linkProUrl : PageParam.linkUrl
            if (/#\/iframe\//.test(fstItem.src)) {
              fstItem.src = fstItem.src.replace(/@loginuid@/ig, sessionStorage.getItem('LoginUID'))
              fstItem.type = 'iframe'
              fstItem.OpenType = 'newtab'
            }
          }
        } catch (e) {}
      }
@@ -233,18 +343,27 @@
                  trdItem.type = PageParam.Template || 'CommonTable'
                  trdItem.OpenType = PageParam.OpenType || 'newtab'
                  trdItem.hidden = PageParam.hidden || 'false'
                  trdItem.menuColor = PageParam.menuColor || ''
                  trdItem.urlFields = PageParam.urlFields || ''
                  if (trdItem.type === 'NewPage') {
                    trdItem.src = PageParam.url || ''
                    trdItem.OpenType = 'newpage'
                    trdItem.src = window.GLOB.systemType === 'production' && PageParam.proUrl ? PageParam.proUrl : PageParam.url || ''
                    if (/#\/iframe\//.test(trdItem.src)) {
                      trdItem.src = trdItem.src.replace(/@loginuid@/ig, sessionStorage.getItem('LoginUID'))
                      trdItem.type = 'iframe'
                      trdItem.OpenType = 'newtab'
                    }
                  } else if (trdItem.OpenType === 'view') {
                    trdItem.OpenType = 'newpage'
                    trdItem.src = '#/view/' + trd.MenuID
                  } else {
                    trdItem.src = '#/tab/' + trd.MenuID
                  }
                } catch (e) {}
              }
              if (trdItem.type !== 'NewPage') {
              } else {
                trdItem.src = '#/tab/' + trd.MenuID
              }
              trdItem.OpenType = trdItem.OpenType.toLowerCase() // NewPage为打开外部页面地址
              if (names.has(trdItem.menu_name)) {
                doublenames.set(trdItem.menu_name, true)
@@ -287,10 +406,7 @@
        loginVisible: true
      })
    } else {
      sessionStorage.setItem('isEditState', 'true')
      this.props.modifyMainMenu(null)
      window.GLOB.mainMenu = null
      this.props.history.replace('/design')
    }
  }
@@ -329,15 +445,16 @@
          sessionStorage.setItem('cloudDataM', res.dataM ? 'true' : '')
          sessionStorage.setItem('cloudRole_id', res.role_id || '')
          sessionStorage.setItem('isEditState', 'true')
          sessionStorage.setItem('CloudLogo', res.open_logo || '')
          if (param.remember) {
            let _url = window.location.href.split('#')[0] + 'cloud'
            localStorage.setItem(_url, window.btoa(window.encodeURIComponent(JSON.stringify({time: new Date().getTime(), username: param.username, password: param.password}))))
            localStorage.setItem(window.GLOB.sysSign + 'cloud', window.btoa(window.encodeURIComponent(JSON.stringify({time: new Date().getTime(), username: param.username, password: param.password}))))
          } else {
            localStorage.setItem(window.GLOB.sysSign + 'cloud', window.btoa(window.encodeURIComponent(JSON.stringify({time: new Date().getTime(), username: param.username, password: ''}))))
          }
          this.props.modifyMainMenu(null)
          window.GLOB.mainMenu = null
          this.props.history.replace('/design')
        } else {
          if (res.message.indexOf('密码错误') > -1) {
@@ -419,69 +536,23 @@
      }
    })
  }
  UNSAFE_componentWillMount () {
    // 组件加载时,获取菜单数据
    this.getRolesMenu()
    let fullName = sessionStorage.getItem('Full_Name') || ''
    let userName = sessionStorage.getItem('User_Name') || ''
    if (fullName.toLowerCase() === userName.toLowerCase()) {
      userName = ''
    }
    this.setState({fullName, userName})
  }
  componentDidMount () {
    // 获取系统的版本信息,延时查询
    setTimeout(() => {
      Api.getAppVersion()
    }, 1000)
    // sessionStorage 跨页面共享
    window.addEventListener('storage', (e) => {
      if (e.key === 'getSessionStorage' && e.newValue === window.GLOB.appkey) {
        localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage))
      } else if (e.key === 'menuUpdate') {
        let menuId = e.newValue.split(',')[1]
        if (menuId) {
          Api.getAppVersion(menuId).then(() => {
            MKEmitter.emit('reloadMenuView', menuId)
          })
        }
      }
    })
  }
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
  gotoDoc = () => {
    if (options.sysType === 'local' && window.GLOB.mainSystemApi) {
    if (window.GLOB.sysType === 'local' && window.GLOB.mainSystemApi) {
      let ssodomain = window.GLOB.mainSystemApi.replace('/webapi/dostars', '')
      let url = `${ssodomain}/doc/index.html#?appkey=${window.GLOB.appkey}&LoginUID=${sessionStorage.getItem('LoginUID')}`
      window.open(url)
    } else if (options.sysType === 'SSO' || options.sysType === 'cloud') {
    } else if (window.GLOB.sysType === 'SSO' || window.GLOB.sysType === 'cloud') {
      window.open(`${window.location.href.replace(/\/admin(.*)|\/index.html(.*)|\/#(.*)/ig, '')}/doc/index.html#?appkey=${window.GLOB.appkey}&LoginUID=${sessionStorage.getItem('LoginUID')}`)
    }
  }
  changeVerMenu(menu, type) {
    if (type === 'first') {
      if (menu.OpenType === 'outpage') {
        window.open(menu.linkUrl)
      if (menu.OpenType === 'newpage') {
        window.open(menu.src)
      } else if (menu.OpenType === 'newtab') {
        MKEmitter.emit('modifyTabs', menu)
      }
    } else {
      if (menu.OpenType === 'newpage') {
@@ -497,25 +568,31 @@
  }
  verup = () => {
    const { dict } = this.state
    confirm({
      title: '页面更新',
      content: '重新加载应用信息',
      title: dict['page_upd'] || '页面更新',
      content: dict['reload_app'] || '重新加载应用信息',
      okText: dict['ok'] || '确定',
      cancelText: dict['cancel'] || '取消',
      onOk() {
        return new Promise(resolve => {
          if (!window.GLOB.IndexDB) {
            notification.warning({
              top: 92,
              message: '更新失败,请刷新页面重试!',
              message: dict['exc_fail'] || '更新失败,请刷新页面重试!',
              duration: 2
            })
            resolve()
          } else {
            Api.updateAppVersion()
            Api.delCacheConfig('all')
            localStorage.removeItem(window.location.href.split('#')[0] + 'AuthCode')
            setTimeout(() => {
              notification.success({
                top: 92,
                message: '更新成功!',
                message: dict['exc_success'] || '更新成功!',
                duration: 2
              })
              resolve()
@@ -529,7 +606,8 @@
  about = () => {
    Modal.success({
      title: '系统版本v' + window.GLOB.appVersion
      title: (window.GLOB.dict['sys_ver'] || '系统版本') + ' v' + window.GLOB.appVersion,
      okText: window.GLOB.dict['got_it'] || '知道了',
    })
  }
@@ -540,30 +618,32 @@
  }
  render () {
    const { mainMenu, collapse } = this.props
    const { thdMenuList, searchkey, debug, menulist } = this.state
    const { thdMenuList, searchkey, debug, menulist, collapse, dict } = this.state
    const navBar = window.GLOB.navBar
    const menu = (
      <Menu className="header-dropdown">
        {debug && <Menu.Item key="switch">
          编辑
          {dict['edit'] || '编辑'}
          <Switch size="small" style={{marginLeft: '7px'}} checked={false} onChange={this.changeEditState} />
        </Menu.Item>}
        <Menu.Item key="password" onClick={this.changePassword}>修改密码</Menu.Item>
        {this.state.systems.length ? <Menu.SubMenu style={{minWidth: '110px'}} title="切换系统">
        <Menu.Item key="password" onClick={this.changePassword}>{dict['ch_pwd'] || '修改密码'}</Menu.Item>
        {this.state.systems.length ? <Menu.SubMenu style={{minWidth: '110px'}} title={dict['swt_sys'] || '切换系统'}>
          {this.state.systems.map((system, index) => (
            <Menu.Item style={{minWidth: '100px', lineHeight: '30px'}} key={'sub' + index} onClick={() => {this.changeSystem(system)}}> {system.AppName} </Menu.Item>
          ))}
        </Menu.SubMenu> : null}
        <Menu.Item key="doc" onClick={this.gotoDoc}>文档中心</Menu.Item>
        <Menu.Item key="doc" onClick={this.gotoDoc}>{dict['doc_center'] || '文档中心'}</Menu.Item>
        <Menu.Item key="verup" onClick={this.verup}>
          页面更新
          {dict['page_upd'] || '页面更新'}
        </Menu.Item>
        {window.GLOB.appVersion ? <Menu.Item key="version" onClick={this.about}>
          关于
        {window.GLOB.WXNotice ? <Menu.Item key="wxnotice" onClick={() => this.setState({wxVisible: true})}>
          微信消息
        </Menu.Item> : null}
        <Menu.Item key="logout" onClick={this.logout}>退出</Menu.Item>
        {window.GLOB.appVersion ? <Menu.Item key="version" onClick={this.about}>
          {dict['about'] || '关于'}
        </Menu.Item> : null}
        <Menu.Item key="logout" onClick={this.logout}>{dict['logout'] || '退出'}</Menu.Item>
      </Menu>
    )
@@ -580,7 +660,7 @@
          <ul className="header-menu">{
            menulist.map(item => {
              return (
                <li key={item.MenuID} onClick={() => {this.changeMenu(item)}} className={mainMenu && mainMenu.MenuID === item.MenuID ? 'active' : ''}>
                <li key={item.MenuID} onClick={() => {this.changeMenu(item)}} className={window.GLOB.mainMenu && window.GLOB.mainMenu.MenuID === item.MenuID ? 'active' : ''}>
                  <span>{item.MenuName}</span>
                </li>
              )
@@ -643,7 +723,7 @@
                            <div className="title" onClick={e => e.stopPropagation()}>{cell.MenuName}</div>
                            <div className="menu-detail">
                              {cell.children && cell.children.map(m => (
                                <div key={m.MenuID} title={m.MenuName} onClick={() => {this.changeVerMenu(m)}}>
                                <div key={m.MenuID} title={m.MenuName} style={m.menuColor ? {color: m.menuColor} : null} onClick={() => {this.changeVerMenu(m)}}>
                                  {m.MenuName}
                                </div>
                              ))}
@@ -718,7 +798,7 @@
        </Dropdown>
        {/* 编辑状态登录 */}
        <Modal
          title="登录开发机"
          title={<span>登录开发机{this.state.lang === 'en-US' ? <span style={{color: 'red'}}>(英文)</span> : ''}</span>}
          visible={this.state.loginVisible}
          onOk={this.loginSubmit}
          width={'430px'}
@@ -730,24 +810,29 @@
        </Modal>
        {/* 修改密码 */}
        <Resetpwd />
        {/* 微信消息 */}
        <Modal
          wrapClassName="mk-wx-sms-modal"
          visible={this.state.wxVisible}
          title="扫码关注  接收消息"
          width={400}
          centered={true}
          onCancel={() => this.setState({wxVisible: false})}
          footer={null}
          destroyOnClose
        >
          <div className="wx-sms-wrap">
            <QrCode card={{qrWidth: 200, color: '#000000'}} value={window.GLOB.baseurl + 'mob/wxnotice.html?userid=' + sessionStorage.getItem('UserID') + '&loginuid=' + sessionStorage.getItem('LoginUID')}/>
            <div className="tip">
              <img src={wxicon} alt=""/>
              <span>微信扫码</span>
              <span>关注公众号</span>
            </div>
          </div>
        </Modal>
      </header>
    )
  }
}
const mapStateToProps = (state) => {
  return {
    collapse: state.collapse,
    mainMenu: state.mainMenu
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    toggleCollapse: (collapse) => dispatch(toggleCollapse(collapse)),
    modifyMainMenu: (mainMenu) => dispatch(modifyMainMenu(mainMenu)),
    logout: () => dispatch(logout())
  }
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Header))
export default withRouter(Header)