list.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <BodyPanlHeader>
  3. <a-button type="primary" @click="setRole()">新增角色</a-button>
  4. <div>
  5. <a-input-search
  6. v-model:value="params.roleName"
  7. style="width: 280px"
  8. placeholder="请输入角色名称"
  9. allow-clear
  10. @search="updateList"
  11. />
  12. </div>
  13. </BodyPanlHeader>
  14. <BodyPanlBody>
  15. <a-table
  16. :data-source="list"
  17. :columns="roleColumns"
  18. :pagination="pagination"
  19. >
  20. <template #bodyCell="{ column, record }">
  21. <template v-if="column.key === 'action'">
  22. <div v-if="!record.defaultRole" class="table-actions">
  23. <a @click="setRole(record)">编辑</a>
  24. <a class="warn" @click="delRoleHandler(record)">删除</a>
  25. </div>
  26. </template>
  27. </template>
  28. </a-table>
  29. </BodyPanlBody>
  30. </template>
  31. <script lang="ts" setup>
  32. import EditRole from './edit.vue'
  33. import { computed, reactive } from 'vue'
  34. import { BodyPanlHeader, BodyPanlBody } from '@/layout/panl'
  35. import { usePaging } from '@/hook'
  36. import { router } from '@/router'
  37. import { renderModal } from '@/helper'
  38. import { roleColumns as baseColumns } from './columns'
  39. import { Modal } from 'ant-design-vue'
  40. import { fetchRoles, addRole, updateRole, deleteRole } from '@/api'
  41. import type { Role } from '@/api'
  42. const roleColumns = [
  43. ...baseColumns,
  44. {
  45. title: '操作',
  46. dataIndex: 'action',
  47. key: 'action'
  48. }
  49. ]
  50. const params = reactive({
  51. roleName: '',
  52. projectId: computed(() => Number(router.currentRoute.value.params.id))
  53. })
  54. const { list, pagination, updateList } = usePaging(fetchRoles, params)
  55. const setRole = (role?: Role) => {
  56. renderModal(EditRole, {
  57. role,
  58. async onSave(role) {
  59. if (role.roleId) {
  60. await updateRole(role as Role)
  61. } else {
  62. await addRole(params.projectId, role)
  63. }
  64. await updateList()
  65. }
  66. })
  67. }
  68. const delRoleHandler = (role: Role) => {
  69. Modal.confirm({
  70. content: '确定要删除此角色',
  71. title: '系统提示',
  72. width: '400px',
  73. okText: '删除',
  74. icon: null,
  75. cancelText: '取消',
  76. onOk: async () => {
  77. try {
  78. await deleteRole(role.roleId)
  79. await updateList()
  80. } catch {}
  81. }
  82. })
  83. }
  84. </script>