yingsa/miniprogram/pages/points/mall/index.js
ethanfly 98f9e64d91 refactor: Enhance store and user management features
- Updated user information retrieval to support store-specific points and ladder user data.
- Refactored store initialization logic to prioritize last selected store.
- Improved API endpoints for fetching matches and user data, ensuring compatibility with store context.
- Adjusted UI components to display store-related information consistently across various pages.
- Enhanced error handling and data fetching logic for better user experience.
2026-02-07 11:09:37 +08:00

187 lines
4.5 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();
},
async onShow() {
const store = app.globalData.currentStore;
// 获取当前门店的积分
if (app.globalData.token && store?.storeId) {
try {
await app.getUserInfo(store.storeId);
} catch (e) {
console.error("获取用户信息失败:", e);
}
}
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);
}
}
const store = app.globalData.currentStore;
if (app.globalData.token && store?.storeId) {
try {
await app.getUserInfo(store.storeId);
} 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 store = app.globalData.currentStore;
if (store?.storeId) {
try {
await app.getUserInfo(store.storeId);
} catch (e) {
console.error("刷新积分失败:", e);
}
}
this.setData({
userInfo: app.globalData.userInfo,
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" });
},
});