uni-data-menu.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. console.log('this.userMenu',this.userMenu);
  55. }
  56. },
  57. immediate: true
  58. },
  59. // TODO 暂时无需监听,需要看后面会出现什么问题
  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. $route: {
  72. immediate: false,
  73. handler(val, old) {
  74. if (val.fullPath !== old.fullPath) {
  75. this.famliy = []
  76. const menu = this.menus.find(m => m.value === val.path)
  77. const menu_id = menu && menu.menu_id
  78. this.getMenuAncestor(menu_id, this.menus)
  79. this.setRoutes && this.setRoutes(this.famliy)
  80. }
  81. }
  82. }
  83. },
  84. created() {
  85. if (this.hasLocalData(this.localdata)) return
  86. // this.load()
  87. },
  88. // computed:{
  89. // userMenu() {
  90. // return this.getUserMenu(this.menus)
  91. // }
  92. // },
  93. methods: {
  94. ...mapActions({
  95. setRoutes: 'app/setRoutes'
  96. }),
  97. getUserMenu(menuList) {
  98. const {
  99. permission,
  100. role
  101. } = uniCloud.getCurrentUserInfo()
  102. // 标记叶子节点
  103. menuList.map(item => {
  104. if (!menuList.some(subMenuItem => subMenuItem.parent_id === item.menu_id)) {
  105. item.isLeafNode = true
  106. }
  107. })
  108. // 删除无权限访问的菜单
  109. if (!role.includes('admin')) {
  110. menuList = menuList.filter(item => {
  111. if (item.isLeafNode) {
  112. if (item.permission && item.permission.length) {
  113. return item.permission.some(item => permission.indexOf(item) > -1)
  114. }
  115. return false
  116. }
  117. return true
  118. })
  119. }
  120. return buildMenus(menuList)
  121. },
  122. onSelect(menu) {
  123. this.famliy = []
  124. this.getMenuAncestor(menu.menu_id, this.menus)
  125. this.emit(menu)
  126. },
  127. emit(menu) {
  128. this.$emit('select', menu, this.famliy)
  129. this.$emit('input', menu.value)
  130. },
  131. hasLocalData(value) {
  132. return Array.isArray(value) && value.length > 0
  133. },
  134. load() {
  135. if (this.mixinDatacomLoading == true) {
  136. return
  137. }
  138. this.mixinDatacomLoading = true
  139. this.mixinDatacomGet().then((res) => {
  140. this.mixinDatacomLoading = false
  141. const {
  142. data,
  143. count
  144. } = res.result
  145. this.menus = data
  146. this.userMenu = this.getUserMenu(this.menus)
  147. console.log('this.userMenu',this.userMenu);
  148. }).catch((err) => {
  149. this.mixinDatacomLoading = false
  150. this.mixinDatacomErrorMessage = err
  151. })
  152. },
  153. getMenuAncestor(menuId, menus) {
  154. menus.forEach(item => {
  155. if (item.menu_id === menuId) {
  156. const route = {
  157. name: item.text
  158. }
  159. const path = item.value
  160. if (path) {
  161. route.to = {
  162. path
  163. }
  164. }
  165. this.famliy.unshift(route)
  166. const parent_id = item.parent_id
  167. if (parent_id) {
  168. this.getMenuAncestor(parent_id, menus)
  169. }
  170. }
  171. })
  172. // return famliy
  173. }
  174. },
  175. }
  176. </script>
  177. <style>
  178. </style>