index.tsx 12 KB

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