Files
JayuCloud/JC-Salon-Frontend/src/views/admin/AnnouncementsView.vue
T

217 lines
9.9 KiB
Vue

<template>
<div class="admin-page">
<div class="page-head">
<div>
<h2>公告管理</h2>
<p>发布后会在用户首页顶部持续滚动展示。</p>
</div>
<a-button type="primary" @click="openCreate">发布公告</a-button>
</div>
<a-table
class="announcement-table"
:columns="columns"
:data-source="announcements"
:loading="loading"
row-key="id"
:scroll="{ x: 'max-content' }"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'content'">
<span class="ellipsis">{{ extractRichText(record.content) }}</span>
</template>
<template v-else-if="column.key === 'status'">
<a-tag :color="record.status === 1 ? 'green' : 'default'">{{ record.status === 1 ? '已发布' : '已撤回' }}</a-tag>
</template>
<template v-else-if="column.key === 'action'">
<a-space>
<a-button size="small" @click="preview(record as AnnouncementVO)">查看</a-button>
<a-button v-if="record.status === 1" size="small" danger @click="revoke(record.id)">撤回</a-button>
</a-space>
</template>
</template>
</a-table>
<a-modal v-model:open="formOpen" title="发布公告" :mask-closable="false" @ok="submit" :confirm-loading="saving">
<a-form layout="vertical">
<a-form-item label="标题">
<a-input v-model:value="form.title" :maxlength="100" />
</a-form-item>
<a-form-item label="内容">
<a-textarea v-model:value="form.content" :rows="4" :maxlength="1000" placeholder="支持 HTML / Markdown,也可以直接输入纯文本" />
</a-form-item>
<a-form-item label="接收范围">
<a-select v-model:value="form.targetType" :options="targetOptions" />
</a-form-item>
<a-form-item v-if="needsShop" label="指定店铺">
<a-select v-model:value="form.shopIds" mode="multiple" :options="shopOptions" />
</a-form-item>
<a-form-item v-if="needsUser" label="指定用户">
<a-select v-model:value="form.userIds" mode="multiple" show-search option-filter-prop="label" :options="userOptions" />
</a-form-item>
<a-form-item label="排序">
<a-input-number v-model:value="form.sortOrder" style="width: 100%" />
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
/**
* AnnouncementsView page script.
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
*/
import { computed, h, onMounted, reactive, ref } from 'vue'
import { message, Modal } from 'ant-design-vue'
/**
* Purpose: AnnouncementVO supports AnnouncementsView.vue by holding or executing the announcement vo page or API role.
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
*/
import { adminAnnouncementApi, type AnnouncementVO, type MessageTargetOptionsVO } from '@/api/admin'
import { extractRichText, renderRichContent } from '@/utils/rich-content'
/** loading is a reactive state variable used by the template and event handlers. */
const loading = ref(false)
/** saving is a reactive state variable used by the template and event handlers. */
const saving = ref(false)
/** formOpen is a reactive state variable used by the template and event handlers. */
const formOpen = ref(false)
/**
* Purpose: announcements supports AnnouncementsView.vue by holding or executing the announcements page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const announcements = ref<AnnouncementVO[]>([])
/**
* Purpose: options supports AnnouncementsView.vue by holding or executing the options page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const options = ref<MessageTargetOptionsVO>({ shops: [], users: [] })
/**
* Purpose: form supports AnnouncementsView.vue by holding or executing the form page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const form = reactive<any>({ title: '', content: '', targetType: 'PLATFORM_ALL', shopIds: [], userIds: [], sortOrder: 0 })
/**
* Purpose: columns supports AnnouncementsView.vue by holding or executing the columns page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const columns = [
{ title: '标题', dataIndex: 'title', key: 'title' },
{ title: '内容', dataIndex: 'content', key: 'content' },
{ title: '发布人', dataIndex: 'publisherName', key: 'publisherName' },
{ title: '范围', dataIndex: 'targetTypeName', key: 'targetTypeName' },
{ title: '状态', dataIndex: 'status', key: 'status' },
{ title: '发布时间', dataIndex: 'publishTime', key: 'publishTime' },
{ title: '操作', key: 'action', width: 150 }
]
/**
* Purpose: targetOptions supports AnnouncementsView.vue by holding or executing the target options page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const targetOptions = [
{ label: '全平台', value: 'PLATFORM_ALL' },
{ label: '当前门店全部用户', value: 'CURRENT_SHOP_ALL' },
{ label: '指定门店全部用户', value: 'SELECTED_SHOPS' },
{ label: '指定用户', value: 'SELECTED_USERS' }
]
/** shopOptions is a reactive state variable used by the template and event handlers. */
const shopOptions = computed(() => options.value.shops.map(item => ({ label: item.shopName, value: item.id })))
/** userOptions is a reactive state variable used by the template and event handlers. */
const userOptions = computed(() => options.value.users.map(item => ({ label: `${item.nickname || item.account} ${item.shopName ? `(${item.shopName})` : ''}`, value: item.id })))
/** needsShop is a reactive state variable used by the template and event handlers. */
const needsShop = computed(() => ['SHOP_ALL', 'SELECTED_SHOPS'].includes(form.targetType))
/** needsUser is a reactive state variable used by the template and event handlers. */
const needsUser = computed(() => form.targetType === 'SELECTED_USERS')
/** load is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const load = async () => {
loading.value = true
try {
const [list, target] = await Promise.all([
adminAnnouncementApi.listAnnouncements(),
adminAnnouncementApi.getTargetOptions().catch(() => ({ shops: [], users: [] }))
])
announcements.value = list || []
options.value = target
} finally {
loading.value = false
}
}
/** openCreate is a local helper used by this page script. */
const openCreate = () => {
Object.assign(form, { title: '', content: '', targetType: 'PLATFORM_ALL', shopIds: [], userIds: [], sortOrder: 0 })
formOpen.value = true
}
/** submit is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const submit = async () => {
if (!form.title || !form.content) {
message.warning('请填写标题和内容')
return
}
saving.value = true
try {
await adminAnnouncementApi.createAnnouncement(form)
message.success('公告已发布')
formOpen.value = false
await load()
} finally {
saving.value = false
}
}
/** preview is a local helper used by this page script. */
const preview = (record: AnnouncementVO) => {
Modal.info({
title: record.title,
width: 560,
content: h('div', {
class: 'announcement-preview-content',
innerHTML: renderRichContent(record.content)
})
})
}
/** revoke is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const revoke = async (id: any) => {
await adminAnnouncementApi.revokeAnnouncement(id)
message.success('公告已撤回')
await load()
}
onMounted(load)
</script>
<style scoped>
.admin-page { display: grid; gap: 18px; }
.page-head { display: flex; justify-content: space-between; gap: 16px; align-items: center; }
.page-head h2 { margin: 0 0 4px; }
.page-head p { margin: 0; color: var(--text-secondary); }
.ellipsis { display: inline-block; max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; }
.announcement-table :deep(.ant-table) { min-width: 880px; }
.announcement-table :deep(.ant-table-cell) { white-space: nowrap; word-break: keep-all; overflow-wrap: normal; }
.announcement-table :deep(.ant-table-content) { overflow-x: auto; }
:global(.announcement-preview-content) { line-height: 1.8; overflow-wrap: break-word; }
:global(.announcement-preview-content *) { max-width: 100%; }
:global(.announcement-preview-content p),
:global(.announcement-preview-content ul),
:global(.announcement-preview-content ol),
:global(.announcement-preview-content blockquote),
:global(.announcement-preview-content pre),
:global(.announcement-preview-content table) { margin: 0 0 12px; }
:global(.announcement-preview-content p:last-child),
:global(.announcement-preview-content ul:last-child),
:global(.announcement-preview-content ol:last-child),
:global(.announcement-preview-content blockquote:last-child),
:global(.announcement-preview-content pre:last-child),
:global(.announcement-preview-content table:last-child) { margin-bottom: 0; }
:global(.announcement-preview-content ul),
:global(.announcement-preview-content ol) { padding-left: 1.25em; }
:global(.announcement-preview-content img) { display: block; max-width: 100%; border-radius: 8px; }
:global(.announcement-preview-content pre) { overflow-x: auto; padding: 10px 12px; border-radius: 8px; background: var(--theme-primary-soft); }
</style>