menus.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div>
  3. <div class="clearflex">
  4. <el-button class="fl-right" size="small" type="primary" @click="relation">确 定</el-button>
  5. </div>
  6. <el-tree
  7. ref="menuTree"
  8. :data="menuTreeData"
  9. :default-checked-keys="menuTreeIds"
  10. :props="menuDefaultProps"
  11. default-expand-all
  12. highlight-current
  13. node-key="ID"
  14. show-checkbox
  15. @check="nodeChange"
  16. >
  17. <template #default="{ node , data }">
  18. <span class="custom-tree-node">
  19. <span>{{ node.label }}</span>
  20. <span>
  21. <el-button
  22. type="text"
  23. size="mini"
  24. :style="{color:row.defaultRouter === data.name?'#E6A23C':'#85ce61'}"
  25. :disabled="!node.checked"
  26. @click="() => setDefault(data)"
  27. >
  28. {{ row.defaultRouter === data.name?"首页":"设为首页" }}
  29. </el-button>
  30. </span>
  31. </span>
  32. </template>
  33. </el-tree>
  34. </div>
  35. </template>
  36. <script>
  37. import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'
  38. import {
  39. updateAuthority
  40. } from '@/api/authority'
  41. export default {
  42. name: 'Menus',
  43. props: {
  44. row: {
  45. default: function() {
  46. return {}
  47. },
  48. type: Object
  49. }
  50. },
  51. data() {
  52. return {
  53. menuTreeData: [],
  54. menuTreeIds: [],
  55. needConfirm: false,
  56. menuDefaultProps: {
  57. children: 'children',
  58. label: function(data) {
  59. return data.meta.title
  60. }
  61. }
  62. }
  63. },
  64. async created() {
  65. // 获取所有菜单树
  66. const res = await getBaseMenuTree()
  67. this.menuTreeData = res.data.menus
  68. const res1 = await getMenuAuthority({ authorityId: this.row.authorityId })
  69. const menus = res1.data.menus
  70. const arr = []
  71. menus.map(item => {
  72. // 防止直接选中父级造成全选
  73. if (!menus.some(same => same.parentId === item.menuId)) {
  74. arr.push(Number(item.menuId))
  75. }
  76. })
  77. this.menuTreeIds = arr
  78. },
  79. methods: {
  80. async setDefault(data) {
  81. const res = await updateAuthority({ authorityId: this.row.authorityId, AuthorityName: this.row.authorityName, parentId: this.row.parentId, defaultRouter: data.name })
  82. if (res.code === 0) {
  83. this.$message({ type: 'success', message: '设置成功' })
  84. this.$emit('changeRow', 'defaultRouter', res.data.authority.defaultRouter)
  85. }
  86. },
  87. nodeChange() {
  88. this.needConfirm = true
  89. },
  90. // 暴露给外层使用的切换拦截统一方法
  91. enterAndNext() {
  92. this.relation()
  93. },
  94. // 关联树 确认方法
  95. async relation() {
  96. const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
  97. const res = await addMenuAuthority({
  98. menus: checkArr,
  99. authorityId: this.row.authorityId
  100. })
  101. if (res.code === 0) {
  102. this.$message({
  103. type: 'success',
  104. message: '菜单设置成功!'
  105. })
  106. }
  107. }
  108. }
  109. }
  110. </script>