hku-class/miniprogram/pages/fund-detail/index.js

67 lines
1.8 KiB
JavaScript

const { get } = require("../../utils/api");
const { showError } = require("../../utils/page-helpers");
function formatAmount(value) {
return Number(value || 0).toFixed(2);
}
function formatDateTime(value) {
if (!value) return "";
return String(value).replace("T", " ").slice(0, 16);
}
function normalizeRecord(record) {
const isIncome = record.type === "income";
const imageUrls = Array.isArray(record.image_urls) ? record.image_urls : [];
return {
...record,
amount_text: formatAmount(record.amount),
signed_amount_text: `${isIncome ? "+" : "-"}¥${formatAmount(record.amount)}`,
type_text: isIncome ? "收入" : "支出",
type_class: isIncome ? "income" : "expense",
image_urls: imageUrls,
image_count_text: imageUrls.length ? `${imageUrls.length} 张凭证` : "未上传凭证",
created_at_text: formatDateTime(record.created_at),
updated_at_text: formatDateTime(record.updated_at)
};
}
Page({
data: {
id: null,
record: null,
loading: false
},
onLoad(options) {
wx.setNavigationBarTitle({ title: "班费详情" });
this.setData({ id: options.id || null });
this.load(options.id);
},
async onPullDownRefresh() {
await this.load(this.data.id);
wx.stopPullDownRefresh();
},
async load(id) {
if (!id) return;
this.setData({ loading: true });
try {
const record = await get(`/api/fund/${id}`);
this.setData({ record: normalizeRecord(record) });
} catch (error) {
showError(error, "加载班费详情失败");
} finally {
this.setData({ loading: false });
}
},
previewImage(event) {
const current = event.currentTarget.dataset.src;
const urls = this.data.record && this.data.record.image_urls ? this.data.record.image_urls : [];
if (!current || !urls.length) return;
wx.previewImage({ current, urls });
}
});