const { del, get } = require("../../utils/api"); const { getModule } = require("../../utils/modules"); const { hasManagePermission } = require("../../utils/permissions"); const { ensureModuleOpen, getActiveClassId, showError } = require("../../utils/page-helpers"); const ENDPOINTS = { announcements: "/api/announcements/", directory: "/api/directory/", timeline: "/api/timeline/", votes: "/api/votes/", schedule: "/api/schedule/", resources: "/api/resources/", reading_corner: "/api/reading/feed", fund: "/api/fund/" }; Page({ data: { moduleKey: "", title: "功能", moduleIcon: "项", items: [], canManage: false, canPostTimeline: false, isTimeline: false, isDirectory: false, isSchedule: false, needsRefresh: false, loading: false }, onLoad(options) { const moduleKey = options.module || ""; const module = getModule(moduleKey); const app = getApp(); const classId = getActiveClassId(); this.setData({ moduleKey, title: module?.title || "功能", moduleIcon: module?.icon || "项", isTimeline: moduleKey === "timeline", isDirectory: moduleKey === "directory", isSchedule: moduleKey === "schedule", canPostTimeline: moduleKey === "timeline", canManage: ["announcements", "votes", "schedule", "fund"].includes(moduleKey) && hasManagePermission(app.globalData.user, classId, moduleKey) }); wx.setNavigationBarTitle({ title: module?.title || "功能" }); if (!module || !ensureModuleOpen(moduleKey)) return; this.load(); }, onShow() { if (this.data.moduleKey) { this.setData({ needsRefresh: false }); this.load(); } }, async onPullDownRefresh() { await this.load(); wx.stopPullDownRefresh(); }, async load() { const classId = getActiveClassId(); const endpoint = ENDPOINTS[this.data.moduleKey]; if (!endpoint) return; this.setData({ loading: true }); try { const res = await get(endpoint, { page_size: 20, class_id: classId }); const rawItems = Array.isArray(res) ? res : res.items || []; const currentUserId = getApp().globalData.user?.id; const items = rawItems.map((item) => ({ ...item, can_delete: this.data.moduleKey === "timeline" && item.author_id === currentUserId, initial: String(item.name || item.author_name || this.data.title || "项").slice(0, 1), schedule_day: item.start_time ? String(item.start_time).slice(8, 10) : "", schedule_month: item.start_time ? `${String(item.start_time).slice(5, 7)}月` : "", committee_text: item.committee_role ? ` · ${item.committee_role}` : "" })); this.setData({ items }); } catch (error) { if (error.message === "该功能当前未开放") { wx.redirectTo({ url: `/pages/module-unavailable/index?title=${encodeURIComponent(this.data.title)}` }); return; } showError(error); } finally { this.setData({ loading: false }); } }, openItem(event) { const id = event.currentTarget.dataset.id; const key = this.data.moduleKey; if (!id) return; if (key === "directory") { wx.navigateTo({ url: `/pages/member-detail/index?id=${id}` }); return; } if (key === "schedule") { wx.navigateTo({ url: `/pages/schedule-detail/index?id=${id}` }); return; } if (key === "timeline") { wx.navigateTo({ url: `/pages/timeline-detail/index?id=${id}` }); return; } }, previewImage(event) { const current = event.currentTarget.dataset.src; const postId = Number(event.currentTarget.dataset.postId); const post = this.data.items.find((item) => item.id === postId); const urls = post?.image_urls || []; if (!current || !urls.length) return; wx.previewImage({ current, urls }); }, openTimelineActions(event) { const id = event.currentTarget.dataset.id; 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/${id}`); wx.showToast({ title: "已删除", icon: "success" }); this.load(); } catch (error) { showError(error, "删除失败"); } } }); } }); }, openManage() { wx.navigateTo({ url: `/pages/manage/index?module=${this.data.moduleKey}` }); }, openTimelineCreate() { wx.navigateTo({ url: "/pages/timeline-create/index" }); } });