menus.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div>
  3. <div class="clearflex">
  4. <el-button @click="relation" class="fl-right" size="small" type="primary">确 定</el-button>
  5. </div>
  6. <el-tree
  7. :data="menuTreeData"
  8. :default-checked-keys="menuTreeIds"
  9. :props="menuDefaultProps"
  10. default-expand-all
  11. highlight-current
  12. node-key="ID"
  13. ref="menuTree"
  14. show-checkbox
  15. ></el-tree>
  16. </div>
  17. </template>
  18. <script>
  19. import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'
  20. export default {
  21. name: 'Menus',
  22. props: {
  23. row: {
  24. default: function() {
  25. return {}
  26. },
  27. type: Object
  28. }
  29. },
  30. data() {
  31. return {
  32. menuTreeData: [],
  33. menuTreeIds: [],
  34. menuDefaultProps: {
  35. children: 'children',
  36. label: function(data){
  37. return data.meta.title
  38. }
  39. }
  40. }
  41. },
  42. methods: {
  43. // 关联树 确认方法
  44. async relation() {
  45. const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
  46. const res = await addMenuAuthority({
  47. menus: checkArr,
  48. authorityId: this.row.authorityId
  49. })
  50. if (res.code == 0) {
  51. this.$message({
  52. type: 'success',
  53. message: '添加成功!'
  54. })
  55. }
  56. }
  57. },
  58. async created() {
  59. // 获取所有菜单树
  60. const res = await getBaseMenuTree()
  61. this.menuTreeData = res.data.menus
  62. const res1 = await getMenuAuthority({ authorityId: this.row.authorityId })
  63. const menus = res1.data.menus
  64. const arr = []
  65. menus.map(item => {
  66. // 防止直接选中父级造成全选
  67. if (!menus.some(same => same.parentId === item.menuId)) {
  68. arr.push(Number(item.menuId))
  69. }
  70. })
  71. this.menuTreeIds = arr
  72. }
  73. }
  74. </script>
  75. <style lang="scss">
  76. </style>