hku-class/miniprogram/pages/schedule-detail/index.js
2026-05-16 23:59:13 +08:00

72 lines
2.2 KiB
JavaScript

const { get } = require("../../utils/api");
const { showError } = require("../../utils/page-helpers");
function formatDateTime(value) {
if (!value) return "";
return String(value).replace("T", " ").slice(0, 16);
}
function scheduleTypeText(type) {
return {
course: "课程",
deadline: "截止日",
activity: "活动"
}[type] || type || "排期";
}
function countdownText(value) {
if (!value) return "";
const targetDate = new Date(value);
if (Number.isNaN(targetDate.getTime())) return "";
const today = new Date();
const targetDay = new Date(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate()).getTime();
const todayDay = new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime();
const diffDays = Math.round((targetDay - todayDay) / (24 * 60 * 60 * 1000));
if (diffDays < 0) return `已逾期 ${Math.abs(diffDays)}`;
if (diffDays === 0) return "今天截止";
if (diffDays === 1) return "明天截止";
return `还有 ${diffDays}`;
}
function countdownClass(text) {
if (!text) return "";
if (text.indexOf("已逾期") === 0) return "overdue";
if (text === "今天截止" || text === "明天截止") return "urgent";
return "";
}
Page({
data: { item: null, loading: false },
onLoad(options) {
wx.setNavigationBarTitle({ title: "排期详情" });
this.load(options.id);
},
async load(id) {
if (!id) return;
this.setData({ loading: true });
try {
const item = await get(`/api/schedule/${id}`);
const countdown_text = item.type === "deadline" ? countdownText(item.start_time) : "";
this.setData({
item: {
...item,
start_time_text: formatDateTime(item.start_time),
end_time_text: formatDateTime(item.end_time),
type_text: scheduleTypeText(item.type),
start_label: item.type === "deadline" ? "截止时间" : "开始时间",
countdown_text,
countdown_class: countdownClass(countdown_text),
location_text: item.location || "地点待定",
description_text: item.description || "暂无说明"
}
});
} catch (error) {
showError(error, "加载排期失败");
} finally {
this.setData({ loading: false });
}
}
});