Home.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <div :class="['home', { 'home-page-main': IS_HOME_PAGE }]">
  3. <div class="home-header">
  4. <div class="head-menu menu-list">
  5. <ul>
  6. <li
  7. v-for="(menu, index) in menus"
  8. :key="index"
  9. :class="[
  10. 'menu-item',
  11. { 'menu-item-act': curMenu.url === menu.url },
  12. ]"
  13. @click="toMenu(menu)"
  14. >
  15. <!-- <i
  16. :class="[
  17. 'icon',`icon-${menu.url}`
  18. ]"
  19. ></i> -->
  20. <span>{{ menu.name }}</span>
  21. </li>
  22. </ul>
  23. </div>
  24. <div class="head-user menu-list">
  25. <ul>
  26. <li v-if="schoolName" @click="toSelectSchool" title="切换学校">
  27. <i class="el-icon-s-home"></i>
  28. <span>{{ schoolName }}</span>
  29. </li>
  30. <li class="menu-item menu-item-account" @click="toResetPwd">
  31. <i class="icon icon-account"></i>
  32. <span :title="username">{{ username }}</span>
  33. </li>
  34. <li class="menu-item" @click="toLogout">
  35. <i class="icon icon-logout" title="退出登录"></i>
  36. </li>
  37. </ul>
  38. </div>
  39. </div>
  40. <div class="home-navs">
  41. <div class="head-logo">
  42. <div class="head-logo-content">
  43. <img v-if="schoolLogo" :src="schoolLogo" alt="知学知考" />
  44. <h1 v-else>知学知考</h1>
  45. </div>
  46. </div>
  47. <el-menu
  48. v-if="curMenu.children && curMenu.children.length"
  49. class="el-menu-home"
  50. active-text-color="#3a5ae5"
  51. text-color="#434656"
  52. router
  53. :default-active="curRouteName"
  54. :default-openeds="curSubMenuNames"
  55. >
  56. <template v-for="submenu in curMenu.children">
  57. <el-submenu
  58. v-if="submenu.children && submenu.children.length"
  59. :key="submenu.id"
  60. :index="submenu.url"
  61. >
  62. <span slot="title">{{ submenu.name }}</span>
  63. <el-menu-item
  64. v-for="nav in submenu.children"
  65. :key="nav.id"
  66. :index="nav.url"
  67. :route="{ name: nav.url }"
  68. >
  69. <span>{{ nav.name }}</span>
  70. <span
  71. class="nav-item-info"
  72. v-if="nav.url === 'WaitTask' && waitTaskCount"
  73. >{{ waitTaskCount }}</span
  74. >
  75. </el-menu-item>
  76. </el-submenu>
  77. <el-menu-item
  78. v-else
  79. :key="submenu.id"
  80. :index="submenu.url"
  81. :route="{ name: submenu.url }"
  82. >
  83. <span>{{ submenu.name }}</span>
  84. </el-menu-item>
  85. </template>
  86. </el-menu>
  87. </div>
  88. <div class="home-body">
  89. <div class="home-body-content">
  90. <div class="home-breadcrumb" v-if="breadcrumbs.length">
  91. <span class="breadcrumb-tips">
  92. <i class="icon icon-location"></i>
  93. <span>当前所在位置:</span>
  94. </span>
  95. <el-breadcrumb separator="/">
  96. <el-breadcrumb-item v-for="bread in breadcrumbs" :key="bread.url">{{
  97. bread.name
  98. }}</el-breadcrumb-item>
  99. </el-breadcrumb>
  100. </div>
  101. <!-- home-view: page detail -->
  102. <div class="home-view">
  103. <router-view />
  104. </div>
  105. </div>
  106. </div>
  107. <view-footer></view-footer>
  108. <!-- 修改密码 -->
  109. <reset-pwd
  110. :user-info="userInfo"
  111. ref="ResetPwd"
  112. @modified="resetPwdModified"
  113. ></reset-pwd>
  114. <!-- 切换学校 -->
  115. <switch-school-dialog v-if="hasMoreSchool" ref="SwitchSchoolDialog">
  116. </switch-school-dialog>
  117. </div>
  118. </template>
  119. <script>
  120. import { mapState, mapActions } from "vuex";
  121. import localMenus from "@/constants/menus-data";
  122. import { sysMenu, logout } from "../modules/login/api";
  123. import ResetPwd from "../modules/base/components/ResetPwd";
  124. import { SYS_ADMIN_NAME } from "@/constants/enumerate";
  125. import staticMenu from "../constants/staticMenu";
  126. import SwitchSchoolDialog from "../modules/login/components/SwitchSchoolDialog.vue";
  127. import ViewFooter from "../components/ViewFooter.vue";
  128. const HOME_PAGE_ROUTE = "HomePage";
  129. export default {
  130. name: "home",
  131. components: { ResetPwd, SwitchSchoolDialog, ViewFooter },
  132. data() {
  133. const user = this.$ls.get("user", {
  134. id: "",
  135. realName: "",
  136. roleList: [],
  137. schoolInfo: [],
  138. });
  139. const IS_SUPER_ADMIN = user.loginName === SYS_ADMIN_NAME;
  140. return {
  141. privileges: [],
  142. menus: [],
  143. curMenu: { url: "", children: [] },
  144. curRouteName: "",
  145. curSubMenuNames: [],
  146. breadcrumbs: [],
  147. validRoutes: [],
  148. username: user.realName,
  149. userRoles: user.roleList,
  150. schoolLogo: this.$ls.get("schoolLogo"),
  151. schoolName: this.$ls.get("schoolName"),
  152. schoolInfo: user.schoolInfo,
  153. userInfo: {
  154. pwdCount: 0,
  155. mobileNumber: 1,
  156. userId: user.id,
  157. },
  158. IS_SUPER_ADMIN,
  159. };
  160. },
  161. watch: {
  162. $route(val) {
  163. if (val.name === "Home") return;
  164. if (val.name === HOME_PAGE_ROUTE) {
  165. return;
  166. }
  167. this.routerChange();
  168. },
  169. },
  170. computed: {
  171. ...mapState("exam", ["waitTaskCount"]),
  172. IS_HOME_PAGE() {
  173. return this.$route.name === HOME_PAGE_ROUTE;
  174. },
  175. hasMoreSchool() {
  176. return this.schoolInfo && this.schoolInfo.length > 1;
  177. },
  178. },
  179. created() {
  180. // this.initData1();
  181. this.initData();
  182. },
  183. methods: {
  184. ...mapActions("exam", ["updateWaitTaskCount"]),
  185. initData1() {
  186. // 开发阶段专用
  187. this.initPrivilegeMap(localMenus);
  188. this.privileges = this.transformMenu(localMenus);
  189. this.menus = this.getMenu();
  190. if (this.IS_HOME_PAGE) return;
  191. if (this.$route.name === "Home") {
  192. this.$router.replace({
  193. name: HOME_PAGE_ROUTE,
  194. });
  195. return;
  196. }
  197. if (!this.validRoutes.includes(this.$route.name)) {
  198. this.$router.replace({
  199. name: "404",
  200. });
  201. return;
  202. }
  203. this.updateBreadcrumbs();
  204. const curMenu = this.menus.find(
  205. (menu) => menu.url === this.breadcrumbs[0].url
  206. );
  207. this.menuChange(curMenu);
  208. if (
  209. this.validRoutes.includes("WaitTask") &&
  210. this.curMenu.url === "exam"
  211. ) {
  212. this.updateWaitTaskCount();
  213. }
  214. },
  215. async initData() {
  216. const data = await sysMenu();
  217. const privileges = [...staticMenu, ...data.privileges];
  218. this.initPrivilegeMap(privileges);
  219. this.privileges = this.transformMenu(privileges);
  220. this.menus = this.getMenu();
  221. if (this.IS_HOME_PAGE) return;
  222. if (this.$route.name === "Home") {
  223. this.toMenu(this.menus[0]);
  224. // if (this.IS_SUPER_ADMIN) {
  225. // this.toMenu(this.menus[0]);
  226. // } else {
  227. // this.$router.replace({
  228. // name: HOME_PAGE_ROUTE
  229. // });
  230. // }
  231. return;
  232. }
  233. if (!this.validRoutes.includes(this.$route.name)) {
  234. this.$router.replace({
  235. name: "404",
  236. });
  237. return;
  238. }
  239. this.updateBreadcrumbs();
  240. const curMenu = this.menus.find(
  241. (menu) => menu.url === this.breadcrumbs[0].url
  242. );
  243. this.menuChange(curMenu);
  244. if (
  245. this.validRoutes.includes("WaitTask") &&
  246. this.curMenu.url === "exam"
  247. ) {
  248. this.updateWaitTaskCount();
  249. }
  250. },
  251. transformMenu(list) {
  252. return list.map((item) => {
  253. return {
  254. id: item.id,
  255. parentId: item.parentId,
  256. name: item.name,
  257. type: item.type,
  258. url: item.url,
  259. };
  260. });
  261. },
  262. getMenu() {
  263. const getChildren = (id) => {
  264. return this.privileges
  265. .filter((item) => item.parentId === id)
  266. .map((item) => {
  267. return { ...item };
  268. });
  269. };
  270. let menus = getChildren("-1");
  271. let validRoutes = [];
  272. const toTree = (menus) => {
  273. menus.forEach((menu) => {
  274. const children = getChildren(menu.id);
  275. if (children.length) {
  276. menu.children = children;
  277. toTree(menu.children);
  278. } else {
  279. validRoutes.push(menu.url);
  280. }
  281. });
  282. };
  283. toTree(menus);
  284. this.validRoutes = validRoutes;
  285. // console.log(JSON.stringify(menus));
  286. return menus;
  287. },
  288. getCurMenuFirstRouter() {
  289. let firstRouteName = "";
  290. let menu = this.curMenu;
  291. while (menu) {
  292. firstRouteName = menu.url;
  293. menu = menu.children && menu.children[0];
  294. }
  295. return firstRouteName;
  296. },
  297. initPrivilegeMap(data) {
  298. let privilegeMap = {};
  299. const pageSetTypes = ["conditions", "buttons", "lists", "links"];
  300. data.forEach((item) => {
  301. privilegeMap[item.url] = [item.id];
  302. pageSetTypes.forEach((type, index) => {
  303. if (item[type] && item[type].length) {
  304. item[type].forEach((elem) => {
  305. privilegeMap[item.url].push(
  306. `${elem.type}_${elem.url}`.toLowerCase()
  307. );
  308. });
  309. }
  310. });
  311. });
  312. this.$store.commit("setPrivilegeMap", privilegeMap);
  313. this.$ls.set("privilegeMap", privilegeMap);
  314. // wait-types
  315. const filterFunc = {
  316. flow: () => {
  317. return (
  318. this.checkPrivilege("link", "edit", "TaskApplyManage") ||
  319. this.checkPrivilege("link", "notReviewEdit", "TaskReviewManage")
  320. );
  321. },
  322. stmms: () => {
  323. return this.checkPrivilege("link", "Submit", "UploadStructure");
  324. },
  325. analysis: () => {
  326. return this.checkPrivilege("link", "window", "DataInitManage");
  327. },
  328. };
  329. const waitTypes = Object.keys(filterFunc).filter((k) => filterFunc[k]());
  330. this.$store.commit("exam/setWaitTypes", waitTypes);
  331. },
  332. menuChange(menu) {
  333. this.curMenu = menu;
  334. this.curSubMenuNames = this.privileges
  335. .filter((item) => item.parentId === menu.id)
  336. .map((item) => item.url);
  337. },
  338. toMenu(menu) {
  339. if (menu.url === HOME_PAGE_ROUTE) {
  340. if (this.$route.name !== HOME_PAGE_ROUTE) {
  341. this.curMenu = menu;
  342. this.$router.push({
  343. name: HOME_PAGE_ROUTE,
  344. });
  345. }
  346. return;
  347. }
  348. if (this.curMenu.url === menu.url) return;
  349. this.menuChange(menu);
  350. const firstRouteName = this.getCurMenuFirstRouter();
  351. this.$router.replace({
  352. name: firstRouteName,
  353. });
  354. },
  355. updateBreadcrumbs() {
  356. this.curRouteName = this.$route.name;
  357. let breadcrumbs = [];
  358. let curBreadcrumb = this.privileges.find(
  359. (item) => item.url === this.curRouteName
  360. );
  361. breadcrumbs.push({ ...curBreadcrumb });
  362. while (curBreadcrumb && curBreadcrumb.parentId !== "-1") {
  363. curBreadcrumb = this.privileges.find(
  364. (item) => item.id === curBreadcrumb.parentId
  365. );
  366. if (curBreadcrumb) breadcrumbs.unshift({ ...curBreadcrumb });
  367. }
  368. this.breadcrumbs = breadcrumbs;
  369. },
  370. routerChange() {
  371. this.updateBreadcrumbs();
  372. // console.log(this.breadcrumbs);
  373. // console.log(this.curMenu);
  374. const curMenu = this.menus.find(
  375. (menu) => menu.url === this.breadcrumbs[0].url
  376. );
  377. this.menuChange(curMenu);
  378. if (
  379. this.validRoutes.includes("WaitTask") &&
  380. this.curMenu.url === "exam"
  381. ) {
  382. this.updateWaitTaskCount();
  383. }
  384. },
  385. toLogout() {
  386. this.$confirm("确定要退出登录吗?", "提示", {
  387. type: "warning",
  388. })
  389. .then(() => {
  390. this.logoutAction();
  391. })
  392. .catch(() => {});
  393. },
  394. async logoutAction() {
  395. await logout();
  396. const returnUrl = this.$ls.get("returnUrl");
  397. if (returnUrl) {
  398. window.location.href = returnUrl;
  399. return;
  400. }
  401. this.$ls.clear();
  402. const paramDomainCode = window.sessionStorage.getItem("paramDomainCode");
  403. const routeDomainCode = window.sessionStorage.getItem("routeDomainCode");
  404. if (paramDomainCode) {
  405. this.$router.push({ name: "Login", query: { code: paramDomainCode } });
  406. } else if (routeDomainCode) {
  407. this.$router.push({ name: "Login", params: { code: routeDomainCode } });
  408. } else {
  409. this.$router.push({ name: "Login" });
  410. }
  411. },
  412. toSelectSchool() {
  413. if (this.IS_SUPER_ADMIN) this.$router.push({ name: "SelectSchool" });
  414. if (this.hasMoreSchool) {
  415. this.$refs.SwitchSchoolDialog.open();
  416. }
  417. },
  418. toResetPwd() {
  419. if (this.IS_SUPER_ADMIN) return;
  420. this.$refs.ResetPwd.open();
  421. },
  422. resetPwdModified() {
  423. this.logoutAction();
  424. },
  425. // other
  426. getSubMenus(menu) {
  427. return this.privileges.filter(
  428. (m) => m.parentId === menu.id && m.type === "MENU"
  429. );
  430. },
  431. getFirstRouter() {
  432. let childNavs = this.privileges;
  433. let firstRouteName = "";
  434. while (childNavs.length) {
  435. firstRouteName = childNavs[0].url;
  436. childNavs = this.privileges.filter(
  437. (item) => item.parentId === childNavs[0].id
  438. );
  439. }
  440. return firstRouteName;
  441. },
  442. },
  443. };
  444. </script>