refactor journal-detail-popup
This commit is contained in:
@ -1,49 +1,28 @@
|
||||
// pages/main/journal-date/index.ts
|
||||
import config from "../../../config/index";
|
||||
import Time from "../../../utils/Time";
|
||||
import { Journal, JournalPageType } from "../../../types/Journal";
|
||||
import { MediaAttachType } from "../../../types/Attachment";
|
||||
|
||||
interface JournalInfo {
|
||||
id: number;
|
||||
date: string;
|
||||
time: string;
|
||||
lat?: number;
|
||||
lng?: number;
|
||||
location?: string;
|
||||
idea?: string;
|
||||
items: Array<{
|
||||
type: number;
|
||||
thumbURL: string;
|
||||
sourceURL: string;
|
||||
mongoId: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
interface SelectedDateInfo {
|
||||
displayDate: string;
|
||||
journals: JournalInfo[];
|
||||
}
|
||||
import Time from "../../../utils/Time";
|
||||
|
||||
interface JournalDateData {
|
||||
journalMap: Record<string, number[]>; // 存储每个日期的日记 id 列表
|
||||
selectedDate: SelectedDateInfo | null;
|
||||
// 存储每个日期的日记 id 列表
|
||||
isLoading: boolean;
|
||||
popupVisible: boolean; // popup 显示状态
|
||||
journalMap: Record<string, number[]>;
|
||||
|
||||
popupIds: number[];
|
||||
popupVisible: boolean;
|
||||
}
|
||||
|
||||
Page({
|
||||
data: <JournalDateData>{
|
||||
journalMap: {},
|
||||
selectedDate: null,
|
||||
isLoading: true,
|
||||
popupVisible: false
|
||||
},
|
||||
journalMap: {},
|
||||
|
||||
popupIds: [],
|
||||
popupVisible: false,
|
||||
},
|
||||
async onLoad() {
|
||||
await this.loadJournals();
|
||||
},
|
||||
|
||||
/** 加载所有日记 */
|
||||
async loadJournals() {
|
||||
this.setData({ isLoading: true });
|
||||
@ -70,24 +49,19 @@ Page({
|
||||
fail: reject
|
||||
});
|
||||
}) || [];
|
||||
|
||||
// 按日期分组,只存储 id
|
||||
const journalMap: Record<string, number[]> = {};
|
||||
list.forEach((journal: any) => {
|
||||
const date = new Date(journal.createdAt);
|
||||
const dateKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
||||
|
||||
const dateKey = Time.toDate(journal.createdAt);
|
||||
if (!journalMap[dateKey]) {
|
||||
journalMap[dateKey] = [];
|
||||
}
|
||||
journalMap[dateKey].push(journal.id);
|
||||
});
|
||||
|
||||
// 按 id 倒序排序每天的日记
|
||||
Object.keys(journalMap).forEach(dateKey => {
|
||||
journalMap[dateKey].sort((a, b) => b - a);
|
||||
});
|
||||
|
||||
this.setData({
|
||||
journalMap,
|
||||
isLoading: false
|
||||
@ -100,12 +74,10 @@ Page({
|
||||
this.setData({ isLoading: false });
|
||||
}
|
||||
},
|
||||
|
||||
/** 日期选择事件(来自 calendar 组件) */
|
||||
onDateSelect(e: WechatMiniprogram.CustomEvent) {
|
||||
const { date, year, month, day } = e.detail;
|
||||
const { date } = e.detail;
|
||||
const journalIds = this.data.journalMap[date];
|
||||
|
||||
if (!journalIds || journalIds.length === 0) {
|
||||
wx.showToast({
|
||||
title: "该日期无日记",
|
||||
@ -113,81 +85,22 @@ Page({
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 调用接口获取详情
|
||||
this.loadJournalsByIds(journalIds, `${year} 年 ${month} 月 ${day} 日`);
|
||||
this.loadJournalsByIds(journalIds);
|
||||
},
|
||||
/** 根据 id 列表加载日记详情 */
|
||||
async loadJournalsByIds(ids: number[], displayDate: string) {
|
||||
wx.showLoading({ title: "加载中...", mask: true });
|
||||
try {
|
||||
const list: Journal[] = await new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${config.url}/journal/list/ids`,
|
||||
method: "POST",
|
||||
header: {
|
||||
Key: wx.getStorageSync("key")
|
||||
},
|
||||
data: ids,
|
||||
success: (resp: any) => {
|
||||
if (resp.data.code === 20000) {
|
||||
resolve(resp.data.data);
|
||||
} else {
|
||||
reject(new Error(resp.data.message || "加载失败"));
|
||||
}
|
||||
},
|
||||
fail: reject
|
||||
});
|
||||
}) || [];
|
||||
|
||||
// 转换为 JournalInfo 格式
|
||||
const journals: JournalInfo[] = list.sort((a, b) => a.createdAt! - b.createdAt!).map((journal: any) => {
|
||||
const date = new Date(journal.createdAt);
|
||||
return {
|
||||
id: journal.id,
|
||||
date: Time.toPassedDateTime(journal.createdAt),
|
||||
time: `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`,
|
||||
lat: journal.lat,
|
||||
lng: journal.lng,
|
||||
location: journal.location,
|
||||
idea: journal.idea,
|
||||
items: journal.items
|
||||
.filter((item: any) => item.attachType === MediaAttachType.THUMB)
|
||||
.map((item: any) => {
|
||||
const ext = JSON.parse(item.ext);
|
||||
return {
|
||||
type: ext.isVideo ? 1 : 0,
|
||||
thumbURL: `${config.url}/attachment/read/${item.mongoId}`,
|
||||
sourceURL: `${config.url}/attachment/read/${ext.sourceMongoId}`,
|
||||
mongoId: item.mongoId,
|
||||
};
|
||||
})
|
||||
};
|
||||
});
|
||||
|
||||
this.setData({
|
||||
selectedDate: {
|
||||
displayDate,
|
||||
journals
|
||||
},
|
||||
popupVisible: true // 显示 popup
|
||||
});
|
||||
|
||||
wx.hideLoading();
|
||||
} catch (err: any) {
|
||||
wx.hideLoading();
|
||||
wx.showToast({
|
||||
title: err.message || "加载失败",
|
||||
icon: "error"
|
||||
});
|
||||
}
|
||||
async loadJournalsByIds(ids: number[]) {
|
||||
this.setData({
|
||||
popupIds: ids,
|
||||
popupVisible: true
|
||||
});
|
||||
wx.hideLoading();
|
||||
},
|
||||
|
||||
/** 关闭详情 */
|
||||
closeDetail() {
|
||||
this.setData({
|
||||
popupVisible: false,
|
||||
selectedDate: null
|
||||
popupIds: []
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user