menus.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. @check="nodeChange"
  11. default-expand-all
  12. highlight-current
  13. node-key="ID"
  14. ref="menuTree"
  15. show-checkbox
  16. ></el-tree>
  17. </div>
  18. </template>
  19. <script>
  20. import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'
  21. export default {
  22. name: 'Menus',
  23. props: {
  24. row: {
  25. default: function() {
  26. return {}
  27. },
  28. type: Object
  29. }
  30. },
  31. data() {
  32. return {
  33. menuTreeData: [],
  34. menuTreeIds: [],
  35. needConfirm:false,
  36. menuDefaultProps: {
  37. children: 'children',
  38. label: function(data){
  39. return data.meta.title
  40. }
  41. }
  42. }
  43. },
  44. methods: {
  45. nodeChange(){
  46. this.needConfirm = true
  47. },
  48. // 暴露给外层使用的切换拦截统一方法
  49. enterAndNext(){
  50. this.relation()
  51. },
  52. // 关联树 确认方法
  53. async relation() {
  54. const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
  55. const res = await addMenuAuthority({
  56. menus: checkArr,
  57. authorityId: this.row.authorityId
  58. })
  59. if (res.code == 0) {
  60. this.$message({
  61. type: 'success',
  62. message: '菜单设置成功!'
  63. })
  64. }
  65. }
  66. },
  67. async created() {
  68. // 获取所有菜单树
  69. const res = await getBaseMenuTree()
  70. this.menuTreeData = res.data.menus
  71. const res1 = await getMenuAuthority({ authorityId: this.row.authorityId })
  72. const menus = res1.data.menus
  73. const arr = []
  74. menus.map(item => {
  75. // 防止直接选中父级造成全选
  76. if (!menus.some(same => same.parentId === item.menuId)) {
  77. arr.push(Number(item.menuId))
  78. }
  79. })
  80. this.menuTreeIds = arr
  81. }
  82. }
  83. </script>
  84. <style lang="scss">
  85. </style>