menus.vue 2.9 KB
Newer Older
1 2 3
<template>
  <div>
    <div class="clearflex">
P
piexlmax 已提交
4
      <el-button class="fl-right" size="mini" type="primary" @click="relation">确 定</el-button>
5 6
    </div>
    <el-tree
何秀钢 已提交
7
      ref="menuTree"
8 9 10 11 12 13 14
      :data="menuTreeData"
      :default-checked-keys="menuTreeIds"
      :props="menuDefaultProps"
      default-expand-all
      highlight-current
      node-key="ID"
      show-checkbox
何秀钢 已提交
15
      @check="nodeChange"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
16
    >
17 18 19 20 21 22 23 24 25 26 27 28 29 30
      <template #default="{ node , data }">
        <span class="custom-tree-node">
          <span>{{ node.label }}</span>
          <span>
            <el-button
              type="text"
              size="mini"
              :style="{color:row.defaultRouter === data.name?'#E6A23C':'#85ce61'}"
              :disabled="!node.checked"
              @click="() => setDefault(data)"
            >
              {{ row.defaultRouter === data.name?"首页":"设为首页" }}
            </el-button>
          </span>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
31
        </span>
32
      </template>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
33
    </el-tree>
34 35
  </div>
</template>
何秀钢 已提交
36

37 38
<script>
import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
39
import {
何秀钢 已提交
40 41
  updateAuthority
} from '@/api/authority'
42 43 44 45 46 47 48 49 50 51 52 53 54 55
export default {
  name: 'Menus',
  props: {
    row: {
      default: function() {
        return {}
      },
      type: Object
    }
  },
  data() {
    return {
      menuTreeData: [],
      menuTreeIds: [],
何秀钢 已提交
56
      needConfirm: false,
57 58
      menuDefaultProps: {
        children: 'children',
何秀钢 已提交
59
        label: function(data) {
60 61
          return data.meta.title
        }
62 63 64
      }
    }
  },
何秀钢 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  async created() {
    // 获取所有菜单树
    const res = await getBaseMenuTree()
    this.menuTreeData = res.data.menus

    const res1 = await getMenuAuthority({ authorityId: this.row.authorityId })
    const menus = res1.data.menus
    const arr = []
    menus.map(item => {
      // 防止直接选中父级造成全选
      if (!menus.some(same => same.parentId === item.menuId)) {
        arr.push(Number(item.menuId))
      }
    })
    this.menuTreeIds = arr
  },
81
  methods: {
何秀钢 已提交
82 83 84 85
    async setDefault(data) {
      const res = await updateAuthority({ authorityId: this.row.authorityId, AuthorityName: this.row.authorityName, parentId: this.row.parentId, defaultRouter: data.name })
      if (res.code === 0) {
        this.$message({ type: 'success', message: '设置成功' })
86
        this.$emit('changeRow', 'defaultRouter', res.data.authority.defaultRouter)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
87 88
      }
    },
何秀钢 已提交
89
    nodeChange() {
90 91 92
      this.needConfirm = true
    },
    // 暴露给外层使用的切换拦截统一方法
何秀钢 已提交
93
    enterAndNext() {
94 95
      this.relation()
    },
96 97 98 99 100 101 102
    // 关联树 确认方法
    async relation() {
      const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
      const res = await addMenuAuthority({
        menus: checkArr,
        authorityId: this.row.authorityId
      })
何秀钢 已提交
103
      if (res.code === 0) {
104 105
        this.$message({
          type: 'success',
106
          message: '菜单设置成功!'
107 108 109 110 111 112
        })
      }
    }
  }
}
</script>