refactor timi-web

This commit is contained in:
Timi
2026-08-26 21:18:02 +08:00
parent fe5d190308
commit f54a129edb
85 changed files with 1745 additions and 180 deletions
+6 -2
View File
@@ -8,7 +8,9 @@
</div>
<pass-time-label class="time" :timestamp="detail.sendAt" underline />
</header>
<section v-text="detail.content" />
<section class="body">
<notify-view :detail="detail" empty-text="暂无通知内容" />
</section>
</article>
<t-empty
v-else
@@ -18,7 +20,9 @@
</template>
<script setup lang="ts">
import { NotifyAPI, type NotifyDetail, PassTimeLabel } from "timi-web";
import { NotifyView } from "timi-tdesign-mobile";
import { PassTimeLabel } from "timi-web";
import { NotifyAPI, type NotifyDetail } from "timi-web-api";
import { DialogPlugin } from "tdesign-mobile-vue";
import { useNavBarStore } from "@/store/navBarStore";
import { getErrorMessage } from "@/utils/error";
+2 -1
View File
@@ -65,7 +65,8 @@
</template>
<script setup lang="ts">
import { NotifyAPI, Time, type NotifyDetail, type Page, type PageResult, useUserStore } from "timi-web";
import { Time } from "timi-web";
import { NotifyAPI, type NotifyDetail, type Page, type PageResult, useUserStore } from "timi-web-api";
import { DialogPlugin } from "tdesign-mobile-vue";
import { Message } from "@/utils/message";
import { useNavBarStore } from "@/store/navBarStore";
+357 -7
View File
@@ -1,22 +1,372 @@
<template>
<div class="notify-management">
<t-empty description="该功能暂不可用" />
<div v-can="corePermissionCodes.notify.read" class="notify-management">
<t-pull-down-refresh
v-model="isRefreshing"
:refresh-timeout="30000"
@refresh="refresh"
@scrolltolower="loadMore"
>
<t-cell-group v-if="pageResult.list.length" class="items" theme="card">
<t-swipe-cell
v-for="item in pageResult.list"
:key="item.id"
class="item"
>
<t-cell
:description="getPreview(item)"
:note="getSendTime(item)"
arrow
hover
@click="toDetail(item)"
>
<template #leftIcon>
<div class="icon" :class="{ scheduled: isScheduled(item) }">
<t-icon :name="isScheduled(item) ? 'time' : 'notification'" size="1.25rem" />
</div>
</template>
<template #title>
<div class="title">
<span v-text="getSubject(item)" />
<t-tag size="small" :theme="getStatusTheme(item)" :content="getStatusText(item)" />
</div>
</template>
<template #description>
<div class="description">
<span v-text="getRecipientSummary(item)" />
<span v-if="item.readCount" v-text="`已读 ${item.readCount}`" />
</div>
</template>
</t-cell>
<template #right>
<t-button
v-if="canCancel(item)"
class="cancel"
theme="primary"
@click.stop="confirmCancel(item)"
>取消</t-button>
<t-button
v-if="canDeleteNotify"
class="delete"
theme="danger"
@click.stop="confirmDelete(item)"
>删除</t-button>
</template>
</t-swipe-cell>
</t-cell-group>
<t-empty v-else-if="!isLoading" description="暂无已发送通知" />
<div class="load-state" v-text="loadStateText" />
</t-pull-down-refresh>
</div>
</template>
<script setup lang="ts">
import { Time } from "timi-web";
import { NotifyAPI, type Notify, type NotifyManagementItem, type Page, type PageResult, useUserStore } from "timi-web-api";
import { DialogPlugin } from "tdesign-mobile-vue";
import { Message } from "@/utils/message";
import { getErrorMessage } from "@/utils/error";
import { useNavBarStore } from "@/store/navBarStore";
import { corePermissionCodes } from "@/utils/permission";
type NotifyTagTheme = "default" | "success" | "warning" | "primary" | "danger";
defineOptions({
name: "NotifyManagement"
});
const pageSize = 20;
const router = useRouter();
const navBarStore = useNavBarStore();
const userStore = useUserStore();
const canReadNotify = computed(() => userStore.hasPermissions(corePermissionCodes.notify.read));
const canCreateNotify = computed(() => userStore.hasPermissions(corePermissionCodes.notify.create));
const canDeleteNotify = computed(() => userStore.hasPermissions(corePermissionCodes.notify.delete));
const canCancelNotify = computed(() => userStore.hasPermissions(corePermissionCodes.notify.cancel));
// ---------- 列表分页 ----------
const isRefreshing = ref(false);
const isLoading = ref(false);
const isNoMore = ref(false);
const page = reactive<Page<Notify>>({
index: 0,
size: pageSize,
orderMap: { createdAt: "DESC" }
});
const pageResult = reactive<PageResult<NotifyManagementItem>>({
total: 0,
list: []
});
const loadStateText = computed(() => {
if (isLoading.value) {
return "正在加载通知";
}
if (isNoMore.value) {
return pageResult.list.length ? "没有更多通知" : "";
}
return "上拉加载更多";
});
// ---------- 展示 ----------
function getSubject(item: NotifyManagementItem): string {
return item.subject?.trim() || "站内通知";
}
function getPreview(item: NotifyManagementItem): string {
const text = item.content?.replace(/\s+/g, " ").trim() || "无通知内容";
return 80 < text.length ? `${text.slice(0, 80)}...` : text;
}
function isScheduled(item: NotifyManagementItem): boolean {
return Boolean(item.sendAt && item.waitingCount && Time.now() < item.sendAt);
}
function getStatusText(item: NotifyManagementItem): string {
if (isScheduled(item)) {
return "待发送";
}
const total = item.recipientCount || 0;
if (0 < total && item.cancelCount === total) {
return "已取消";
}
if (item.failCount || item.timeoutCount) {
return "部分失败";
}
return "已发送";
}
function getStatusTheme(item: NotifyManagementItem): NotifyTagTheme {
if (isScheduled(item)) {
return "warning";
}
if (getStatusText(item) === "已取消") {
return "default";
}
if (getStatusText(item) === "部分失败") {
return "danger";
}
return "success";
}
function getRecipientSummary(item: NotifyManagementItem): string {
return `${item.recipientCount || 0} 位接收用户`;
}
function getSendTime(item: NotifyManagementItem): string {
if (isScheduled(item)) {
return `定时 ${Time.toDateTime(item.sendAt)}`;
}
return Time.toPassedDateTime(item.sendAt || item.createdAt) || "-";
}
function canCancel(item: NotifyManagementItem): boolean {
return canCancelNotify.value && isScheduled(item);
}
// ---------- 列表操作 ----------
async function loadMore(): Promise<void> {
if (isLoading.value || isNoMore.value || !canReadNotify.value) {
return;
}
isLoading.value = true;
try {
const result = await NotifyAPI.listManagement(page);
pageResult.total = result.total || 0;
pageResult.list.push(...(result.list || []));
page.index += 1;
isNoMore.value = pageResult.list.length >= pageResult.total || (result.list || []).length < page.size;
} catch (error) {
Message.error(getErrorMessage(error, "通知列表加载失败"));
} finally {
isLoading.value = false;
}
}
async function reloadList(): Promise<void> {
page.index = 0;
pageResult.total = 0;
pageResult.list = [];
isNoMore.value = false;
await loadMore();
}
async function refresh(): Promise<void> {
isRefreshing.value = true;
try {
await reloadList();
} finally {
isRefreshing.value = false;
}
}
async function toDetail(item: NotifyManagementItem): Promise<void> {
if (!item.id) {
return;
}
await router.push({
name: "NotifyManagementDetail",
params: { id: item.id }
});
}
async function toCreate(): Promise<void> {
await router.push({ name: "NotifyManagementCreate" });
}
async function confirmCancel(item: NotifyManagementItem): Promise<void> {
if (!item.id || !canCancel(item)) {
return;
}
const notifyId = item.id;
const dialog = DialogPlugin.confirm({
title: "取消定时通知",
content: "确定取消这条尚未发送的通知吗?",
cancelBtn: "返回",
confirmBtn: "取消发送",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
try {
await NotifyAPI.managementCancel(notifyId);
await reloadList();
Message.success("通知已取消");
} catch (error) {
Message.error(getErrorMessage(error, "通知取消失败"));
}
}
});
}
async function confirmDelete(item: NotifyManagementItem): Promise<void> {
if (!item.id || !canDeleteNotify.value) {
return;
}
const notifyId = item.id;
const dialog = DialogPlugin.confirm({
title: "删除通知",
content: "确定删除这条通知吗?删除后收件人也将无法查看。",
cancelBtn: "返回",
confirmBtn: "删除",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
try {
await NotifyAPI.managementDelete(notifyId);
await reloadList();
Message.success("通知已删除");
} catch (error) {
Message.error(getErrorMessage(error, "通知删除失败"));
}
}
});
}
// ---------- 生命周期 ----------
onMounted(async () => {
navBarStore.clearRight();
if (!canReadNotify.value) {
return;
}
if (canCreateNotify.value) {
navBarStore.setRightText("新建", "var(--td-brand-color)", toCreate);
}
await reloadList();
});
onUnmounted(() => navBarStore.clearRight());
</script>
<style scoped lang="less">
.notify-management {
gap: .75rem;
padding: calc(var(--app-nav-height) + 1rem) 1rem 1rem 1rem;
display: flex;
min-height: 100%;
align-items: center;
justify-content: center;
padding: calc(var(--app-nav-height) + 1rem) 0 1rem;
.items {
overflow: hidden;
.item {
.cancel,
.delete {
height: 100%;
padding: 0 1rem;
border-radius: 0;
}
:deep(.t-cell__title) {
flex: 1 1 0;
min-width: 0;
}
:deep(.t-cell__description) {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
:deep(.t-cell__note) {
white-space: nowrap;
}
.icon {
width: 3.5rem;
height: 3.5rem;
color: #64748b;
display: flex;
background: #e2e8f0;
align-items: center;
border-radius: .875rem;
justify-content: center;
&.scheduled {
color: #b45309;
background: #fef3c7;
}
}
.title,
.description {
display: flex;
align-items: center;
}
.title {
gap: .5rem;
> span:first-child {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.description {
gap: .75rem;
}
}
}
}
:global(html.theme-dark) {
.notify-management {
.icon {
color: #94a3b8;
background: #1e293b;
&.scheduled {
color: #fbbf24;
background: rgba(180, 83, 9, .2);
}
}
}
}
</style>
+383
View File
@@ -0,0 +1,383 @@
<template>
<div v-can="corePermissionCodes.notify.create" class="notify-management-create">
<t-empty v-if="!canCreate" description="暂无发送通知权限" />
<t-form v-else class="form" label-align="left" label-width="5rem">
<t-form-item label="通知标题" required-mark>
<t-input
v-model.trim="subject"
borderless
maxlength="100"
placeholder="请输入通知标题"
/>
</t-form-item>
<notify-recipient-scope-picker
v-if="isGlobalScope"
v-model="selectedStoreId"
:options="storeScopeOptions"
@update:model-value="handleStoreScopeChange"
/>
<notify-recipient-picker
v-model="selectedUserIdList"
:users="recipientList"
:self-user="selfRecipient"
:loading="isRecipientLoading"
/>
<t-form-item label="发送时间" arrow @click="openSchedulePicker">
<div class="schedule-value">
<span v-text="scheduleAtText || '立即发送'" />
<t-button
v-if="scheduleAtText"
size="extra-small"
content="立即发送"
@click.stop="clearSchedule"
/>
</div>
</t-form-item>
<section class="editor-wrapper">
<notify-editor
ref="notifyEditorRef"
class="editor"
v-model="draftDetail"
placeholder="请输入通知内容"
/>
</section>
</t-form>
</div>
<t-popup v-model="schedulePickerVisible" placement="bottom">
<t-date-time-picker
v-model="scheduleDraftText"
:start="minimumScheduleText"
:mode="['date', 'minute']"
format="YYYY-MM-DD HH:mm"
title="选择发送时间"
@cancel="schedulePickerVisible = false"
@confirm="confirmSchedule"
/>
</t-popup>
</template>
<script setup lang="ts">
import { NotifyEditor, type NotifyEditorExpose } from "timi-tdesign-mobile";
import { Time } from "timi-web";
import { type Attachment as ApiAttachment, type NotifyContentType, type NotifyDetail, useUserStore } from "timi-web-api";
import { Message } from "@/utils/message";
import { getErrorMessage } from "@/utils/error";
import GaoNotifyAPI from "@/api/GaoNotifyAPI";
import type { ManagedUser } from "@/types/ManagedUser";
import { corePermissionCodes, coreRoleCodes, gaoRoleCodes, permissionCodes } from "@/utils/permission";
import { useNavBarStore } from "@/store/navBarStore";
import { useCurrentStoreStore } from "@/store/currentStoreStore";
import NotifyRecipientScopePicker, { type NotifyRecipientScopeOption } from "@/pages/notify/component/NotifyRecipientScopePicker.vue";
import NotifyRecipientPicker from "@/pages/notify/component/NotifyRecipientPicker.vue";
const allStoreValue = "__ALL_STORES__";
defineOptions({
name: "NotifyManagementCreate"
});
const router = useRouter();
const navBarStore = useNavBarStore();
const userStore = useUserStore();
const currentStoreStore = useCurrentStoreStore();
const notifyEditorRef = ref<NotifyEditorExpose>();
const isGlobalScope = computed(() => userStore.hasRoles([coreRoleCodes.admin, gaoRoleCodes.globalManager], "or"));
const canReadRecipients = computed(() => userStore.hasPermissions([permissionCodes.user.read, permissionCodes.store.read], "or"));
const canCreate = computed(() => userStore.hasPermissions(corePermissionCodes.notify.create) && canReadRecipients.value);
// ---------- 通知表单 ----------
const subject = ref("");
const draftDetail = ref<NotifyDetail>({
contentType: "TIP_TAP",
content: "",
attachmentList: []
});
const selectedUserIdList = ref<string[]>([]);
const isSaving = ref(false);
const selectedStoreId = ref(allStoreValue);
// ---------- 接收用户 ----------
const isRecipientLoading = ref(false);
const recipientList = ref<ManagedUser[]>([]);
const loginUserId = computed(() => userStore.loginUser.user?.id || userStore.loginUser.id || "");
const selfRecipient = computed<ManagedUser | undefined>(() => {
if (!loginUserId.value) {
return undefined;
}
const user = userStore.loginUser.user;
return {
userId: loginUserId.value,
user: user ? {
id: user.id,
name: user.name,
nick: user.nick,
phoneNo: user.phoneNo
} : undefined
};
});
const storeScopeOptions = computed<NotifyRecipientScopeOption[]>(() => [
...(isGlobalScope.value ? [{ label: "所有门店", value: allStoreValue }] : []),
...currentStoreStore.storeList
.map(item => ({
label: item.name || item.code || "未命名门店",
value: item.id || ""
}))
.filter(item => item.value)
]);
// ---------- 定时发送 ----------
const schedulePickerVisible = ref(false);
const scheduleAtText = ref("");
const scheduleDraftText = ref("");
const minimumScheduleText = computed(() => Time.formatDateTime(new Date(Time.now() + Time.M)));
function getContentText(content: string): string {
const parser = new DOMParser();
return parser.parseFromString(content, "text/html").body.textContent?.trim() || "";
}
function hasMediaContent(content: string): boolean {
const parser = new DOMParser();
const doc = parser.parseFromString(content, "text/html");
return Boolean(doc.querySelector("img, video"));
}
function getScheduleTimestamp(): number | undefined {
if (!scheduleAtText.value) {
return undefined;
}
const timestamp = new Date(scheduleAtText.value.replace(" ", "T")).getTime();
return Number.isNaN(timestamp) ? undefined : timestamp;
}
function validateForm(): boolean {
if (!subject.value.trim()) {
Message.warning("请输入通知标题");
return false;
}
if (!selectedUserIdList.value.length) {
Message.warning("请选择至少一位接收用户");
return false;
}
const content = draftDetail.value.content || "";
if (!getContentText(content) && !hasMediaContent(content)) {
Message.warning("请输入通知内容");
return false;
}
const scheduleAt = getScheduleTimestamp();
if (scheduleAtText.value && (!scheduleAt || scheduleAt < Time.now())) {
Message.warning("定时发送时间必须晚于当前时间");
return false;
}
return true;
}
async function saveNotification(): Promise<void> {
if (isSaving.value || !validateForm()) {
return;
}
isSaving.value = true;
try {
const editorValue = await notifyEditorRef.value?.submit();
const detailValue = editorValue || draftDetail.value;
const sendAt = getScheduleTimestamp();
const contentType: NotifyContentType = detailValue.contentType === "TEXT" ? "TEXT" : "TIP_TAP";
const detailList: NotifyDetail[] = selectedUserIdList.value.map(sendTo => ({
msgType: "INTERNAL",
subject: subject.value.trim(),
content: detailValue.content || "",
contentType,
attachmentList: (detailValue.attachmentList || []) as unknown as ApiAttachment[],
sendTo,
...(sendAt ? { sendAt } : {})
}));
const result = await GaoNotifyAPI.create({ detailList });
Message.success(sendAt ? "定时通知已创建" : "通知已发送");
if (result.id) {
await router.replace({
name: "NotifyManagementDetail",
params: { id: result.id }
});
return;
}
await router.replace({ name: "NotifyManagement" });
} catch (error) {
Message.error(getErrorMessage(error, "通知保存失败"));
} finally {
isSaving.value = false;
}
}
// ---------- 定时发送 ----------
function openSchedulePicker(): void {
scheduleDraftText.value = scheduleAtText.value || Time.formatDateTime(new Date(Time.now() + Time.M));
schedulePickerVisible.value = true;
}
function confirmSchedule(): void {
const timestamp = new Date(scheduleDraftText.value.replace(" ", "T")).getTime();
if (Number.isNaN(timestamp) || timestamp < Time.now()) {
Message.warning("定时发送时间必须晚于当前时间");
return;
}
scheduleAtText.value = scheduleDraftText.value;
schedulePickerVisible.value = false;
}
function clearSchedule(): void {
scheduleAtText.value = "";
scheduleDraftText.value = "";
}
// ---------- 接收用户 ----------
async function loadRecipients(): Promise<void> {
if (isRecipientLoading.value) {
return;
}
isRecipientLoading.value = true;
try {
const pageSize = 100;
const list: ManagedUser[] = [];
let pageIndex = 0;
let total = 0;
const storeId = selectedStoreId.value && selectedStoreId.value !== allStoreValue
? selectedStoreId.value
: undefined;
while (true) {
const result = await GaoNotifyAPI.listRecipients(
{ index: pageIndex, size: pageSize },
storeId ? { storeId } : undefined
);
const currentList = result.list || [];
list.push(...currentList);
total = result.total || 0;
pageIndex += 1;
if (!currentList.length || list.length >= total || currentList.length < pageSize) {
break;
}
}
const userMap = new Map<string, ManagedUser>();
for (const item of list) {
const userId = item.userId || item.user?.id;
if (userId && userId !== loginUserId.value) {
userMap.set(userId, { ...item, userId });
}
}
recipientList.value = Array.from(userMap.values());
} catch (error) {
Message.error(getErrorMessage(error, "接收用户加载失败"));
} finally {
isRecipientLoading.value = false;
}
}
async function handleStoreScopeChange(): Promise<void> {
selectedUserIdList.value = [];
await loadRecipients();
}
// ---------- 生命周期 ----------
onMounted(async () => {
navBarStore.clearRight();
if (!canCreate.value) {
return;
}
if (isGlobalScope.value) {
await currentStoreStore.loadStoreList();
} else {
const loginStore = await currentStoreStore.loadLoginStore();
selectedStoreId.value = loginStore?.id || currentStoreStore.currentStoreId || "";
}
navBarStore.setRightText("发送", "var(--td-brand-color)", saveNotification);
await loadRecipients();
});
onUnmounted(() => navBarStore.clearRight());
</script>
<style scoped lang="less">
.notify-management-create {
min-height: 100%;
padding-top: var(--app-nav-height);
.form {
margin: 0;
display: grid;
.schedule-value {
gap: .5rem;
width: 100%;
display: flex;
align-items: center;
justify-content: flex-end;
}
.editor-wrapper {
width: 100%;
min-width: 0;
max-width: 100%;
overflow: visible;
.editor {
width: 100%;
min-width: 0;
max-width: 100%;
border: none;
border-radius: 0;
}
:deep(.tp-editor) {
width: 100%;
min-width: 0;
max-width: 100%;
height: auto;
overflow: visible;
}
:deep(.content) {
overflow: visible;
}
:deep(.toolbar) {
width: 100%;
min-width: 0;
max-width: 100%;
top: var(--app-nav-height);
z-index: 10;
position: sticky;
}
:deep(.toolbar .scroll) {
width: 100%;
min-width: 0;
max-width: 100%;
}
}
.tip {
color: #64748b;
margin: 0 1rem;
font-size: .8125rem;
line-height: 1.5;
}
}
}
:global(html.theme-dark) {
.notify-management-create {
.tip {
color: #94a3b8;
}
}
}
</style>
+416
View File
@@ -0,0 +1,416 @@
<template>
<div v-can="corePermissionCodes.notify.read" class="notify-management-detail">
<t-empty
v-if="!detail"
:description="isLoading ? '正在加载通知' : '通知不存在'"
/>
<template v-else>
<article class="article">
<header class="header">
<h1 v-text="getSubject(detail)" />
<div class="time" v-text="getSendTime(detail.detailList?.[0])" />
</header>
<section class="body">
<notify-view
v-if="mainDetail"
:detail="mainDetail"
empty-text="暂无通知内容"
/>
<div v-else class="empty-content">暂无通知内容</div>
</section>
</article>
<t-cell-group title="通知状态" theme="card">
<t-cell title="接收用户" :note="`${detail.detailList?.length || 0} 人`" />
<t-cell title="已读用户" :note="`${readCount} 人`" />
<t-cell title="当前状态">
<template #note>
<t-tag size="small" :theme="getSummaryTheme" :content="getSummaryStatus" />
</template>
</t-cell>
</t-cell-group>
<div v-if="canCancelNotify && canCancel" class="actions">
<t-button theme="primary" variant="outline" block :loading="isCanceling" @click="confirmCancel">
取消发送
</t-button>
</div>
<div v-if="canDeleteNotify" class="actions">
<t-button theme="danger" variant="outline" block :loading="isDeleting" @click="confirmDelete">
删除通知
</t-button>
</div>
<t-segmented
v-model="recipientFilter"
class="filter"
block
:options="recipientFilterOptions"
/>
<t-cell-group v-if="filteredDetailList.length" title="发送用户" theme="card" class="recipients">
<t-cell v-for="item in filteredDetailList" :key="item.id">
<template #title>
<div class="recipient-title">
<span v-text="getRecipientName(item)" />
<t-tag size="small" :theme="getReadTheme(item)" :content="getReadText(item)" />
</div>
</template>
<template #description>
<div class="recipient-description">
<span v-text="getRecipientContact(item)" />
<span v-if="item.resultDesc" v-text="item.resultDesc" />
</div>
</template>
<template #note>
<span v-text="getReadTime(item)" />
</template>
</t-cell>
</t-cell-group>
<t-empty v-else description="暂无匹配的接收用户" />
</template>
</div>
</template>
<script setup lang="ts">
import { NotifyView } from "timi-tdesign-mobile";
import { Time } from "timi-web";
import { NotifyAPI, type Notify, type NotifyDetail, useUserStore } from "timi-web-api";
import { DialogPlugin } from "tdesign-mobile-vue";
import { Message } from "@/utils/message";
import { getErrorMessage } from "@/utils/error";
import { useNavBarStore } from "@/store/navBarStore";
import { corePermissionCodes } from "@/utils/permission";
type RecipientFilter = "all" | "unread" | "read";
type NotifyTagTheme = "default" | "success" | "warning" | "primary" | "danger";
defineOptions({
name: "NotifyManagementDetail"
});
const route = useRoute();
const router = useRouter();
const navBarStore = useNavBarStore();
const userStore = useUserStore();
const canDeleteNotify = computed(() => userStore.hasPermissions(corePermissionCodes.notify.delete));
const canCancelNotify = computed(() => userStore.hasPermissions(corePermissionCodes.notify.cancel));
// ---------- 通知数据 ----------
const detail = ref<Notify>();
const isLoading = ref(false);
const isCanceling = ref(false);
const isDeleting = ref(false);
const recipientFilter = ref<RecipientFilter>("all");
let loadToken = 0;
const recipientFilterOptions = [
{ value: "all" as RecipientFilter, label: "全部" },
{ value: "unread" as RecipientFilter, label: "未读" },
{ value: "read" as RecipientFilter, label: "已读" }
];
const mainDetail = computed(() => detail.value?.detailList?.[0]);
const readCount = computed(() => detail.value?.detailList?.filter(item => Boolean(item.readAt)).length || 0);
const filteredDetailList = computed(() => {
const detailList = detail.value?.detailList || [];
if (recipientFilter.value === "unread") {
return detailList.filter(item => !item.readAt);
}
if (recipientFilter.value === "read") {
return detailList.filter(item => Boolean(item.readAt));
}
return detailList;
});
const canCancel = computed(() => detail.value?.detailList?.some(item => isCancelable(item)) || false);
const getSummaryStatus = computed(() => {
const detailList = detail.value?.detailList || [];
if (canCancel.value) {
return "待发送";
}
if (detailList.length && detailList.every(item => item.status === "CANCEL")) {
return "已取消";
}
if (detailList.some(item => item.status === "FAIL" || item.status === "TIMEOUT")) {
return "部分失败";
}
return "已发送";
});
const getSummaryTheme = computed<NotifyTagTheme>(() => {
if (getSummaryStatus.value === "待发送") {
return "warning";
}
if (getSummaryStatus.value === "已取消") {
return "default";
}
if (getSummaryStatus.value === "部分失败") {
return "danger";
}
return "success";
});
async function loadDetail(): Promise<void> {
const requestToken = loadToken + 1;
loadToken = requestToken;
detail.value = undefined;
navBarStore.clearRight();
const notifyId = String(route.params.id || "");
if (!notifyId) {
return;
}
isLoading.value = true;
try {
const result = await NotifyAPI.managementDetail(notifyId);
if (requestToken !== loadToken) {
return;
}
detail.value = result;
setRightAction();
} catch (error) {
Message.error(getErrorMessage(error, "通知详情加载失败"));
} finally {
if (requestToken === loadToken) {
isLoading.value = false;
}
}
}
function setRightAction(): void {
if (detail.value?.id && canDeleteNotify.value) {
navBarStore.setRightText("删除", "var(--td-error-color)", confirmDelete);
}
}
// ---------- 通知操作 ----------
function isCancelable(item: NotifyDetail): boolean {
return item.status === "WAITING" && Boolean(item.sendAt && Time.now() < item.sendAt);
}
async function confirmCancel(): Promise<void> {
const notifyId = detail.value?.id;
if (!notifyId || !canCancelNotify.value || !canCancel.value || isCanceling.value) {
return;
}
const dialog = DialogPlugin.confirm({
title: "取消定时通知",
content: "确定取消这条尚未发送的通知吗?",
cancelBtn: "返回",
confirmBtn: "取消发送",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
isCanceling.value = true;
try {
await NotifyAPI.managementCancel(notifyId);
await loadDetail();
Message.success("通知已取消");
} catch (error) {
Message.error(getErrorMessage(error, "通知取消失败"));
} finally {
isCanceling.value = false;
}
}
});
}
async function confirmDelete(): Promise<void> {
const notifyId = detail.value?.id;
if (!notifyId || !canDeleteNotify.value || isDeleting.value) {
return;
}
const dialog = DialogPlugin.confirm({
title: "删除通知",
content: "确定删除这条通知吗?删除后收件人也将无法查看。",
cancelBtn: "返回",
confirmBtn: "删除",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
isDeleting.value = true;
navBarStore.clearRight();
try {
await NotifyAPI.managementDelete(notifyId);
Message.success("通知已删除");
await router.replace({ name: "NotifyManagement" });
} catch (error) {
Message.error(getErrorMessage(error, "通知删除失败"));
setRightAction();
} finally {
isDeleting.value = false;
}
}
});
}
// ---------- 展示 ----------
function getSubject(item: Notify): string {
return item.detailList?.[0]?.subject?.trim() || "站内通知";
}
function getSendTime(item?: NotifyDetail): string {
if (!item?.sendAt) {
return "-";
}
if (isCancelable(item)) {
return `定时 ${Time.toDateTime(item.sendAt)}`;
}
return Time.toDateTime(item.sendAt);
}
function getRecipientName(item: NotifyDetail): string {
return item.toUser?.nick?.trim() || item.toUser?.name?.trim() || item.sendTo || "未知用户";
}
function getRecipientContact(item: NotifyDetail): string {
return item.toUser?.phoneNo || item.sendTo || "-";
}
function getReadText(item: NotifyDetail): string {
if (isCancelable(item)) {
return "待发送";
}
if (item.status === "CANCEL") {
return "已取消";
}
if (item.status === "FAIL" || item.status === "TIMEOUT") {
return item.status === "TIMEOUT" ? "已超时" : "发送失败";
}
return item.readAt ? "已读" : "未读";
}
function getReadTheme(item: NotifyDetail): NotifyTagTheme {
if (isCancelable(item)) {
return "warning";
}
if (item.status === "CANCEL" || item.status === "FAIL" || item.status === "TIMEOUT") {
return "danger";
}
return item.readAt ? "primary" : "default";
}
function getReadTime(item: NotifyDetail): string {
return item.readAt ? `已读 ${Time.toDateTime(item.readAt)}` : "未读";
}
// ---------- 生命周期 ----------
watch(() => route.params.id, async () => {
await loadDetail();
}, { immediate: true });
onMounted(() => navBarStore.clearRight());
onUnmounted(() => navBarStore.clearRight());
</script>
<style scoped lang="less">
.notify-management-detail {
gap: 1rem;
min-height: 100%;
padding: calc(var(--app-nav-height) + 1rem) 0 1rem;
display: flex;
flex-direction: column;
.article {
padding: 1.25rem 1rem;
margin: 0 1rem;
background: var(--td-bg-color-container);
box-shadow: var(--td-shadow-1);
border-radius: 1.4rem;
}
.header {
text-align: center;
padding-bottom: 1rem;
border-bottom: 1px solid rgba(148, 163, 184, .2);
h1 {
margin: 0;
font-size: 1.25rem;
line-height: 1.5;
}
.time {
color: #64748b;
margin-top: .5rem;
font-size: .8125rem;
}
}
.body {
color: #334155;
padding-top: 1rem;
font-size: .9375rem;
line-height: 1.8;
word-break: break-word;
.empty-content {
color: #64748b;
text-align: center;
}
}
.actions {
padding: 0 1rem;
}
.filter {
width: calc(100% - 2rem);
margin: 0 1rem;
}
.recipients {
:deep(.t-cell__title) {
flex: 1 1 0;
min-width: 0;
}
:deep(.t-cell__description) {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.recipient-title,
.recipient-description {
display: flex;
align-items: center;
}
.recipient-title {
gap: .5rem;
> span:first-child {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.recipient-description {
gap: .75rem;
}
}
}
:global(html.theme-dark) {
.notify-management-detail {
.header {
.time {
color: #94a3b8;
}
}
.body {
color: #cbd5e1;
.empty-content {
color: #94a3b8;
}
}
}
}
</style>
@@ -0,0 +1,251 @@
<template>
<t-form-item
label="接收用户"
required-mark
:arrow="true"
@click="openPicker"
>
<div class="selected-text" v-text="selectedText" />
</t-form-item>
<t-popup v-model="visible" placement="bottom" destroy-on-close>
<div class="recipient-picker">
<header class="picker-header">
<t-button variant="text" size="small" @click="cancelPicker">取消</t-button>
<h3>选择接收用户</h3>
<t-button theme="primary" variant="text" size="small" @click="confirmPicker">确定</t-button>
</header>
<div class="search-wrapper">
<t-input
v-model.trim="keyword"
class="search"
borderless
clearable
placeholder="搜索姓名、昵称或手机号"
/>
</div>
<div class="selection-actions">
<span class="selection-count" v-text="`已选择 ${selectedUserIdList.length} 人`" />
<t-button
variant="text"
size="small"
:disabled="!filteredIdList.length"
@click="toggleSelectAll"
>
<span v-text="allFilteredSelected ? '取消全选' : '全选当前结果'" />
</t-button>
</div>
<div class="recipient-list">
<t-checkbox-group
v-if="filteredList.length"
v-model="selectedUserIdList"
class="recipient-options"
>
<t-checkbox
v-for="item in filteredList"
:key="item.userId"
:value="item.userId"
class="recipient-option"
borderless
>
<div class="recipient-info">
<span class="recipient-name" v-text="getRecipientName(item)" />
<span class="recipient-phone" v-text="item.user?.phoneNo || item.userId" />
</div>
</t-checkbox>
</t-checkbox-group>
<t-empty
v-else
:description="loading ? '正在加载用户' : '暂无匹配用户'"
/>
</div>
</div>
</t-popup>
</template>
<script setup lang="ts">
import type { ManagedUser } from "@/types/ManagedUser";
const props = withDefaults(defineProps<{
modelValue: string[];
users: ManagedUser[];
selfUser?: ManagedUser;
loading?: boolean;
}>(), {
loading: false
});
const emit = defineEmits<{
"update:modelValue": [value: string[]];
}>();
const visible = ref(false);
const keyword = ref("");
const draftSelectedUserIdList = ref<string[]>([]);
const selectedUserIdList = computed({
get: () => props.modelValue,
set: value => emit("update:modelValue", value)
});
const availableList = computed(() => {
const selfUserId = props.selfUser?.userId;
const userList = selfUserId
? props.users.filter(item => item.userId !== selfUserId)
: props.users;
return props.selfUser ? [props.selfUser, ...userList] : userList;
});
const filteredList = computed(() => {
const normalizedKeyword = keyword.value.toLowerCase();
if (!normalizedKeyword) {
return availableList.value;
}
return availableList.value.filter(item => {
const user = item.user;
return [item.userId, user?.name, user?.nick, user?.phoneNo]
.some(value => Boolean(value?.toLowerCase().includes(normalizedKeyword)));
});
});
const filteredIdList = computed(() => filteredList.value
.map(item => item.userId)
.filter((item): item is string => Boolean(item)));
const allFilteredSelected = computed(() => 0 < filteredIdList.value.length
&& filteredIdList.value.every(id => selectedUserIdList.value.includes(id)));
const selectedText = computed(() => selectedUserIdList.value.length
? `已选择 ${selectedUserIdList.value.length} 位用户`
: "请选择接收用户");
function getRecipientName(item: ManagedUser): string {
if (item.userId === props.selfUser?.userId) {
return `自己(${item.user?.nick || item.user?.name || "当前账号"})`;
}
return item.user?.nick || item.user?.name || item.userId || "未命名用户";
}
function openPicker(): void {
draftSelectedUserIdList.value = [...selectedUserIdList.value];
keyword.value = "";
visible.value = true;
}
function cancelPicker(): void {
selectedUserIdList.value = [...draftSelectedUserIdList.value];
visible.value = false;
}
function confirmPicker(): void {
draftSelectedUserIdList.value = [...selectedUserIdList.value];
visible.value = false;
}
function toggleSelectAll(): void {
const filteredIdSet = new Set(filteredIdList.value);
if (allFilteredSelected.value) {
selectedUserIdList.value = selectedUserIdList.value.filter(id => !filteredIdSet.has(id));
return;
}
selectedUserIdList.value = Array.from(new Set([
...selectedUserIdList.value,
...filteredIdList.value
]));
}
</script>
<style scoped lang="less">
.selected-text {
color: #52607a;
width: 100%;
overflow: hidden;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
}
.recipient-picker {
display: flex;
height: 80vh;
padding-top: 1rem;
background: var(--td-bg-color-container);
flex-direction: column;
.picker-header {
padding: 0 1rem;
display: grid;
grid-template-columns: 4rem 1fr 4rem;
align-items: center;
h3 {
margin: 0;
text-align: center;
}
}
.search-wrapper {
padding: 1rem 0 .5rem;
box-shadow: 0 4px 4px rgba(0, 0, 0, .05);
.search {
margin: 0 1rem;
border: 1px solid var(--td-component-border);
border-radius: .875rem;
}
}
.selection-actions {
padding: .25rem 1rem;
display: flex;
align-items: center;
justify-content: space-between;
.selection-count {
color: #64748b;
font-size: .8125rem;
}
}
.recipient-list {
flex: 1;
overflow: auto;
min-height: 0;
padding: 0 1rem env(safe-area-inset-bottom, 0);
.recipient-options {
display: grid;
.recipient-option {
padding: .75rem 0;
border-bottom: 1px solid var(--td-component-border);
.recipient-info {
gap: .25rem;
display: flex;
flex-direction: column;
}
.recipient-name {
font-size: .9375rem;
}
.recipient-phone {
color: #64748b;
font-size: .75rem;
}
}
}
}
}
:global(html.theme-dark) {
.selected-text {
color: #94a3b8;
}
.selection-count,
.recipient-phone {
color: #94a3b8;
}
}
</style>
@@ -0,0 +1,98 @@
<template>
<t-form-item label="用户范围" arrow @click="openPicker">
<div class="scope-text" v-text="selectedLabel" />
</t-form-item>
<t-popup v-model="visible" placement="bottom" destroy-on-close>
<div class="scope-picker">
<header class="picker-header">
<t-button variant="text" size="small" @click="cancelPicker">取消</t-button>
<h3>选择用户范围</h3>
<t-button theme="primary" variant="text" size="small" @click="confirmPicker">确定</t-button>
</header>
<t-radio-group v-model="draftValue" class="scope-options">
<t-radio
v-for="item in options"
:key="item.value"
:block="false"
borderless
:value="item.value"
>
<span v-text="item.label" />
</t-radio>
</t-radio-group>
</div>
</t-popup>
</template>
<script setup lang="ts">
export type NotifyRecipientScopeOption = {
label: string;
value: string;
};
const props = defineProps<{
modelValue: string;
options: NotifyRecipientScopeOption[];
}>();
const emit = defineEmits<{
"update:modelValue": [value: string];
}>();
const visible = ref(false);
const draftValue = ref(props.modelValue);
const selectedLabel = computed(() => props.options.find(item => item.value === props.modelValue)?.label || "请选择用户范围");
function openPicker(): void {
draftValue.value = props.modelValue;
visible.value = true;
}
function cancelPicker(): void {
visible.value = false;
}
function confirmPicker(): void {
emit("update:modelValue", draftValue.value);
visible.value = false;
}
</script>
<style scoped lang="less">
.scope-text {
color: #52607a;
width: 100%;
overflow: hidden;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
}
.scope-picker {
padding: 1rem 1rem env(safe-area-inset-bottom, 0);
background: var(--td-bg-color-container);
.picker-header {
display: grid;
grid-template-columns: 4rem 1fr 4rem;
align-items: center;
h3 {
margin: 0;
text-align: center;
}
}
.scope-options {
gap: .5rem;
margin-top: 1rem;
display: grid;
}
}
:global(html.theme-dark) {
.scope-text {
color: #94a3b8;
}
}
</style>