uni-data-menu.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view>
  3. <uni-nav-menu :active="value" activeKey="value" :activeTextColor="activeTextColor" :uniqueOpened="uniqueOpened"
  4. @select="onSelect">
  5. <uni-menu-sidebar :data="userMenu"></uni-menu-sidebar>
  6. <uni-menu-sidebar :data="staticMenu"></uni-menu-sidebar>
  7. </uni-nav-menu>
  8. </view>
  9. </template>
  10. <script>
  11. import {
  12. mapActions
  13. } from 'vuex'
  14. import {
  15. buildMenus
  16. } from './util.js'
  17. export default {
  18. data() {
  19. return {
  20. menus: [],
  21. userMenu: [],
  22. famliy: [],
  23. };
  24. },
  25. mixins: [uniCloud.mixinDatacom],
  26. props: {
  27. // 当前激活菜单的 url
  28. value: {
  29. type: String,
  30. default: ''
  31. },
  32. // 当前激活菜单的文字颜色
  33. activeTextColor: {
  34. type: String,
  35. default: '#42B983'
  36. },
  37. // 是否只保持一个子菜单的展开
  38. uniqueOpened: {
  39. type: Boolean,
  40. default: false
  41. },
  42. staticMenu: {
  43. type: Array,
  44. default () {
  45. return []
  46. }
  47. }
  48. },
  49. watch: {
  50. localdata: {
  51. handler(newval) {
  52. if (this.hasLocalData(newval)) {
  53. this.userMenu = newval
  54. }
  55. },
  56. immediate: true
  57. },
  58. // TODO 暂时无需监听,需要看后面会出现什么问题
  59. // #ifdef H5
  60. menus: {
  61. immediate: true,
  62. handler(newVal,oldVal) {
  63. const item = this.menus.find(m => m.value === this.$route.path)
  64. // 设置面包屑
  65. if(item){
  66. this.getMenuAncestor(item.menu_id, newVal)
  67. item && this.setRoutes && this.setRoutes(this.famliy)
  68. }
  69. }
  70. },
  71. // #endif
  72. $route: {
  73. immediate: false,
  74. handler(val, old) {
  75. if (val.fullPath !== old.fullPath) {
  76. this.famliy = []
  77. const menu = this.menus.find(m => m.value === val.path)
  78. const menu_id = menu && menu.menu_id
  79. this.getMenuAncestor(menu_id, this.menus)
  80. this.setRoutes && this.setRoutes(this.famliy)
  81. }
  82. }
  83. }
  84. },
  85. created() {
  86. if (this.hasLocalData(this.localdata)) return
  87. // #ifndef H5
  88. this.load()
  89. // #endif
  90. },
  91. // computed:{
  92. // userMenu() {
  93. // return this.getUserMenu(this.menus)
  94. // }
  95. // },
  96. methods: {
  97. ...mapActions({
  98. setRoutes: 'app/setRoutes'
  99. }),
  100. getUserMenu(menuList) {
  101. const {
  102. permission,
  103. role
  104. } = uniCloud.getCurrentUserInfo()
  105. // 标记叶子节点
  106. menuList.map(item => {
  107. if (!menuList.some(subMenuItem => subMenuItem.parent_id === item.menu_id)) {
  108. item.isLeafNode = true
  109. }
  110. })
  111. // 删除无权限访问的菜单
  112. if (!role.includes('admin')) {
  113. menuList = menuList.filter(item => {
  114. if (item.isLeafNode) {
  115. if (item.permission && item.permission.length) {
  116. return item.permission.some(item => permission.indexOf(item) > -1)
  117. }
  118. return false
  119. }
  120. return true
  121. })
  122. }
  123. return buildMenus(menuList)
  124. },
  125. onSelect(menu) {
  126. this.famliy = []
  127. this.getMenuAncestor(menu.menu_id, this.menus)
  128. this.emit(menu)
  129. },
  130. emit(menu) {
  131. this.$emit('select', menu, this.famliy)
  132. this.$emit('input', menu.value)
  133. },
  134. hasLocalData(value) {
  135. return Array.isArray(value) && value.length > 0
  136. },
  137. load() {
  138. if (this.mixinDatacomLoading == true) {
  139. return
  140. }
  141. this.mixinDatacomLoading = true
  142. this.mixinDatacomGet().then((res) => {
  143. this.mixinDatacomLoading = false
  144. const {
  145. data,
  146. count
  147. } = res.result
  148. this.menus = data
  149. this.userMenu = this.getUserMenu(this.menus)
  150. }).catch((err) => {
  151. this.mixinDatacomLoading = false
  152. this.mixinDatacomErrorMessage = err
  153. })
  154. },
  155. getMenuAncestor(menuId, menus) {
  156. menus.forEach(item => {
  157. if (item.menu_id === menuId) {
  158. const route = {
  159. name: item.text
  160. }
  161. const path = item.value
  162. if (path) {
  163. route.to = {
  164. path
  165. }
  166. }
  167. this.famliy.unshift(route)
  168. const parent_id = item.parent_id
  169. if (parent_id) {
  170. this.getMenuAncestor(parent_id, menus)
  171. }
  172. }
  173. })
  174. // return famliy
  175. }
  176. },
  177. }
  178. </script>
  179. <style>
  180. </style>