352 lines
8.3 KiB
Vue
352 lines
8.3 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="article?.id && canUpdate && !isDeleting"
|
||
@click="toEdit"
|
||
theme="primary"
|
||
variant="text"
|
||
size="small"
|
||
content="编辑"
|
||
/>
|
||
</template>
|
||
</t-navbar>
|
||
<main class="tui-page-content scroll column">
|
||
<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" />
|
||
<time-label
|
||
class="time gray"
|
||
v-if="article.createdAt"
|
||
:timestamp="article.createdAt"
|
||
underline
|
||
/>
|
||
</header>
|
||
<div v-if="additionalPhotoItemList.length" class="additional-photos-wrapper" v-entrance-transition>
|
||
<attachment-selector :items="additionalPhotoItemList" readonly />
|
||
</div>
|
||
<section class="content" v-entrance-transition>
|
||
<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>
|
||
<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>
|
||
</main>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ArticleView } from "timi-tdesign-mobile";
|
||
import { DialogPlugin } from "tdesign-mobile-vue";
|
||
import { TimeLabel } from "timi-web";
|
||
import { UserAPI, type Attachment, useUserStore } from "timi-web-api";
|
||
import StoreDiaryAPI from "@/api/StoreDiaryAPI";
|
||
import AttachmentSelector from "@/components/attachmentSelector/AttachmentSelector.vue";
|
||
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
||
import type { PhotoItem } from "@/types/Photo";
|
||
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";
|
||
|
||
|
||
|
||
|
||
|
||
import type { StoreDiary } from "@/types/StoreDiary";
|
||
import { useExpandToMaster, usePane } from "timi-tdesign-mobile";
|
||
|
||
defineOptions({
|
||
name: "StoreDiaryDetail"
|
||
});
|
||
|
||
const props = withDefaults(defineProps<{
|
||
/** 指定日记 ID 时忽略路由参数,用于平板主从分栏的内联渲染。 */
|
||
diaryId?: string;
|
||
}>(), {
|
||
diaryId: ""
|
||
});
|
||
|
||
const emit = defineEmits<{
|
||
/** 内联在右栏时请求切换到编辑视图 */
|
||
edit: [];
|
||
}>();
|
||
|
||
const route = useRoute();
|
||
const router = useRouter();
|
||
const userStore = useUserStore();
|
||
|
||
function resolveId(): string {
|
||
return props.diaryId || String(route.params.id || "");
|
||
}
|
||
|
||
// 竖屏整页进来后转成横屏:回到列表页并恢复右栏选中,而不是继续停在整页详情
|
||
useExpandToMaster({
|
||
path: "/management/store-diaries",
|
||
id: () => resolveId()
|
||
});
|
||
/** 非空说明当前被内联在主从分栏的右栏里 */
|
||
const pane = usePane();
|
||
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;
|
||
|
||
const additionalPhotoItemList = computed<PhotoItem[]>(() => (article.value?.attachmentList || [])
|
||
.filter(item => item.attachType === "ADDITIONAL" && !!item.id)
|
||
.map(item => ({
|
||
attachment: item as Partial<Attachment>,
|
||
attachType: "ADDITIONAL",
|
||
previewUrl: getAttachmentReadURL(item.id || ""),
|
||
url: getAttachmentReadURL(item.id || "", 90)
|
||
})));
|
||
|
||
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();
|
||
}
|
||
|
||
|
||
async function loadArticle(): Promise<void> {
|
||
const requestToken = loadToken + 1;
|
||
loadToken = requestToken;
|
||
article.value = undefined;
|
||
const id = resolveId();
|
||
if (!id) {
|
||
return;
|
||
}
|
||
|
||
isLoading.value = true;
|
||
try {
|
||
await prepareStoreScope();
|
||
const result = await StoreDiaryAPI.detail(id);
|
||
if (requestToken !== loadToken) {
|
||
return;
|
||
}
|
||
article.value = result;
|
||
} 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;
|
||
}
|
||
|
||
// 分栏时右栏直接切到编辑视图,不跳路由,列表上下文保留
|
||
if (pane) {
|
||
emit("edit");
|
||
return;
|
||
}
|
||
|
||
await router.push({
|
||
name: "StoreDiaryEdit",
|
||
params: { id: article.value.id }
|
||
});
|
||
}
|
||
|
||
function goBack(): void {
|
||
if (pane) {
|
||
pane.close();
|
||
return;
|
||
}
|
||
|
||
void router.back();
|
||
}
|
||
|
||
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;
|
||
try {
|
||
await StoreDiaryAPI.remove(diaryId);
|
||
Message.success("门店日记已删除");
|
||
if (pane) {
|
||
pane.close();
|
||
return;
|
||
}
|
||
await router.replace({ name: "StoreDiaryList" });
|
||
} catch (error) {
|
||
Message.error(getErrorMessage(error, "门店日记删除失败"));
|
||
} finally {
|
||
isDeleting.value = false;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
onMounted(loadArticle);
|
||
watch(() => [props.diaryId, route.params.id], () => {
|
||
void loadArticle();
|
||
});
|
||
|
||
</script>
|
||
|
||
<style scoped lang="less">
|
||
.store-diary-detail {
|
||
padding: 0 0 calc(1rem + var(--tui-safe-bottom, 0px));
|
||
|
||
.header {
|
||
margin: 0 1rem;
|
||
padding: 1.25rem 0;
|
||
border-bottom: 1px solid rgba(148, 163, 184, .2);
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
.additional-photos-wrapper {
|
||
margin: 1rem 1rem 0 1rem;
|
||
border-bottom: 1px solid rgba(148, 163, 184, .2);
|
||
padding-bottom: 1rem;
|
||
}
|
||
|
||
.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>
|