apis.vue 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<template>
  <div>
    <div class="clearflex">
      <el-button @click="authApiEnter" class="fl-right" size="small" type="primary">确 定</el-button>
    </div>
    <el-tree
      :data="apiTreeData"
      :default-checked-keys="apiTreeIds"
      :props="apiDefaultProps"
      default-expand-all
      highlight-current
12
      node-key="onlyId"
13 14 15 16 17 18 19
      ref="apiTree"
      show-checkbox
    ></el-tree>
  </div>
</template>
<script>
import { getAllApis } from '@/api/api'
R
rainyan 已提交
20
import { UpdateCasbin, getPolicyPathByAuthorityId } from '@/api/casbin'
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
export default {
  name: 'Apis',
  props: {
    row: {
      default: function() {
        return {}
      },
      type: Object
    }
  },
  data() {
    return {
      apiTreeData: [],
      apiTreeIds: [],
      apiDefaultProps: {
        children: 'children',
        label: 'description'
      }
    }
  },
  methods: {
    // 创建api树方法
    buildApiTree(apis) {
      const apiObj = new Object()
      apis &&
        apis.map(item => {
47
        item.onlyId = "p:"+item.path+"m:"+item.method
48 49
          if (apiObj.hasOwnProperty(item.apiGroup)) {
            apiObj[item.apiGroup].push(item)
50
          } else {
51
            Object.assign(apiObj, { [item.apiGroup]: [item] })
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
          }
        })
      const apiTree = []
      for (const key in apiObj) {
        const treeNode = {
          ID: key,
          description: key + '',
          children: apiObj[key]
        }
        apiTree.push(treeNode)
      }
      return apiTree
    },
    // 关联关系确定
    async authApiEnter() {
67 68 69 70 71 72 73 74 75
      const checkArr = this.$refs.apiTree.getCheckedNodes(true)
      var casbinInfos = []
      checkArr&&checkArr.map(item=>{
        var casbinInfo = {
          path:item.path,
          method:item.method
        }
        casbinInfos.push(casbinInfo)
      })
R
rainyan 已提交
76
      const res = await UpdateCasbin({
77
        authorityId: this.activeUserId,
78
        casbinInfos
79
      })
80
      if (res.code == 0) {
81 82 83 84 85 86 87 88
        this.$message({ type: 'success', message: res.msg })
      }
    }
  },
  async created() {
    // 获取api并整理成树结构
    const res2 = await getAllApis()
    const apis = res2.data.apis
89
   
90 91 92 93 94
    this.apiTreeData = this.buildApiTree(apis)
    const res = await getPolicyPathByAuthorityId({
      authorityId: this.row.authorityId
    })
    this.activeUserId = this.row.authorityId
95 96 97 98
    this.apiTreeIds = []
    res.data.paths&&res.data.paths.map(item=>{
      this.apiTreeIds.push("p:"+item.path+"m:"+item.method)
    })
99 100 101 102 103
  }
}
</script>
<style lang="scss">
</style>