admin-menu.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div id="admin-menu" style="background-color: rgb(0, 20, 42);">
  3. <el-row>
  4. <el-col :span="24">
  5. <div class="logo">
  6. <img src="https://img.alicdn.com/tfs/TB13UQpnYGYBuNjy0FoXXciBFXa-242-134.png" width="40" />
  7. <span class="site-name">双困生培训系统</span>
  8. </div>
  9. </el-col>
  10. <el-col :span="24">
  11. <template>
  12. <el-menu mode="vertical" :show-timeout="200" background-color="#00142a" text-color="hsla(0, 0%, 100%, .65)"
  13. active-text-color="#409EFF" :unique-opened="true" :default-active="defualtActive">
  14. <scroll-bar>
  15. <menu-item :menus="menu" :level="0"></menu-item>
  16. </scroll-bar>
  17. </el-menu>
  18. </template>
  19. </el-col>
  20. </el-row>
  21. </div>
  22. </template>
  23. <script>
  24. import menuItem from './menu/menu-item.vue';
  25. import * as menus from '@frame/config/menu-config';
  26. import scrollBar from './scrollBar.vue';
  27. import _ from 'lodash';
  28. import { mapState, createNamespacedHelpers } from 'vuex';
  29. const { mapActions: userMenu } = createNamespacedHelpers('userMenu');
  30. export default {
  31. name: 'admin-menu',
  32. props: {},
  33. components: {
  34. scrollBar,
  35. menuItem,
  36. },
  37. data: () => ({
  38. menu: [],
  39. project: 'center',
  40. }),
  41. created() { },
  42. computed: {
  43. ...mapState(['user', 'defaultOption']),
  44. project_modules() {
  45. return process.env.VUE_APP_MODULE;
  46. },
  47. defualtActive() {
  48. return this.$route.path;
  49. },
  50. },
  51. mounted() {
  52. let arr = _.get(menus, 'menu', []); //this.project_modules
  53. // let r = this.filterMenu(_.cloneDeep(arr), this.project_modules);
  54. // this.$set(this, `menu`, r);
  55. },
  56. methods: {
  57. ...userMenu(['getMenu']),
  58. selectMenu(path, modules) {
  59. if (this.project_modules === modules) this.$router.push({ path: path });
  60. },
  61. filterMenu(menus, umod) {
  62. let nm = menus.map(menu => {
  63. //mod 不存在:检查下面每一子项是否有限制
  64. //mod 存在:检查是否符合要求,符合,检查子项;不符合,略过
  65. let mod = _.get(menu, 'module');
  66. if ((mod && mod.includes(umod)) || !mod) {
  67. let children = _.get(menu, 'children');
  68. if (children) {
  69. menu.children = this.filterMenu(children, umod);
  70. }
  71. return menu;
  72. }
  73. });
  74. return _.compact(nm);
  75. },
  76. async getUserMenu() {
  77. this.requestMenu();
  78. // const sessionMenu = sessionStorage.getItem('userMenu');
  79. // if (sessionMenu) {
  80. // this.$set(this, `menu`, JSON.parse(sessionMenu));
  81. // } else {
  82. // this.requestMenu();
  83. // }
  84. },
  85. async requestMenu() {
  86. const { type, id: userid } = this.user;
  87. const condition = { project: 'center', type, userid };
  88. const res = await this.getMenu(condition);
  89. if (this.$checkRes(res)) {
  90. this.$set(this, `menu`, res.data);
  91. sessionStorage.setItem('userMenu', JSON.stringify(res.data));
  92. }
  93. },
  94. },
  95. watch: {
  96. user: {
  97. deep: true,
  98. immediate: true,
  99. handler(val) {
  100. if (this.menu.length > 0) return;
  101. if (val) {
  102. const { type, id } = val;
  103. if (type && id) this.getUserMenu();
  104. }
  105. },
  106. },
  107. },
  108. };
  109. </script>
  110. <style lang="less" scoped>
  111. .logo {
  112. display: flex;
  113. justify-content: center;
  114. align-items: center;
  115. height: 4rem;
  116. line-height: 4rem;
  117. background: #002140;
  118. color: #fff;
  119. text-align: center;
  120. font-size: 1.1rem;
  121. font-weight: 600;
  122. overflow: hidden;
  123. }
  124. .site-name {
  125. margin-left: 0.325rem;
  126. }
  127. .sidebar-container {
  128. box-shadow: 0.125rem 0 0.375rem rgba(0, 21, 41, 0.35);
  129. transition: width 0.28s;
  130. width: 12rem !important;
  131. height: 100%;
  132. position: fixed;
  133. top: 0;
  134. bottom: 0;
  135. left: 0;
  136. z-index: 1001;
  137. overflow: hidden;
  138. a {
  139. display: inline-block;
  140. width: 100%;
  141. }
  142. .el-menu {
  143. padding-top: 1rem;
  144. width: 100% !important;
  145. border: none;
  146. }
  147. }
  148. </style>