apis.vue 2.7 KB

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