feat: add employee workspace operations admin
This commit is contained in:
@@ -0,0 +1,680 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from "vue";
|
||||
import {
|
||||
ElMessage,
|
||||
ElMessageBox,
|
||||
type FormInstance,
|
||||
type FormRules
|
||||
} from "element-plus";
|
||||
import {
|
||||
archiveAnnouncement,
|
||||
createAnnouncement,
|
||||
listAllStores,
|
||||
listAnnouncements,
|
||||
listEmployeeOptions,
|
||||
listRoles,
|
||||
publishAnnouncement,
|
||||
updateAnnouncement,
|
||||
type Announcement,
|
||||
type AnnouncementImportance,
|
||||
type AnnouncementPayload,
|
||||
type AnnouncementStatus,
|
||||
type AnnouncementTargetScope,
|
||||
type Employee,
|
||||
type RoleOption,
|
||||
type Store
|
||||
} from "@/api/access";
|
||||
import { hasMenuAction, hasPerms } from "@/utils/auth";
|
||||
|
||||
import Plus from "~icons/ep/plus";
|
||||
import Search from "~icons/ep/search";
|
||||
import Refresh from "~icons/ep/refresh";
|
||||
import EditPen from "~icons/ep/edit-pen";
|
||||
import Promotion from "~icons/ep/promotion";
|
||||
import Box from "~icons/ep/box";
|
||||
|
||||
defineOptions({
|
||||
name: "AnnouncementManagement"
|
||||
});
|
||||
|
||||
type AnnouncementFormState = AnnouncementPayload & {
|
||||
id?: number;
|
||||
};
|
||||
|
||||
const tableLoading = ref(false);
|
||||
const optionLoading = ref(false);
|
||||
const submitLoading = ref(false);
|
||||
const dialogVisible = ref(false);
|
||||
const formRef = ref<FormInstance>();
|
||||
const announcements = ref<Announcement[]>([]);
|
||||
const stores = ref<Store[]>([]);
|
||||
const roles = ref<RoleOption[]>([]);
|
||||
const employees = ref<Employee[]>([]);
|
||||
|
||||
const query = reactive({
|
||||
status: undefined as AnnouncementStatus | undefined,
|
||||
importance: undefined as AnnouncementImportance | undefined,
|
||||
keyword: "",
|
||||
page: 1,
|
||||
pageSize: 20
|
||||
});
|
||||
|
||||
const pagination = reactive({
|
||||
total: 0,
|
||||
totalPages: 0
|
||||
});
|
||||
|
||||
const form = reactive<AnnouncementFormState>({
|
||||
title: "",
|
||||
content: "",
|
||||
importance: "NORMAL",
|
||||
targetScope: "ALL_STORES",
|
||||
targetStoreIds: [],
|
||||
targetRoleIds: [],
|
||||
targetEmployeeIds: []
|
||||
});
|
||||
|
||||
const rules: FormRules<AnnouncementFormState> = {
|
||||
title: [
|
||||
{ required: true, message: "请输入公告标题", trigger: "blur" },
|
||||
{ max: 100, message: "标题不能超过 100 个字符", trigger: "blur" }
|
||||
],
|
||||
content: [
|
||||
{ required: true, message: "请输入公告内容", trigger: "blur" },
|
||||
{ max: 5000, message: "内容不能超过 5000 个字符", trigger: "blur" }
|
||||
],
|
||||
importance: [
|
||||
{ required: true, message: "请选择重要级别", trigger: "change" }
|
||||
],
|
||||
targetScope: [
|
||||
{ required: true, message: "请选择目标范围", trigger: "change" }
|
||||
]
|
||||
};
|
||||
|
||||
const dialogTitle = computed(() => (form.id ? "编辑公告" : "新建公告"));
|
||||
const canManage = computed(
|
||||
() =>
|
||||
hasMenuAction("announcements", "manage") || hasPerms("announcement:manage")
|
||||
);
|
||||
|
||||
const statusMap: Record<AnnouncementStatus, { label: string; type: string }> = {
|
||||
DRAFT: { label: "草稿", type: "info" },
|
||||
PUBLISHED: { label: "已发布", type: "success" },
|
||||
ARCHIVED: { label: "已归档", type: "warning" }
|
||||
};
|
||||
|
||||
const importanceMap: Record<
|
||||
AnnouncementImportance,
|
||||
{ label: string; type: string }
|
||||
> = {
|
||||
NORMAL: { label: "普通", type: "" },
|
||||
IMPORTANT: { label: "重要", type: "warning" },
|
||||
URGENT: { label: "紧急", type: "danger" }
|
||||
};
|
||||
|
||||
const scopeMap: Record<AnnouncementTargetScope, string> = {
|
||||
ALL_STORES: "全部门店",
|
||||
STORES: "指定门店",
|
||||
ROLES: "指定角色",
|
||||
EMPLOYEES: "指定员工"
|
||||
};
|
||||
|
||||
function getErrorMessage(error: unknown, fallback: string) {
|
||||
const message = (
|
||||
error as { response?: { data?: { error?: { message?: string } } } }
|
||||
)?.response?.data?.error?.message;
|
||||
|
||||
if (message) return message;
|
||||
if (error instanceof Error && error.message) return error.message;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function formatTime(value?: string | null) {
|
||||
if (!value) return "-";
|
||||
return new Date(value).toLocaleString("zh-CN", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit"
|
||||
});
|
||||
}
|
||||
|
||||
function resetFormState() {
|
||||
Object.assign(form, {
|
||||
id: undefined,
|
||||
title: "",
|
||||
content: "",
|
||||
importance: "NORMAL",
|
||||
targetScope: "ALL_STORES",
|
||||
targetStoreIds: [],
|
||||
targetRoleIds: [],
|
||||
targetEmployeeIds: []
|
||||
});
|
||||
formRef.value?.clearValidate();
|
||||
}
|
||||
|
||||
function buildPayload(): AnnouncementPayload {
|
||||
return {
|
||||
title: form.title.trim(),
|
||||
content: form.content.trim(),
|
||||
importance: form.importance,
|
||||
targetScope: form.targetScope,
|
||||
targetStoreIds: form.targetScope === "STORES" ? form.targetStoreIds : [],
|
||||
targetRoleIds: form.targetScope === "ROLES" ? form.targetRoleIds : [],
|
||||
targetEmployeeIds:
|
||||
form.targetScope === "EMPLOYEES" ? form.targetEmployeeIds : []
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchOptions() {
|
||||
optionLoading.value = true;
|
||||
try {
|
||||
const [storeResult, roleResult, employeeResult] = await Promise.all([
|
||||
listAllStores({ includeInactive: true }),
|
||||
listRoles(),
|
||||
listEmployeeOptions()
|
||||
]);
|
||||
stores.value = storeResult.data;
|
||||
roles.value = roleResult.data;
|
||||
employees.value = employeeResult.data;
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "加载目标选项失败"));
|
||||
} finally {
|
||||
optionLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchAnnouncements() {
|
||||
tableLoading.value = true;
|
||||
try {
|
||||
const result = await listAnnouncements({
|
||||
status: query.status,
|
||||
importance: query.importance,
|
||||
keyword: query.keyword.trim() || undefined,
|
||||
page: query.page,
|
||||
pageSize: query.pageSize
|
||||
});
|
||||
announcements.value = result.data.items;
|
||||
pagination.total = result.data.pagination.total;
|
||||
pagination.totalPages = result.data.pagination.totalPages;
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "加载公告列表失败"));
|
||||
} finally {
|
||||
tableLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function handleSearch() {
|
||||
query.page = 1;
|
||||
fetchAnnouncements();
|
||||
}
|
||||
|
||||
function handleReset() {
|
||||
query.status = undefined;
|
||||
query.importance = undefined;
|
||||
query.keyword = "";
|
||||
query.page = 1;
|
||||
query.pageSize = 20;
|
||||
fetchAnnouncements();
|
||||
}
|
||||
|
||||
function handlePageChange(page: number) {
|
||||
query.page = page;
|
||||
fetchAnnouncements();
|
||||
}
|
||||
|
||||
function handleSizeChange(pageSize: number) {
|
||||
query.page = 1;
|
||||
query.pageSize = pageSize;
|
||||
fetchAnnouncements();
|
||||
}
|
||||
|
||||
function openCreateDialog() {
|
||||
if (!canManage.value) return;
|
||||
resetFormState();
|
||||
dialogVisible.value = true;
|
||||
fetchOptions();
|
||||
}
|
||||
|
||||
function openEditDialog(row: Announcement) {
|
||||
if (!canManage.value) return;
|
||||
Object.assign(form, {
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
content: row.content,
|
||||
importance: row.importance,
|
||||
targetScope: row.targetScope,
|
||||
targetStoreIds: row.targetStoreIds ?? [],
|
||||
targetRoleIds: row.targetRoleIds ?? [],
|
||||
targetEmployeeIds: row.targetEmployeeIds ?? []
|
||||
});
|
||||
dialogVisible.value = true;
|
||||
fetchOptions();
|
||||
formRef.value?.clearValidate();
|
||||
}
|
||||
|
||||
async function submitForm(publish = false) {
|
||||
if (!canManage.value) return;
|
||||
await formRef.value?.validate();
|
||||
submitLoading.value = true;
|
||||
|
||||
try {
|
||||
const payload = buildPayload();
|
||||
const result = form.id
|
||||
? await updateAnnouncement(form.id, payload)
|
||||
: await createAnnouncement(payload);
|
||||
|
||||
if (publish) {
|
||||
await publishAnnouncement(result.data.id);
|
||||
ElMessage.success("公告已发布");
|
||||
} else {
|
||||
ElMessage.success(form.id ? "公告已保存" : "公告草稿已保存");
|
||||
}
|
||||
|
||||
dialogVisible.value = false;
|
||||
fetchAnnouncements();
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "保存公告失败"));
|
||||
} finally {
|
||||
submitLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handlePublish(row: Announcement) {
|
||||
if (!canManage.value) return;
|
||||
try {
|
||||
await publishAnnouncement(row.id);
|
||||
ElMessage.success("公告已发布");
|
||||
fetchAnnouncements();
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "发布公告失败"));
|
||||
}
|
||||
}
|
||||
|
||||
async function handleArchive(row: Announcement) {
|
||||
if (!canManage.value) return;
|
||||
try {
|
||||
await ElMessageBox.confirm(`确认归档公告「${row.title}」?`, "归档公告", {
|
||||
type: "warning",
|
||||
confirmButtonText: "归档",
|
||||
cancelButtonText: "取消"
|
||||
});
|
||||
await archiveAnnouncement(row.id);
|
||||
ElMessage.success("公告已归档");
|
||||
fetchAnnouncements();
|
||||
} catch (error) {
|
||||
if (error !== "cancel") {
|
||||
ElMessage.error(getErrorMessage(error, "归档公告失败"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(fetchAnnouncements);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="operation-page">
|
||||
<div class="page-heading">
|
||||
<div>
|
||||
<p class="eyebrow">员工工作台运营</p>
|
||||
<h1>公告管理</h1>
|
||||
</div>
|
||||
<el-button
|
||||
v-if="canManage"
|
||||
type="primary"
|
||||
:icon="Plus"
|
||||
@click="openCreateDialog"
|
||||
>
|
||||
新建公告
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<div class="toolbar">
|
||||
<el-select
|
||||
v-model="query.status"
|
||||
clearable
|
||||
placeholder="全部状态"
|
||||
class="toolbar-control"
|
||||
@change="handleSearch"
|
||||
>
|
||||
<el-option label="草稿" value="DRAFT" />
|
||||
<el-option label="已发布" value="PUBLISHED" />
|
||||
<el-option label="已归档" value="ARCHIVED" />
|
||||
</el-select>
|
||||
<el-select
|
||||
v-model="query.importance"
|
||||
clearable
|
||||
placeholder="全部级别"
|
||||
class="toolbar-control"
|
||||
@change="handleSearch"
|
||||
>
|
||||
<el-option label="普通" value="NORMAL" />
|
||||
<el-option label="重要" value="IMPORTANT" />
|
||||
<el-option label="紧急" value="URGENT" />
|
||||
</el-select>
|
||||
<el-input
|
||||
v-model="query.keyword"
|
||||
clearable
|
||||
class="keyword-input"
|
||||
placeholder="搜索标题或内容"
|
||||
@clear="handleSearch"
|
||||
@keyup.enter="handleSearch"
|
||||
/>
|
||||
<div class="toolbar-actions">
|
||||
<el-button type="primary" :icon="Search" @click="handleSearch">
|
||||
查询
|
||||
</el-button>
|
||||
<el-button :icon="Refresh" @click="handleReset">重置</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-shell">
|
||||
<el-table
|
||||
v-loading="tableLoading"
|
||||
:data="announcements"
|
||||
row-key="id"
|
||||
stripe
|
||||
>
|
||||
<el-table-column prop="title" label="标题" min-width="220" />
|
||||
<el-table-column label="重要级别" width="110">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="importanceMap[row.importance].type" effect="light">
|
||||
{{ importanceMap[row.importance].label }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="目标范围" width="130">
|
||||
<template #default="{ row }">
|
||||
{{ scopeMap[row.targetScope] }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="状态" width="110">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="statusMap[row.status].type" effect="light">
|
||||
{{ statusMap[row.status].label }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="publisherName" label="发布人" min-width="120">
|
||||
<template #default="{ row }">{{ row.publisherName || "-" }}</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="发布时间" width="180">
|
||||
<template #default="{ row }">{{
|
||||
formatTime(row.publishedAt)
|
||||
}}</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="readCount" label="已读人数" width="100" />
|
||||
<el-table-column
|
||||
v-if="canManage"
|
||||
label="操作"
|
||||
width="220"
|
||||
fixed="right"
|
||||
>
|
||||
<template #default="{ row }">
|
||||
<el-button
|
||||
link
|
||||
type="primary"
|
||||
:icon="EditPen"
|
||||
@click="openEditDialog(row)"
|
||||
>
|
||||
编辑
|
||||
</el-button>
|
||||
<el-button
|
||||
v-if="row.status !== 'PUBLISHED'"
|
||||
link
|
||||
type="success"
|
||||
:icon="Promotion"
|
||||
@click="handlePublish(row)"
|
||||
>
|
||||
发布
|
||||
</el-button>
|
||||
<el-button
|
||||
v-if="row.status !== 'ARCHIVED'"
|
||||
link
|
||||
type="warning"
|
||||
:icon="Box"
|
||||
@click="handleArchive(row)"
|
||||
>
|
||||
归档
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<div class="pagination-row">
|
||||
<span
|
||||
>共 {{ pagination.total }} 条,{{ pagination.totalPages }} 页</span
|
||||
>
|
||||
<el-pagination
|
||||
v-model:current-page="query.page"
|
||||
v-model:page-size="query.pageSize"
|
||||
background
|
||||
layout="sizes, prev, pager, next"
|
||||
:total="pagination.total"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
@current-change="handlePageChange"
|
||||
@size-change="handleSizeChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<el-dialog
|
||||
v-model="dialogVisible"
|
||||
:title="dialogTitle"
|
||||
width="640px"
|
||||
destroy-on-close
|
||||
>
|
||||
<el-form ref="formRef" :model="form" :rules="rules" label-position="top">
|
||||
<el-form-item label="标题" prop="title">
|
||||
<el-input v-model="form.title" maxlength="100" placeholder="请输入" />
|
||||
</el-form-item>
|
||||
<el-form-item label="内容" prop="content">
|
||||
<el-input
|
||||
v-model="form.content"
|
||||
type="textarea"
|
||||
maxlength="5000"
|
||||
show-word-limit
|
||||
:rows="6"
|
||||
placeholder="请输入公告内容"
|
||||
/>
|
||||
</el-form-item>
|
||||
<div class="form-grid">
|
||||
<el-form-item label="重要级别" prop="importance">
|
||||
<el-select v-model="form.importance" class="full-width">
|
||||
<el-option label="普通" value="NORMAL" />
|
||||
<el-option label="重要" value="IMPORTANT" />
|
||||
<el-option label="紧急" value="URGENT" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="目标范围" prop="targetScope">
|
||||
<el-select v-model="form.targetScope" class="full-width">
|
||||
<el-option label="全部门店" value="ALL_STORES" />
|
||||
<el-option label="指定门店" value="STORES" />
|
||||
<el-option label="指定角色" value="ROLES" />
|
||||
<el-option label="指定员工" value="EMPLOYEES" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</div>
|
||||
<el-form-item v-if="form.targetScope === 'STORES'" label="目标门店">
|
||||
<el-select
|
||||
v-model="form.targetStoreIds"
|
||||
multiple
|
||||
filterable
|
||||
class="full-width"
|
||||
:loading="optionLoading"
|
||||
>
|
||||
<el-option
|
||||
v-for="store in stores"
|
||||
:key="store.id"
|
||||
:label="store.name"
|
||||
:value="store.id"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item v-if="form.targetScope === 'ROLES'" label="目标角色">
|
||||
<el-select
|
||||
v-model="form.targetRoleIds"
|
||||
multiple
|
||||
filterable
|
||||
class="full-width"
|
||||
:loading="optionLoading"
|
||||
>
|
||||
<el-option
|
||||
v-for="role in roles"
|
||||
:key="role.id"
|
||||
:label="`${role.name}(${role.code})`"
|
||||
:value="role.id"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item v-if="form.targetScope === 'EMPLOYEES'" label="目标员工">
|
||||
<el-select
|
||||
v-model="form.targetEmployeeIds"
|
||||
multiple
|
||||
filterable
|
||||
class="full-width"
|
||||
:loading="optionLoading"
|
||||
>
|
||||
<el-option
|
||||
v-for="employee in employees"
|
||||
:key="employee.id"
|
||||
:label="`${employee.name}(${employee.phone})`"
|
||||
:value="employee.id"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<template #footer>
|
||||
<el-button @click="dialogVisible = false">取消</el-button>
|
||||
<el-button :loading="submitLoading" @click="submitForm(false)">
|
||||
保存草稿
|
||||
</el-button>
|
||||
<el-button
|
||||
type="primary"
|
||||
:loading="submitLoading"
|
||||
@click="submitForm(true)"
|
||||
>
|
||||
发布
|
||||
</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.operation-page {
|
||||
min-height: 100%;
|
||||
padding: 20px;
|
||||
background: #f6f8fb;
|
||||
}
|
||||
|
||||
.page-heading {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 16px;
|
||||
|
||||
h1 {
|
||||
margin: 4px 0 0;
|
||||
font-size: 24px;
|
||||
font-weight: 650;
|
||||
color: #111827;
|
||||
letter-spacing: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.toolbar,
|
||||
.table-shell {
|
||||
background: #fff;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
padding: 14px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.toolbar-control {
|
||||
width: 180px;
|
||||
}
|
||||
|
||||
.keyword-input {
|
||||
width: 260px;
|
||||
}
|
||||
|
||||
.toolbar-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.table-shell {
|
||||
padding: 8px 0 14px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.pagination-row {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 14px 16px 0;
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.full-width {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@media (width <= 760px) {
|
||||
.operation-page {
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.page-heading {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.toolbar-control,
|
||||
.keyword-input,
|
||||
.toolbar-actions {
|
||||
width: 100%;
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
.toolbar-actions {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.pagination-row {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user