这版是公共号授权版
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { request } from "../../utils/request";
|
||||
import { appId } from "../../env";
|
||||
import { receiveRedPacket } from "../../utils/util";
|
||||
|
||||
const app = getApp<IAppOption>();
|
||||
|
||||
const formatDate = (isoString: string): string => {
|
||||
const date = new Date(isoString);
|
||||
const year = date.getFullYear();
|
||||
@@ -41,6 +41,13 @@ const isRetry = (state: number) => {
|
||||
return FAILED_STATES.includes(state);
|
||||
};
|
||||
|
||||
// 领取后延迟刷新的定时器 ID,用于在切 tab 或页面卸载时取消
|
||||
let refreshTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
// 缓存登录信息,避免每次请求重复读取 storage
|
||||
let cachedOpenId = wx.getStorageSync("openId");
|
||||
let cachedAppId = wx.getStorageSync("appId");
|
||||
|
||||
Page({
|
||||
data: {
|
||||
list: [] as any[],
|
||||
@@ -60,7 +67,6 @@ Page({
|
||||
pendingAmount: '0.00',
|
||||
expiredAmount: '0.00',
|
||||
},
|
||||
store: {},
|
||||
safeBottom: app.globalData.safeBottom,
|
||||
safeTop: app.globalData.safeTop,
|
||||
},
|
||||
@@ -71,6 +77,8 @@ Page({
|
||||
|
||||
onReachBottom() {
|
||||
if (this.data.loading || !this.data.hasMore) return;
|
||||
// 有延迟刷新待执行时,不触发滚动加载,避免重复请求
|
||||
if (refreshTimer) return;
|
||||
const nextPage = this.data.current + 1;
|
||||
this.fetchRecords(nextPage, true, this.data.tabs[this.data.activeTab].type);
|
||||
},
|
||||
@@ -79,15 +87,31 @@ Page({
|
||||
const index = e.detail.index;
|
||||
const tab = this.data.tabs[index];
|
||||
this.setData({ activeTab: index });
|
||||
// 切换 tab 时取消待执行的延迟刷新,避免用旧 tab 发无效请求
|
||||
if (refreshTimer) {
|
||||
clearTimeout(refreshTimer);
|
||||
refreshTimer = null;
|
||||
}
|
||||
this.fetchRecords(1, false, tab.type);
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 页面卸载时取消待执行的延迟刷新
|
||||
if (refreshTimer) {
|
||||
clearTimeout(refreshTimer);
|
||||
refreshTimer = null;
|
||||
}
|
||||
},
|
||||
|
||||
async handleReceive(e: any) {
|
||||
const { id, outrewardsid } = e.currentTarget.dataset;
|
||||
|
||||
// 防止重复点击
|
||||
if (this.data.loading) return;
|
||||
|
||||
const params: Record<string, any> = {
|
||||
appId: appId,
|
||||
outRewardsId: outrewardsid,
|
||||
openId: wx.getStorageSync("openId"),
|
||||
appId: cachedAppId,
|
||||
openId: cachedOpenId,
|
||||
};
|
||||
if (outrewardsid) params.outRewardsId = outrewardsid;
|
||||
if (id) params.redId = id;
|
||||
@@ -96,11 +120,11 @@ Page({
|
||||
await receiveRedPacket(
|
||||
params,
|
||||
'/app/general/receive-red',
|
||||
appId
|
||||
cachedAppId
|
||||
);
|
||||
|
||||
// 乐观更新:先立即更新本地数据,避免后端状态同步延迟导致展示不友好
|
||||
const { list, summary, activeTab, tabs } = this.data;
|
||||
const { list, summary } = this.data;
|
||||
const targetItem = list.find((item: any) => item.id === id);
|
||||
|
||||
if (targetItem) {
|
||||
@@ -122,22 +146,21 @@ Page({
|
||||
this.setData({ list: updatedList, summary: updatedSummary });
|
||||
}
|
||||
|
||||
// 确保后端有足够时间同步,直接刷新
|
||||
setTimeout(() => {
|
||||
this.fetchRecords(1, false, tabs[activeTab].type);
|
||||
// 确保后端有足够时间同步后刷新,使用实时 activeTab 避免闭包过期
|
||||
this.setData({ loading: true });
|
||||
refreshTimer = setTimeout(() => {
|
||||
refreshTimer = null;
|
||||
this.setData({ loading: false });
|
||||
this.fetchRecords(1, false, this.data.tabs[this.data.activeTab].type);
|
||||
}, 3000);
|
||||
} catch (err) {
|
||||
this.setData({ loading: false });
|
||||
console.error('领取红包失败:', err);
|
||||
}
|
||||
},
|
||||
|
||||
async fetchRecords(page: number, append = false, type: number) {
|
||||
const openId = wx.getStorageSync("openId");
|
||||
|
||||
this.setData({
|
||||
store: app.globalData.store,
|
||||
});
|
||||
if (!openId) {
|
||||
if (!cachedOpenId) {
|
||||
wx.showToast({ title: "请先登录", icon: "none" });
|
||||
return;
|
||||
}
|
||||
@@ -149,20 +172,18 @@ Page({
|
||||
}
|
||||
|
||||
try {
|
||||
const params: Record<string, any> = {};
|
||||
params.openId = openId;
|
||||
params.appId = appId;
|
||||
params.current = page;
|
||||
params.pageSize = 15;
|
||||
params.type = type;
|
||||
|
||||
const res = await request({
|
||||
options: {
|
||||
url: "/app/saas/query-red",
|
||||
data: params,
|
||||
data: {
|
||||
openId: cachedOpenId,
|
||||
appId: cachedAppId,
|
||||
current: page,
|
||||
pageSize: this.data.pageSize,
|
||||
type,
|
||||
},
|
||||
},
|
||||
isLoading: !append,
|
||||
// needLogin: false,
|
||||
});
|
||||
|
||||
const records = res.data?.records || res.records || [];
|
||||
@@ -203,4 +224,4 @@ Page({
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user