ruoyi-plus-soybean/src/views/manage/role/index.vue

214 lines
5.3 KiB
Vue
Raw Normal View History

2024-01-28 00:44:21 +08:00
<script setup lang="tsx">
import { ref } from 'vue';
import { NButton, NPopconfirm, NTag } from 'naive-ui';
2024-01-28 00:44:21 +08:00
import { useBoolean } from '@sa/hooks';
import { fetchGetRoleList } from '@/service/api';
import { useAppStore } from '@/store/modules/app';
import { useTable } from '@/hooks/common/table';
import { $t } from '@/locales';
2024-01-28 23:38:44 +08:00
import { enableStatusRecord } from '@/constants/business';
import RoleOperateDrawer, { type OperateType } from './modules/role-operate-drawer.vue';
import RoleSearch from './modules/role-search.vue';
2024-01-28 00:44:21 +08:00
const appStore = useAppStore();
const { bool: drawerVisible, setTrue: openDrawer } = useBoolean();
const {
columns,
filteredColumns,
data,
loading,
pagination,
getData,
searchParams,
updateSearchParams,
resetSearchParams
} = useTable<Api.SystemManage.Role, typeof fetchGetRoleList, 'index' | 'operate'>({
2024-01-28 00:44:21 +08:00
apiFn: fetchGetRoleList,
apiParams: {
current: 1,
size: 10,
// if you want to use the searchParams in Form, you need to define the following properties, and the value is null
// the value can not be undefined, otherwise the property in Form will not be reactive
2024-01-28 23:38:44 +08:00
status: null,
roleName: null,
2024-01-28 23:38:44 +08:00
roleCode: null
2024-01-28 00:44:21 +08:00
},
transformer: res => {
const { records = [], current = 1, size = 10, total = 0 } = res.data || {};
return {
data: records,
pageNum: current,
pageSize: size,
total
};
},
onPaginationChanged(pg) {
const { page, pageSize } = pg;
updateSearchParams({
current: page,
size: pageSize
});
getData();
},
2024-01-28 00:44:21 +08:00
columns: () => [
{
type: 'selection',
align: 'center',
width: 48
},
{
key: 'index',
title: $t('common.index'),
render: (_, index): string => getIndex(index),
width: 64,
align: 'center'
},
{
key: 'roleName',
title: $t('page.manage.role.roleName'),
align: 'center',
minWidth: 120
2024-01-28 00:44:21 +08:00
},
{
key: 'roleCode',
title: $t('page.manage.role.roleCode'),
align: 'center',
minWidth: 120
2024-01-28 00:44:21 +08:00
},
{
key: 'roleDesc',
title: $t('page.manage.role.roleDesc'),
minWidth: 120
2024-01-28 00:44:21 +08:00
},
{
2024-01-28 23:38:44 +08:00
key: 'status',
2024-01-28 00:44:21 +08:00
title: $t('page.manage.role.roleStatus'),
align: 'center',
width: 100,
render: row => {
2024-01-28 23:38:44 +08:00
if (row.status === null) {
return null;
}
2024-01-28 23:38:44 +08:00
const tagMap: Record<Api.Common.EnableStatus, NaiveUI.ThemeColor> = {
1: 'success',
2: 'warning'
};
2024-01-28 23:38:44 +08:00
const label = $t(enableStatusRecord[row.status]);
2024-01-28 23:38:44 +08:00
return <NTag type={tagMap[row.status]}>{label}</NTag>;
}
2024-01-28 00:44:21 +08:00
},
{
key: 'operate',
title: $t('common.operate'),
align: 'center',
width: 130,
render: row => (
<div class="flex-center gap-8px">
<NButton type="primary" ghost size="small" onClick={() => handleEdit(row.id)}>
{$t('common.edit')}
</NButton>
<NPopconfirm onPositiveClick={() => handleDelete(row.id)}>
{{
default: () => $t('common.confirmDelete'),
trigger: () => (
<NButton type="error" ghost size="small">
{$t('common.delete')}
</NButton>
)
}}
</NPopconfirm>
</div>
)
}
]
});
const operateType = ref<OperateType>('add');
function handleAdd() {
operateType.value = 'add';
openDrawer();
}
const checkedRowKeys = ref<string[]>([]);
async function handleBatchDelete() {
// request
console.log(checkedRowKeys.value);
window.$message?.success($t('common.deleteSuccess'));
checkedRowKeys.value = [];
getData();
}
/** the editing row data */
const editingData = ref<Api.SystemManage.Role | null>(null);
function handleEdit(id: number) {
operateType.value = 'edit';
editingData.value = data.value.find(item => item.id === id) || null;
openDrawer();
}
async function handleDelete(id: number) {
// request
console.log(id);
window.$message?.success($t('common.deleteSuccess'));
getData();
}
function getIndex(index: number) {
const { page = 0, pageSize = 10 } = pagination;
return String((page - 1) * pageSize + index + 1);
}
</script>
2023-11-17 08:45:00 +08:00
2023-06-18 22:23:42 +08:00
<template>
2024-01-28 00:44:21 +08:00
<div class="flex-vertical-stretch gap-16px overflow-hidden <sm:overflow-auto">
<RoleSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getData" />
2024-01-28 00:44:21 +08:00
<NCard :title="$t('page.manage.role.title')" :bordered="false" size="small" class="card-wrapper sm:flex-1-hidden">
<template #header-extra>
<TableHeaderOperation
v-model:columns="filteredColumns"
:disabled-delete="checkedRowKeys.length === 0"
:loading="loading"
@add="handleAdd"
@delete="handleBatchDelete"
@refresh="getData"
/>
2024-01-28 00:44:21 +08:00
</template>
<NDataTable
v-model:checked-row-keys="checkedRowKeys"
:columns="columns"
:data="data"
size="small"
:flex-height="!appStore.isMobile"
:scroll-x="702"
2024-01-28 00:44:21 +08:00
:loading="loading"
remote
2024-01-28 00:44:21 +08:00
:pagination="pagination"
:row-key="item => item.id"
class="sm:h-full"
/>
<RoleOperateDrawer
2024-01-28 00:44:21 +08:00
v-model:visible="drawerVisible"
:operate-type="operateType"
:row-data="editingData"
@submitted="getData"
/>
</NCard>
</div>
2023-06-18 22:23:42 +08:00
</template>
<style scoped></style>