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

237 lines
6.2 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
2025-11-12 16:56:18 +08:00
import type { Article } from './modules/types';
2025-04-22 22:10:33 +08:00
2025-11-05 17:01:42 +08:00
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
2025-11-05 17:01:42 +08:00
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
2025-11-13 16:12:44 +08:00
import { deleteDraft, getDraftPage } from '#/api/mp/draft';
2025-11-12 16:56:18 +08:00
import { submitFreePublish } from '#/api/mp/freePublish';
import { createEmptyNewsItem } from '#/views/mp/draft/modules/types';
2025-11-05 17:01:42 +08:00
import { useGridColumns, useGridFormSchema } from './data';
2025-11-12 16:56:18 +08:00
import DraftTableCell from './modules/draft-table.vue';
2025-11-05 17:01:42 +08:00
import Form from './modules/form.vue';
defineOptions({ name: 'MpDraft' });
2025-11-12 16:56:18 +08:00
/** 刷新表格 */
function handleRefresh() {
gridApi.query();
}
2025-11-05 17:01:42 +08:00
/** 新增草稿 */
2025-11-05 17:01:42 +08:00
async function handleCreate() {
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
2025-11-12 16:56:18 +08:00
if (!accountId) {
2025-11-05 17:01:42 +08:00
message.warning('请先选择公众号');
return;
}
formModalApi
.setData({
isCreating: true,
accountId,
newsList: [createEmptyNewsItem()],
})
.open();
}
/** 修改草稿 */
2025-11-05 17:01:42 +08:00
async function handleEdit(row: Article) {
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
2025-11-12 16:56:18 +08:00
if (!accountId) {
2025-11-05 17:01:42 +08:00
message.warning('请先选择公众号');
return;
}
formModalApi
.setData({
isCreating: false,
accountId,
mediaId: row.mediaId,
2025-11-12 16:56:18 +08:00
newsList: row.content.newsItem,
2025-11-05 17:01:42 +08:00
})
.open();
}
/** 删除草稿 */
async function handleDelete(row: Article) {
2025-11-05 17:01:42 +08:00
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
2025-11-12 16:56:18 +08:00
if (!accountId) {
2025-11-05 17:01:42 +08:00
message.warning('请先选择公众号');
return;
}
await confirm('此操作将永久删除该草稿, 是否继续?');
const hideLoading = message.loading({
content: '删除中...',
duration: 0,
});
2025-11-05 17:01:42 +08:00
try {
await deleteDraft(accountId, row.mediaId);
message.success('删除成功');
2025-11-12 16:56:18 +08:00
handleRefresh();
} finally {
hideLoading();
2025-11-05 17:01:42 +08:00
}
}
/** 发布草稿 */
async function handlePublish(row: Article) {
2025-11-05 17:01:42 +08:00
const formValues = await gridApi.formApi.getValues();
const accountId = formValues.accountId;
2025-11-12 16:56:18 +08:00
if (!accountId) {
2025-11-05 17:01:42 +08:00
message.warning('请先选择公众号');
return;
}
await confirm(
'你正在通过发布的方式发表内容。 发布不占用群发次数,一天可多次发布。' +
'已发布内容不会推送给用户,也不会展示在公众号主页中。 ' +
'发布后,你可以前往发表记录获取链接,也可以将发布内容添加到自定义菜单、自动回复、话题和页面模板中。',
);
const hideLoading = message.loading({
content: '发布中...',
duration: 0,
});
2025-11-05 17:01:42 +08:00
try {
await submitFreePublish(accountId, row.mediaId);
message.success('发布成功');
2025-11-12 16:56:18 +08:00
handleRefresh();
} finally {
hideLoading();
2025-11-05 17:01:42 +08:00
}
}
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
2025-11-12 16:56:18 +08:00
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
submitOnChange: true,
},
gridOptions: {
columns: useGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
2025-11-14 11:18:50 +08:00
const drafts = await getDraftPage({
2025-11-12 16:56:18 +08:00
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
// 将 thumbUrl 转成 picUrl保证 wx-news 组件可以预览封面
drafts.list.forEach((draft: any) => {
const newsList = draft.content?.newsItem;
if (newsList) {
newsList.forEach((item: any) => {
item.picUrl = item.thumbUrl || item.picUrl;
});
}
});
return {
list: drafts.list as unknown as Article[],
total: drafts.total,
};
},
},
},
rowConfig: {
keyField: 'mediaId',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
// TODO @hw这里有点纠结一般是 MpDraftApi.Article但是一改貌似就 linter 告警了。
2025-11-12 16:56:18 +08:00
} as VxeTableGridOptions<Article>,
2025-11-05 17:01:42 +08:00
});
</script>
<template>
2025-11-05 17:01:42 +08:00
<Page auto-content-height>
2025-11-12 16:56:18 +08:00
<template #doc>
<DocAlert title="公众号图文" url="https://doc.iocoder.cn/mp/article/" />
</template>
2025-11-05 17:01:42 +08:00
2025-11-12 16:56:18 +08:00
<FormModal @success="handleRefresh" />
2025-11-05 17:01:42 +08:00
<Grid table-title="草稿列表">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['图文草稿']),
2025-11-05 17:01:42 +08:00
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['mp:draft:create'],
onClick: handleCreate,
},
]"
/>
</template>
<template #content="{ row }">
<DraftTableCell :row="row" />
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: '发布',
type: 'link',
icon: ACTION_ICON.UPLOAD,
auth: ['mp:free-publish:submit'],
onClick: handlePublish.bind(null, row),
},
{
label: '编辑',
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['mp:draft:update'],
onClick: handleEdit.bind(null, row),
},
{
label: '删除',
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['mp:draft:delete'],
popConfirm: {
title: '是否确认删除此数据?',
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
2025-04-22 22:10:33 +08:00
</template>
2025-11-05 17:01:42 +08:00
<style lang="scss" scoped>
:deep(.vxe-table--body-wrapper) {
.vxe-table--body {
.vxe-body--column {
.vxe-cell {
2025-11-13 18:35:10 +08:00
height: auto !important;
2025-11-13 18:35:44 +08:00
padding: 0;
2025-11-13 18:35:10 +08:00
img {
width: 300px !important;
}
2025-11-05 17:01:42 +08:00
}
}
}
}
</style>