RoleDrawer.vue
6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<template>
<BasicDrawer
v-bind="$attrs"
@register="registerDrawer"
:title="'角色岗位管理'"
width="550px"
:showFooter="false"
>
<!-- 新增角色区域 -->
<div class="mb-4 flex">
<a-input v-model:value="roleName" placeholder="请输入角色名称" class="mr-2" style="width: 40%" />
<!-- <a-input v-model:value="roleCode" placeholder="请输入角色编码" class="mr-2" style="width: 40%" /> -->
<a-button type="primary" @click="handleCreate">新增角色岗位</a-button>
</div>
<!-- 编辑角色区域 -->
<div v-if="editingRecord" class="mb-4 p-4 border border-blue-300 rounded bg-blue-50">
<div class="flex items-center justify-between mb-2">
<h4 class="text-blue-600 font-medium">编辑角色岗位</h4>
<a-button size="small" @click="cancelEdit">取消</a-button>
</div>
<div class="flex">
<a-input v-model:value="editForm.name" placeholder="请输入角色名称" class="mr-2" style="width: 40%" />
<!-- <a-input v-model:value="editForm.code" placeholder="请输入角色编码" class="mr-2" style="width: 40%" /> -->
<a-button type="primary" @click="handleEditSubmit">保存</a-button>
</div>
</div>
<BasicTable @register="registerTable">
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'action'">
<TableAction
:actions="[
{
icon: 'clarity:note-edit-line',
tooltip: '编辑角色岗位',
onClick: handleEdit.bind(null, record),
},
{
icon: 'ant-design:delete-outlined',
color: 'error',
tooltip: '删除角色岗位',
popConfirm: {
title: '是否确认删除',
placement: 'left',
confirm: handleDelete.bind(null, record),
},
},
]"
/>
</template>
</template>
</BasicTable>
</BasicDrawer>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { getRoleList, addRole, editRole, deleteRole } from '/@/api/project/account';
import { useMessage } from '/@/hooks/web/useMessage';
const columns = [
{
title: '角色名称',
dataIndex: 'name',
width: 150,
},
// {
// title: '角色编码',
// dataIndex: 'code',
// width: 150,
// },
];
export default defineComponent({
name: 'RoleDrawer',
components: { BasicDrawer, BasicTable, TableAction },
setup() {
const { createMessage } = useMessage();
const roleName = ref('');
// const roleCode = ref('');
const editingRecord = ref(null);
const editForm = ref({
id: '',
name: '',
// code: '',
});
// 获取所有角色列表
const [registerTable, { reload }] = useTable({
api: getRoleList,
columns,
pagination: false,
striped: false,
useSearchForm: false,
showTableSetting: false,
bordered: true,
showIndexColumn: false,
canResize: false,
actionColumn: {
width: 120,
title: '操作',
dataIndex: 'action',
},
rowKey: 'id',
});
const [registerDrawer] = useDrawerInner(async () => {
reload();
// 重置编辑状态
editingRecord.value = null;
// editForm.value = { id: '', name: '', code: '' };
editForm.value = { id: '', name: '' };
});
function handleEdit(record: Recordable) {
editingRecord.value = record;
editForm.value = {
id: record.id,
name: record.name,
// code: record.code || '',
};
}
function cancelEdit() {
editingRecord.value = null;
// editForm.value = { id: '', name: '', code: '' };
editForm.value = { id: '', name: ''};
}
async function handleEditSubmit() {
if (!editForm.value.name) {
createMessage.warning('角色名称不能为空');
return;
}
// if (!editForm.value.code) {
// createMessage.warning('角色编码不能为空');
// return;
// }
await editRole({
id: editForm.value.id,
name: editForm.value.name,
// code: editForm.value.code,
});
createMessage.success('编辑成功');
cancelEdit();
reload();
}
async function handleDelete(record: Recordable) {
await deleteRole({ id: record.id });
createMessage.success('删除成功');
reload();
}
async function handleCreate() {
if (!roleName.value) {
createMessage.warning('角色名称不能为空');
return;
}
// if (!roleCode.value) {
// createMessage.warning('角色编码不能为空');
// return;
// }
// 添加角色
await addRole({
name: roleName.value,
// code: roleCode.value,
});
createMessage.success('添加成功');
roleName.value = '';
// roleCode.value = '';
reload();
}
return {
registerDrawer,
registerTable,
roleName,
// roleCode,
editingRecord,
editForm,
handleCreate,
handleEdit,
handleDelete,
handleEditSubmit,
cancelEdit,
};
},
});
</script>