Home.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 class="head-menu-btn" @click="showMenu">
  39. <span><i class="el-icon-menu"></i></span>
  40. </div>
  41. </div>
  42. <div class="home-navs" v-if="curNav">
  43. <div class="nav-head">
  44. <i :class="['icon', `icon-${curNav.router}-gray`]"></i>
  45. <span>{{ curNav.title }}</span>
  46. </div>
  47. <ul class="nav-list">
  48. <li
  49. class="nav-item"
  50. v-for="(nav, subNo) in curNav.children"
  51. :key="subNo"
  52. >
  53. <div
  54. :class="[
  55. 'nav-item-main',
  56. { 'nav-item-main-act': curSubIndex === subNo }
  57. ]"
  58. @click="switchNav(subNo)"
  59. >
  60. <p class="nav-item-cont">{{ nav.title }}</p>
  61. <span class="nav-item-icon nav-item-icon-right">
  62. <i
  63. :class="[
  64. 'icon',
  65. curSubIndex === subNo
  66. ? 'icon-arrow-right-act'
  67. : 'icon-arrow-right'
  68. ]"
  69. ></i>
  70. </span>
  71. </div>
  72. </li>
  73. </ul>
  74. </div>
  75. <div class="home-body">
  76. <div class="home-main">
  77. <div class="home-breadcrumb" v-if="breadcrumbs.length">
  78. <el-breadcrumb separator=">">
  79. <el-breadcrumb-item :to="{ name: 'Home' }">
  80. <i class="icon icon-home" style="margin-top: -2px;"></i>
  81. </el-breadcrumb-item>
  82. <el-breadcrumb-item
  83. v-for="(bread, index) in breadcrumbs"
  84. :key="index"
  85. >{{ bread.title }}</el-breadcrumb-item
  86. >
  87. </el-breadcrumb>
  88. </div>
  89. <!-- home-view: page detail -->
  90. <div class="home-view">
  91. <router-view />
  92. </div>
  93. </div>
  94. </div>
  95. <!-- popover menu-->
  96. <el-dialog
  97. class="menu-dialog"
  98. :visible.sync="menuDailogIsShow"
  99. title="导航菜单"
  100. top="60px"
  101. fullscreen
  102. :close-on-click-modal="false"
  103. :close-on-press-escape="false"
  104. append-to-body
  105. >
  106. <div
  107. class="home-navs home-navs-full"
  108. v-for="(mainNav, mainNo) in navs"
  109. :key="mainNo"
  110. >
  111. <div class="nav-head">
  112. <i :class="['icon', `icon-${mainNav.router}-gray`]"></i>
  113. <span>{{ mainNav.title }}</span>
  114. </div>
  115. <ul class="nav-list">
  116. <li
  117. class="nav-item"
  118. v-for="(nav, subNo) in mainNav.children"
  119. :key="subNo"
  120. >
  121. <div
  122. :class="[
  123. 'nav-item-main',
  124. {
  125. 'nav-item-main-act':
  126. curMainIndex === mainNo && curSubIndex === subNo
  127. }
  128. ]"
  129. @click="switchNavDetail(mainNo, subNo)"
  130. >
  131. <p class="nav-item-cont">{{ nav.title }}</p>
  132. <span class="nav-item-icon nav-item-icon-right">
  133. <i
  134. :class="[
  135. 'icon',
  136. curMainIndex === mainNo && curSubIndex === subNo
  137. ? 'icon-arrow-right-act'
  138. : 'icon-arrow-right'
  139. ]"
  140. ></i>
  141. </span>
  142. </div>
  143. </li>
  144. </ul>
  145. </div>
  146. </el-dialog>
  147. </div>
  148. </template>
  149. <script>
  150. import localNavs from "@/constants/navs";
  151. import { deepCopy } from "@/plugins/utils";
  152. import { MENU_ROUTER_DICT } from "@/constants/enumerate";
  153. import { logout, sysMenu } from "../modules/login/api";
  154. export default {
  155. name: "home",
  156. data() {
  157. return {
  158. navs: [],
  159. validRoutes: [],
  160. curMainIndex: 0,
  161. curSubIndex: 0,
  162. breadcrumbs: [],
  163. menuDailogIsShow: false
  164. };
  165. },
  166. watch: {
  167. $route(val) {
  168. this.actCurNav();
  169. }
  170. },
  171. computed: {
  172. curNav() {
  173. return this.navs[this.curMainIndex];
  174. }
  175. },
  176. created() {
  177. this.getMenus();
  178. // this.navs = localNavs;
  179. // this.actCurNav();
  180. },
  181. methods: {
  182. async getMenus() {
  183. const data = await sysMenu();
  184. this.navs = this.menusToTree(data.records);
  185. if (this.$route.name === "Home") {
  186. this.$router.replace({
  187. name: this.navs[0].children[0].router
  188. });
  189. } else {
  190. if (!this.validRoutes.includes(this.$route.name)) {
  191. this.$router.replace({
  192. name: "404"
  193. });
  194. }
  195. }
  196. this.actCurNav();
  197. },
  198. menusToTree(menus) {
  199. let navTree = deepCopy(localNavs);
  200. let validRoutes = [];
  201. const anchorNav = (menu, navs) => {
  202. const name = MENU_ROUTER_DICT[menu.url];
  203. navs.forEach(item => {
  204. if (item.router === name) {
  205. item.fixed = true;
  206. validRoutes.push(item.router);
  207. if (menu.parentId !== null && item["children"]) {
  208. item.children.map(el => {
  209. el.fixed = true;
  210. validRoutes.push(el.router);
  211. });
  212. }
  213. return;
  214. } else {
  215. if (item["children"]) anchorNav(menu, item.children);
  216. }
  217. });
  218. };
  219. const clearNoFixed = navs => {
  220. let list = [];
  221. navs.forEach(nav => {
  222. if (nav["fixed"]) {
  223. let navItem = { title: nav.title, router: nav.router };
  224. if (nav["children"])
  225. navItem.children = clearNoFixed(nav["children"]);
  226. list.push(navItem);
  227. }
  228. });
  229. return list;
  230. };
  231. menus.forEach(menu => {
  232. anchorNav(menu, navTree);
  233. });
  234. this.validRoutes = validRoutes;
  235. return clearNoFixed(navTree);
  236. },
  237. switchNav(subNo) {
  238. const curSubItem = this.curNav.children[subNo];
  239. if (curSubItem.router === this.$route.name) return;
  240. this.$router.push({ name: curSubItem.router });
  241. },
  242. actCurNav() {
  243. const relate = this.$route.meta && this.$route.meta.relate;
  244. const routerName = relate ? relate.split("/")[0] : this.$route.name;
  245. this.navs.forEach((item, index) => {
  246. item.children.forEach((elem, pindex) => {
  247. if (elem.router === routerName) {
  248. this.curSubIndex = pindex;
  249. this.curMainIndex = index;
  250. this.breadcrumbs = [
  251. { title: item.title, router: item.router },
  252. { title: elem.title, router: elem.router }
  253. ];
  254. }
  255. });
  256. });
  257. if (relate) this.getMoreBreadcrumbs(relate);
  258. },
  259. getMoreBreadcrumbs(relate) {
  260. const curSubNav = this.navs[this.curMainIndex].children[this.curSubIndex];
  261. if (!curSubNav.children || !curSubNav.children.length) return;
  262. relate
  263. .split("/")
  264. .slice(1)
  265. .map(routerName => {
  266. const matchRouter = curSubNav.children.find(
  267. elem => elem.router === routerName
  268. );
  269. if (matchRouter)
  270. this.breadcrumbs.push({
  271. title: matchRouter.title,
  272. router: matchRouter.router
  273. });
  274. });
  275. },
  276. toPage(mainIndex, subIndex) {
  277. const elem = this.navs[mainIndex].children[subIndex];
  278. if (elem.router === this.$route.name) return;
  279. this.$router.push({
  280. name: elem.router
  281. });
  282. },
  283. toLogout() {
  284. this.$confirm("确定要退出登录吗?", "提示", {
  285. cancelButtonClass: "el-button--primary",
  286. confirmButtonClass: "el-button--default-act",
  287. type: "warning"
  288. })
  289. .then(async () => {
  290. await logout(this.$ls.get("user", { id: "" }).id);
  291. this.$ls.clear();
  292. this.$router.push({ name: "Login" });
  293. })
  294. .catch(() => {});
  295. },
  296. // popover menu
  297. showMenu() {
  298. this.menuDailogIsShow = !this.menuDailogIsShow;
  299. },
  300. switchNavDetail(mainNo, subNo) {
  301. this.curMainIndex = mainNo;
  302. this.switchNav(subNo);
  303. this.showMenu();
  304. }
  305. }
  306. };
  307. </script>