add StoreDiary
This commit is contained in:
@@ -0,0 +1,505 @@
|
||||
<template>
|
||||
<div v-can="permissionCodes.storeDiary.read" class="store-diary-list">
|
||||
<t-pull-down-refresh
|
||||
class="list-wrapper"
|
||||
v-model="isRefreshing"
|
||||
:refresh-timeout="30000"
|
||||
@refresh="refresh"
|
||||
@scrolltolower="loadMore"
|
||||
>
|
||||
<section class="calendar" v-entrance-transition>
|
||||
<t-calendar
|
||||
:key="calendarRenderKey"
|
||||
class="calendar-view"
|
||||
:value="selectedCalendarDate"
|
||||
:format="calendarFormat"
|
||||
switch-mode="month"
|
||||
:use-popup="false"
|
||||
@panel-change="handleCalendarPanelChange"
|
||||
@select="selectCalendarDate"
|
||||
/>
|
||||
</section>
|
||||
<t-cell-group v-if="pageResult.list.length" class="items fix-last-cell-line" theme="card">
|
||||
<t-swipe-cell
|
||||
v-for="(item, index) in pageResult.list"
|
||||
:key="item.id || index"
|
||||
class="item"
|
||||
v-entrance-transition="index * 30"
|
||||
>
|
||||
<t-cell
|
||||
class="cell"
|
||||
:title="getTitle(item)"
|
||||
:description="getDescription(item)"
|
||||
arrow
|
||||
@click="toDetail(item)"
|
||||
>
|
||||
<template #note>
|
||||
<pass-time-label :timestamp="item.updatedAt || item.createdAt" underline />
|
||||
</template>
|
||||
</t-cell>
|
||||
<template v-if="canDelete" #right>
|
||||
<t-button
|
||||
class="swipe-cell-button"
|
||||
theme="danger"
|
||||
:loading="isDeleting && deletingId === item.id"
|
||||
@click.stop="confirmDelete(item)"
|
||||
>
|
||||
删除
|
||||
</t-button>
|
||||
</template>
|
||||
</t-swipe-cell>
|
||||
</t-cell-group>
|
||||
<div class="load-state" v-text="loadStateText" />
|
||||
</t-pull-down-refresh>
|
||||
</div>
|
||||
<nav-bar-action-menu v-if="canShowActionMenu" :actions="actionList" />
|
||||
<bottom-popup
|
||||
v-model="diaryPopupVisible"
|
||||
:title="`${selectedDateText} 门店日记`"
|
||||
height="min(80vh, 40rem)"
|
||||
left-button-text="关闭"
|
||||
right-button-text=""
|
||||
@left-button-click="closeDiaryPopup"
|
||||
>
|
||||
<div class="diary-popup">
|
||||
<t-cell-group v-if="selectedDateDiaries.length" theme="card">
|
||||
<t-cell
|
||||
v-for="(item, index) in selectedDateDiaries"
|
||||
:key="item.id || index"
|
||||
class="cell"
|
||||
:title="getTitle(item)"
|
||||
:description="getDescription(item)"
|
||||
arrow
|
||||
hover
|
||||
@click="toSelectedDiary(item)"
|
||||
>
|
||||
<template #note>
|
||||
<pass-time-label :timestamp="item.updatedAt || item.createdAt" underline />
|
||||
</template>
|
||||
</t-cell>
|
||||
</t-cell-group>
|
||||
<div v-else class="popup-empty" v-text="'该日期暂无门店日记'" />
|
||||
</div>
|
||||
</bottom-popup>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { BottomPopup } from "timi-tdesign-mobile";
|
||||
import { type CalendarProps, DialogPlugin, type TDate } from "tdesign-mobile-vue";
|
||||
import { type PageResult, useUserStore } from "timi-web-api";
|
||||
import { PassTimeLabel, Time } from "timi-web";
|
||||
import StoreDiaryAPI, { type StoreDiaryListOptions } from "@/api/StoreDiaryAPI";
|
||||
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
||||
import { useNavBarStore } from "@/store/navBarStore";
|
||||
import type { NavBarAction } from "@/types/NavBarAction";
|
||||
import type { StoreDiary } from "@/types/StoreDiary";
|
||||
import { getErrorMessage } from "@/utils/error";
|
||||
import { Message } from "@/utils/message";
|
||||
import { permissionCodes } from "@/utils/permission";
|
||||
|
||||
defineOptions({
|
||||
name: "StoreDiaryList"
|
||||
});
|
||||
|
||||
const router = useRouter();
|
||||
const navBarStore = useNavBarStore();
|
||||
const userStore = useUserStore();
|
||||
const currentStoreStore = useCurrentStoreStore();
|
||||
const canCreate = computed(() => userStore.hasPermissions(permissionCodes.storeDiary.create));
|
||||
const canDelete = computed(() => userStore.hasPermissions(permissionCodes.storeDiary.delete));
|
||||
const canShowActionMenu = computed(() => canCreate.value);
|
||||
|
||||
// ---------- 日历 ----------
|
||||
|
||||
const today = new Date();
|
||||
const selectedCalendarDate = ref<Date>(today);
|
||||
const calendarMonth = reactive({
|
||||
year: today.getFullYear(),
|
||||
month: today.getMonth() + 1
|
||||
});
|
||||
const calendarRenderKey = ref(0);
|
||||
const calendarDiaryList = ref<StoreDiary[]>([]);
|
||||
const isCalendarLoading = ref(false);
|
||||
const diaryPopupVisible = ref(false);
|
||||
const selectedDateText = ref("");
|
||||
const selectedDateDiaries = ref<StoreDiary[]>([]);
|
||||
let calendarLoadToken = 0;
|
||||
|
||||
const diaryDateMap = computed(() => {
|
||||
const map = new Map<string, StoreDiary[]>();
|
||||
for (const item of calendarDiaryList.value) {
|
||||
const dateText = getDiaryDateText(item);
|
||||
if (!dateText) {
|
||||
continue;
|
||||
}
|
||||
const diaries = map.get(dateText) || [];
|
||||
diaries.push(item);
|
||||
map.set(dateText, diaries);
|
||||
}
|
||||
|
||||
for (const diaries of map.values()) {
|
||||
diaries.sort((left, right) => getDiaryTimestamp(right) - getDiaryTimestamp(left));
|
||||
}
|
||||
return map;
|
||||
});
|
||||
|
||||
const calendarFormat = computed<NonNullable<CalendarProps["format"]>>(() => {
|
||||
const dateMap = diaryDateMap.value;
|
||||
return (dateItem: TDate) => {
|
||||
const diaries = dateMap.get(Time.formatDate(dateItem.date)) || [];
|
||||
if (diaries.length < 1) {
|
||||
return dateItem;
|
||||
}
|
||||
return {
|
||||
...dateItem,
|
||||
className: "has-diary",
|
||||
suffix: String(diaries.length)
|
||||
};
|
||||
};
|
||||
});
|
||||
|
||||
// ---------- 顶部操作 ----------
|
||||
|
||||
const actionList = computed<NavBarAction[]>(() => {
|
||||
if (!canCreate.value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [{
|
||||
action: toCreate,
|
||||
theme: "primary",
|
||||
icon: "add",
|
||||
key: "create",
|
||||
text: "新增日记"
|
||||
}];
|
||||
});
|
||||
|
||||
// ---------- 列表数据 ----------
|
||||
|
||||
const page = reactive({
|
||||
index: 0,
|
||||
size: 20
|
||||
});
|
||||
const pageResult = reactive<PageResult<StoreDiary>>({
|
||||
total: 0,
|
||||
list: []
|
||||
});
|
||||
const isRefreshing = ref(false);
|
||||
const isLoading = ref(false);
|
||||
const isNoMore = ref(false);
|
||||
const isDeleting = ref(false);
|
||||
const deletingId = ref("");
|
||||
let loadToken = 0;
|
||||
|
||||
const loadStateText = computed(() => {
|
||||
if (isLoading.value) {
|
||||
return "正在加载门店日记";
|
||||
}
|
||||
if (isNoMore.value) {
|
||||
return pageResult.list.length ? `共 ${pageResult.total} 篇日记` : "暂无门店日记";
|
||||
}
|
||||
return pageResult.list.length ? "上拉加载更多" : "暂无门店日记";
|
||||
});
|
||||
|
||||
function getCurrentMonthListOptions(): StoreDiaryListOptions {
|
||||
const beginDate = new Date(calendarMonth.year, calendarMonth.month - 1, 1, 0, 0, 0, 0);
|
||||
const endDate = new Date(calendarMonth.year, calendarMonth.month, 1, 0, 0, 0, 0);
|
||||
return {
|
||||
beginCreatedAt: Time.toTimestamp(beginDate),
|
||||
endCreatedAt: Time.toTimestamp(endDate),
|
||||
storeId: currentStoreStore.currentStoreId
|
||||
};
|
||||
}
|
||||
|
||||
async function loadCalendarDiaries(): Promise<void> {
|
||||
const requestToken = calendarLoadToken + 1;
|
||||
calendarLoadToken = requestToken;
|
||||
isCalendarLoading.value = true;
|
||||
try {
|
||||
const result = await StoreDiaryAPI.list({
|
||||
index: 0,
|
||||
size: 1000
|
||||
}, getCurrentMonthListOptions());
|
||||
if (requestToken !== calendarLoadToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
calendarDiaryList.value = result.list || [];
|
||||
calendarRenderKey.value += 1;
|
||||
} finally {
|
||||
if (requestToken === calendarLoadToken) {
|
||||
isCalendarLoading.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMore(): Promise<void> {
|
||||
if (isLoading.value || isNoMore.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const requestToken = loadToken + 1;
|
||||
loadToken = requestToken;
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const result = await StoreDiaryAPI.list(page, getCurrentMonthListOptions());
|
||||
if (requestToken !== loadToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
const list = result.list || [];
|
||||
pageResult.total = result.total || 0;
|
||||
pageResult.list = pageResult.list.concat(list);
|
||||
page.index += 1;
|
||||
isNoMore.value = pageResult.list.length >= pageResult.total || list.length < page.size;
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "门店日记加载失败"));
|
||||
} finally {
|
||||
if (requestToken === loadToken) {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function reload(): Promise<void> {
|
||||
loadToken += 1;
|
||||
page.index = 0;
|
||||
pageResult.total = 0;
|
||||
pageResult.list = [];
|
||||
isNoMore.value = false;
|
||||
await loadMore();
|
||||
}
|
||||
|
||||
async function refresh(): Promise<void> {
|
||||
isRefreshing.value = true;
|
||||
try {
|
||||
await Promise.all([reload(), loadCalendarDiaries()]);
|
||||
} finally {
|
||||
isRefreshing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 展示 ----------
|
||||
|
||||
function getDiaryTimestamp(item: StoreDiary): number {
|
||||
return item.createdAt || item.updatedAt || 0;
|
||||
}
|
||||
|
||||
function getDiaryDateText(item: StoreDiary): string {
|
||||
const date = Time.toDateObject(getDiaryTimestamp(item));
|
||||
return date ? Time.formatDate(date) : "";
|
||||
}
|
||||
|
||||
function getTitle(item: StoreDiary): string {
|
||||
return item.title?.trim() || "未命名日记";
|
||||
}
|
||||
|
||||
function getDescription(item: StoreDiary): string {
|
||||
return item.description?.trim() || "暂无摘要";
|
||||
}
|
||||
|
||||
// ---------- 导航 ----------
|
||||
|
||||
async function handleCalendarPanelChange(context: { year: number; month: number }): Promise<void> {
|
||||
if (context.year === calendarMonth.year && context.month === calendarMonth.month) {
|
||||
return;
|
||||
}
|
||||
calendarMonth.year = context.year;
|
||||
calendarMonth.month = context.month;
|
||||
selectedCalendarDate.value = new Date(context.year, context.month - 1, 1);
|
||||
diaryPopupVisible.value = false;
|
||||
await Promise.all([reload(), loadCalendarDiaries()]);
|
||||
}
|
||||
|
||||
async function selectCalendarDate(date: Date): Promise<void> {
|
||||
selectedCalendarDate.value = date;
|
||||
const diaries = diaryDateMap.value.get(Time.formatDate(date)) || [];
|
||||
if (diaries.length < 1) {
|
||||
return;
|
||||
}
|
||||
if (diaries.length < 2) {
|
||||
const [diary] = diaries;
|
||||
if (diary) {
|
||||
await toDetail(diary);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
selectedDateText.value = Time.formatDate(date);
|
||||
selectedDateDiaries.value = [...diaries];
|
||||
diaryPopupVisible.value = true;
|
||||
}
|
||||
|
||||
function closeDiaryPopup(): void {
|
||||
diaryPopupVisible.value = false;
|
||||
}
|
||||
|
||||
async function toSelectedDiary(item: StoreDiary): Promise<void> {
|
||||
closeDiaryPopup();
|
||||
await toDetail(item);
|
||||
}
|
||||
|
||||
async function toCreate(): Promise<void> {
|
||||
await router.push({ name: "StoreDiaryCreate" });
|
||||
}
|
||||
|
||||
async function toDetail(item: StoreDiary): Promise<void> {
|
||||
if (!item.id) {
|
||||
return;
|
||||
}
|
||||
await router.push({
|
||||
name: "StoreDiaryDetail",
|
||||
params: { id: item.id }
|
||||
});
|
||||
}
|
||||
|
||||
// ---------- 删除 ----------
|
||||
|
||||
async function confirmDelete(item: StoreDiary): Promise<void> {
|
||||
if (!item.id || !canDelete.value || isDeleting.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const diaryId = item.id;
|
||||
const dialog = DialogPlugin.confirm({
|
||||
title: "删除门店日记",
|
||||
content: `确定删除“${getTitle(item)}”吗?删除后无法恢复。`,
|
||||
cancelBtn: "取消",
|
||||
confirmBtn: "删除",
|
||||
onCancel() {
|
||||
dialog.destroy();
|
||||
},
|
||||
async onConfirm() {
|
||||
dialog.destroy();
|
||||
isDeleting.value = true;
|
||||
deletingId.value = diaryId;
|
||||
try {
|
||||
await StoreDiaryAPI.remove(diaryId);
|
||||
await Promise.all([reload(), loadCalendarDiaries()]);
|
||||
Message.success("门店日记已删除");
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "门店日记删除失败"));
|
||||
} finally {
|
||||
isDeleting.value = false;
|
||||
deletingId.value = "";
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---------- 生命周期 ----------
|
||||
|
||||
async function preparePage(): Promise<void> {
|
||||
await currentStoreStore.loadLoginStore();
|
||||
await Promise.all([reload(), loadCalendarDiaries()]);
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
navBarStore.clearRight();
|
||||
navBarStore.setCustomRightVisible(canShowActionMenu.value);
|
||||
try {
|
||||
await preparePage();
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "门店日记页面加载失败"));
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => navBarStore.clearRight());
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.store-diary-list {
|
||||
padding: calc(var(--app-nav-height) + 1rem) 0 1rem;
|
||||
|
||||
.list-wrapper {
|
||||
min-height: 70vh;
|
||||
|
||||
.calendar {
|
||||
margin: 0 1rem 1rem;
|
||||
overflow: hidden;
|
||||
background: var(--td-bg-color-container);
|
||||
box-shadow: var(--td-shadow-1);
|
||||
border-radius: .75rem;
|
||||
|
||||
:deep(.t-calendar__title) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
:deep(.t-calendar-header) {
|
||||
padding-top: .5rem;
|
||||
}
|
||||
|
||||
:deep(.t-calendar) {
|
||||
overflow: hidden;
|
||||
border-radius: .75rem;
|
||||
|
||||
&__months {
|
||||
height: auto;
|
||||
}
|
||||
|
||||
&__dates-item {
|
||||
height: 2.5rem;
|
||||
margin-top: 0;
|
||||
align-items: unset;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.has-diary) {
|
||||
font-weight: 700;
|
||||
--td-calendar-item-suffix-color: var(--td-brand-color);
|
||||
}
|
||||
}
|
||||
|
||||
.items {
|
||||
overflow: hidden;
|
||||
}
|
||||
}
|
||||
|
||||
.load-state {
|
||||
padding: 1rem;
|
||||
color: var(--td-text-color-placeholder);
|
||||
font-size: .8125rem;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
.cell {
|
||||
:deep(.t-cell__title) {
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
:deep(.t-cell__title-text),
|
||||
:deep(.t-cell__description) {
|
||||
overflow: hidden;
|
||||
min-width: 0;
|
||||
white-space: nowrap;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
:deep(.t-cell__note) {
|
||||
flex: 0 0 auto;
|
||||
min-width: 0;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
||||
.diary-popup {
|
||||
padding-bottom: .5rem;
|
||||
|
||||
.popup-empty {
|
||||
padding: 2rem 1rem;
|
||||
color: var(--td-text-color-placeholder);
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
|
||||
:global(html.theme-dark) {
|
||||
.store-diary-list {
|
||||
.description {
|
||||
color: #94a3b8;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user