feat: 新增编码规则管理前端页面
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
export namespace BaseCodeRuleApi {
|
||||
/** 分段配置 */
|
||||
export interface SegmentConfig {
|
||||
type: 'PREFIX' | 'DATE' | 'SEQUENCE';
|
||||
/** PREFIX 段:固定值 */
|
||||
value?: string;
|
||||
/** DATE 段:日期格式 */
|
||||
format?: string;
|
||||
/** SEQUENCE 段:重置维度 */
|
||||
resetBy?: 'DAY' | 'MONTH' | 'YEAR' | 'GLOBAL';
|
||||
/** SEQUENCE 段:起始值 */
|
||||
startAt?: number;
|
||||
/** SEQUENCE 段:补位长度 */
|
||||
paddingLen?: number;
|
||||
/** SEQUENCE 段:补位字符 */
|
||||
paddingChar?: string;
|
||||
/** SEQUENCE 段:最大值上限 */
|
||||
maxValue?: number;
|
||||
}
|
||||
|
||||
/** 编码规则 */
|
||||
export interface CodeRule {
|
||||
id?: number;
|
||||
ruleName: string;
|
||||
ruleCode: string;
|
||||
segments: SegmentConfig[];
|
||||
separator?: string;
|
||||
letterCase?: number;
|
||||
totalLength?: number;
|
||||
seqStrategy?: string;
|
||||
status?: number;
|
||||
remark?: string;
|
||||
createTime?: string;
|
||||
}
|
||||
}
|
||||
|
||||
/** 查询编码规则分页 */
|
||||
export async function getCodeRulePage(params: {
|
||||
pageNo: number;
|
||||
pageSize: number;
|
||||
ruleName?: string;
|
||||
ruleCode?: string;
|
||||
status?: number;
|
||||
createTime?: string[];
|
||||
}) {
|
||||
return requestClient.get<{
|
||||
list: BaseCodeRuleApi.CodeRule[];
|
||||
total: number;
|
||||
}>('/base/code-rule/page', { params });
|
||||
}
|
||||
|
||||
/** 查询编码规则详情 */
|
||||
export async function getCodeRule(id: number) {
|
||||
return requestClient.get<BaseCodeRuleApi.CodeRule>(
|
||||
`/base/code-rule/get?id=${id}`,
|
||||
);
|
||||
}
|
||||
|
||||
/** 查询编码规则精简列表(下拉选项) */
|
||||
export async function getSimpleCodeRuleList() {
|
||||
return requestClient.get<BaseCodeRuleApi.CodeRule[]>(
|
||||
'/base/code-rule/simple-list',
|
||||
);
|
||||
}
|
||||
|
||||
/** 新增编码规则 */
|
||||
export async function createCodeRule(data: BaseCodeRuleApi.CodeRule) {
|
||||
return requestClient.post('/base/code-rule/create', data);
|
||||
}
|
||||
|
||||
/** 修改编码规则 */
|
||||
export async function updateCodeRule(data: BaseCodeRuleApi.CodeRule) {
|
||||
return requestClient.put('/base/code-rule/update', data);
|
||||
}
|
||||
|
||||
/** 删除编码规则 */
|
||||
export async function deleteCodeRule(id: number) {
|
||||
return requestClient.delete(`/base/code-rule/delete?id=${id}`);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/base/code-rule',
|
||||
component: () => import('#/views/base/codegen/index.vue'),
|
||||
name: 'BaseCodeRule',
|
||||
meta: {
|
||||
title: '编码规则',
|
||||
icon: 'ant-design:barcode-outlined',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
||||
@@ -0,0 +1,226 @@
|
||||
import type { VbenFormSchema } from '#/adapter/form';
|
||||
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||
import type { BaseCodeRuleApi } from '#/api/base/codegen';
|
||||
|
||||
import { getRangePickerDefaultProps } from '#/utils';
|
||||
|
||||
/** 字母大小写选项 */
|
||||
const LETTER_CASE_OPTIONS = [
|
||||
{ label: '原样', value: 0 },
|
||||
{ label: '全大写', value: 1 },
|
||||
{ label: '全小写', value: 2 },
|
||||
];
|
||||
|
||||
/** 序号策略选项 */
|
||||
const SEQ_STRATEGY_OPTIONS = [
|
||||
{ label: '号段预分配', value: 'segment' },
|
||||
{ label: '分布式锁实时', value: 'lock' },
|
||||
];
|
||||
|
||||
/** 状态选项 */
|
||||
const STATUS_OPTIONS = [
|
||||
{ label: '启用', value: 0 },
|
||||
{ label: '禁用', value: 1 },
|
||||
];
|
||||
|
||||
/** 新增/修改的表单 */
|
||||
export function useFormSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
{
|
||||
fieldName: 'id',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
triggerFields: [''],
|
||||
show: () => false,
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'ruleName',
|
||||
label: '规则名称',
|
||||
rules: 'required',
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
placeholder: '请输入规则名称',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'ruleCode',
|
||||
label: '规则编码',
|
||||
rules: 'required',
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
placeholder: '请输入规则编码(唯一标识)',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'segments',
|
||||
label: '分段配置',
|
||||
rules: 'required',
|
||||
component: 'Textarea',
|
||||
componentProps: {
|
||||
placeholder:
|
||||
'JSON数组格式,例如:\n[{"type":"PREFIX","value":"ORD"},{"type":"DATE","format":"yyyyMMdd"},{"type":"SEQUENCE","resetBy":"DAY","startAt":1,"paddingLen":4,"paddingChar":"0","maxValue":9999}]',
|
||||
rows: 6,
|
||||
},
|
||||
help: 'JSON数组格式。type: PREFIX/DATE/SEQUENCE',
|
||||
},
|
||||
{
|
||||
fieldName: 'separator',
|
||||
label: '分隔符',
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
placeholder: '如 -、/、_,留空则不使用分隔符',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'letterCase',
|
||||
label: '字母大小写',
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
options: LETTER_CASE_OPTIONS,
|
||||
buttonStyle: 'solid',
|
||||
optionType: 'button',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'totalLength',
|
||||
label: '总长度约束',
|
||||
component: 'InputNumber',
|
||||
componentProps: {
|
||||
class: '!w-full',
|
||||
min: 0,
|
||||
placeholder: '0 表示不限制',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'seqStrategy',
|
||||
label: '序号策略',
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
options: SEQ_STRATEGY_OPTIONS,
|
||||
buttonStyle: 'solid',
|
||||
optionType: 'button',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'status',
|
||||
label: '状态',
|
||||
component: 'RadioGroup',
|
||||
componentProps: {
|
||||
options: STATUS_OPTIONS,
|
||||
buttonStyle: 'solid',
|
||||
optionType: 'button',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'remark',
|
||||
label: '备注',
|
||||
component: 'Textarea',
|
||||
componentProps: {
|
||||
placeholder: '请输入备注',
|
||||
rows: 3,
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/** 列表的搜索表单 */
|
||||
export function useGridFormSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
{
|
||||
fieldName: 'ruleName',
|
||||
label: '规则名称',
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
placeholder: '请输入规则名称',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'ruleCode',
|
||||
label: '规则编码',
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
placeholder: '请输入规则编码',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'status',
|
||||
label: '状态',
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
placeholder: '请选择',
|
||||
options: STATUS_OPTIONS,
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'createTime',
|
||||
label: '创建时间',
|
||||
component: 'RangePicker',
|
||||
componentProps: {
|
||||
...getRangePickerDefaultProps(),
|
||||
allowClear: true,
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/** 列表的字段 */
|
||||
export function useGridColumns(): VxeTableGridOptions<BaseCodeRuleApi.CodeRule>['columns'] {
|
||||
return [
|
||||
{ type: 'checkbox', width: 40 },
|
||||
{
|
||||
field: 'ruleName',
|
||||
title: '规则名称',
|
||||
minWidth: 160,
|
||||
},
|
||||
{
|
||||
field: 'ruleCode',
|
||||
title: '规则编码',
|
||||
minWidth: 160,
|
||||
},
|
||||
{
|
||||
field: 'segmentConfig',
|
||||
title: '分段配置',
|
||||
minWidth: 200,
|
||||
formatter: ({ cellValue }: { cellValue: BaseCodeRuleApi.SegmentConfig[] }) => {
|
||||
if (!cellValue || cellValue.length === 0) return '-';
|
||||
return cellValue.map((s) => s.type).join(' + ');
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'separator',
|
||||
title: '分隔符',
|
||||
minWidth: 80,
|
||||
formatter: ({ cellValue }: { cellValue: string }) => cellValue || '-',
|
||||
},
|
||||
{
|
||||
field: 'seqStrategy',
|
||||
title: '序号策略',
|
||||
minWidth: 120,
|
||||
formatter: ({ cellValue }: { cellValue: string }) =>
|
||||
cellValue === 'lock' ? '分布式锁实时' : '号段预分配',
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
minWidth: 80,
|
||||
formatter: ({ cellValue }: { cellValue: number }) =>
|
||||
cellValue === 0 ? '启用' : '禁用',
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 180,
|
||||
formatter: 'formatDateTime',
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 180,
|
||||
fixed: 'right',
|
||||
slots: { default: 'actions' },
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
<script lang="ts" setup>
|
||||
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||
import type { BaseCodeRuleApi } from '#/api/base/codegen';
|
||||
|
||||
import { ref } from 'vue';
|
||||
|
||||
import { confirm, Page, useVbenModal } from '@vben/common-ui';
|
||||
import { isEmpty } from '@vben/utils';
|
||||
|
||||
import { message } from 'antdv-next';
|
||||
|
||||
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { deleteCodeRule, getCodeRulePage } from '#/api/base/codegen';
|
||||
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: BaseCodeRuleApi.CodeRule) {
|
||||
formModalApi.setData(row).open();
|
||||
}
|
||||
|
||||
/** 删除编码规则 */
|
||||
async function handleDelete(row: BaseCodeRuleApi.CodeRule) {
|
||||
const hideLoading = message.loading({
|
||||
content: $t('ui.actionMessage.deleting', [row.ruleName]),
|
||||
duration: 0,
|
||||
});
|
||||
try {
|
||||
await deleteCodeRule(row.id!);
|
||||
message.success($t('ui.actionMessage.deleteSuccess', [row.ruleName]));
|
||||
handleRefresh();
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
/** 批量删除编码规则 */
|
||||
async function handleDeleteBatch() {
|
||||
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
|
||||
const hideLoading = message.loading({
|
||||
content: $t('ui.actionMessage.deletingBatch'),
|
||||
duration: 0,
|
||||
});
|
||||
try {
|
||||
// 逐个删除
|
||||
for (const id of checkedIds.value) {
|
||||
await deleteCodeRule(id);
|
||||
}
|
||||
checkedIds.value = [];
|
||||
message.success($t('ui.actionMessage.deleteSuccess'));
|
||||
handleRefresh();
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
const checkedIds = ref<number[]>([]);
|
||||
function handleRowCheckboxChange({
|
||||
records,
|
||||
}: {
|
||||
records: BaseCodeRuleApi.CodeRule[];
|
||||
}) {
|
||||
checkedIds.value = records.map((item) => item.id!);
|
||||
}
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions: {
|
||||
columns: useGridColumns(),
|
||||
height: 'auto',
|
||||
pagerConfig: {
|
||||
enabled: true,
|
||||
},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async ({ page }) => {
|
||||
const params = {
|
||||
pageNo: page.currentPage,
|
||||
pageSize: page.pageSize,
|
||||
...formData.value,
|
||||
};
|
||||
return await getCodeRulePage(params);
|
||||
},
|
||||
},
|
||||
},
|
||||
rowConfig: {
|
||||
keyField: 'id',
|
||||
isHover: true,
|
||||
},
|
||||
toolbarConfig: {
|
||||
refresh: true,
|
||||
search: true,
|
||||
},
|
||||
} as VxeTableGridOptions<BaseCodeRuleApi.CodeRule>,
|
||||
gridEvents: {
|
||||
checkboxAll: handleRowCheckboxChange,
|
||||
checkboxChange: handleRowCheckboxChange,
|
||||
},
|
||||
formOptions: {
|
||||
schema: useGridFormSchema(),
|
||||
},
|
||||
});
|
||||
|
||||
const formData = ref<Record<string, unknown>>({});
|
||||
</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: ['base:code-rule:create'],
|
||||
onClick: handleCreate,
|
||||
},
|
||||
{
|
||||
label: $t('ui.actionTitle.deleteBatch'),
|
||||
type: 'primary',
|
||||
danger: true,
|
||||
icon: ACTION_ICON.DELETE,
|
||||
auth: ['base:code-rule:delete'],
|
||||
disabled: isEmpty(checkedIds),
|
||||
onClick: handleDeleteBatch,
|
||||
},
|
||||
]"
|
||||
/>
|
||||
</template>
|
||||
<template #actions="{ row }">
|
||||
<TableAction
|
||||
:actions="[
|
||||
{
|
||||
label: $t('common.edit'),
|
||||
type: 'link',
|
||||
icon: ACTION_ICON.EDIT,
|
||||
auth: ['base:code-rule:update'],
|
||||
onClick: handleEdit.bind(null, row),
|
||||
},
|
||||
{
|
||||
label: $t('common.delete'),
|
||||
type: 'link',
|
||||
danger: true,
|
||||
icon: ACTION_ICON.DELETE,
|
||||
auth: ['base:code-rule:delete'],
|
||||
popConfirm: {
|
||||
title: $t('ui.actionMessage.deleteConfirm', [row.ruleName]),
|
||||
confirm: handleDelete.bind(null, row),
|
||||
},
|
||||
},
|
||||
]"
|
||||
/>
|
||||
</template>
|
||||
</Grid>
|
||||
</Page>
|
||||
</template>
|
||||
@@ -0,0 +1,103 @@
|
||||
<script lang="ts" setup>
|
||||
import type { BaseCodeRuleApi } from '#/api/base/codegen';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { message } from 'antdv-next';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import {
|
||||
createCodeRule,
|
||||
getCodeRule,
|
||||
updateCodeRule,
|
||||
} from '#/api/base/codegen';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
import { useFormSchema } from '../data';
|
||||
|
||||
const emit = defineEmits(['success']);
|
||||
const formData = ref<BaseCodeRuleApi.CodeRule>();
|
||||
const getTitle = computed(() => {
|
||||
return formData.value?.id
|
||||
? $t('ui.actionTitle.edit', ['编码规则'])
|
||||
: $t('ui.actionTitle.create', ['编码规则']);
|
||||
});
|
||||
|
||||
const [Form, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
formItemClass: 'col-span-2',
|
||||
labelWidth: 100,
|
||||
},
|
||||
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 Record<string, unknown>;
|
||||
// 处理 segments JSON 字符串
|
||||
const submitData: Record<string, unknown> = { ...data };
|
||||
if (typeof submitData.segments === 'string') {
|
||||
try {
|
||||
submitData.segments = JSON.parse(submitData.segments as string);
|
||||
} catch {
|
||||
message.error('分段配置 JSON 格式不正确');
|
||||
modalApi.unlock();
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
await (formData.value?.id
|
||||
? updateCodeRule(submitData as BaseCodeRuleApi.CodeRule)
|
||||
: createCodeRule(submitData as BaseCodeRuleApi.CodeRule));
|
||||
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<BaseCodeRuleApi.CodeRule>();
|
||||
if (!data || !data.id) {
|
||||
await formApi.setValues(data);
|
||||
return;
|
||||
}
|
||||
modalApi.lock();
|
||||
try {
|
||||
formData.value = await getCodeRule(data.id);
|
||||
// segments 数组转 JSON 字符串用于表单展示
|
||||
const values = {
|
||||
...formData.value,
|
||||
segments: formData.value?.segments
|
||||
? JSON.stringify(formData.value.segments)
|
||||
: '',
|
||||
};
|
||||
await formApi.setValues(values);
|
||||
} finally {
|
||||
modalApi.unlock();
|
||||
}
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal :title="getTitle">
|
||||
<Form class="mx-4" />
|
||||
</Modal>
|
||||
</template>
|
||||
Reference in New Issue
Block a user