This commit is contained in:
2025-10-11 16:19:57 +08:00
parent 45ec2bbb7d
commit fdea1383d0
27 changed files with 1662 additions and 977 deletions
+208 -13
View File
@@ -6,6 +6,7 @@ import {
Label,
Sprite,
UITransform,
ScrollView,
} from "cc";
import li_BaseView from "db://assets/Scripts/Main/Common/li_BaseView";
import Utils from "db://assets/Scripts/Main/Common/Utils";
@@ -67,6 +68,13 @@ export class ThemePanel extends li_BaseView {
private themeScroll: Node;
private recomandScroll: Node;
private recomandScrollView: ScrollView = null;
// 推荐列表分页状态
private currentPage: number = 1;
private pageLimit: number = 10;
private isLoadingMore: boolean = false;
private hasMoreData: boolean = true;
onLoadCT() {
Utils.parseNode(this.node, this._nodeTab);
@@ -81,6 +89,12 @@ export class ThemePanel extends li_BaseView {
this.themeScroll = this._nodeTab.themeScroll;
this.recomandScroll = this._nodeTab.recomandScroll;
// 获取推荐列表的 ScrollView 组件
if (this.recomandScroll) {
this.recomandScrollView =
this.recomandScroll.getComponentInChildren(ScrollView);
}
this.registerListener();
this.itemInst = this._nodeTab.ThemeItem.getComponent(ThemeItem);
this.itemInst.node.active = false;
@@ -145,7 +159,7 @@ export class ThemePanel extends li_BaseView {
logger.log("[ThemePanel] 开始强制刷新数据");
await Promise.all([
MainController.I.refreshThemeData(),
MainController.I.refreshDailyRecommend(),
MainController.I.refreshRecommendList(),
]);
logger.log("[ThemePanel] 数据刷新完成");
@@ -167,8 +181,8 @@ export class ThemePanel extends li_BaseView {
throw new Error("Operation was aborted");
}
// 处理每日推荐数据
await this.handleRecommendData(preloadStatus.dailyRecommend);
// 处理推荐列表数据
await this.handleRecommendData(preloadStatus.recommendList);
// 检查是否已被取消
if (signal.aborted) {
@@ -240,7 +254,7 @@ export class ThemePanel extends li_BaseView {
*/
private async handleRecommendData(isPreloaded: boolean): Promise<void> {
if (isPreloaded) {
logger.log("[ThemePanel] 使用预加载的每日推荐数据");
logger.log("[ThemePanel] 使用预加载的推荐列表数据");
// 直接使用预加载的数据渲染界面
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
if (girlData) {
@@ -249,25 +263,35 @@ export class ThemePanel extends li_BaseView {
}
// 如果没有预加载数据,则请求数据
logger.log("[ThemePanel] 请求每日推荐数据");
const reqData = {};
let res = await GirlService.I.reqDailyRecommend(reqData);
logger.log("[ThemePanel] 请求推荐列表数据");
const reqData = {
page: 1,
limit: 10,
};
let res = await GirlService.I.reqRecommendList(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
girlData.setDailyRecommend(res.data);
girlData.setRecommendList(res.data);
}
}
/**
* 渲染每日推荐数据
* 渲染推荐列表数据(首次渲染)
*/
private renderRecommendData(): void {
if (this.recItemCache.length > 0) return;
// 重置分页状态
this.currentPage = 1;
this.hasMoreData = true;
this.isLoadingMore = false;
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
let girlIDs = [];
girlIDs.push(girlData.getRecommendGirlId());
// 获取推荐列表的所有 ID
let girlIDs = girlData.getRecommendListIds();
// 遍历创建推荐项
for (let id of girlIDs) {
let newNode = instantiate(this.recItemInst.node);
let item = newNode.getComponent(RecomandItem);
@@ -278,7 +302,127 @@ export class ThemePanel extends li_BaseView {
this.recGirlContent.addChild(newNode);
}
console.error("未实现每日推荐数据渲染");
// 检查首次加载的数据量,判断是否还有更多数据
if (girlIDs.length < this.pageLimit) {
this.hasMoreData = false;
}
logger.log(`[ThemePanel] 渲染推荐列表完成,共 ${girlIDs.length} 个推荐项`);
}
/**
* 滚动到底部回调(Cocos Creator 内置事件)
*/
private onRecommendScrollToBottom(): void {
logger.log("[ThemePanel] 推荐列表滚动到底部");
this.loadMoreRecommendData();
}
/**
* 滚动中回调,用于检测接近底部
*/
private onRecommendScrolling(): void {
if (!this.recomandScrollView) return;
// 获取滚动进度(0-1
const scrollOffset = this.recomandScrollView.getScrollOffset();
const maxScrollOffset = this.recomandScrollView.getMaxScrollOffset();
// 当滚动到距离底部 100 像素以内时,开始加载
if (maxScrollOffset.y > 0 && scrollOffset.y >= maxScrollOffset.y - 100) {
this.loadMoreRecommendData();
}
}
/**
* 加载下一页推荐数据
*/
private async loadMoreRecommendData(): Promise<void> {
// 防止重复加载
if (this.isLoadingMore) {
logger.log("[ThemePanel] 正在加载中,跳过");
return;
}
// 检查是否还有更多数据
if (!this.hasMoreData) {
logger.log("[ThemePanel] 没有更多数据了");
return;
}
try {
this.isLoadingMore = true;
this.currentPage++;
logger.log(`[ThemePanel] 开始加载第 ${this.currentPage} 页推荐数据`);
const reqData = {
page: this.currentPage,
limit: this.pageLimit,
};
const res = await GirlService.I.reqRecommendList(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
// 追加数据到 GirlData
girlData.addRecommendList(res.data);
// 检查是否还有更多数据
const girls = res.data.girls || [];
if (girls.length < this.pageLimit) {
this.hasMoreData = false;
logger.log("[ThemePanel] 已加载所有推荐数据");
}
// 渲染新增数据
this.renderMoreRecommendData(girls.length);
logger.log(
`[ThemePanel] 第 ${this.currentPage} 页加载完成,新增 ${girls.length}`
);
} else {
logger.warn("[ThemePanel] 加载下一页推荐数据失败:", res);
// 加载失败,回退页码
this.currentPage--;
}
} catch (error) {
logger.error("[ThemePanel] 加载下一页推荐数据异常:", error);
// 加载失败,回退页码
this.currentPage--;
} finally {
this.isLoadingMore = false;
}
}
/**
* 渲染新增的推荐数据
* @param newCount 新增数据的数量
*/
private renderMoreRecommendData(newCount: number): void {
if (newCount <= 0) return;
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
const allGirlIDs = girlData.getRecommendListIds();
// 只渲染新增的部分(从末尾开始取 newCount 个)
const startIndex = allGirlIDs.length - newCount;
const newGirlIDs = allGirlIDs.slice(startIndex);
logger.log(`[ThemePanel] 开始渲染 ${newCount} 个新增推荐项`);
for (let id of newGirlIDs) {
let newNode = instantiate(this.recItemInst.node);
let item = newNode.getComponent(RecomandItem);
item.init();
item.refresh(id);
newNode.active = true;
this.recItemCache.push(item);
this.recGirlContent.addChild(newNode);
}
logger.log(`[ThemePanel] 新增推荐项渲染完成`);
}
/**
@@ -302,6 +446,20 @@ export class ThemePanel extends li_BaseView {
GButton.BandClick(this.recomandListBtn, this.openRecomandList, this);
GButton.BandClick(this.moreThemeBtn, this.openMoreTheme, this);
// 注册推荐列表滚动事件
if (this.recomandScrollView) {
this.recomandScrollView.node.on(
"scroll-to-bottom",
this.onRecommendScrollToBottom,
this
);
this.recomandScrollView.node.on(
"scrolling",
this.onRecommendScrolling,
this
);
}
Utils.addInnerEL(InnerMsgCode.LanguageChange, this, this.onLanguageChange);
Utils.addInnerEL(InnerMsgCode.BalanceChange, this, this.onBalanceChange);
Utils.addInnerEL(
@@ -394,8 +552,9 @@ export class ThemePanel extends li_BaseView {
* 清理缓存节点
*/
private clearCache(): void {
// 清理主题缓存
if (this.cache && this.cache.length > 0) {
logger.log(`[ThemePanel] 清理 ${this.cache.length} 个缓存节点`);
logger.log(`[ThemePanel] 清理 ${this.cache.length}主题缓存节点`);
for (let i = this.cache.length - 1; i >= 0; i--) {
if (this.cache[i] && this.cache[i].node && this.cache[i].node.isValid) {
this.cache[i].node.destroy();
@@ -403,6 +562,28 @@ export class ThemePanel extends li_BaseView {
}
this.cache = [];
}
// 清理推荐项缓存
if (this.recItemCache && this.recItemCache.length > 0) {
logger.log(
`[ThemePanel] 清理 ${this.recItemCache.length} 个推荐项缓存节点`
);
for (let i = this.recItemCache.length - 1; i >= 0; i--) {
if (
this.recItemCache[i] &&
this.recItemCache[i].node &&
this.recItemCache[i].node.isValid
) {
this.recItemCache[i].node.destroy();
}
}
this.recItemCache = [];
}
// 重置推荐列表分页状态
this.currentPage = 1;
this.hasMoreData = true;
this.isLoadingMore = false;
}
/**
@@ -437,6 +618,20 @@ export class ThemePanel extends li_BaseView {
this.isLoading = false;
this.loadingPromise = null;
// 移除推荐列表滚动事件监听
if (this.recomandScrollView) {
this.recomandScrollView.node.off(
"scroll-to-bottom",
this.onRecommendScrollToBottom,
this
);
this.recomandScrollView.node.off(
"scrolling",
this.onRecommendScrolling,
this
);
}
// 移除事件监听器(修复回调函数引用问题)
Utils.removeInnerEL(
InnerMsgCode.LanguageChange,