feat:岗位 post 的实现 80%(crud 功能)

This commit is contained in:
YunaiV
2025-03-29 19:19:32 +08:00
parent bfa84c6611
commit cac69a0283
9 changed files with 380 additions and 291 deletions

View File

@@ -1,54 +0,0 @@
<script lang="ts" setup>
import { ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
import { createPost, getPost, updatePost } from '#/api/system/post';
import { modalSchema } from './post.data';
defineOptions({ name: 'PostModel' });
const isUpdate = ref(false);
const [Form, formApi] = useVbenForm({
schema: modalSchema,
handleSubmit: onSubmit,
showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
onCancel: async () => {
modalApi.close();
await formApi.resetForm();
},
onConfirm: async () => {
await formApi.validateAndSubmitForm();
},
async onOpenChange(isOpen: boolean) {
if (isOpen) {
const { id } = modalApi.getData<Record<string, any>>();
isUpdate.value = !!id;
if (id) {
const values = await getPost(id);
formApi.setValues(values);
}
}
},
title: isUpdate.value ? '新增岗位' : '编辑岗位',
});
async function onSubmit(values: Record<string, any>) {
await (isUpdate.value
? updatePost(values as any)
: createPost(values as any));
modalApi.close();
await formApi.resetForm();
}
</script>
<template>
<Modal class="w-1/2">
<Form />
</Modal>
</template>

View File

@@ -0,0 +1,146 @@
import {type VbenFormSchema, z} from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemPostApi } from '#/api/system/post';
import { DICT_TYPE, getDictOptions } from '#/utils/dict';
import { CommonStatusEnum } from '#/utils/constants';
export function useFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'id',
label: 'id',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
component: 'Input',
fieldName: 'name',
label: '岗位名称',
rules: 'required',
},
{
component: 'Input',
fieldName: 'code',
label: '岗位编码',
rules: 'required',
},
{
component: 'InputNumber',
componentProps: {
min: 0,
class: 'w-full',
controlsPosition: 'right',
placeholder: '请输入岗位顺序',
},
fieldName: 'sort',
label: '岗位顺序',
rules: 'required',
},
{
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
buttonStyle: 'solid',
optionType: 'button',
},
fieldName: 'status',
label: '岗位岗位',
rules: z.number().default(CommonStatusEnum.ENABLE),
},
{
component: 'Textarea',
fieldName: 'remark',
label: '岗位备注',
},
];
}
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'name',
label: '岗位名称',
},
{
component: 'Input',
fieldName: 'code',
label: '岗位编码',
},
{
component: 'Select',
componentProps: {
allowClear: true,
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
},
fieldName: 'status',
label: '岗位状态',
}
];
}
export function useGridColumns<T = SystemPostApi.SystemPost>(
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
return [
{
field: 'id',
title: '岗位编号',
minWidth: 200,
},
{
field: 'name',
title: '岗位名称',
minWidth: 200,
},
{
field: 'code',
title: '岗位编码',
minWidth: 200,
},
{
field: 'sort',
title: '岗位顺序',
minWidth: 100,
},
{
field: 'remark',
title: '岗位备注',
minWidth: 200,
},
{
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.COMMON_STATUS },
},
field: 'status',
title: '状态',
minWidth: 100,
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
align: 'center',
cellRender: {
attrs: {
nameField: 'name',
nameTitle: '岗位',
onClick: onActionClick,
},
name: 'CellOperation',
},
field: 'operation',
fixed: 'right',
title: '操作',
minWidth: 130,
},
];
}

View File

@@ -1,115 +1,117 @@
<script lang="ts" setup>
import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SystemPostApi } from '#/api/system/post';
import { $t } from '#/locales';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getPostPage, deletePost, exportPost } from '#/api/system/post';
import { Page, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { Button, message } from 'ant-design-vue';
import { Plus } from '@vben/icons';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { exportPost, getPostPage, type PostVO } from '#/api/system/post';
import { columns, formSchema } from './post.data';
import PostModal from './PostModal.vue';
defineOptions({ name: 'SystemPost' });
const formOptions: VbenFormProps = {
// 默认展开
collapsed: false,
schema: formSchema,
// 控制表单是否显示折叠按钮
showCollapseButton: true,
// 按下回车时是否提交表单
submitOnEnter: false,
};
const gridOptions: VxeGridProps<PostVO> = {
checkboxConfig: {
highlight: true,
labelField: 'id',
},
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getPostPage({
page: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
};
const [Grid, tableApi] = useVbenVxeGrid({ formOptions, gridOptions });
import { useGridColumns, useGridFormSchema } from './data';
import Form from './modules/form.vue';
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: PostModal,
connectedComponent: Form,
destroyOnClose: true,
});
function handleCreate() {
formModalApi.setData({
valuse: {},
/** 编辑岗位 */
function onEdit(row: SystemPostApi.SystemPost) {
formModalApi.setData(row).open();
}
/** 创建岗位 */
function onCreate() {
formModalApi.setData(null).open();
}
/** 删除岗位 */
async function onDelete(row: SystemPostApi.SystemPost) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0,
key: 'action_process_msg',
});
formModalApi.open();
try {
await deletePost(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
key: 'action_process_msg',
});
refreshGrid();
} catch (error) {
hideLoading();
}
}
async function handleEdit(id: number) {
formModalApi.setData({ id });
formModalApi.open();
/** 表格操作按钮的回调函数 */
function onActionClick({
code,
row,
}: OnActionClickParams<SystemPostApi.SystemPost>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
// TODO
function handleDelete(id: number) {
message.success(id);
}
// TODO
async function handleExport() {
await exportPost(tableApi.formApi.form.values);
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema()
},
gridOptions: {
columns: useGridColumns(onActionClick),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getPostPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
},
toolbarConfig: {
export: true, // TODO @芋艿:导出
refresh: { code: 'query' },
search: true,
},
} as VxeTableGridOptions<SystemPostApi.SystemPost>,
});
/** 刷新表格 */
function refreshGrid() {
gridApi.query();
}
</script>
<template>
<Page auto-content-height>
<Grid>
<template #toolbar-actions>
<Button
type="primary"
v-access:code="['system:post:create']"
@click="handleCreate"
>
{{ $t('page.action.add') }}
</Button>
<Button
class="ml-4"
v-access:code="['system:post:export']"
@click="handleExport"
>
{{ $t('page.action.export') }}
</Button>
</template>
<template #action="{ row }">
<Button
type="link"
v-access:code="['system:post:update']"
@click="handleEdit(row.id)"
>
{{ $t('page.action.edit') }}
</Button>
<Button
danger
type="link"
v-access:code="['system:post:delete']"
@click="handleDelete(row.id)"
>
{{ $t('page.action.delete') }}
<FormModal @success="refreshGrid" />
<Grid table-title="岗位列表">
<template #toolbar-tools>
<Button type="primary" @click="onCreate">
<Plus class="size-5" />
{{ $t('ui.actionTitle.create', ['岗位']) }}
</Button>
</template>
</Grid>
<FormModal />
</Page>
</template>

View File

@@ -0,0 +1,86 @@
<script lang="ts" setup>
import type { SystemPostApi } from '#/api/system/post';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { Button, message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { createPost, updatePost, getPost } from '#/api/system/post';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<SystemPostApi.SystemPost>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', ['岗位'])
: $t('ui.actionTitle.create', ['岗位']);
});
const [Form, formApi] = useVbenForm({
layout: 'horizontal',
schema: useFormSchema(),
showDefaultActions: false,
});
function resetForm() {
formApi.resetForm();
formApi.setValues(formData.value || {});
}
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
const data = (await formApi.getValues()) as SystemPostApi.SystemPost;
try {
await (formData.value?.id
? updatePost(data)
: createPost(data));
await modalApi.close();
emit('success');
message.success({
content: $t('ui.actionMessage.operationSuccess'),
key: 'action_process_msg',
});
} finally {
modalApi.lock(false);
}
},
async onOpenChange(isOpen) {
if (!isOpen) {
return;
}
const data = modalApi.getData<SystemPostApi.SystemPost>();
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
formData.value = await getPost(data.id as number);
await formApi.setValues(formData.value);
} finally {
modalApi.lock(false);
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form class="mx-4" />
<template #prepend-footer>
<div class="flex-auto">
<Button type="primary" danger @click="resetForm">
{{ $t('common.reset') }}
</Button>
</div>
</template>
</Modal>
</template>

View File

@@ -1,96 +0,0 @@
import type { VxeGridProps } from '#/adapter/vxe-table';
import { $t } from '@vben/locales';
import { type VbenFormSchema } from '#/adapter/form';
import { getDictOptions } from '#/utils/dict';
export const formSchema: VbenFormSchema[] = [
{
component: 'Input',
fieldName: 'name',
label: '岗位名称',
},
{
component: 'Input',
fieldName: 'code',
label: '岗位编码',
},
// TODO: dict
{
component: 'Select',
componentProps: {
allowClear: true,
options: getDictOptions('common_status', 'number'),
placeholder: '请选择',
},
fieldName: 'status',
label: '状态',
},
];
export const columns: VxeGridProps['columns'] = [
{ title: '序号', type: 'seq', width: 50 },
{ field: 'id', title: '岗位编号' },
{ field: 'name', title: '岗位名称' },
{ field: 'code', title: '岗位编码' },
{ field: 'sort', title: '岗位顺序' },
{ field: 'remark', title: '岗位备注' },
{
field: 'status',
title: '状态',
cellRender: { name: 'CellDict', props: { type: 'common_status' } },
},
{ field: 'createTime', formatter: 'formatDateTime', title: '创建时间' },
{
field: 'action',
fixed: 'right',
slots: { default: 'action' },
title: $t('page.action.action'),
width: 160,
},
];
export const modalSchema: VbenFormSchema[] = [
{
component: 'Input',
fieldName: 'id',
label: 'id',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
component: 'Input',
fieldName: 'name',
label: '岗位标题',
rules: 'required',
},
{
component: 'Input',
fieldName: 'code',
label: '岗位编码',
rules: 'required',
},
{
component: 'Input',
fieldName: 'sort',
label: '岗位顺序',
rules: 'required',
},
{
component: 'Select',
componentProps: {
options: getDictOptions('common_status', 'number'),
},
fieldName: 'status',
label: '状态',
rules: 'required',
},
{
component: 'Textarea',
fieldName: 'remark',
label: '备注',
},
];