505 lines
12 KiB
Vue
505 lines
12 KiB
Vue
<template>
|
|
<div class="page-layout navbar store-diary-list">
|
|
<t-navbar
|
|
fixed
|
|
:z-index="20"
|
|
title="门店日记"
|
|
left-arrow
|
|
@left-click="$router.back()"
|
|
>
|
|
<template #right>
|
|
<t-button
|
|
v-if="canCreate"
|
|
theme="primary"
|
|
variant="text"
|
|
size="small"
|
|
content="新增"
|
|
@click="toCreate"
|
|
/>
|
|
</template>
|
|
</t-navbar>
|
|
<main class="page-content scroll column list">
|
|
<t-pull-down-refresh
|
|
v-can="permissionCodes.storeDiary.read"
|
|
class="store-diary-list pull-refresh"
|
|
v-model="isRefreshing"
|
|
:refresh-timeout="30000"
|
|
@refresh="refresh"
|
|
@scrolltolower="loadMore"
|
|
>
|
|
<div class="content">
|
|
<section class="calendar" v-entrance-transition>
|
|
<t-calendar
|
|
:key="calendarRenderKey"
|
|
class="calendar-view"
|
|
:value="selectedCalendarDate"
|
|
:min-date="calendarMinDate"
|
|
:format="calendarFormat"
|
|
switch-mode="month"
|
|
:use-popup="false"
|
|
@panel-change="handleCalendarPanelChange"
|
|
@select="selectCalendarDate"
|
|
/>
|
|
</section>
|
|
<t-button
|
|
v-if="canCreate"
|
|
class="cell-button"
|
|
theme="primary"
|
|
@click="toCreateToday"
|
|
>
|
|
新增今天日记
|
|
</t-button>
|
|
<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="{ step: 30 }"
|
|
>
|
|
<store-diary-list-item :diary="item" @select="toDetail" />
|
|
<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>
|
|
<center-tips :content="loadStateText" />
|
|
</div>
|
|
</t-pull-down-refresh>
|
|
</main>
|
|
|
|
<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">
|
|
<store-diary-list-item
|
|
v-for="(item, index) in selectedDateDiaries"
|
|
:key="item.id || index"
|
|
:diary="item"
|
|
hover
|
|
@select="toSelectedDiary"
|
|
/>
|
|
</t-cell-group>
|
|
<div v-else class="popup-empty" v-text="'该日期暂无门店日记'" />
|
|
</div>
|
|
</bottom-popup>
|
|
</div>
|
|
</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 { Time } from "timi-web";
|
|
import StoreDiaryAPI, { type StoreDiaryListOptions } from "@/api/StoreDiaryAPI";
|
|
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
|
import type { StoreDiary } from "@/types/StoreDiary";
|
|
import { getErrorMessage } from "@/utils/error";
|
|
import { Message } from "@/utils/message";
|
|
import { permissionCodes } from "@/utils/permission";
|
|
|
|
import StoreDiaryListItem from "@/pages/store/component/StoreDiaryListItem.vue";
|
|
|
|
defineOptions({
|
|
name: "StoreDiaryList"
|
|
});
|
|
|
|
const router = useRouter();
|
|
const route = useRoute();
|
|
const userStore = useUserStore();
|
|
const currentStoreStore = useCurrentStoreStore();
|
|
const canCreate = computed(() => userStore.hasPermissions(permissionCodes.storeDiary.create));
|
|
const canDelete = computed(() => userStore.hasPermissions(permissionCodes.storeDiary.delete));
|
|
|
|
// ---------- 日历 ----------
|
|
|
|
const today = new Date();
|
|
const calendarMinDate = new Date(today.getFullYear() - 2, 0, 1);
|
|
const initialCalendarDate = parseCalendarDate(route.query.calendarDate);
|
|
const selectedCalendarDate = ref<Date>(initialCalendarDate);
|
|
const calendarMonth = reactive({
|
|
year: initialCalendarDate.getFullYear(),
|
|
month: initialCalendarDate.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.toDate(dateItem.date)) || [];
|
|
if (diaries.length < 1) {
|
|
return dateItem;
|
|
}
|
|
return {
|
|
...dateItem,
|
|
className: "has-diary",
|
|
suffix: String(diaries.length)
|
|
};
|
|
};
|
|
});
|
|
|
|
// ---------- 列表数据 ----------
|
|
|
|
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.toDate(date) : "";
|
|
}
|
|
|
|
function parseCalendarDate(value: unknown): Date {
|
|
const text = Array.isArray(value) ? String(value[0] || "") : String(value || "");
|
|
const [year, month, day] = text.split("-").map(Number);
|
|
const date = new Date(year, month - 1, day);
|
|
if (
|
|
!Number.isInteger(year)
|
|
|| !Number.isInteger(month)
|
|
|| !Number.isInteger(day)
|
|
|| month < 1
|
|
|| 12 < month
|
|
|| day < 1
|
|
|| date.getFullYear() !== year
|
|
|| date.getMonth() + 1 !== month
|
|
|| date.getDate() !== day
|
|
) {
|
|
return today;
|
|
}
|
|
return date;
|
|
}
|
|
|
|
// ---------- 导航 ----------
|
|
|
|
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.toDate(date)) || [];
|
|
if (diaries.length < 1) {
|
|
return;
|
|
}
|
|
if (diaries.length < 2) {
|
|
const [diary] = diaries;
|
|
if (diary) {
|
|
await toDetail(diary);
|
|
}
|
|
return;
|
|
}
|
|
|
|
selectedDateText.value = Time.toDate(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 toCreateToday(): Promise<void> {
|
|
await router.push({
|
|
name: "StoreDiaryCreate",
|
|
query: { title: Time.toDate(new Date()) }
|
|
});
|
|
}
|
|
|
|
async function toDetail(item: StoreDiary): Promise<void> {
|
|
if (!item.id) {
|
|
return;
|
|
}
|
|
await router.replace({
|
|
name: "StoreDiaryList",
|
|
query: {
|
|
...route.query,
|
|
calendarDate: getDiaryDateText(item) || Time.toDate(selectedCalendarDate.value)
|
|
}
|
|
});
|
|
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: `确定删除“${item.title?.trim() || "未命名日记"}”吗?删除后无法恢复。`,
|
|
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 () => {
|
|
|
|
try {
|
|
await preparePage();
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "门店日记页面加载失败"));
|
|
}
|
|
});
|
|
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.store-diary-list {
|
|
padding: calc(var(--app-nav-height) + 1rem) 0 1rem;
|
|
min-height: 70vh;
|
|
|
|
.content {
|
|
gap: 1rem;
|
|
display: flex;
|
|
flex-direction: column;
|
|
|
|
.calendar {
|
|
margin: 0 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;
|
|
}
|
|
}
|
|
}
|
|
|
|
.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>
|