Files
gao-customer-h5/src/pages/notify/UserNotifyDetail.vue
T

207 lines
4.5 KiB
Vue

<template>
<div class="tui-page-layout tui-navbar">
<t-navbar
fixed
:z-index="20"
title="通知详情"
left-arrow
@left-click="goBack"
>
<template #right>
<t-button
v-if="detail?.id"
@click="confirmDelete"
theme="danger"
variant="text"
size="small"
content="删除"
/>
</template>
</t-navbar>
<main class="tui-page-content scroll column bg-white">
<div v-can="corePermissionCodes.notify.read" class="notify-detail">
<notify-content
v-if="detail"
:detail="detail"
:sender="getSenderName(detail)"
:timestamp="detail.sendAt"
:resolve-attachment-url="resolveAttachmentUrl"
/>
<t-empty
v-else
:description="isLoading ? '正在加载通知' : '通知不存在'"
/>
</div>
</main>
</div>
</template>
<script setup lang="ts">
import { NotifyAPI, type NotifyDetail } from "timi-web-api";
import { DialogPlugin } from "tdesign-mobile-vue";
import { getAttachmentReadURL } from "@/utils/attachment";
import { corePermissionCodes } from "@/utils/permission";
import { Message } from "@/utils/message.ts";
import { useExpandToMaster, usePane } from "timi-tdesign-mobile";
import NotifyContent from "@/pages/notify/component/NotifyContent.vue";
import { getErrorMessage } from "@/utils/error";
const props = withDefaults(defineProps<{
/** 指定通知 ID 时忽略路由参数,用于平板主从分栏的内联渲染。 */
notifyId?: string;
}>(), {
notifyId: ""
});
const emit = defineEmits<{
read: [];
}>();
const route = useRoute();
const router = useRouter();
function resolveId(): string {
return props.notifyId || String(route.params.id || "");
}
// 竖屏整页进来后转成横屏:回到列表页并恢复右栏选中,而不是继续停在整页详情
useExpandToMaster({
path: "/notify",
id: () => resolveId()
});
/** 非空说明当前被内联在主从分栏的右栏里 */
const pane = usePane();
function goBack(): void {
if (pane) {
pane.close();
return;
}
void router.back();
}
defineOptions({
name: "UserNotifyDetail"
});
// ---------- 通知数据 ----------
const detail = ref<NotifyDetail>();
const isLoading = ref(false);
const isDeleting = ref(false);
let loadToken = 0;
async function loadDetail(): Promise<void> {
const requestToken = loadToken + 1;
loadToken = requestToken;
const detailId = resolveId();
if (!detailId) {
detail.value = undefined;
return;
}
detail.value = undefined;
isLoading.value = true;
try {
const result = await NotifyAPI.internalDetail(detailId);
if (requestToken !== loadToken) {
return;
}
detail.value = result;
await markRead(result, requestToken);
} catch (error) {
console.warn("通知详情加载失败", error);
Message.error("通知详情加载失败");
} finally {
if (requestToken === loadToken) {
isLoading.value = false;
}
}
}
async function confirmDelete(): Promise<void> {
const detailId = detail.value?.id;
if (!detailId || isDeleting.value) {
return;
}
const dialog = DialogPlugin.confirm({
title: "删除通知",
content: "确定删除这条通知吗?删除后将不再显示。",
cancelBtn: "取消",
confirmBtn: "删除",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
await deleteDetail(detailId);
}
});
}
async function deleteDetail(detailId: string): Promise<void> {
if (isDeleting.value) {
return;
}
isDeleting.value = true;
try {
await NotifyAPI.deleteInternal(detailId);
Message.success("通知已删除");
if (pane) {
pane.close();
return;
}
await router.replace({ name: "UserNotifyList" });
} catch (error) {
Message.error(getErrorMessage(error, "通知删除失败"));
} finally {
isDeleting.value = false;
}
}
async function markRead(item: NotifyDetail, requestToken: number): Promise<void> {
if (!item.id || item.readAt) {
return;
}
try {
await NotifyAPI.markInternalRead(item.id);
emit("read");
if (requestToken === loadToken && detail.value) {
detail.value = {
...detail.value,
readAt: Date.now()
};
}
} catch (error) {
console.warn("通知标记已读失败", error);
}
}
// ---------- 展示 ----------
function getSenderName(item: NotifyDetail): string {
return item.fromUser?.nick?.trim() || item.fromUser?.name?.trim() || "系统通知";
}
function resolveAttachmentUrl(attachmentId: string): string {
return getAttachmentReadURL(attachmentId);
}
// ---------- 生命周期 ----------
watch(() => [props.notifyId, route.params.id], async () => {
await loadDetail();
}, { immediate: true });
</script>