add StoreDiary

This commit is contained in:
Timi
2026-08-31 17:32:01 +08:00
parent 95a400cd93
commit 1e83fdf883
9 changed files with 1288 additions and 1 deletions
+268
View File
@@ -0,0 +1,268 @@
<template>
<div v-can="permissionCodes.storeDiary.read" class="store-diary-detail">
<template v-if="article">
<header class="header" v-entrance-transition>
<h1 v-text="article.title || '未命名日记'" />
<div v-if="article.description" class="description" v-text="article.description" />
<pass-time-label
class="time gray"
v-if="article.createdAt"
:timestamp="article.createdAt"
underline
/>
</header>
<t-divider />
<section class="content" v-entrance-transition="70">
<article-view
:article="article"
empty-text="暂无日记正文"
:resolve-attachment-url="resolveAttachmentUrl"
/>
</section>
<footer class="footer gray">
<div class="updater">
<div>最近更新人:</div>
<div v-if="article.updater" class="updater">
<t-image
class="avatar"
:src="getUpdaterAvatar(article.updater)"
:alt="getUpdaterName(article.updater)"
fit="cover"
/>
<span v-text="getUpdaterName(article.updater)" />
</div>
<span v-else v-text="'未知用户'" />
</div>
<div class="updated-at">
<span>最近更新时间:</span>
<pass-time-label v-if="article.updatedAt" :timestamp="article.updatedAt" />
<span v-else v-text="'未知时间'" />
</div>
</footer>
<t-button
v-if="canDelete"
class="delete cell-button"
theme="danger"
block
:loading="isDeleting"
@click="confirmDelete"
>
删除日记
</t-button>
</template>
<t-empty v-else :description="isLoading ? '正在加载门店日记' : '门店日记不存在'" />
</div>
</template>
<script setup lang="ts">
import { ArticleView } from "timi-tdesign-mobile";
import { DialogPlugin } from "tdesign-mobile-vue";
import { PassTimeLabel } from "timi-web";
import { UserAPI, useUserStore } from "timi-web-api";
import StoreDiaryAPI from "@/api/StoreDiaryAPI";
import { useCurrentStoreStore } from "@/store/currentStoreStore";
import { useNavBarStore } from "@/store/navBarStore";
import type { StoreDiary } from "@/types/StoreDiary";
import { getAttachmentReadURL } from "@/utils/attachment";
import { buildFallbackAvatar } from "@/utils/avatar";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { permissionCodes } from "@/utils/permission";
defineOptions({
name: "StoreDiaryDetail"
});
const route = useRoute();
const router = useRouter();
const navBarStore = useNavBarStore();
const userStore = useUserStore();
const currentStoreStore = useCurrentStoreStore();
const canUpdate = computed(() => userStore.hasPermissions(permissionCodes.storeDiary.update));
const canDelete = computed(() => userStore.hasPermissions(permissionCodes.storeDiary.delete));
// ---------- 日记数据 ----------
const article = ref<StoreDiary>();
const isLoading = ref(false);
const isDeleting = ref(false);
let loadToken = 0;
function resolveAttachmentUrl(attachmentId: string): string {
return getAttachmentReadURL(attachmentId);
}
function getUpdaterName(item: NonNullable<StoreDiary["updater"]>): string {
return item.nick?.trim() || item.name?.trim() || "未知用户";
}
function getUpdaterAvatar(item: NonNullable<StoreDiary["updater"]>): string {
return UserAPI.getAvatarURL(item) || buildFallbackAvatar(getUpdaterName(item));
}
async function prepareStoreScope(): Promise<void> {
await currentStoreStore.loadLoginStore();
}
function setRightAction(): void {
if (article.value?.id && canUpdate.value) {
navBarStore.setRightText("编辑", "var(--td-brand-color)", toEdit);
return;
}
navBarStore.clearRight();
}
async function loadArticle(): Promise<void> {
const requestToken = loadToken + 1;
loadToken = requestToken;
article.value = undefined;
navBarStore.clearRight();
const id = String(route.params.id || "");
if (!id) {
return;
}
isLoading.value = true;
try {
await prepareStoreScope();
const result = await StoreDiaryAPI.detail(id);
if (requestToken !== loadToken) {
return;
}
article.value = result;
setRightAction();
} catch (error) {
Message.error(getErrorMessage(error, "门店日记加载失败"));
} finally {
if (requestToken === loadToken) {
isLoading.value = false;
}
}
}
// ---------- 操作 ----------
async function toEdit(): Promise<void> {
if (!article.value?.id || !canUpdate.value) {
return;
}
await router.push({
name: "StoreDiaryEdit",
params: { id: article.value.id }
});
}
async function confirmDelete(): Promise<void> {
if (!article.value?.id || !canDelete.value || isDeleting.value) {
return;
}
const diaryId = article.value.id;
const dialog = DialogPlugin.confirm({
title: "删除门店日记",
content: `确定删除“${article.value.title || "未命名日记"}”吗?删除后无法恢复。`,
cancelBtn: "取消",
confirmBtn: "删除",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
isDeleting.value = true;
navBarStore.clearRight();
try {
await StoreDiaryAPI.remove(diaryId);
Message.success("门店日记已删除");
await router.replace({ name: "StoreDiaryList" });
} catch (error) {
Message.error(getErrorMessage(error, "门店日记删除失败"));
setRightAction();
} finally {
isDeleting.value = false;
}
}
});
}
onMounted(loadArticle);
onUnmounted(() => navBarStore.clearRight());
</script>
<style scoped lang="less">
.store-diary-detail {
padding: var(--app-nav-height) 0 calc(1rem + env(safe-area-inset-bottom, 0));
.header {
padding: 1.25rem 1rem;
h1 {
margin: 0;
font-size: 1.35rem;
text-align: center;
line-height: 1.5;
word-break: break-word;
}
.time {
display: block;
margin-top: 1rem;
font-size: .875rem;
text-align: center;
}
.description {
color: var(--td-text-color-secondary);
font-size: .875rem;
margin-top: .75rem;
word-break: break-word;
line-height: 1.6;
white-space: pre-wrap;
}
}
.content {
padding: 1rem;
:deep(.content) {
border: none;
padding: 0;
}
}
.footer {
padding: 0 1rem;
font-size: .875rem;
.updater {
gap: .5rem;
display: flex;
align-items: center;
justify-content: center;
.avatar {
width: 1.5rem;
height: 1.5rem;
border-radius: 50%;
}
}
.updated-at {
text-align: center;
}
}
.delete {
margin-top: 3rem;
}
}
:global(html.theme-dark) {
.store-diary-detail {
.meta,
.description {
color: #94a3b8;
}
}
}
</style>