index.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import React, { useCallback, useEffect, 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 } 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 logoImg from "@/assets/img/logo.png";
  16. import NotFound from "@/components/NotFound";
  17. import { RouterType, RouterTypeRow } from "@/types";
  18. import tabLeftArr from "./data";
  19. import MyPopconfirm from "@/components/MyPopconfirm";
  20. import { UserListType } from "../Z1user/Z1auth";
  21. import { D3_APIgetInfo } from "@/store/action/D3role";
  22. function Layout() {
  23. // 当前路径选中的左侧菜单
  24. const location = useLocation();
  25. const [path, setPath] = useState("");
  26. useEffect(() => {
  27. const arr = location.pathname.split("/");
  28. let pathTemp = "/";
  29. if (arr[1]) pathTemp = "/" + arr[1];
  30. setPath(pathTemp);
  31. }, [location]);
  32. // 获取用户权限信息
  33. const getUserAuthFu = useCallback(async () => {
  34. const userInfo = getTokenInfo().user;
  35. const res = await D3_APIgetInfo(userInfo.roleId);
  36. if (res.code === 0) {
  37. console.log(123456,res);
  38. const tempList: UserListType[] = res.data.permission || [];
  39. const isOkIdArr = tempList.filter((c) => c.authority).map((v) => v.id);
  40. // 是管理员
  41. if (userInfo.isAdmin === 1) {
  42. isOkIdArr.push(2100);
  43. isOkIdArr.push(2200);
  44. isOkIdArr.push(2300);
  45. }
  46. const tempArr: RouterTypeRow = [];
  47. tabLeftArr.forEach((v1) => {
  48. if (v1.son && v1.son[0]) {
  49. v1.son.forEach((v2) => {
  50. if (isOkIdArr.includes(v2.id)) {
  51. tempArr.push(v2);
  52. }
  53. });
  54. }
  55. });
  56. setRouterCom(tempArr);
  57. // 如果当前页面没有权限了,跳转有权限的第一个页面
  58. const urlAll = window.location.hash;
  59. const isNowPath = urlAll.replace("#", "");
  60. const pathArr = tempArr.map((v) => v.path);
  61. if (!pathArr.includes(isNowPath)) {
  62. history.push(pathArr[0]);
  63. }
  64. const resList = tabLeftArr.map((v) => ({
  65. ...v,
  66. son: v.son.filter((c) => isOkIdArr.includes(c.id)),
  67. }));
  68. setList(resList);
  69. }
  70. }, []);
  71. useEffect(() => {
  72. getUserAuthFu();
  73. }, [getUserAuthFu]);
  74. // 左侧菜单 信息
  75. const [list, setList] = useState([] as RouterType);
  76. // 路由信息(过滤之后的)
  77. const [RouterCom, setRouterCom] = useState<RouterTypeRow>([]);
  78. // useEffect(() => {
  79. // console.log(123, list);
  80. // }, [list]);
  81. // 点击跳转
  82. const pathCutFu = useCallback((path: string) => {
  83. history.push(path);
  84. }, []);
  85. // 修改密码相关
  86. const [open, setOpen] = useState(false);
  87. // 拿到新密码的输入框的值
  88. const oldPasswordValue = useRef("");
  89. const checkPassWord = (rule: any, value: any = "") => {
  90. if (value !== oldPasswordValue.current)
  91. return Promise.reject("新密码不一致!");
  92. else return Promise.resolve(value);
  93. };
  94. const onFinish = async (values: any) => {
  95. // 通过校验之后发送请求
  96. if (values.oldPassword === values.newPassword)
  97. return MessageFu.warning("新旧密码不能相同!");
  98. const obj = {
  99. oldPassword: encodeStr(Base64.encode(values.oldPassword)),
  100. newPassword: encodeStr(Base64.encode(values.newPassword)),
  101. };
  102. const res: any = await passWordEditAPI(obj);
  103. if (res.code === 0) {
  104. MessageFu.success("修改成功!");
  105. loginExit();
  106. }
  107. };
  108. // 点击退出登录
  109. const loginExit = () => {
  110. removeTokenInfo();
  111. history.push("/login");
  112. };
  113. return (
  114. <div className={styles.Layout}>
  115. {/* 左边 */}
  116. <div className="layoutLeft">
  117. <div className="layoutLeftTop">
  118. <img src={logoImg} alt="" />
  119. </div>
  120. {/* 左边主体 */}
  121. <div className="layoutLeftMain">
  122. {list.map((v) => (
  123. <div
  124. className={classNames("layoutLRowBox")}
  125. key={v.id}
  126. hidden={!v.son.length}
  127. >
  128. {/* 这个项目没有一级目录 */}
  129. {/* <div className="layoutLRowBoxTxt">{v.name}</div> */}
  130. {v.son.map((v2) => (
  131. <div
  132. key={v2.id}
  133. className={classNames(
  134. "layoutLRowBoxRow",
  135. path === v2.path ? "active" : ""
  136. )}
  137. onClick={() => pathCutFu(v2.path)}
  138. >
  139. {v2.name}
  140. </div>
  141. ))}
  142. </div>
  143. ))}
  144. </div>
  145. </div>
  146. {/* 右边 */}
  147. <div className="layoutRight">
  148. <div className="layoutRightTop">
  149. {/* 用户相关 */}
  150. <div className="user">
  151. {getTokenInfo().user.realName ||
  152. getTokenInfo().user.userName ||
  153. "匿名"}
  154. <div className="userInco userInco1">
  155. <CaretUpOutlined rev={undefined} />
  156. </div>
  157. <div className="userInco userInco2">
  158. <CaretDownOutlined rev={undefined} />
  159. </div>
  160. <div className="userSet">
  161. <div>
  162. <span onClick={() => setOpen(true)}>修改密码</span>
  163. <MyPopconfirm
  164. txtK="退出登录"
  165. onConfirm={loginExit}
  166. Dom="退出登录"
  167. loc="bottom"
  168. />
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. {/* 右边主体 */}
  174. <div className="layoutRightMain">
  175. {/* 二级路由页面 */}
  176. <div className="mainBoxR">
  177. <React.Suspense fallback={<SpinLoding />}>
  178. <Switch>
  179. {RouterCom.map((v) => (
  180. <AuthRoute key={v.id} exact path={v.path} component={v.Com} />
  181. ))}
  182. <Route path="*" component={NotFound} />
  183. </Switch>
  184. </React.Suspense>
  185. </div>
  186. </div>
  187. </div>
  188. {/* 点击修改密码打开的对话框 */}
  189. <Modal
  190. destroyOnClose
  191. open={open}
  192. title="修改密码"
  193. onCancel={() => setOpen(false)}
  194. footer={
  195. [] // 设置footer为空,去掉 取消 确定默认按钮
  196. }
  197. >
  198. <Form
  199. scrollToFirstError={true}
  200. name="basic"
  201. labelCol={{ span: 5 }}
  202. wrapperCol={{ span: 16 }}
  203. onFinish={onFinish}
  204. autoComplete="off"
  205. >
  206. <Form.Item
  207. label="旧密码"
  208. name="oldPassword"
  209. rules={[{ required: true, message: "不能为空!" }]}
  210. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  211. >
  212. <Input.Password maxLength={20} />
  213. </Form.Item>
  214. <Form.Item
  215. label="新密码"
  216. name="newPassword"
  217. rules={[
  218. { required: true, message: "不能为空!" },
  219. { min: 6, max: 15, message: "密码长度为6-15个字符!" },
  220. ]}
  221. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  222. >
  223. <Input.Password
  224. maxLength={15}
  225. onChange={(e) => (oldPasswordValue.current = e.target.value)}
  226. />
  227. </Form.Item>
  228. <Form.Item
  229. label="确定新密码"
  230. name="checkPass"
  231. rules={[{ validator: checkPassWord }]}
  232. getValueFromEvent={(e) => e.target.value.replace(/\s+/g, "")}
  233. >
  234. <Input.Password maxLength={15} />
  235. </Form.Item>
  236. <Form.Item wrapperCol={{ offset: 14, span: 16 }}>
  237. <Button onClick={() => setOpen(false)}>取消</Button>
  238. &emsp;
  239. <Button type="primary" htmlType="submit">
  240. 确定
  241. </Button>
  242. </Form.Item>
  243. </Form>
  244. </Modal>
  245. </div>
  246. );
  247. }
  248. // 使用 React.memo 来优化组件,避免组件的无效更新,类似 类组件里面的PureComponent
  249. const MemoLayout = React.memo(Layout);
  250. export default MemoLayout;