2025-11-24 22:37:11 +08:00

140 lines
5.0 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const api_appUser = require("../../api/appUser.js");
const _sfc_main = {
__name: "index",
setup(__props) {
const currentTab = common_vendor.ref("pending");
const orders = common_vendor.ref([]);
const loading = common_vendor.ref(false);
const loadingMore = common_vendor.ref(false);
const error = common_vendor.ref("");
const page = common_vendor.ref(1);
const pageSize = common_vendor.ref(20);
const hasMore = common_vendor.ref(true);
function formatTime(t) {
if (!t)
return "";
const d = typeof t === "string" ? new Date(t) : new Date(t);
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, "0");
const day = String(d.getDate()).padStart(2, "0");
const hh = String(d.getHours()).padStart(2, "0");
const mm = String(d.getMinutes()).padStart(2, "0");
return `${y}-${m}-${day} ${hh}:${mm}`;
}
function formatAmount(a) {
if (a === void 0 || a === null)
return "";
const n = Number(a);
if (Number.isNaN(n))
return String(a);
return `¥${n.toFixed(2)}`;
}
function statusText(s) {
const v = String(s || "").toLowerCase();
if (v.includes("pend"))
return "待付款";
if (v.includes("paid") || v.includes("complete") || v.includes("done"))
return "已完成";
return s || "";
}
function switchTab(tab) {
if (currentTab.value === tab)
return;
currentTab.value = tab;
fetchOrders(false);
}
function apiStatus() {
return currentTab.value === "pending" ? "pending" : "completed";
}
async function fetchOrders(append) {
const user_id = common_vendor.index.getStorageSync("user_id");
const token = common_vendor.index.getStorageSync("token");
const phoneBound = !!common_vendor.index.getStorageSync("phone_bound");
if (!user_id || !token || !phoneBound) {
common_vendor.index.showModal({
title: "提示",
content: "请先登录并绑定手机号",
confirmText: "去登录",
success: (res) => {
if (res.confirm) {
common_vendor.index.navigateTo({ url: "/pages/login/index" });
}
}
});
return;
}
if (append) {
if (!hasMore.value || loadingMore.value)
return;
loadingMore.value = true;
page.value = page.value + 1;
} else {
loading.value = true;
page.value = 1;
hasMore.value = true;
orders.value = [];
}
error.value = "";
try {
const list = await api_appUser.getOrders(user_id, apiStatus(), page.value, pageSize.value);
const items = Array.isArray(list) ? list : list && list.items || [];
const total = list && list.total || 0;
orders.value = append ? orders.value.concat(items) : items;
if (total) {
hasMore.value = orders.value.length < total;
} else {
hasMore.value = items.length === pageSize.value;
}
} catch (e) {
error.value = e && (e.message || e.errMsg) || "获取订单失败";
} finally {
if (append) {
loadingMore.value = false;
} else {
loading.value = false;
}
}
}
common_vendor.onLoad((opts) => {
const s = opts && opts.status || "";
if (s === "completed" || s === "pending")
currentTab.value = s;
fetchOrders(false);
});
common_vendor.onReachBottom(() => {
fetchOrders(true);
});
return (_ctx, _cache) => {
return common_vendor.e({
a: currentTab.value === "pending" ? 1 : "",
b: common_vendor.o(($event) => switchTab("pending")),
c: currentTab.value === "completed" ? 1 : "",
d: common_vendor.o(($event) => switchTab("completed")),
e: error.value
}, error.value ? {
f: common_vendor.t(error.value)
} : {}, {
g: orders.value.length === 0 && !loading.value
}, orders.value.length === 0 && !loading.value ? {} : {}, {
h: common_vendor.f(orders.value, (item, k0, i0) => {
return {
a: common_vendor.t(item.title || item.subject || "订单"),
b: common_vendor.t(formatTime(item.created_at || item.time)),
c: common_vendor.t(formatAmount(item.total_amount || item.amount || item.price)),
d: common_vendor.t(statusText(item.status || item.pay_status || item.state)),
e: item.id || item.order_no
};
}),
i: loadingMore.value
}, loadingMore.value ? {} : !hasMore.value && orders.value.length > 0 ? {} : {}, {
j: !hasMore.value && orders.value.length > 0
});
};
}
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-e1e6274e"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/orders/index.js.map