直播更新

This commit is contained in:
陈誉午
2026-08-03 11:05:19 +08:00
parent 994b267f5c
commit d0776f9676
12 changed files with 288 additions and 177 deletions
@@ -4,12 +4,14 @@
height: calc(100vh - 162rpx);
display: flex;
flex-direction: column;
padding: 28rpx 20rpx;
}
/* ===== 汇总卡片 ===== */
.summary-section {
padding: 24rpx 24rpx 0;
}
// .summary-section {
// padding: 24rpx 24rpx 0;
// }
.summary-card {
display: flex;
@@ -23,7 +25,6 @@
.total-card {
background: linear-gradient(135deg, #ff4d4f 0%, #ff7875 100%);
margin-bottom: 16rpx;
padding: 28rpx 20rpx;
}
.total-card .card-label {
@@ -72,7 +73,7 @@
}
.tabs-wrap {
margin: 16rpx;
margin-top: 16rpx;
}
/* ===== 红包列表 ===== */
@@ -94,7 +95,7 @@
padding: 24rpx 30rpx;
background: #fff;
border-bottom: 1px solid #eee;
margin: 0 24rpx;
margin: 0;
border-radius: 12rpx;
margin-bottom: 12rpx;
}
@@ -3,14 +3,12 @@ import { request } from "../../../utils/request";
import { receiveRedPacket } from "../../../utils/util";
const app = getApp<IAppOption>();
const pad = (n: number): string => String(n).padStart(2, "0");
const formatDate = (isoString: string): string => {
const date = new Date(isoString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes}`;
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
};
const RED_STATE_MAP: Record<number, string> = {
@@ -27,19 +25,13 @@ const RED_STATE_MAP: Record<number, string> = {
50: "已过期",
};
const getStateText = (state: number) => {
return RED_STATE_MAP[state] || "发放中";
};
const getStateText = (state: number): string => RED_STATE_MAP[state] || "发放中";
const FAILED_STATES = [30, 31, 32, 33, 34];
const canReceive = (state: number) => {
return state === 11 || state === 21 || FAILED_STATES.includes(state);
};
const canReceive = (state: number): boolean => state === 11 || state === 21 || FAILED_STATES.includes(state);
const isRetry = (state: number) => {
return FAILED_STATES.includes(state);
};
const isRetry = (state: number): boolean => FAILED_STATES.includes(state);
Page({
data: {
@@ -60,19 +52,18 @@ Page({
pendingAmount: '0.00',
expiredAmount: '0.00',
},
store: {},
safeBottom: app.globalData.safeBottom,
safeTop: app.globalData.safeTop,
},
async onLoad() {
await this.fetchRecords(1, false, this.data.tabs[0].type);
onLoad() {
this.fetchRecords(1, false, this.data.tabs[0].type);
},
onReachBottom() {
if (this.data.loading || !this.data.hasMore) return;
const nextPage = this.data.current + 1;
this.fetchRecords(nextPage, true, this.data.tabs[this.data.activeTab].type);
const { loading, hasMore, current, activeTab, tabs } = this.data;
if (loading || !hasMore) return;
this.fetchRecords(current + 1, true, tabs[activeTab].type);
},
onTabChange(e: any) {
@@ -84,34 +75,49 @@ Page({
async handleReceive(e: any) {
const { id } = e.currentTarget.dataset;
const { code } = this.data.store as Record<string, any>;
const { code } = app.globalData.store || {};
const params = {
appId,
code,
openId: wx.getStorageSync("openId"),
redId: id,
}
try {
await receiveRedPacket(
params,
{ appId, code, openId: wx.getStorageSync("openId"), redId: id },
'/app/general/receive-red',
appId
);
// 领取成功,刷新当前列表
this.fetchRecords(1, false, this.data.tabs[this.data.activeTab].type);
// 乐观更新:先立即更新本地数据,避免后端状态同步延迟导致展示不友好
const { list, summary, activeTab, tabs } = this.data;
const targetItem = list.find((item: any) => item.id === id);
if (targetItem) {
const amount = targetItem.amount || 0;
const amountYuan = amount / 100;
const updatedList = list.map((item: any) =>
item.id === id
? { ...item, state: 40, stateText: getStateText(40), canReceive: false, isRetry: false }
: item
);
const updatedSummary = {
...summary,
pendingAmount: Math.max(0, parseFloat(summary.pendingAmount) - amountYuan).toFixed(2),
receivedAmount: (parseFloat(summary.receivedAmount) + amountYuan).toFixed(2),
};
this.setData({ list: updatedList, summary: updatedSummary });
}
// 确保后端有足够时间同步,直接刷新
setTimeout(() => {
this.fetchRecords(1, false, tabs[activeTab].type);
}, 3000);
} catch (err) {
console.error('领取红包失败:', err);
}
},
async fetchRecords(page: number, append = false, type: number) {
const app = getApp<IAppOption>();
const openId = wx.getStorageSync("openId");
this.setData({
store: app.globalData.store,
});
if (!openId) {
wx.showToast({ title: "请先登录", icon: "none" });
return;
@@ -119,48 +125,34 @@ Page({
if (append) {
this.setData({ loading: true });
} else {
wx.showLoading({ title: "加载中" });
}
try {
let res: any;
const { state, seeId, code } = this.data.store as Record<string, any>;
const params: Record<string, any> = {};
params.openId = openId;
params.current = page;
params.pageSize = 15;
params.type = type;
const { state, seeId, code } = app.globalData.store || {};
const params: Record<string, any> = {
openId,
current: page,
pageSize: this.data.pageSize,
type,
};
if (code) params.code = code;
if (state) params.state = state;
if (seeId) params.seeId = seeId;
// if (code) {
res = await request({
options: {
url: "/app/video-watch/record",
data: params,
},
isLoading: !append,
// needLogin: false,
});
// } else {
// res = await request({
// options: {
// url: "/live/watch/record",
// data: params,
// },
// isLoading: !append,
// needLogin: false,
// });
// }
const res: any = await request({
options: {
url: "/app/video-watch/record",
data: params,
},
isLoading: !append,
});
const records = res.data?.records || res.records || [];
const items = records.map((item: any) => ({
...item,
date: formatDate(item.createdTime),
stateText: getStateText(item.state),
amountText: `${(item.amount / 100).toFixed(2)}`,
amountText: (item.amount / 100).toFixed(2),
canReceive: canReceive(item.state),
isRetry: isRetry(item.state),
}));
@@ -171,7 +163,6 @@ Page({
hasMore: records.length >= this.data.pageSize,
});
// 汇总数据在 res.data 根层级,单位是分,转为元
const d = res.data;
if (d && d.totalAmount !== undefined) {
this.setData({
@@ -188,8 +179,6 @@ Page({
} finally {
if (append) {
this.setData({ loading: false });
} else {
wx.hideLoading();
}
}
},
@@ -7,14 +7,17 @@
<text class="card-amount total-amount">¥{{summary.totalAmount}}</text>
</view>
<view class="summary-row">
<view class="summary-card sub-card">
<text class="card-label">待领取金额</text>
<text class="card-amount pending">¥{{summary.pendingAmount}}</text>
</view>
<view class="summary-card sub-card">
<text class="card-label">已领取金额</text>
<text class="card-amount received">¥{{summary.receivedAmount}}</text>
</view>
<view class="summary-card sub-card">
<text class="card-label">已过期金额</text>
<text class="card-amount expired">¥{{summary.expiredAmount}}</text>