app-func-form.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!--
  2. 应用功能独有表单
  3. @Author: linqian
  4. @Date: 2021-07-11 15:03
  5. -->
  6. <template>
  7. <div>
  8. <transfer-tree
  9. ref="transferTree"
  10. filterable
  11. :titles="['可选资源', '已选资源']"
  12. v-model="selectedValue"
  13. :data="data"
  14. children-name="child"
  15. pid-name="p"
  16. :open-all="false"
  17. :transfer-open-node="true"
  18. :filter-node-from="handleSearch"
  19. value-name="id"
  20. >
  21. </transfer-tree>
  22. <div v-footer>
  23. <dg-button @click="handleCancel">取消</dg-button>
  24. <dg-button type="primary" @click="handleSubmit">确定</dg-button>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. import transferTree from '@/pages/common/transfer-tree';
  30. import { allAppFuncTree, userHasAuthFunIds } from '@/api/permission-selfhelp-manage';
  31. export default {
  32. props: {
  33. selectKeys: [Array],
  34. // 是否需要过滤父节点
  35. needFilter: {
  36. type: Boolean,
  37. default: false
  38. },
  39. type: String
  40. },
  41. components: { transferTree },
  42. data() {
  43. return {
  44. approveContent: this.value,
  45. selectedValue: [],
  46. havefunIdsOnlyChild: [],
  47. havefunIds: [],
  48. data: []
  49. };
  50. },
  51. computed: {},
  52. methods: {
  53. findIndexArray(data, id, indexArray) {
  54. let arr = Array.from(indexArray);
  55. for (let i = 0, len = data.length; i < len; i++) {
  56. arr.push(data[i].label);
  57. if (data[i].id === id) {
  58. return arr;
  59. }
  60. let children = data[i].child;
  61. if (children && children.length) {
  62. let result = this.findIndexArray(children, id, arr);
  63. if (result) return result;
  64. }
  65. arr.pop();
  66. }
  67. return false;
  68. },
  69. // 树节点转成文字
  70. nodeTransferLabel(keys, tree) {
  71. const funcAttr = keys.map((item) => {
  72. return this.findIndexArray(tree, item, []);
  73. });
  74. let attr = [];
  75. for (let i = 0; i < funcAttr.length; i++) {
  76. const element = funcAttr[i].join('/');
  77. attr.push(element);
  78. }
  79. return attr.join(',');
  80. },
  81. handleCancel() {
  82. this.$emit('close');
  83. },
  84. handleSubmit() {
  85. // 申请访问的资源,剔除已经申请过的资源
  86. const applyKeys = this.selectedValue.filter((item) => !this.havefunIdsOnlyChild.includes(item));
  87. const approveContent = this.nodeTransferLabel(applyKeys, this.$refs.transferTree.targetData);
  88. // 撤销的资源, 已有资源与已选资源作比对
  89. const cancelKeys = this.havefunIdsOnlyChild.filter((item) => !this.selectedValue.includes(item));
  90. const cancelContent = this.nodeTransferLabel(cancelKeys, this.data);
  91. // const completeContent = `${approveContent}'\n'${cancelContent}`
  92. // 获取目标树所有的key,包含父节点
  93. const allKeys = this.$refs.transferTree.allKeyValue(false, this.$refs.transferTree.targetData);
  94. // 实际保存的节点,剔除应用节点
  95. const resourceInfos = allKeys
  96. .map((item) => this.$refs.transferTree.$refs['to-tree'].getNode(item).data)
  97. .map((item) => {
  98. return {
  99. appId: item.appId,
  100. funId: item.id
  101. };
  102. })
  103. .filter((item) => item.appId);
  104. // 需要验证重复申请的资源,剔除该用户已经拥有的资源
  105. const validRepeatNode = resourceInfos
  106. .filter((item) => !this.havefunIds.includes(item.funId))
  107. .map((item) => item.funId);
  108. // 已选资源有变更,则可以直接保存,无变更,则需要至少选中一条
  109. // 判断有无变更的标准:resourceInfos的funId 与getSetFunIdsByUser方法返回的已选资源id作匹配
  110. if (applyKeys.length == 0 && cancelKeys.length == 0) {
  111. this.$message.warning('已选资源无变更。');
  112. } else {
  113. this.$emit('success', approveContent, cancelContent, this.selectedValue, resourceInfos);
  114. }
  115. },
  116. handleSearch(value, data) {
  117. console.log(value, data.label);
  118. if (!value) return true;
  119. return data.label.indexOf(value) !== -1;
  120. },
  121. // 获取申请人已被授权得功能id
  122. getSetFunIdsByUser() {
  123. return new Promise((resolve) => {
  124. const params = {
  125. userId: this.$store.getters.user.id
  126. };
  127. userHasAuthFunIds(params)
  128. .then((res) => {
  129. resolve(res.data.content);
  130. })
  131. .catch(() => resolve([]));
  132. });
  133. },
  134. // 获取全量的菜单树
  135. getAllTree() {
  136. return new Promise((resolve) => {
  137. allAppFuncTree().then((res) => {
  138. resolve(res.data.content);
  139. });
  140. });
  141. },
  142. // 获取子节点,去除父节点key
  143. getChildKeys(data, val) {
  144. let keys = [];
  145. function filterParentKey(_data, key) {
  146. _data.filter((item) => {
  147. if (item.id == key && !item.isTreeNode) {
  148. keys.push(key);
  149. return;
  150. } else {
  151. if (item.child.length > 0) {
  152. filterParentKey(item.child, key);
  153. } else {
  154. return;
  155. }
  156. }
  157. });
  158. }
  159. for (let i = 0; i < val.length; i++) {
  160. const key = val[i];
  161. filterParentKey(data, key);
  162. }
  163. return keys;
  164. }
  165. },
  166. async created() {
  167. this.data = await this.getAllTree();
  168. // 已有资源,包含了父节点
  169. this.havefunIds = await this.getSetFunIdsByUser();
  170. // 已有资源,只是子节点
  171. this.havefunIdsOnlyChild = this.getChildKeys(this.data, this.havefunIds);
  172. const keys = this.selectKeys;
  173. if (this.needFilter && this.havefunIds.length > 0) {
  174. this.selectedValue = [...this.getChildKeys(this.data, keys)];
  175. if (this.type == 'add') {
  176. this.selectedValue = [...this.selectedValue, ...this.havefunIdsOnlyChild];
  177. }
  178. } else {
  179. this.selectedValue = keys;
  180. }
  181. },
  182. mounted() {}
  183. };
  184. </script>
  185. <style lang='scss' scoped>
  186. @import url('../index.scss');
  187. </style>