SidebarItem.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div v-if="!item.hidden">
  3. <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
  4. <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path, onlyOneChild.query)">
  5. <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
  6. <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="getTitle(onlyOneChild.meta.title)" />
  7. </el-menu-item>
  8. </app-link>
  9. </template>
  10. <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
  11. <template slot="title">
  12. <item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="getTitle(item.meta.title)" />
  13. </template>
  14. <sidebar-item
  15. v-for="child in item.children"
  16. :key="child.path"
  17. :is-nest="true"
  18. :item="child"
  19. :base-path="resolvePath(child.path)"
  20. class="nest-menu"
  21. />
  22. </el-submenu>
  23. </div>
  24. </template>
  25. <script>
  26. import path from 'path'
  27. import { isExternal } from '@/utils/validate'
  28. import Item from './Item'
  29. import AppLink from './Link'
  30. import FixiOSBug from './FixiOSBug'
  31. export default {
  32. name: 'SidebarItem',
  33. components: { Item, AppLink },
  34. mixins: [FixiOSBug],
  35. props: {
  36. // route object
  37. item: {
  38. type: Object,
  39. required: true
  40. },
  41. isNest: {
  42. type: Boolean,
  43. default: false
  44. },
  45. basePath: {
  46. type: String,
  47. default: ''
  48. }
  49. },
  50. data() {
  51. this.onlyOneChild = null
  52. return {}
  53. },
  54. methods: {
  55. // 转换title,如果是i18n key则翻译,否则通过映射表转换
  56. getTitle(title) {
  57. if (!title) return '';
  58. // 如果是i18n key格式(router.xxx),则进行翻译
  59. if (title.startsWith('router.')) {
  60. try {
  61. return this.$t(title);
  62. } catch (e) {
  63. return title;
  64. }
  65. }
  66. // 创建title到i18n key的映射
  67. const titleMap = {
  68. '首页': 'router.home',
  69. '个人中心': 'router.profile',
  70. '分配角色': 'router.assignRole',
  71. 'SOP管理-新建SOP': 'router.sopNew',
  72. 'SOP管理-编辑SOP': 'router.sopEdit',
  73. 'SOP管理-查看SOP': 'router.sopView',
  74. '作业管理-新建作业票': 'router.jobNew',
  75. '作业管理-编辑作业票': 'router.jobEdit',
  76. '作业执行-作业状态': 'router.jobExecute',
  77. '预览': 'router.preview',
  78. '工艺详情': 'router.craftDetail',
  79. '物资使用说明详情': 'router.materialInstructionDetail',
  80. '设备详情': 'router.deviceDetail',
  81. '详情': 'router.detail',
  82. '物资规格': 'router.materialSpec',
  83. '分配用户': 'router.assignUser',
  84. '字典数据': 'router.dictData',
  85. '规则组成': 'router.ruleComposition'
  86. };
  87. // 如果title在映射中,使用i18n翻译
  88. if (titleMap[title]) {
  89. try {
  90. return this.$t(titleMap[title]);
  91. } catch (e) {
  92. return title;
  93. }
  94. }
  95. // 如果不在映射中,可能是从接口获取的,原样返回
  96. return title;
  97. },
  98. hasOneShowingChild(children = [], parent) {
  99. if (!children) {
  100. children = [];
  101. }
  102. const showingChildren = children.filter(item => {
  103. if (item.hidden) {
  104. return false
  105. } else {
  106. // Temp set(will be used if only has one showing child)
  107. this.onlyOneChild = item
  108. return true
  109. }
  110. })
  111. // When there is only one child router, the child router is displayed by default
  112. if (showingChildren.length === 1) {
  113. return true
  114. }
  115. // Show parent if there are no child router to display
  116. if (showingChildren.length === 0) {
  117. this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
  118. return true
  119. }
  120. return false
  121. },
  122. resolvePath(routePath, routeQuery) {
  123. if (isExternal(routePath)) {
  124. return routePath
  125. }
  126. if (isExternal(this.basePath)) {
  127. return this.basePath
  128. }
  129. if (routeQuery) {
  130. let query = JSON.parse(routeQuery);
  131. return { path: path.resolve(this.basePath, routePath), query: query }
  132. }
  133. return path.resolve(this.basePath, routePath)
  134. }
  135. }
  136. }
  137. </script>