index.tsx 7.6 KB

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