hku-class/miniprogram/pages/timeline-detail/index.js
2026-05-12 23:10:05 +08:00

141 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { del, get, post } = require("../../utils/api");
const { showError } = require("../../utils/page-helpers");
Page({
data: {
id: null,
post: null,
canDelete: false,
comments: [],
commentText: "",
replyTo: "",
inputPlaceholder: "写下评论",
loading: false,
submitting: false
},
onLoad(options) {
wx.setNavigationBarTitle({ title: "动态详情" });
this.setData({ id: options.id });
this.load();
},
async onPullDownRefresh() {
await this.load();
wx.stopPullDownRefresh();
},
async load() {
if (!this.data.id) return;
this.setData({ loading: true });
try {
const [postDetail, commentsRes] = await Promise.all([
get(`/api/timeline/${this.data.id}`),
get(`/api/timeline/${this.data.id}/comments`, { page_size: 50 })
]);
this.setData({
post: postDetail,
canDelete: postDetail.author_id === getApp().globalData.user?.id,
comments: (commentsRes.items || []).map((item) => ({
...item,
initial: String(item.author_name || "评").slice(0, 1)
}))
});
} catch (error) {
showError(error, "加载动态失败");
} finally {
this.setData({ loading: false });
}
},
async toggleLike() {
if (!this.data.id || this.data.submitting) return;
this.setData({ submitting: true });
try {
await post(`/api/timeline/${this.data.id}/like`);
await this.load();
} catch (error) {
showError(error, "操作失败");
} finally {
this.setData({ submitting: false });
}
},
onCommentInput(event) {
this.setData({ commentText: event.detail.value });
},
previewImage(event) {
const current = event.currentTarget.dataset.src;
const urls = this.data.post?.image_urls || [];
if (!current || !urls.length) return;
wx.previewImage({ current, urls });
},
openActions() {
wx.showActionSheet({
itemList: ["删除动态"],
itemColor: "#b42318",
success: () => {
wx.showModal({
title: "删除动态",
content: "删除后无法恢复,确认删除这条动态?",
confirmText: "删除",
confirmColor: "#b42318",
success: async (res) => {
if (!res.confirm) return;
try {
await del(`/api/timeline/${this.data.id}`);
wx.showToast({ title: "已删除", icon: "success" });
const pages = getCurrentPages();
const previousPage = pages[pages.length - 2];
if (previousPage?.setData) previousPage.setData({ needsRefresh: true });
setTimeout(() => wx.navigateBack(), 500);
} catch (error) {
showError(error, "删除失败");
}
}
});
}
});
},
replyComment(event) {
const name = event.currentTarget.dataset.name || "";
this.setData({
replyTo: name,
inputPlaceholder: name ? `回复 ${name}` : "写下评论"
});
},
cancelReply() {
this.setData({
replyTo: "",
inputPlaceholder: "写下评论"
});
},
async submitComment() {
const text = this.data.commentText.trim();
if (!text) {
wx.showToast({ title: "请输入评论", icon: "none" });
return;
}
const content = this.data.replyTo ? `回复 @${this.data.replyTo}${text}` : text;
this.setData({ submitting: true });
try {
await post(`/api/timeline/${this.data.id}/comments`, { content });
this.setData({
commentText: "",
replyTo: "",
inputPlaceholder: "写下评论"
});
await this.load();
} catch (error) {
showError(error, "评论失败");
} finally {
this.setData({ submitting: false });
}
}
});