SideMenu.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div class="sidemenu">
  3. <div style="height: 100%" v-if="mode">
  4. <el-menu
  5. :style="{width: width + 'px', display: 'inline-block', height: '100%'}"
  6. background-color="#464C5B"
  7. text-color="#fff"
  8. :default-active="activeName"
  9. :active-text-color="color">
  10. <el-menu-item
  11. v-for="(item) in menuList"
  12. :class="[item.title === activeName && activeName!== '首页' ? 'menuSet' : '']"
  13. :index="item.title"
  14. :key="item.id"
  15. @click="jump(item)">
  16. <template slot="title">
  17. <span>{{item.title}}</span>
  18. </template>
  19. </el-menu-item>
  20. </el-menu>
  21. <div v-show="isShow && activeName!== '首页'" class="subsidemenu">
  22. <div class="menu-header">{{selectItem.title}}</div>
  23. <ul class="menu-item-children">
  24. <li
  25. v-for="item in selectItem.children"
  26. class="menuChildren"
  27. :style="{color: item.url == activeUrl ? color : '#999'}"
  28. @click="go(item)"
  29. :key="item.id">{{item.title}}</li>
  30. </ul>
  31. </div>
  32. </div>
  33. <div style="height: 100%" v-else>
  34. <el-menu
  35. router
  36. :style="{width: '249px', display: 'inline-block', height: '100%'}"
  37. background-color="#464C5B"
  38. text-color="#fff"
  39. :default-active="routerName"
  40. :active-text-color="color">
  41. <template v-for="item in menuList">
  42. <el-submenu :key="item.id" v-if="item.children.length > 0 && item.url !== 'dashboard'" :index="item.url">
  43. <span slot="title">{{item.title}}</span>
  44. <el-menu-item v-for="list in item.children" :key="list.id" :index="list.url">{{list.title}}</el-menu-item>
  45. </el-submenu>
  46. <el-menu-item :key="item.id" v-else :index="item.url">{{item.title}}</el-menu-item>
  47. </template>
  48. </el-menu>
  49. </div>
  50. </div>
  51. </template>
  52. <script>
  53. import { mapState } from 'vuex'
  54. import { getUserMenu} from "../utils/api";
  55. export default {
  56. name: "SideMenu",
  57. computed: {
  58. ...mapState(['color', 'mode'])
  59. },
  60. data() {
  61. return {
  62. menuList: [],
  63. width: 249,
  64. isShow: false,
  65. selectItem: {},
  66. activeUrl: '',
  67. activeName: '首页',
  68. routerName: 'dashboard'
  69. }
  70. },
  71. watch: {
  72. $route(newVal) {
  73. // 标题名称
  74. let name = newVal.meta.title;
  75. let parentName = newVal.meta.parentName;
  76. let routerName = newVal.meta.permission;
  77. this.$emit('setRouterName', {name: name, router: routerName});
  78. if(this.mode) {
  79. this.activeName = parentName;
  80. this.activeUrl = routerName
  81. var that = this;
  82. that.selectItem = that.getFilter(that.menuList, parentName)[0];
  83. if (that.selectItem.children.length > 0) {
  84. that.isShow = true;
  85. } else {
  86. that.isShow = false;
  87. }
  88. } else {
  89. this.routerName = routerName
  90. }
  91. },
  92. mode(newVal) {
  93. // 路由地址
  94. let routerName = this.$route.meta.permission;
  95. // 上级菜单名字
  96. let parentName = this.$route.meta.parentName;
  97. if(newVal) {
  98. this.selectItem = this.getFilter(this.menuList, parentName)[0];
  99. this.activeUrl = routerName;
  100. this.activeName = parentName;
  101. if (this.selectItem.children.length > 0) {
  102. this.isShow = true;
  103. } else {
  104. this.isShow = false;
  105. }
  106. } else {
  107. this.routerName = routerName;
  108. }
  109. },
  110. isShow(newVal) {
  111. if(newVal) {
  112. if(this.activeName === '首页') {
  113. this.width = 249;
  114. } else {
  115. this.width = 100;
  116. }
  117. } else {
  118. this.width = 249;
  119. }
  120. },
  121. },
  122. methods: {
  123. init() {
  124. let params = {
  125. userId: localStorage.getItem('UserId')
  126. }
  127. let parentName = this.$route.meta.parentName;
  128. getUserMenu(params).then(res => {
  129. if(res.code === 10000) {
  130. this.menuList = res.data;
  131. this.selectItem = this.getFilter(this.menuList, parentName)[0];
  132. if (this.selectItem.children.length > 0) {
  133. this.isShow = true;
  134. } else {
  135. this.isShow = false;
  136. }
  137. }
  138. })
  139. },
  140. jump(item) {
  141. console.log(item);
  142. this.activeName = item.title;
  143. if(item.children.length > 0 && item.url !== "dashboard") {
  144. this.isShow = true;
  145. this.selectItem = item;
  146. } else {
  147. this.isShow = false;
  148. this.$router.push({
  149. path: '/' + item.url
  150. })
  151. }
  152. },
  153. go(item) {
  154. this.activeUrl = item.url;
  155. this.$router.push({
  156. path: '/' + item.url
  157. })
  158. },
  159. // 过滤找到自己上级
  160. getFilter(list, name) {
  161. let childrenList = [];
  162. const targetList = list.filter((item) => {
  163. if(item.title == name) {
  164. return true
  165. } else if(item.children && item.children.length) {
  166. childrenList = childrenList.concat(this.getFilter(item.children, name));
  167. return false
  168. } else {
  169. return false;
  170. }
  171. })
  172. return targetList.concat(childrenList);
  173. }
  174. },
  175. created() {
  176. this.init()
  177. },
  178. mounted() {
  179. // // 路由地址
  180. let routerName = this.$route.meta.permission;
  181. // 上级菜单名字
  182. let parentName = this.$route.meta.parentName;
  183. if(this.mode) {
  184. this.activeUrl = routerName;
  185. this.activeName = parentName;
  186. } else {
  187. this.routerName = routerName;
  188. }
  189. }
  190. }
  191. </script>
  192. <style scoped>
  193. .sidemenu {
  194. height: calc(100vh - 75px);
  195. overflow-y: auto;
  196. position: relative;
  197. padding: 0;
  198. margin: 0;
  199. }
  200. .subsidemenu {
  201. position: absolute;
  202. top: 0;
  203. right: 0;
  204. width: 149px;
  205. height: calc(100vh - 75px);
  206. float: right;
  207. border-right: 1px solid #ddd;
  208. }
  209. .menu-header {
  210. width: 100%;
  211. height: 50px;
  212. line-height: 50px;
  213. font-size: 16px;
  214. color: #999;
  215. text-align: center;
  216. border-bottom: 1px solid #ddd;
  217. background-color: rgb(242, 242, 242);
  218. }
  219. .menu-item-children {
  220. width: 100%;
  221. height: calc(100% - 52px);
  222. background-color: #EAEDF1;
  223. margin: 0;
  224. padding: 0;
  225. list-style: none;
  226. }
  227. .menuChildren {
  228. height: 40px;
  229. line-height: 40px;
  230. text-align: center;
  231. border-bottom: 1px solid #ddd;
  232. font-size: 14px;
  233. cursor: pointer;
  234. color: #BCBCBC;
  235. }
  236. .menuSet {
  237. position: relative;
  238. }
  239. .menuSet:after {
  240. content: '';
  241. position: absolute;
  242. width: 18px;
  243. height: 18px;
  244. top: 20px;
  245. left: 92px;
  246. transform: rotate(45deg);
  247. background-color: #EAEDF1;
  248. }
  249. </style>