120 lines
4.0 KiB
JavaScript
120 lines
4.0 KiB
JavaScript
const { post } = require("../../utils/api");
|
|
const { getModule } = require("../../utils/modules");
|
|
const { hasManagePermission } = require("../../utils/permissions");
|
|
const { ensureModuleOpen, getActiveClassId, showError } = require("../../utils/page-helpers");
|
|
|
|
const FORM_DEFAULTS = {
|
|
announcements: { title: "", content: "", is_pinned: false },
|
|
votes: { title: "", description: "", options_text: "", vote_type: "single", is_anonymous: false },
|
|
schedule: { title: "", type: "course", start_time: "", location: "", description: "" },
|
|
fund: { type: "expense", amount: "", category: "", description: "", record_date: "" }
|
|
};
|
|
|
|
Page({
|
|
data: {
|
|
moduleKey: "",
|
|
title: "新增",
|
|
form: {},
|
|
isAnnouncements: false,
|
|
isVotes: false,
|
|
isSchedule: false,
|
|
isFund: false,
|
|
scheduleTypes: ["course", "deadline", "activity"],
|
|
fundTypes: ["income", "expense"],
|
|
loading: false
|
|
},
|
|
|
|
onLoad(options) {
|
|
const moduleKey = options.module || "";
|
|
const module = getModule(moduleKey);
|
|
const classId = getActiveClassId();
|
|
const user = getApp().globalData.user;
|
|
if (!module || !ensureModuleOpen(moduleKey) || !hasManagePermission(user, classId, moduleKey)) {
|
|
wx.redirectTo({
|
|
url: `/pages/module-unavailable/index?title=${encodeURIComponent(module?.title || "功能")}`
|
|
});
|
|
return;
|
|
}
|
|
this.setData({
|
|
moduleKey,
|
|
title: `新增${module.title}`,
|
|
form: Object.assign({}, FORM_DEFAULTS[moduleKey] || {}),
|
|
isAnnouncements: moduleKey === "announcements",
|
|
isVotes: moduleKey === "votes",
|
|
isSchedule: moduleKey === "schedule",
|
|
isFund: moduleKey === "fund"
|
|
});
|
|
wx.setNavigationBarTitle({ title: `新增${module.title}` });
|
|
},
|
|
|
|
onInput(event) {
|
|
const field = event.currentTarget.dataset.field;
|
|
this.setData({ [`form.${field}`]: event.detail.value });
|
|
},
|
|
|
|
onSwitch(event) {
|
|
const field = event.currentTarget.dataset.field;
|
|
this.setData({ [`form.${field}`]: event.detail.value });
|
|
},
|
|
|
|
onPicker(event) {
|
|
const field = event.currentTarget.dataset.field;
|
|
const value = event.currentTarget.dataset.values.split(",")[Number(event.detail.value)];
|
|
this.setData({ [`form.${field}`]: value });
|
|
},
|
|
|
|
async submit() {
|
|
const classId = getActiveClassId();
|
|
const moduleKey = this.data.moduleKey;
|
|
const form = this.data.form;
|
|
this.setData({ loading: true });
|
|
try {
|
|
if (moduleKey === "announcements") {
|
|
await post(`/api/announcements/?class_id=${classId}`, {
|
|
title: form.title,
|
|
content: form.content || null,
|
|
is_pinned: Boolean(form.is_pinned)
|
|
});
|
|
}
|
|
if (moduleKey === "votes") {
|
|
const options = String(form.options_text || "")
|
|
.split("\n")
|
|
.map((item) => item.trim())
|
|
.filter(Boolean);
|
|
await post(`/api/votes/?class_id=${classId}`, {
|
|
title: form.title,
|
|
description: form.description || null,
|
|
vote_type: form.vote_type || "single",
|
|
is_anonymous: Boolean(form.is_anonymous),
|
|
max_choices: form.vote_type === "multiple" ? Math.max(2, options.length) : 1,
|
|
options
|
|
});
|
|
}
|
|
if (moduleKey === "schedule") {
|
|
await post(`/api/schedule/?class_id=${classId}`, {
|
|
title: form.title,
|
|
type: form.type,
|
|
start_time: form.start_time,
|
|
location: form.location || null,
|
|
description: form.description || null
|
|
});
|
|
}
|
|
if (moduleKey === "fund") {
|
|
await post(`/api/fund/?class_id=${classId}`, {
|
|
type: form.type,
|
|
amount: Number(form.amount),
|
|
category: form.category,
|
|
description: form.description || null,
|
|
record_date: form.record_date
|
|
});
|
|
}
|
|
wx.showToast({ title: "已保存", icon: "success" });
|
|
setTimeout(() => wx.navigateBack(), 500);
|
|
} catch (error) {
|
|
showError(error, "保存失败");
|
|
} finally {
|
|
this.setData({ loading: false });
|
|
}
|
|
}
|
|
});
|