SidebarItem.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div v-if="!item.hidden">
  3. <template
  4. v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
  5. <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path, onlyOneChild.query)">
  6. <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
  7. <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title"/>
  8. </el-menu-item>
  9. </app-link>
  10. </template>
  11. <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
  12. <template slot="title">
  13. <item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title"/>
  14. </template>
  15. <sidebar-item
  16. v-for="child in item.children"
  17. :key="child.path"
  18. :is-nest="true"
  19. :item="child"
  20. :base-path="resolvePath(child.path)"
  21. class="nest-menu"
  22. />
  23. </el-submenu>
  24. </div>
  25. </template>
  26. <script>
  27. import path from 'path'
  28. import {isExternal} from '@/utils/validate'
  29. import Item from './Item'
  30. import AppLink from './Link'
  31. import FixiOSBug from './FixiOSBug'
  32. import {mapState} from "vuex";
  33. export default {
  34. name: 'SidebarItem',
  35. components: {Item, AppLink},
  36. mixins: [FixiOSBug],
  37. props: {
  38. // route object
  39. item: {
  40. type: Object,
  41. required: true
  42. },
  43. isNest: {
  44. type: Boolean,
  45. default: false
  46. },
  47. basePath: {
  48. type: String,
  49. default: ''
  50. }
  51. },
  52. data() {
  53. this.onlyOneChild = null
  54. return {}
  55. },
  56. computed: {
  57. ...mapState({
  58. userInfo: (state) => state.user,
  59. }),
  60. },
  61. methods: {
  62. hasOneShowingChild(children = [], parent) {
  63. if (!children) {
  64. children = [];
  65. }
  66. const showingChildren = children.filter(item => {
  67. if (item.hidden) {
  68. return false
  69. } else {
  70. // Temp set(will be used if only has one showing child)
  71. this.onlyOneChild = item
  72. return true
  73. }
  74. })
  75. // When there is only one child router, the child router is displayed by default
  76. if (showingChildren.length === 1) {
  77. return true
  78. }
  79. // Show parent if there are no child router to display
  80. if (showingChildren.length === 0) {
  81. this.onlyOneChild = {...parent, path: '', noShowingChildren: true}
  82. return true
  83. }
  84. return false
  85. },
  86. resolvePath(routePath, routeQuery) {
  87. if (isExternal(routePath)) {
  88. return routePath
  89. }
  90. if (isExternal(this.basePath)) {
  91. if (routeQuery) {
  92. let query = JSON.parse(routeQuery);
  93. if (query.key) {
  94. // 设置code
  95. query.key = window.localStorage.getItem("setoauthUUID" + this.userInfo.name);
  96. query.tenantCode = this.userInfo.tenant.tenantCode
  97. query.redirect_uri = encodeURIComponent(query.redirect_uri);
  98. query.params = encodeURIComponent(query.params);
  99. let baseURL = this.basePath
  100. Object.keys(query).forEach((key, index) => {
  101. if (index == 0) {
  102. baseURL += '&' + key + '=' + query[key];
  103. } else {
  104. baseURL += '&' + key + '=' + query[key];
  105. }
  106. })
  107. return baseURL;
  108. }
  109. }
  110. return this.basePath
  111. }
  112. if (routeQuery) {
  113. let query = JSON.parse(routeQuery);
  114. return {path: path.resolve(this.basePath, routePath), query: query}
  115. }
  116. return path.resolve(this.basePath, routePath)
  117. }
  118. }
  119. }
  120. </script>