feat(@vben/web-antd): 项目管理前端页面和请求拦截器适配

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
lzh
2026-04-16 23:08:25 +08:00
parent 5faaa3c051
commit 44b2dd9d05
6 changed files with 485 additions and 0 deletions

View File

@@ -86,6 +86,8 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) {
config.headers['visit-tenant-id'] = tenantEnable
? accessStore.visitTenantId
: undefined;
// 添加项目编号
config.headers['project-id'] = accessStore.projectId;
// 是否 API 加密
if ((config.headers || {}).isEncrypt) {
@@ -182,6 +184,8 @@ baseRequestClient.addRequestInterceptor({
config.headers['visit-tenant-id'] = tenantEnable
? accessStore.visitTenantId
: undefined;
// 添加项目编号
config.headers['project-id'] = accessStore.projectId;
return config;
},
});

View File

@@ -0,0 +1,48 @@
import type { PageParam, PageResult } from '@vben/request';
import { requestClient } from '#/api/request';
export namespace SystemProjectApi {
export interface Project {
id?: number;
name: string;
code: string;
status: number;
contactName?: string;
contactMobile?: string;
address?: string;
remark?: string;
createTime?: Date;
}
}
export function getProjectPage(params: PageParam) {
return requestClient.get<PageResult<SystemProjectApi.Project>>(
'/system/project/page',
{ params },
);
}
export function getSimpleProjectList() {
return requestClient.get<SystemProjectApi.Project[]>(
'/system/project/simple-list',
);
}
export function getProject(id: number) {
return requestClient.get<SystemProjectApi.Project>(
`/system/project/get?id=${id}`,
);
}
export function createProject(data: SystemProjectApi.Project) {
return requestClient.post('/system/project/create', data);
}
export function updateProject(data: SystemProjectApi.Project) {
return requestClient.put('/system/project/update', data);
}
export function deleteProject(id: number) {
return requestClient.delete(`/system/project/delete?id=${id}`);
}

View File

@@ -0,0 +1,171 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { z } from '#/adapter/form';
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: '项目名称',
component: 'Input',
componentProps: {
placeholder: '请输入项目名称',
},
rules: 'required',
},
{
fieldName: 'code',
label: '项目编码',
component: 'Input',
componentProps: {
placeholder: '请输入项目编码',
},
rules: 'required',
},
{
fieldName: 'status',
label: '项目状态',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
buttonStyle: 'solid',
optionType: 'button',
},
rules: z.number().default(CommonStatusEnum.ENABLE),
},
{
fieldName: 'contactName',
label: '联系人',
component: 'Input',
componentProps: {
placeholder: '请输入联系人',
},
},
{
fieldName: 'contactMobile',
label: '联系手机',
component: 'Input',
componentProps: {
placeholder: '请输入联系手机',
},
rules: 'mobile',
},
{
fieldName: 'address',
label: '项目地址',
component: 'Input',
componentProps: {
placeholder: '请输入项目地址',
},
},
{
fieldName: 'remark',
label: '备注',
component: 'Textarea',
componentProps: {
placeholder: '请输入备注',
rows: 3,
},
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'name',
label: '项目名称',
component: 'Input',
componentProps: {
placeholder: '请输入项目名称',
allowClear: true,
},
},
{
fieldName: 'code',
label: '项目编码',
component: 'Input',
componentProps: {
placeholder: '请输入项目编码',
allowClear: true,
},
},
{
fieldName: 'status',
label: '状态',
component: 'Select',
componentProps: {
placeholder: '请选择状态',
allowClear: true,
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
},
},
];
}
/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
return [
{ type: 'checkbox', width: 40 },
{
field: 'id',
title: '项目编号',
minWidth: 100,
},
{
field: 'name',
title: '项目名称',
minWidth: 180,
},
{
field: 'code',
title: '项目编码',
minWidth: 180,
},
{
field: 'contactName',
title: '联系人',
minWidth: 100,
},
{
field: 'contactMobile',
title: '联系手机',
minWidth: 180,
},
{
field: 'status',
title: '项目状态',
minWidth: 100,
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.COMMON_STATUS },
},
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
title: '操作',
width: 130,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@@ -0,0 +1,169 @@
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemProjectApi } from '#/api/system/project';
import { ref } from 'vue';
import { confirm, Page, useVbenModal } from '@vben/common-ui';
import { isEmpty } from '@vben/utils';
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteProject,
getProjectPage,
} from '#/api/system/project';
import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
import Form from './modules/form.vue';
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
/** 刷新表格 */
function handleRefresh() {
gridApi.query();
}
/** 创建项目 */
function handleCreate() {
formModalApi.setData(null).open();
}
/** 编辑项目 */
function handleEdit(row: SystemProjectApi.Project) {
formModalApi.setData(row).open();
}
/** 删除项目 */
async function handleDelete(row: SystemProjectApi.Project) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0,
});
try {
await deleteProject(row.id!);
message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
hideLoading();
}
}
/** 批量删除项目 */
async function handleDeleteBatch() {
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
const hideLoading = message.loading({
content: $t('ui.actionMessage.deletingBatch'),
duration: 0,
});
try {
await Promise.all(checkedIds.value.map((id) => deleteProject(id)));
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally {
hideLoading();
}
}
const checkedIds = ref<number[]>([]);
function handleRowCheckboxChange({
records,
}: {
records: SystemProjectApi.Project[];
}) {
checkedIds.value = records.map((item) => item.id!);
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getProjectPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
} as VxeTableGridOptions<SystemProjectApi.Project>,
gridEvents: {
checkboxAll: handleRowCheckboxChange,
checkboxChange: handleRowCheckboxChange,
},
});
</script>
<template>
<Page auto-content-height>
<FormModal @success="handleRefresh" />
<Grid table-title="项目列表">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['项目']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['system:project:create'],
onClick: handleCreate,
},
{
label: $t('ui.actionTitle.deleteBatch'),
type: 'primary',
danger: true,
icon: ACTION_ICON.DELETE,
disabled: isEmpty(checkedIds),
auth: ['system:project:delete'],
onClick: handleDeleteBatch,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['system:project:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['system:project:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>

View File

@@ -0,0 +1,84 @@
<script lang="ts" setup>
import type { SystemProjectApi } from '#/api/system/project';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createProject,
getProject,
updateProject,
} from '#/api/system/project';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<SystemProjectApi.Project>();
const getTitle = computed(() => {
return formData.value
? $t('ui.actionTitle.edit', ['项目'])
: $t('ui.actionTitle.create', ['项目']);
});
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
},
wrapperClass: 'grid-cols-1',
layout: 'horizontal',
schema: useFormSchema(),
showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
// 提交表单
const data = (await formApi.getValues()) as SystemProjectApi.Project;
try {
await (formData.value ? updateProject(data) : createProject(data));
// 关闭并提示
await modalApi.close();
emit('success');
message.success($t('ui.actionMessage.operationSuccess'));
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
return;
}
// 加载数据
const data = modalApi.getData<SystemProjectApi.Project>();
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
formData.value = await getProject(data.id);
// 设置到 values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form class="mx-4" />
</Modal>
</template>

View File

@@ -51,6 +51,10 @@ interface AccessState {
* 访问租户编号
*/
visitTenantId: null | number;
/**
* 当前项目编号
*/
projectId: null | number;
}
/**
@@ -105,6 +109,9 @@ export const useAccessStore = defineStore('core-access', {
setTenantId(tenantId: null | number) {
this.tenantId = tenantId;
},
setProjectId(projectId: null | number) {
this.projectId = projectId;
},
setVisitTenantId(visitTenantId: number) {
this.visitTenantId = visitTenantId;
},
@@ -121,6 +128,7 @@ export const useAccessStore = defineStore('core-access', {
'accessCodes',
'tenantId',
'visitTenantId',
'projectId',
'isLockScreen',
'lockScreenPassword',
],
@@ -137,6 +145,7 @@ export const useAccessStore = defineStore('core-access', {
refreshToken: null,
tenantId: null,
visitTenantId: null,
projectId: null,
}),
});