- 新增选手资料页面,支持查看选手信息及比赛记录 - 添加多个SVG图标替换原有emoji,提升视觉一致性 - 扩展CSS变量系统,增加信息、成功、警告、危险等状态颜色 - 优化多个页面的样式,统一使用CSS变量和现代设计语言 - 修复可选链操作符兼容性问题,改用传统条件判断 - 改进数据加载逻辑,使用Object.assign替代展开运算符 - 调整开发环境配置,使用本地服务器地址
166 lines
3.8 KiB
JavaScript
166 lines
3.8 KiB
JavaScript
const app = getApp();
|
|
|
|
Page({
|
|
data: {
|
|
userInfo: null,
|
|
currentStore: null,
|
|
products: [],
|
|
loading: false,
|
|
page: 1,
|
|
pageSize: 20,
|
|
hasMore: true,
|
|
showProductModal: false,
|
|
currentProduct: null,
|
|
},
|
|
|
|
onLoad() {
|
|
this.initData();
|
|
},
|
|
|
|
onShow() {
|
|
this.setData({
|
|
userInfo: app.globalData.userInfo,
|
|
currentStore: app.globalData.currentStore,
|
|
});
|
|
|
|
// 门店切换后刷新商品
|
|
if (app.globalData.storeChanged) {
|
|
app.globalData.storeChanged = false;
|
|
this.setData({ page: 1, hasMore: true, products: [] });
|
|
this.fetchProducts();
|
|
}
|
|
},
|
|
|
|
onPullDownRefresh() {
|
|
this.setData({ page: 1, hasMore: true });
|
|
this.fetchProducts().then(() => {
|
|
wx.stopPullDownRefresh();
|
|
});
|
|
},
|
|
|
|
onReachBottom() {
|
|
if (this.data.hasMore && !this.data.loading) {
|
|
this.loadMore();
|
|
}
|
|
},
|
|
|
|
async initData() {
|
|
if (!app.globalData.token) {
|
|
try {
|
|
await app.login();
|
|
} catch (e) {
|
|
console.error("登录失败:", e);
|
|
}
|
|
}
|
|
|
|
this.setData({
|
|
userInfo: app.globalData.userInfo,
|
|
currentStore: app.globalData.currentStore,
|
|
});
|
|
this.fetchProducts();
|
|
},
|
|
|
|
async fetchProducts() {
|
|
this.setData({ loading: true });
|
|
|
|
try {
|
|
const params = {
|
|
page: this.data.page,
|
|
pageSize: this.data.pageSize,
|
|
};
|
|
|
|
// 根据当前门店筛选商品
|
|
if (this.data.currentStore && this.data.currentStore.storeId) {
|
|
params.store_id = this.data.currentStore.storeId;
|
|
}
|
|
|
|
const res = await app.request("/api/points/products", params);
|
|
|
|
const products = res.data.list || [];
|
|
this.setData({
|
|
products:
|
|
this.data.page === 1 ? products : this.data.products.concat(products),
|
|
hasMore: products.length >= this.data.pageSize,
|
|
});
|
|
} catch (e) {
|
|
console.error("获取商品列表失败:", e);
|
|
} finally {
|
|
this.setData({ loading: false });
|
|
}
|
|
},
|
|
|
|
loadMore() {
|
|
this.setData({ page: this.data.page + 1 });
|
|
this.fetchProducts();
|
|
},
|
|
|
|
viewProduct(e) {
|
|
const product = e.currentTarget.dataset.product;
|
|
this.setData({
|
|
currentProduct: product,
|
|
showProductModal: true,
|
|
});
|
|
},
|
|
|
|
closeProductModal() {
|
|
this.setData({ showProductModal: false });
|
|
},
|
|
|
|
async exchangeProduct() {
|
|
const product = this.data.currentProduct;
|
|
|
|
wx.showModal({
|
|
title: "确认兑换",
|
|
content: `确定使用 ${product.pointsRequired} 积分兑换「${product.name}」吗?\n请到 ${product.storeName} 领取`,
|
|
success: async (res) => {
|
|
if (!res.confirm) return;
|
|
|
|
wx.showLoading({ title: "兑换中..." });
|
|
|
|
try {
|
|
const exchangeRes = await app.request(
|
|
"/api/points/exchange",
|
|
{
|
|
product_id: product.id,
|
|
},
|
|
"POST",
|
|
);
|
|
|
|
wx.hideLoading();
|
|
|
|
// 更新用户积分
|
|
const newPoints =
|
|
this.data.userInfo.totalPoints - product.pointsRequired;
|
|
app.globalData.userInfo.totalPoints = newPoints;
|
|
this.setData({
|
|
"userInfo.totalPoints": newPoints,
|
|
showProductModal: false,
|
|
});
|
|
|
|
wx.showModal({
|
|
title: "兑换成功",
|
|
content: `请到 ${product.storeName} 出示兑换码领取\n兑换码: ${exchangeRes.data.exchangeCode}`,
|
|
showCancel: false,
|
|
success: () => {
|
|
wx.navigateTo({ url: "/pages/points/order/index" });
|
|
},
|
|
});
|
|
|
|
this.fetchProducts();
|
|
} catch (e) {
|
|
wx.hideLoading();
|
|
console.error("兑换失败:", e);
|
|
}
|
|
},
|
|
});
|
|
},
|
|
|
|
goToRecords() {
|
|
wx.navigateTo({ url: "/pages/points/records/index" });
|
|
},
|
|
|
|
goToOrders() {
|
|
wx.navigateTo({ url: "/pages/points/order/index" });
|
|
},
|
|
});
|