Home.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <div class="home">
  3. <div class="home-header">
  4. <div class="head-logo">
  5. <h1>逸教云</h1>
  6. </div>
  7. <div class="head-menu menu-list">
  8. <ul>
  9. <li
  10. v-for="(nav, index) in navs"
  11. :key="index"
  12. @click="toPage(index, 0)"
  13. :class="{ 'menu-item-act': curMainIndex === index }"
  14. >
  15. <div class="menu-item">
  16. <i :class="['icon', `icon-${nav.router}`]"></i>
  17. <span>{{ nav.title }}</span>
  18. </div>
  19. </li>
  20. </ul>
  21. </div>
  22. <div class="head-user menu-list">
  23. <ul>
  24. <li>
  25. <div class="menu-item">
  26. <i class="icon icon-account"></i>
  27. <span>账户设置</span>
  28. </div>
  29. </li>
  30. <li @click="toLogout">
  31. <div class="menu-item">
  32. <i class="icon icon-shut"></i>
  33. <span>退出登录</span>
  34. </div>
  35. </li>
  36. </ul>
  37. </div>
  38. </div>
  39. <div class="home-navs" v-if="curNav">
  40. <div class="nav-head">
  41. <i :class="['icon', `icon-${curNav.router}-gray`]"></i>
  42. <span>{{ curNav.title }}</span>
  43. </div>
  44. <ul class="nav-list">
  45. <li
  46. class="nav-item"
  47. v-for="(nav, subNo) in curNav.children"
  48. :key="subNo"
  49. >
  50. <div
  51. :class="[
  52. 'nav-item-main',
  53. { 'nav-item-main-act': curSubIndex === subNo }
  54. ]"
  55. @click="switchNav(subNo)"
  56. >
  57. <p class="nav-item-cont">{{ nav.title }}</p>
  58. <span class="nav-item-icon nav-item-icon-right">
  59. <i
  60. :class="[
  61. 'icon',
  62. curSubIndex === subNo
  63. ? 'icon-arrow-right-act'
  64. : 'icon-arrow-right'
  65. ]"
  66. ></i>
  67. </span>
  68. </div>
  69. </li>
  70. </ul>
  71. </div>
  72. <div class="home-body">
  73. <div class="home-main">
  74. <div class="home-breadcrumb" v-if="breadcrumbs.length">
  75. <el-breadcrumb separator=">">
  76. <el-breadcrumb-item :to="{ name: 'Home' }">
  77. <i class="icon icon-home" style="margin-top: -2px;"></i>
  78. </el-breadcrumb-item>
  79. <el-breadcrumb-item
  80. v-for="(bread, index) in breadcrumbs"
  81. :key="index"
  82. >{{ bread.title }}</el-breadcrumb-item
  83. >
  84. </el-breadcrumb>
  85. </div>
  86. <!-- home-view: page detail -->
  87. <div class="home-view">
  88. <router-view />
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </template>
  94. <script>
  95. import localNavs from "@/constants/navs";
  96. import { deepCopy } from "@/plugins/utils";
  97. import { MENU_ROUTER_DICT } from "@/constants/enumerate";
  98. import { logout, sysMenu } from "../modules/login/api";
  99. export default {
  100. name: "home",
  101. data() {
  102. return {
  103. navs: [],
  104. validRoutes: [],
  105. curMainIndex: 0,
  106. curSubIndex: 0,
  107. breadcrumbs: []
  108. };
  109. },
  110. watch: {
  111. $route(val) {
  112. this.actCurNav();
  113. }
  114. },
  115. computed: {
  116. curNav() {
  117. return this.navs[this.curMainIndex];
  118. }
  119. },
  120. created() {
  121. this.getMenus();
  122. },
  123. methods: {
  124. async getMenus() {
  125. const data = await sysMenu();
  126. this.navs = this.menusToTree(data.records);
  127. console.log(this.$route.name);
  128. if (this.$route.name === "Home") {
  129. this.$router.replace({
  130. name: this.navs[0].children[0].router
  131. });
  132. } else {
  133. if (!this.validRoutes.includes(this.$route.name)) {
  134. this.$router.replace({
  135. name: "404"
  136. });
  137. }
  138. }
  139. this.actCurNav();
  140. },
  141. menusToTree(menus) {
  142. let navTree = deepCopy(localNavs);
  143. let validRoutes = [];
  144. const anchorNav = (menu, navs) => {
  145. const name = MENU_ROUTER_DICT[menu.url];
  146. navs.forEach(item => {
  147. if (item.router === name) {
  148. item.fixed = true;
  149. validRoutes.push(item.router);
  150. if (menu.parentId !== null && item["children"]) {
  151. item.children.map(el => {
  152. el.fixed = true;
  153. validRoutes.push(el.router);
  154. });
  155. }
  156. return;
  157. } else {
  158. if (item["children"]) anchorNav(menu, item.children);
  159. }
  160. });
  161. };
  162. const clearNoFixed = navs => {
  163. let list = [];
  164. navs.forEach(nav => {
  165. if (nav["fixed"]) {
  166. let navItem = { title: nav.title, router: nav.router };
  167. if (nav["children"])
  168. navItem.children = clearNoFixed(nav["children"]);
  169. list.push(navItem);
  170. }
  171. });
  172. return list;
  173. };
  174. menus.forEach(menu => {
  175. anchorNav(menu, navTree);
  176. });
  177. this.validRoutes = validRoutes;
  178. return clearNoFixed(navTree);
  179. },
  180. switchNav(subNo) {
  181. const curSubItem = this.curNav.children[subNo];
  182. if (curSubItem.router === this.$route.name) return;
  183. this.$router.push({ name: curSubItem.router });
  184. },
  185. actCurNav() {
  186. const relate = this.$route.meta && this.$route.meta.relate;
  187. const routerName = relate ? relate.split("/")[0] : this.$route.name;
  188. this.navs.forEach((item, index) => {
  189. item.children.forEach((elem, pindex) => {
  190. if (elem.router === routerName) {
  191. this.curSubIndex = pindex;
  192. this.curMainIndex = index;
  193. this.breadcrumbs = [
  194. { title: item.title, router: item.router },
  195. { title: elem.title, router: elem.router }
  196. ];
  197. }
  198. });
  199. });
  200. if (relate) this.getMoreBreadcrumbs(relate);
  201. },
  202. getMoreBreadcrumbs(relate) {
  203. const curSubNav = this.navs[this.curMainIndex].children[this.curSubIndex];
  204. if (!curSubNav.children || !curSubNav.children.length) return;
  205. relate
  206. .split("/")
  207. .slice(1)
  208. .map(routerName => {
  209. const matchRouter = curSubNav.children.find(
  210. elem => elem.router === routerName
  211. );
  212. if (matchRouter)
  213. this.breadcrumbs.push({
  214. title: matchRouter.title,
  215. router: matchRouter.router
  216. });
  217. });
  218. },
  219. toPage(mainIndex, subIndex) {
  220. const elem = this.navs[mainIndex].children[subIndex];
  221. if (elem.router === this.$route.name) return;
  222. this.$router.push({
  223. name: elem.router
  224. });
  225. },
  226. toLogout() {
  227. this.$confirm("确定要退出登录吗?", "提示", {
  228. cancelButtonClass: "el-button--primary",
  229. confirmButtonClass: "el-button--default-act",
  230. type: "warning"
  231. })
  232. .then(async () => {
  233. await logout(this.$ls.get("user", { id: "" }).id);
  234. this.$ls.clear();
  235. this.$router.push({ name: "Login" });
  236. })
  237. .catch(() => {});
  238. }
  239. }
  240. };
  241. </script>