index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
  2. import { CaretUpOutlined, CaretDownOutlined } from '@ant-design/icons'
  3. import styles from './index.module.scss'
  4. import SpinLoding from '@/components/SpinLoding'
  5. import { Route, Switch, useLocation } from 'react-router-dom'
  6. import AuthRoute from '@/components/AuthRoute'
  7. import classNames from 'classnames'
  8. import history from '@/utils/history'
  9. import { Button, Form, Input, Modal, Popconfirm } from 'antd'
  10. import { Base64 } from 'js-base64'
  11. import encodeStr from '@/utils/pass'
  12. import { passWordEditAPI } from '@/store/action/layout'
  13. import { getTokenInfo, removeTokenInfo } from '@/utils/storage'
  14. import { MessageFu } from '@/utils/message'
  15. import logonImg from '@/assets/img/logo.png'
  16. const NotFound = React.lazy(() => import('@/components/NotFound'))
  17. function Layout() {
  18. const list = useMemo(() => {
  19. return [
  20. {
  21. id: 100,
  22. img: 1,
  23. name: '场景管理',
  24. path: '/list',
  25. Com: React.lazy(() => import('../A1List'))
  26. },
  27. {
  28. id: 900,
  29. img: 2,
  30. name: '用户管理',
  31. path: '/user',
  32. Com: React.lazy(() => import('../A9User'))
  33. },
  34. {
  35. id: 901,
  36. img: 3,
  37. name: '系统日志',
  38. path: '/log',
  39. Com: React.lazy(() => import('../Z2log'))
  40. }
  41. ]
  42. }, [])
  43. useEffect(() => {
  44. // 如果是超级管理员
  45. // const userInfo = getTokenInfo().user
  46. // if (userInfo.isAdmin === 1) {
  47. // list.push({
  48. // id: 900,
  49. // name: '用户管理',
  50. // path: '/user',
  51. // Com: React.lazy(() => import('../A9User'))
  52. // })
  53. // }
  54. }, [list])
  55. // 点击跳转
  56. const pathCutFu = useCallback((path: string) => {
  57. history.push(path)
  58. }, [])
  59. // 当前路径选中的左侧菜单
  60. const location = useLocation()
  61. const [path, setPath] = useState('')
  62. useEffect(() => {
  63. const arr = location.pathname.split('/')
  64. let pathTemp = '/'
  65. if (arr[1]) pathTemp = '/' + arr[1]
  66. setPath(pathTemp)
  67. }, [location])
  68. const userInfo = useMemo(() => {
  69. return getTokenInfo().user
  70. }, [])
  71. // 修改密码相关
  72. const [open, setOpen] = useState(false)
  73. // 拿到新密码的输入框的值
  74. const oldPasswordValue = useRef('')
  75. const checkPassWord = (rule: any, value: any = '') => {
  76. if (value !== oldPasswordValue.current) return Promise.reject('新密码不一致!')
  77. else return Promise.resolve(value)
  78. }
  79. const onFinish = async (values: any) => {
  80. // 通过校验之后发送请求
  81. if (values.oldPassword === values.newPassword)
  82. return MessageFu.warning('新旧密码不能相同!')
  83. const obj = {
  84. oldPassword: encodeStr(Base64.encode(values.oldPassword)),
  85. newPassword: encodeStr(Base64.encode(values.newPassword))
  86. }
  87. const res: any = await passWordEditAPI(obj)
  88. if (res.code === 0) {
  89. MessageFu.success('修改成功!')
  90. loginExit()
  91. }
  92. }
  93. // 点击退出登录
  94. const loginExit = () => {
  95. removeTokenInfo()
  96. history.push('/login')
  97. }
  98. return (
  99. <div className={styles.Layout}>
  100. {/* 左边 */}
  101. <div className='layoutLeft'>
  102. <div className='layoutLeftTop'>
  103. {userInfo.isAdmin === 1 ? <img src={logonImg} alt='' /> : null}
  104. <div className='layTxt'>数字孪生建模平台</div>
  105. </div>
  106. {/* 左边主体 */}
  107. <div className='layoutLeftMain'>
  108. {list.map(v => (
  109. <div
  110. key={v.id}
  111. onClick={() => pathCutFu(v.path)}
  112. className={classNames('mainBoxL2Row', v.path === path ? 'active' : '')}
  113. >
  114. <div className='txt'>
  115. <img
  116. className='txtImg1'
  117. src={require(`../../assets/img/tuBiao/${v.img}.png`)}
  118. alt=''
  119. />
  120. <img
  121. className='txtImg2'
  122. src={require(`../../assets/img/tuBiao/${v.img}Ac.png`)}
  123. alt=''
  124. />
  125. {v.name}
  126. </div>
  127. </div>
  128. ))}
  129. </div>
  130. </div>
  131. {/* 右边 */}
  132. <div className='layoutRight'>
  133. <div className='layoutRightTop'>
  134. {/* 用户相关 */}
  135. <div className='user'>
  136. {userInfo.realName}
  137. <div className='userInco userInco1'>
  138. <CaretUpOutlined />
  139. </div>
  140. <div className='userInco userInco2'>
  141. <CaretDownOutlined />
  142. </div>
  143. <div className='userSet'>
  144. <span onClick={() => setOpen(true)} hidden>
  145. 修改密码
  146. </span>
  147. <Popconfirm
  148. placement='bottom'
  149. title='确定退出吗?'
  150. okText='确定'
  151. cancelText='取消'
  152. okButtonProps={{ loading: false }}
  153. onConfirm={loginExit}
  154. >
  155. 退出登录
  156. </Popconfirm>
  157. </div>
  158. </div>
  159. </div>
  160. {/* 右边主体 */}
  161. <div className='layoutRightMain'>
  162. {/* 二级路由页面 */}
  163. <div className='mainBoxR'>
  164. <React.Suspense fallback={<SpinLoding />}>
  165. <Switch>
  166. {list.map(v => (
  167. <AuthRoute key={v.id} exact path={v.path} component={v.Com} />
  168. ))}
  169. <Route path='*' component={NotFound} />
  170. </Switch>
  171. </React.Suspense>
  172. </div>
  173. </div>
  174. </div>
  175. {/* 点击修改密码打开的对话框 */}
  176. <Modal
  177. destroyOnClose
  178. open={open}
  179. title='修改密码'
  180. onCancel={() => setOpen(false)}
  181. footer={
  182. [] // 设置footer为空,去掉 取消 确定默认按钮
  183. }
  184. >
  185. <Form
  186. name='basic'
  187. labelCol={{ span: 5 }}
  188. wrapperCol={{ span: 16 }}
  189. onFinish={onFinish}
  190. autoComplete='off'
  191. >
  192. <Form.Item
  193. label='旧密码'
  194. name='oldPassword'
  195. rules={[{ required: true, message: '不能为空!' }]}
  196. >
  197. <Input.Password maxLength={15} />
  198. </Form.Item>
  199. <Form.Item
  200. label='新密码'
  201. name='newPassword'
  202. rules={[
  203. { required: true, message: '不能为空!' },
  204. { min: 6, max: 15, message: '密码长度为6-15个字符!' }
  205. ]}
  206. >
  207. <Input.Password
  208. maxLength={15}
  209. onChange={e => (oldPasswordValue.current = e.target.value)}
  210. />
  211. </Form.Item>
  212. <Form.Item
  213. label='确定新密码'
  214. name='checkPass'
  215. rules={[{ validator: checkPassWord }]}
  216. >
  217. <Input.Password maxLength={15} />
  218. </Form.Item>
  219. <Form.Item wrapperCol={{ offset: 14, span: 16 }}>
  220. <Button onClick={() => setOpen(false)}>取消</Button>
  221. &emsp;
  222. <Button type='primary' htmlType='submit'>
  223. 确定
  224. </Button>
  225. </Form.Item>
  226. </Form>
  227. </Modal>
  228. </div>
  229. )
  230. }
  231. // 使用 React.memo 来优化组件,避免组件的无效更新,类似 类组件里面的PureComponent
  232. const MemoLayout = React.memo(Layout)
  233. export default MemoLayout