index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 { getDictListAPI, passWordEditAPI } from "@/store/action/login";
  19. import { getTokenInfo, removeTokenInfo } from "@/utils/storage";
  20. import { useDispatch, useSelector } from "react-redux";
  21. // import inco1Ac from "@/assets/img/inco1Ac.png";
  22. import inco2Ac from "@/assets/img/inco2Ac.png";
  23. import inco3Ac from "@/assets/img/inco3Ac.png";
  24. import { MessageFu } from "@/utils/message";
  25. import logoImg from "@/assets/img/logo.png";
  26. import { barrageGetUnauditedAPI } from "@/store/action/B4Barrage";
  27. import { RootState } from "@/store";
  28. import { getPermissionsAPI } from "@/store/action/C2Role";
  29. const NotFound = React.lazy(() => import("@/components/NotFound"));
  30. function Layout() {
  31. const dispatch = useDispatch();
  32. // 进页面获取弹幕管未审核个数
  33. useEffect(() => {
  34. dispatch(barrageGetUnauditedAPI());
  35. }, [dispatch]);
  36. const barrageNum = useSelector(
  37. (state: RootState) => state.barrageReducer.stateNum
  38. );
  39. type ListTempType = {
  40. title: string;
  41. incoAc: any;
  42. son: {
  43. id: number;
  44. name: string;
  45. path: string;
  46. done: boolean;
  47. Com: React.LazyExoticComponent<
  48. React.MemoExoticComponent<() => JSX.Element>
  49. >;
  50. }[];
  51. }[];
  52. const listTemp = useMemo(() => {
  53. const arr: ListTempType = [
  54. // {
  55. // title: "数据统计",
  56. // incoAc: inco1Ac,
  57. // son: [
  58. // {
  59. // id: 110,
  60. // name: "热度统计",
  61. // path: "/",
  62. // done: false,
  63. // Com: React.lazy(() => import("../A1Hot")),
  64. // },
  65. // ],
  66. // },
  67. {
  68. title: "内容管理",
  69. incoAc: inco2Ac,
  70. son: [
  71. // {
  72. // id: 220,
  73. // name: "场景管理",
  74. // path: "/scene",
  75. // done: false,
  76. // Com: React.lazy(() => import("../B1Scene")),
  77. // },
  78. {
  79. id: 230,
  80. name: "馆藏管理",
  81. // path: "/goods",
  82. path: "/",
  83. done: false,
  84. Com: React.lazy(() => import("../B2Goods")),
  85. },
  86. {
  87. id: 240,
  88. name: "万物墙管理",
  89. path: "/wall",
  90. done: false,
  91. Com: React.lazy(() => import("../B3Wall")),
  92. },
  93. {
  94. id: 250,
  95. name: "弹幕管理",
  96. path: "/barrage",
  97. done: false,
  98. Com: React.lazy(() => import("../B4Barrage")),
  99. },
  100. {
  101. id: 260,
  102. name: "题库管理",
  103. path: "/topic",
  104. done: false,
  105. Com: React.lazy(() => import("../B5Topic")),
  106. },
  107. // {
  108. // id: 270,
  109. // name: "智能导览管理",
  110. // path: "/smart",
  111. // done: false,
  112. // Com: React.lazy(() => import("../B6Smart")),
  113. // },
  114. ],
  115. },
  116. ];
  117. return arr;
  118. }, []);
  119. // 是超级管理员
  120. useEffect(() => {
  121. const userInfo = getTokenInfo().user;
  122. if (userInfo.isAdmin === 1) {
  123. listTemp.push({
  124. title: "系统管理",
  125. incoAc: inco3Ac,
  126. son: [
  127. {
  128. id: 800,
  129. name: "用户管理",
  130. path: "/user",
  131. done: true,
  132. Com: React.lazy(() => import("../C1User")),
  133. },
  134. {
  135. id: 900,
  136. name: "角色管理",
  137. path: "/role",
  138. done: true,
  139. Com: React.lazy(() => import("../C2Role")),
  140. },
  141. {
  142. id: 1000,
  143. name: "操作日志",
  144. path: "/log",
  145. done: true,
  146. Com: React.lazy(() => import("../C3Log")),
  147. },
  148. ],
  149. });
  150. }
  151. }, [listTemp]);
  152. const authPageArr = useSelector(
  153. (state: RootState) => state.loginStore.authPageArr
  154. );
  155. // 权限的数据和页面判断
  156. useEffect(() => {
  157. authPageArr.forEach((v1) => {
  158. v1.children?.forEach((v2) => {
  159. if (v2.authority) {
  160. listTemp.forEach((v3) => {
  161. v3.son.forEach((v4) => {
  162. if (v2.id === v4.id) v4.done = true;
  163. });
  164. });
  165. }
  166. });
  167. });
  168. const arr = listTemp.map((v1) => {
  169. return {
  170. ...v1,
  171. son: v1.son.filter((v2) => v2.done),
  172. };
  173. });
  174. setList(arr);
  175. }, [authPageArr, listTemp]);
  176. const [list, setList] = useState(listTemp);
  177. // 进页面看看第一个页面有权限的是哪一个
  178. const timeRef = useRef(0);
  179. useEffect(() => {
  180. const userInfo = getTokenInfo().user;
  181. if (userInfo.isAdmin !== 1) {
  182. clearTimeout(timeRef.current);
  183. timeRef.current = window.setTimeout(() => {
  184. const newList: any = [];
  185. list.forEach((v1) => {
  186. v1.son.forEach((v2) => {
  187. newList.push(v2);
  188. });
  189. });
  190. if (newList[0] && newList[0].id !== 110)
  191. history.replace(newList[0].path);
  192. }, 100);
  193. }
  194. }, [list]);
  195. // 进页面获取所有下拉信息和权限信息
  196. useEffect(() => {
  197. dispatch(getDictListAPI());
  198. dispatch(getPermissionsAPI());
  199. }, [dispatch]);
  200. // 点击跳转
  201. const pathCutFu = useCallback((path: string) => {
  202. history.push(path);
  203. }, []);
  204. // 当前路径选中的左侧菜单
  205. const location = useLocation();
  206. const [path, setPath] = useState("");
  207. useEffect(() => {
  208. const arr = location.pathname.split("/");
  209. let pathTemp = "/";
  210. if (arr[1]) pathTemp = "/" + arr[1];
  211. setPath(pathTemp);
  212. }, [location]);
  213. // 第一级菜单选中高亮
  214. const row1ActiveFu = useCallback(
  215. (title: string) => {
  216. let flag = false;
  217. list.forEach((v1) => {
  218. v1.son.forEach((v2) => {
  219. if (v2.path === path && v1.title === title) flag = true;
  220. });
  221. });
  222. return flag;
  223. },
  224. [list, path]
  225. );
  226. const userInfo = useMemo(() => {
  227. return getTokenInfo().user;
  228. }, []);
  229. // 修改密码相关
  230. const [open, setOpen] = useState(false);
  231. // 拿到新密码的输入框的值
  232. const oldPasswordValue = useRef("");
  233. const checkPassWord = (rule: any, value: any = "") => {
  234. if (value !== oldPasswordValue.current)
  235. return Promise.reject("新密码不一致!");
  236. else return Promise.resolve(value);
  237. };
  238. const onFinish = async (values: any) => {
  239. // 通过校验之后发送请求
  240. if (values.oldPassword === values.newPassword)
  241. return MessageFu.warning("新旧密码不能相同!");
  242. const obj = {
  243. oldPassword: encodeStr(Base64.encode(values.oldPassword)),
  244. newPassword: encodeStr(Base64.encode(values.newPassword)),
  245. };
  246. const res: any = await passWordEditAPI(obj);
  247. if (res.code === 0) {
  248. MessageFu.success("修改成功!");
  249. loginExit();
  250. }
  251. };
  252. // 点击退出登录
  253. const loginExit = () => {
  254. removeTokenInfo();
  255. history.push("/login");
  256. };
  257. return (
  258. <div className={styles.Layout}>
  259. {/* 左边 */}
  260. <div className="layoutLeft">
  261. <div className="layoutLeftTop">
  262. <img src={logoImg} alt="" />
  263. </div>
  264. {/* 左边主体 */}
  265. <div className="layoutLeftMain">
  266. {list.map((v1) => (
  267. <div className="mainBoxL2RowBox" key={v1.title}>
  268. <div
  269. style={{
  270. display: v1.son.every((v) => !v.done) ? "none" : "flex",
  271. }}
  272. className={classNames(
  273. "mainBoxL2RowBoxTit",
  274. row1ActiveFu(v1.title) ? "activeRow1" : ""
  275. )}
  276. >
  277. <img src={v1.incoAc} alt="" />
  278. <div className="txt">{v1.title}</div>
  279. </div>
  280. {v1.son.map((v2) => (
  281. <div
  282. key={v2.id}
  283. onClick={() => pathCutFu(v2.path)}
  284. className={classNames(
  285. "mainBoxL2Row",
  286. v2.path === path ? "active" : ""
  287. )}
  288. >
  289. {v2.id === 250 && barrageNum ? (
  290. <div className="rowTip">{barrageNum}</div>
  291. ) : null}
  292. <div className="txt">{v2.name}</div>
  293. </div>
  294. ))}
  295. </div>
  296. ))}
  297. </div>
  298. </div>
  299. {/* 右边 */}
  300. <div className="layoutRight">
  301. <div className="layoutRightTop">
  302. {/* 用户相关 */}
  303. <div className="user">
  304. {userInfo.realName}
  305. <div className="userInco userInco1">
  306. <CaretUpOutlined />
  307. </div>
  308. <div className="userInco userInco2">
  309. <CaretDownOutlined />
  310. </div>
  311. <div className="userSet">
  312. <span onClick={() => setOpen(true)}>修改密码</span>
  313. <Popconfirm
  314. placement="bottom"
  315. title="确定退出吗?"
  316. okText="确定"
  317. cancelText="取消"
  318. onConfirm={loginExit}
  319. >
  320. 退出登录
  321. </Popconfirm>
  322. </div>
  323. </div>
  324. </div>
  325. {/* 右边主体 */}
  326. <div className="layoutRightMain">
  327. {/* 二级路由页面 */}
  328. <div className="mainBoxR">
  329. <React.Suspense fallback={<SpinLoding />}>
  330. <Switch>
  331. {list.map((v1) => {
  332. return v1.son.map((v2) => (
  333. <AuthRoute
  334. key={v2.id}
  335. exact
  336. path={v2.path}
  337. component={v2.Com}
  338. />
  339. ));
  340. })}
  341. <Route path="*" component={NotFound} />
  342. </Switch>
  343. </React.Suspense>
  344. </div>
  345. </div>
  346. </div>
  347. {/* 点击修改密码打开的对话框 */}
  348. <Modal
  349. destroyOnClose
  350. open={open}
  351. title="修改密码"
  352. onCancel={() => setOpen(false)}
  353. footer={
  354. [] // 设置footer为空,去掉 取消 确定默认按钮
  355. }
  356. >
  357. <Form
  358. name="basic"
  359. labelCol={{ span: 5 }}
  360. wrapperCol={{ span: 16 }}
  361. onFinish={onFinish}
  362. autoComplete="off"
  363. >
  364. <Form.Item
  365. label="旧密码"
  366. name="oldPassword"
  367. rules={[{ required: true, message: "不能为空!" }]}
  368. >
  369. <Input.Password maxLength={15} />
  370. </Form.Item>
  371. <Form.Item
  372. label="新密码"
  373. name="newPassword"
  374. rules={[
  375. { required: true, message: "不能为空!" },
  376. { min: 6, max: 15, message: "密码长度为6-15个字符!" },
  377. ]}
  378. >
  379. <Input.Password
  380. maxLength={15}
  381. onChange={(e) => (oldPasswordValue.current = e.target.value)}
  382. />
  383. </Form.Item>
  384. <Form.Item
  385. label="确定新密码"
  386. name="checkPass"
  387. rules={[{ validator: checkPassWord }]}
  388. >
  389. <Input.Password maxLength={15} />
  390. </Form.Item>
  391. <Form.Item wrapperCol={{ offset: 14, span: 16 }}>
  392. <Button onClick={() => setOpen(false)}>取消</Button>
  393. &emsp;
  394. <Button type="primary" htmlType="submit">
  395. 确定
  396. </Button>
  397. </Form.Item>
  398. </Form>
  399. </Modal>
  400. </div>
  401. );
  402. }
  403. // 使用 React.memo 来优化组件,避免组件的无效更新,类似 类组件里面的PureComponent
  404. const MemoLayout = React.memo(Layout);
  405. export default MemoLayout;