UserPermModal.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" title="API授权" :width="adaptiveWidth" @ok="handleSubmit" :showFooter="showFooter">
  3. <a-table :data-source="APIList" :columns="columns" :row-selection="rowSelection" :row-key="(record) => record.id"> </a-table>
  4. </BasicModal>
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref, computed, onMounted, unref } from 'vue';
  8. import { BasicModal, useModalInner } from '/@/components/Modal';
  9. import { apiManageList, apiPermPagelist, bathAddApiPerm } from './user.api';
  10. import { useDrawerAdaptiveWidth } from '/@/hooks/jeecg/useAdaptiveWidth';
  11. import { message, Modal } from 'ant-design-vue';
  12. // 声明Emits
  13. const emit = defineEmits(['success', 'register']);
  14. const showFooter = ref(true);
  15. const APIList = ref([]);
  16. const selectedRowKeys = ref<any>([]);
  17. const userID = ref('');
  18. const columns = [
  19. {
  20. title: 'API名称',
  21. dataIndex: 'apiName',
  22. key: 'apiName',
  23. align: 'center',
  24. },
  25. {
  26. title: '请求路径',
  27. dataIndex: 'reqUrl',
  28. key: 'reqUrl',
  29. align: 'center',
  30. },
  31. {
  32. title: 'HTTP方法',
  33. dataIndex: 'reqMethod',
  34. key: 'reqMethod',
  35. align: 'center',
  36. },
  37. ];
  38. //表单赋值
  39. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  40. setModalProps({ confirmLoading: false });
  41. userID.value = data.userID;
  42. await getAllApiList();
  43. await getPermApiList();
  44. });
  45. const { adaptiveWidth } = useDrawerAdaptiveWidth();
  46. // 行选择配置
  47. const rowSelection = {
  48. selectedRowKeys,
  49. onChange: (selectedKeys) => {
  50. selectedRowKeys.value = selectedKeys;
  51. },
  52. };
  53. //表单提交事件
  54. async function handleSubmit() {
  55. try {
  56. setModalProps({ confirmLoading: true });
  57. // 参数
  58. const params = {
  59. apiIdList: selectedRowKeys.value.length > 0 ? selectedRowKeys.value.join(',') : {}, // 转换为逗号分隔的字符串
  60. userId: userID.value, // 用户ID
  61. };
  62. const response = await bathAddApiPerm(params);
  63. console.log('提交的ID:', selectedRowKeys.value);
  64. console.log('API响应:', response);
  65. message.success('提交成功');
  66. //关闭弹窗
  67. closeModal();
  68. //刷新列表
  69. emit('success');
  70. } finally {
  71. setModalProps({ confirmLoading: false });
  72. }
  73. }
  74. async function getPermApiList() {
  75. const result = await apiPermPagelist({ pageNo: 1, pageSize: 1000, userId: userID.value });
  76. const permApiIds = result.records?.map((item) => String(item.apiId)) || [];
  77. selectedRowKeys.value = [...permApiIds];
  78. return result;
  79. }
  80. // 获取接口列表
  81. async function getAllApiList() {
  82. const result = await apiManageList({ pageNo: 1, pageSize: 1000 });
  83. APIList.value = result.records;
  84. }
  85. onMounted(async () => {
  86. await getAllApiList();
  87. });
  88. </script>
  89. <style scoped>
  90. .selected-info {
  91. background: #cccc;
  92. padding: 15px;
  93. border-radius: 6px;
  94. margin-bottom: 20px;
  95. }
  96. </style>