- Added a new method to fetch the default store for users without a login, allowing them to browse store rankings. - Updated the wxLogin function to streamline the login process for users with existing phone numbers, enabling direct token retrieval. - Refactored various page components to utilize the new getDefaultStore method for better user experience when accessing store information. - Enhanced error handling and data synchronization for user and store information across multiple pages.
190 lines
4.6 KiB
JavaScript
190 lines
4.6 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.wxLogin();
|
|
} catch (e) {
|
|
console.error("微信登录失败:", e);
|
|
}
|
|
if (!app.globalData.currentStore) {
|
|
await app.getDefaultStore();
|
|
}
|
|
}
|
|
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" });
|
|
},
|
|
});
|