feat:【ele】mp/tag 的代码迁移

This commit is contained in:
YunaiV
2025-10-25 15:01:31 +08:00
parent fde9ddf468
commit a3890a120f
10 changed files with 369 additions and 19 deletions

View File

@@ -45,7 +45,7 @@ async function handleDelete(row: MpAccountApi.Account) {
duration: 0,
});
try {
await deleteAccount(row.id as number);
await deleteAccount(row.id!);
message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
@@ -60,7 +60,7 @@ async function handleGenerateQrCode(row: MpAccountApi.Account) {
duration: 0,
});
try {
await generateAccountQrCode(row.id as number);
await generateAccountQrCode(row.id!);
message.success($t('ui.actionMessage.operationSuccess'));
handleRefresh();
} finally {
@@ -75,7 +75,7 @@ async function handleCleanQuota(row: MpAccountApi.Account) {
duration: 0,
});
try {
await clearAccountQuota(row.id as number);
await clearAccountQuota(row.id!);
message.success($t('ui.actionMessage.operationSuccess'));
} finally {
hideLoading();

View File

@@ -22,7 +22,6 @@ export function useGridFormSchema(): VbenFormSchema[] {
value: item.id,
})),
placeholder: '请选择公众号',
allowClear: true,
},
defaultValue: accountList[0]?.id,
},

View File

@@ -4,6 +4,7 @@ import type { MpAccountApi } from '#/api/mp/account';
import { getSimpleAccountList } from '#/api/mp/account';
/** 关联数据 */
let accountList: MpAccountApi.AccountSimple[] = [];
getSimpleAccountList().then((data) => (accountList = data));
@@ -45,14 +46,13 @@ export function useGridFormSchema(): VbenFormSchema[] {
{
fieldName: 'accountId',
label: '公众号',
component: 'Select',
component: 'ApiSelect',
componentProps: {
options: accountList.map((item) => ({
label: item.name,
value: item.id,
})),
placeholder: '请选择公众号',
allowClear: true,
},
defaultValue: accountList[0]?.id,
},
@@ -65,19 +65,23 @@ export function useGridColumns(): VxeGridPropTypes.Columns {
{
title: '编号',
field: 'id',
minWidth: 80,
},
{
title: '标签名称',
field: 'name',
minWidth: 150,
},
{
title: '粉丝数',
field: 'count',
minWidth: 100,
},
{
title: '创建时间',
field: 'createTime',
formatter: 'formatDateTime',
minWidth: 180,
},
{
title: '操作',

View File

@@ -27,6 +27,10 @@ function handleRefresh() {
async function handleCreate() {
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
if (!accountId) {
message.warning('请先选择公众号');
return;
}
formModalApi.setData({ accountId }).open();
}
@@ -34,6 +38,10 @@ async function handleCreate() {
async function handleEdit(row: MpTagApi.Tag) {
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
if (!accountId) {
message.warning('请先选择公众号');
return;
}
formModalApi.setData({ row, accountId }).open();
}
@@ -44,15 +52,14 @@ async function handleDelete(row: MpTagApi.Tag) {
duration: 0,
});
try {
await deleteTag(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
});
await deleteTag(row.id!);
message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
hideLoading();
}
}
/** 同步标签 */
async function handleSync() {
const formValues = await gridApi.formApi.getValues();
@@ -69,9 +76,7 @@ async function handleSync() {
});
try {
await syncTag(accountId);
message.success({
content: '同步标签成功',
});
message.success('同步标签成功');
handleRefresh();
} finally {
hideLoading();
@@ -99,6 +104,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,

View File

@@ -66,7 +66,7 @@ const [Modal, modalApi] = useVbenModal({
}
modalApi.lock();
try {
formData.value = await getTag(data.row.id as number);
formData.value = await getTag(data.row.id!);
// 设置到 values
await formApi.setValues(formData.value);
} finally {
@@ -77,7 +77,7 @@ const [Modal, modalApi] = useVbenModal({
</script>
<template>
<Modal class="w-2/5" :title="getTitle">
<Modal :title="getTitle" class="w-2/5">
<Form class="mx-4" />
</Modal>
</template>

View File

@@ -44,7 +44,7 @@ async function handleDelete(row: MpAccountApi.Account) {
text: $t('ui.actionMessage.deleting', [row.name]),
});
try {
await deleteAccount(row.id as number);
await deleteAccount(row.id!);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
@@ -58,7 +58,7 @@ async function handleGenerateQrCode(row: MpAccountApi.Account) {
text: '正在生成二维码中...',
});
try {
await generateAccountQrCode(row.id as number);
await generateAccountQrCode(row.id!);
ElMessage.success($t('ui.actionMessage.operationSuccess'));
handleRefresh();
} finally {
@@ -72,7 +72,7 @@ async function handleCleanQuota(row: MpAccountApi.Account) {
text: $t('ui.actionMessage.processing', ['清空 API 配额']),
});
try {
await clearAccountQuota(row.id as number);
await clearAccountQuota(row.id!);
ElMessage.success($t('ui.actionMessage.operationSuccess'));
} finally {
loadingInstance.close();

View File

@@ -22,7 +22,6 @@ export function useGridFormSchema(): VbenFormSchema[] {
value: item.id,
})),
placeholder: '请选择公众号',
clearable: true,
},
defaultValue: accountList[0]?.id,
},

View File

@@ -0,0 +1,93 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeGridPropTypes } from '#/adapter/vxe-table';
import type { MpAccountApi } from '#/api/mp/account';
import { getSimpleAccountList } from '#/api/mp/account';
/** 关联数据 */
let accountList: MpAccountApi.AccountSimple[] = [];
getSimpleAccountList().then((data) => (accountList = data));
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'id',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'accountId',
label: '公众号',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: '标签名称',
component: 'Input',
rules: 'required',
componentProps: {
placeholder: '请输入名称',
},
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'accountId',
label: '公众号',
component: 'ApiSelect',
componentProps: {
options: accountList.map((item) => ({
label: item.name,
value: item.id,
})),
placeholder: '请选择公众号',
},
defaultValue: accountList[0]?.id,
},
];
}
/** 表格列配置 */
export function useGridColumns(): VxeGridPropTypes.Columns {
return [
{
title: '编号',
field: 'id',
minWidth: 80,
},
{
title: '标签名称',
field: 'name',
minWidth: 150,
},
{
title: '粉丝数',
field: 'count',
minWidth: 100,
},
{
title: '创建时间',
field: 'createTime',
formatter: 'formatDateTime',
minWidth: 180,
},
{
title: '操作',
width: 140,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@@ -0,0 +1,166 @@
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MpTagApi } from '#/api/mp/tag';
import { confirm, Page, useVbenModal } from '@vben/common-ui';
import { ElLoading, ElMessage } from 'element-plus';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteTag, getTagPage, syncTag } from '#/api/mp/tag';
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();
}
/** 创建标签 */
async function handleCreate() {
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
if (!accountId) {
ElMessage.warning('请先选择公众号');
return;
}
formModalApi.setData({ accountId }).open();
}
/** 编辑标签 */
async function handleEdit(row: MpTagApi.Tag) {
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
if (!accountId) {
ElMessage.warning('请先选择公众号');
return;
}
formModalApi.setData({ row, accountId }).open();
}
/** 删除标签 */
async function handleDelete(row: MpTagApi.Tag) {
const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deleting', [row.name]),
});
try {
await deleteTag(row.id!);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
loadingInstance.close();
}
}
/** 同步标签 */
async function handleSync() {
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
if (!accountId) {
ElMessage.warning('请先选择公众号');
return;
}
await confirm('是否确认同步标签?');
const loadingInstance = ElLoading.service({
text: '正在同步标签...',
});
try {
await syncTag(accountId);
ElMessage.success('同步标签成功');
handleRefresh();
} finally {
loadingInstance.close();
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getTagPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
} as VxeTableGridOptions<MpTagApi.Tag>,
});
</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: ['mp:tag:create'],
onClick: handleCreate,
},
{
label: '同步',
type: 'primary',
icon: 'lucide:refresh-ccw',
auth: ['mp:tag:sync'],
onClick: handleSync,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'primary',
link: true,
icon: ACTION_ICON.EDIT,
auth: ['mp:tag:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'danger',
link: true,
icon: ACTION_ICON.DELETE,
auth: ['mp:tag:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>

View File

@@ -0,0 +1,83 @@
<script lang="ts" setup>
import type { MpTagApi } from '#/api/mp/tag';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { ElMessage } from 'element-plus';
import { useVbenForm } from '#/adapter/form';
import { createTag, getTag, updateTag } from '#/api/mp/tag';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<MpTagApi.Tag>();
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: 120,
},
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 MpTagApi.Tag;
try {
await (formData.value?.id ? updateTag(data) : createTag(data));
// 关闭并提示
await modalApi.close();
emit('success');
ElMessage.success($t('ui.actionMessage.operationSuccess'));
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
return;
}
// 加载数据
const data = modalApi.getData<{ accountId: number; row: MpTagApi.Tag }>();
if (!data || !data.row || !data.accountId) {
return;
}
modalApi.lock();
try {
formData.value = await getTag(data.row.id!);
// 设置到 values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal :title="getTitle" class="w-2/5">
<Form class="mx-4" />
</Modal>
</template>