Files
iot-device-management-frontend/apps/web-antd/src/views/mp/autoReply/index.vue

222 lines
6.0 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
2025-11-04 16:53:08 +08:00
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MpAutoReplyApi } from '#/api/mp/autoReply';
2025-04-22 22:10:33 +08:00
2025-11-12 16:56:18 +08:00
import { computed, nextTick, ref } from 'vue';
2025-11-04 16:53:08 +08:00
2025-11-12 16:56:18 +08:00
import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui';
2025-11-04 16:53:08 +08:00
import { IconifyIcon } from '@vben/icons';
import { message, Row, Tabs } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
2025-11-13 16:12:44 +08:00
import {
deleteAutoReply,
getAutoReply,
getAutoReplyPage,
} from '#/api/mp/autoReply';
2025-11-04 16:53:08 +08:00
import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
2025-11-12 16:56:18 +08:00
import ReplyContentCell from './modules/content.vue';
2025-11-04 16:53:08 +08:00
import Form from './modules/form.vue';
2025-11-12 16:56:18 +08:00
import { MsgType } from './types';
2025-11-04 16:53:08 +08:00
defineOptions({ name: 'MpAutoReply' });
const msgType = ref<string>(String(MsgType.Keyword)); // 消息类型
const showCreateButton = computed(() => {
if (Number(msgType.value) !== MsgType.Follow) {
return true;
}
try {
const tableData = gridApi.grid?.getTableData();
return (tableData?.tableData?.length || 0) <= 0;
} catch {
return true;
}
}); // 计算是否显示新增按钮:关注时回复类型只有在没有数据时才显示
/** 刷新表格 */
function handleRefresh() {
gridApi.query();
}
/** 切换回复类型 */
2025-11-13 16:12:44 +08:00
async function onTabChange(tabName: any) {
msgType.value = tabName;
2025-11-04 16:53:08 +08:00
await nextTick();
// 更新 columns
2025-11-04 16:53:08 +08:00
const columns = useGridColumns(Number(msgType.value) as MsgType);
if (columns) {
// 使用 setGridOptions 更新列配置
gridApi.setGridOptions({ columns });
// 等待列配置更新完成
await nextTick();
}
// 查询数据
2025-11-04 16:53:08 +08:00
await gridApi.query();
}
/** 新增自动回复 */
2025-11-04 16:53:08 +08:00
async function handleCreate() {
const formValues = await gridApi.formApi.getValues();
formModalApi
.setData({
msgType: Number(msgType.value) as MsgType,
accountId: formValues.accountId,
})
.open();
}
/** 修改自动回复 */
2025-11-04 16:53:08 +08:00
async function handleEdit(row: any) {
2025-11-13 16:12:44 +08:00
const data = (await getAutoReply(row.id)) as any;
2025-11-04 16:53:08 +08:00
formModalApi
.setData({
msgType: Number(msgType.value) as MsgType,
2025-11-12 16:56:18 +08:00
accountId: row.accountId,
2025-11-04 16:53:08 +08:00
row: data,
})
.open();
}
/** 删除自动回复 */
2025-11-04 16:53:08 +08:00
async function handleDelete(row: any) {
await confirm('是否确认删除此数据?');
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', ['自动回复']),
duration: 0,
});
try {
2025-11-13 16:12:44 +08:00
await deleteAutoReply(row.id);
2025-11-04 16:53:08 +08:00
message.success('删除成功');
2025-11-12 16:56:18 +08:00
handleRefresh();
2025-11-04 16:53:08 +08:00
} finally {
hideLoading();
}
}
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
submitOnChange: true, // 表单值变化时自动提交,这样 accountId 会被正确传递到查询函数
2025-11-04 16:53:08 +08:00
},
gridOptions: {
columns: useGridColumns(Number(msgType.value) as MsgType),
2025-11-12 16:56:18 +08:00
height: 'auto',
2025-11-04 16:53:08 +08:00
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
2025-11-13 16:12:44 +08:00
return await getAutoReplyPage({
2025-11-04 16:53:08 +08:00
pageNo: page.currentPage,
pageSize: page.pageSize,
type: Number(msgType.value) as MsgType,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
} as VxeTableGridOptions<MpAutoReplyApi.AutoReply>,
2025-11-04 16:53:08 +08:00
});
</script>
<template>
2025-11-04 16:53:08 +08:00
<Page auto-content-height>
2025-11-12 16:56:18 +08:00
<template #doc>
<DocAlert title="自动回复" url="https://doc.iocoder.cn/mp/auto-reply/" />
</template>
<FormModal @success="handleRefresh" />
<Grid>
2025-11-12 16:56:18 +08:00
<template #toolbar-actions>
<Tabs
v-model:active-key="msgType"
class="w-full"
@change="(activeKey) => onTabChange(activeKey as string)"
>
<Tabs.TabPane :key="String(MsgType.Follow)">
<template #tab>
<Row align="middle">
<IconifyIcon icon="ep:star" class="mr-2px" /> 关注时回复
</Row>
</template>
</Tabs.TabPane>
<Tabs.TabPane :key="String(MsgType.Message)">
<template #tab>
<Row align="middle">
<IconifyIcon icon="ep:chat-line-round" class="mr-2px" />
消息回复
</Row>
</template>
</Tabs.TabPane>
<Tabs.TabPane :key="String(MsgType.Keyword)">
<template #tab>
<Row align="middle">
<IconifyIcon icon="fa:newspaper-o" class="mr-2px" /> 关键词回复
</Row>
</template>
</Tabs.TabPane>
</Tabs>
</template>
<!-- 第三层table -->
<template #toolbar-tools>
<TableAction
v-if="showCreateButton"
:actions="[
{
label: $t('ui.actionTitle.create', ['自动回复']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['mp:auto-reply:create'],
onClick: handleCreate,
},
]"
/>
</template>
<template #replyContent="{ row }">
<ReplyContentCell :row="row" />
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['mp:auto-reply:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['mp:auto-reply:delete'],
popConfirm: {
title: '是否确认删除此数据?',
confirm: handleDelete.bind(null, row),
2025-11-04 16:53:08 +08:00
},
2025-11-12 16:56:18 +08:00
},
]"
/>
</template>
</Grid>
</Page>
2025-04-22 22:10:33 +08:00
</template>