admin-menu.vue 3.9 KB

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