cb/assets/Script/Message.ts

871 lines
36 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Utils from "./module/Pay/Utils";
import { MiniGameSdk } from "./Sdk/MiniGameSdk";
const { ccclass, property } = cc._decorator;
@ccclass
export default class Message extends cc.Component {
@property(cc.Label)
selectLabel: cc.Label = null;
@property(cc.Label)
jushiLabel: cc.Label = null;
@property(cc.Node)
node2: cc.Node = null;
@property(cc.Node)
node3: cc.Node = null;
@property(cc.Prefab)
contentNode: cc.Prefab = null;
@property(cc.Prefab)
optionNode: cc.Prefab = null;
@property(cc.Prefab)
menuLabel: cc.Prefab = null;
private comment: any[] = [];
private jushiList: any = null;
private currentJushi: any = null;
private currentOption: any = null;
// 存储所有内容节点的数组
private contentNodes: cc.Node[] = [];
private selectIndex: number = -1;
private recordMenu: any = null;
// 存储每列的值key是列索引(0,1,2...)
private columnValues: Map<number, string> = new Map();
// 保存原始模板
private originalTemplate: string = '';
// 记录占位符总数
private placeholderCount: number = 0;
private _pos: number = 1;
protected onLoad(): void {
}
start() {
}
showNode(nodeType: any, pos: number) {
this.node.getChildByName("commentNode").active = false;
this.node.getChildByName("levelWordNode").active = false;
this._pos = pos;
console.log("nodeType", nodeType);
if (nodeType === "commentNode") {
this.node.getChildByName("commentNode").active = true;
this.onShowComment();
} else {
this.node.getChildByName("levelWordNode").active = true;
this.onShowLevelWord();
}
}
onShowComment() {
// const passCheckJson = cc.fx.GameConfig.PASS_CHECK;
const message = cc.fx.GameConfig.MESSAGE_MODEL;
// 解析JSON数据并赋值给comment
// const jsonData = jsonAsset.json;
console.log("解析后的JSON数据:", message);
// 遍历jsonData的所有键
for (const key in message) {
if (Array.isArray(message[key])) {
if (key === "模板") {
// 将"模板"键的数据赋值给this.jushiList
// this.jushiList = jsonData[key];
this.jushiList = {
keyName: key, // 使用键名作为标识
data: message[key] // 保存对应的数据
};
} else {
// 将其他键的数据添加到this.comment中
this.comment.push({
keyName: key, // 使用键名作为标识
data: message[key] // 保存对应的数据
});
}
}
}
this.onCreateJushi();
console.log("this.jushiList", this.jushiList);
console.log("this.comment", this.comment);
// });
}
onCreateJushi() {
// 创建contentNode节点
this.currentJushi = cc.instantiate(this.contentNode);
this.node2.addChild(this.currentJushi);
this.currentJushi.active = false;
let contentBg = this.currentJushi.getChildByName("contentBg");
const contentHeight = this.jushiList.data.length * 60 + 20;
contentBg.setContentSize(768, contentHeight);
// 获取contentNode上的脚本组件
const contentScript = this.currentJushi.getComponent('MessageContent');
// 定义回调函数
const callback = (index: number, data: any) => {
// 处理回调逻辑
console.log("回调数据:", index, data);
// this.selectLabel.string = data.context;
// this.onJushiClick(index, data);
// this.node3.active = true;
if (data && data.context) {
// 保存原始模板
this.originalTemplate = data.context;
// 统计占位符数量
const matches = data.context.match(/\{0\}/g);
this.placeholderCount = matches ? matches.length : 0;
let updatedText = data.context;
for (let i = 0; i < this.placeholderCount; i++) {
console.log("数据显示", i);
}
this.selectLabel.string = data.context;
// 创建对应数量的creatorOptionNodes
this.createJushiOptionNodes(this.placeholderCount);
}
this.onJushiClick(index, data);
};
if (contentScript) {
// 设置数据
contentScript.setContentData(this.jushiList.data, callback);
}
// 创建optionNode节点
// this.createOptionNodes();
}
createJushiOptionNodes(placeholderCount: number) {
// 清除现有的jushi选项节点
this.node3.removeAllChildren();
this.contentNodes = []; // 同时清空内容节点数组
this.selectIndex = -1; // 重置选择索引
this.recordMenu = null; // 重置记录的菜单
this.node3.active = true;
// 重置占位符值映射
this.columnValues.clear();
// 重置原始模板和占位符数量
this.placeholderCount = placeholderCount;
// 初始化占位符值数组
for (let i = 0; i < placeholderCount; i++) {
// 创建基础的选项节点
this.createOptionNodes(i);
}
}
// 创建optionNode节点
createOptionNodes(index: number) {
// 定义节点尺寸和间距
const itemWidth = 240; // 节点宽度
const itemHeight = 98; // 节点高度
const spacingX = 20; // X轴间距
const spacingY = 22; // Y轴间距 (120 - 98)
let node = new cc.Node();
// node.name = `placeholder_${index}`;
node.groupIndex = index;
this.node3.addChild(node, this.placeholderCount - index);
// 为不同组的选项设置不同的Y位置但确保都在可见区域内
node.setPosition(0, -index * 300); // 使用固定的间距来避免位置计算错误
console.log("创建选项节点", index, -index * 260, node.position);
// 遍历this.comment数组创建相应数量的optionNode
for (let i = 0; i < this.comment.length; i++) {
const optionNode = cc.instantiate(this.optionNode);
// this.node3.addChild(optionNode, this.comment.length-i);
node.addChild(optionNode, this.comment.length - i);
console.log("设置大小", this.node3.getContentSize());
// 计算位置每排3个上下排列
const row = Math.floor(i / 3); // 行号0为上排1为下排以此类推
const col = i % 3; // 列号0, 1, 2
// 设置位置基于宽240高98的元素
// 以node3的锚点(0.5, 1)为参考点,从顶部开始向下排列
const totalColumnsInRow = Math.min(3, this.comment.length - row * 3); // 当前行的总列数
// 计算起始X位置使每行节点居中显示
const startX = (-(totalColumnsInRow - 1)) * (itemWidth + spacingX) / 2; // 计算起始X位置使整行居中
const x = startX + col * (itemWidth + spacingX); // 每个节点间隔为宽度+间距
// 从顶部开始,依次向下排列(使用负值,因为锚点在顶部)
const y = -row * (itemHeight + spacingY); // 从上到下分布,每行间距为高度+间距
optionNode.setPosition(x, y);
// 获取contentLabel并设置文本为keyName
const contentLabel = optionNode.getChildByName("contentLabel");
if (contentLabel) {
const labelComponent = contentLabel.getComponent(cc.Label);
if (labelComponent) {
labelComponent.string = this.comment[i].keyName;
}
}
const menuBtn = optionNode.getChildByName("menu");
const background_1 = menuBtn ? menuBtn.getChildByName("background_1") : null;
const background_2 = menuBtn ? menuBtn.getChildByName("background_2") : null;
if (background_2) background_2.active = false;
// 存储选项节点和其背景状态的映射关系
if (menuBtn) {
// 使用闭包保存i的值创建一个唯一的索引
const optionIndex = index * this.comment.length + i; // 计算全局索引
menuBtn.on(cc.Node.EventType.TOUCH_END, (event) => {
console.log(`this.comment[${i}] = `, this.comment[i]);
// 可以在这里添加更多处理逻辑
if (background_1) background_1.active = false;
if (background_2) background_2.active = true;
this.onCreateOption(optionIndex, this.comment[i], menuBtn, index);
}, this)
if (this.comment[i].data && Array.isArray(this.comment[i].data)) {
// 创建内容节点
const contentNode = cc.instantiate(this.contentNode);
optionNode.addChild(contentNode);
this.contentNodes.push(contentNode);
// 设置内容节点的位置(默认隐藏在选项节点下方)
contentNode.setPosition(0, -contentNode.height / 2 - 60);
contentNode.active = false;
// 获取contentBg并设置尺寸
let contentBg = contentNode.getChildByName("contentBg");
if (contentBg) {
const contentHeight = this.comment[i].data.length * 60 + 20;
contentBg.setContentSize(268, contentHeight); // 宽默认268高度根据数据长度计算
// 获取contentNode上的脚本组件
const contentScript = contentNode.getComponent('MessageContent');
// const dataContext =
// 定义回调函数
const callback = (num: number, data: any) => {
// 处理回调逻辑
console.log("内容节点回调数据:", num, data, `来自第${i}个选项`);
this.onCreateOption();
// this.selectLabel
if (this.selectLabel && data && data.context) {
// 设置指定列的值
this.columnValues.set(index, data.context);
// 更新显示
this.updateDisplay();
// 检查是否所有占位符都已替换
let allReplaced = true;
for (let j = 0; j < this.placeholderCount; j++) {
if (!this.columnValues.has(j)) {
allReplaced = false;
break;
}
}
if (allReplaced) {
this.node3.active = false;
}
// 重置所有选项的背景状态
this.resetAllOptionBackgrounds();
}
};
if (contentScript) {
// 设置数据
contentScript.setContentData(this.comment[i].data, callback);
}
}
}
}
}
// 动态调整node3的尺寸以适应所有选项节点
if (this.comment.length > 0) {
const totalRows = Math.ceil(this.comment.length / 3);
const totalColumns = Math.min(3, this.comment.length); // 第一行的列数
// 计算node3需要的宽度和高度
const requiredWidth = totalColumns > 1 ? (totalColumns - 1) * spacingX + totalColumns * itemWidth : itemWidth;
const requiredHeight = (totalRows - 1) * (itemHeight + spacingY) + itemHeight;
// 设置node3的尺寸稍微增加一些边距
this.node3.width = requiredWidth + 20; // 添加10像素边距
this.node3.height = requiredHeight + 20; // 添加10像素边距
}
}
// 重置所有选项的背景状态为白色
resetAllOptionBackgrounds() {
for (let j = 0; j < this.node3.childrenCount; j++) {
const groupNode = this.node3.children[j];
// 遍历组节点的所有子节点(即选项节点)
for (let k = 0; k < groupNode.childrenCount; k++) {
const optionNode = groupNode.children[k];
const menuBtn = optionNode.getChildByName("menu");
const background_1 = menuBtn ? menuBtn.getChildByName("background_1") : null;
const background_2 = menuBtn ? menuBtn.getChildByName("background_2") : null;
if (background_1) background_1.active = true;
if (background_2) background_2.active = false;
}
}
}
private updateChildNodeVisibility(selectedIndex: number) {
if (!this.node3) return;
console.log("updateChildNodeVisibility", selectedIndex);
// 如果当前索引小于选中的索引,则隐藏
// if (i < selectedIndex) {
// child.active = false;
// } else {
// // 否则显示
// child.active = true;
// }
if (selectedIndex === 1) {
for (let i = 0; i < this.node3.childrenCount; i++) {
const child = this.node3.children[i];
if (i === 2) {
child.active = false;
}
}
}
if (selectedIndex === 2) {
for (let i = 0; i < this.node3.childrenCount; i++) {
const child = this.node3.children[i];
if (i > 0) {
child.active = false;
}
}
}
}
onCreateOption(index?: number, data?: any, clickedMenuBtn?: cc.Node, optionIndex?: number) {
// 计算node3的位置使用固定的y值
if (optionIndex !== undefined && optionIndex >= 0) {
// 根据点击的索引设置node3的固定y位置
let targetY = 0;
switch (optionIndex) {
case 0:
targetY = 0;
break;
case 1:
targetY = 300;
break;
case 2:
targetY = 600;
break;
default:
targetY = optionIndex * 300;
break;
}
console.log("node3的y位置:", optionIndex);
if (this.node3) {
this.node3.y = 218 + targetY;
if (optionIndex !== 0) {
this.updateChildNodeVisibility(optionIndex);
}
}
} else {
// 如果没有有效索引即关闭所有子节点则设置为默认位置index为0的位置
if (this.node3) {
this.node3.y = 218; // 回到index为0的位置
for (let i = 0; i < this.node3.childrenCount; i++) {
const child = this.node3.children[i];
child.active = true;
}
}
}
if (this.selectIndex !== -1) {
this.recordMenu.getChildByName("menu");
this.recordMenu.getChildByName("background_1").active = true;
this.recordMenu.getChildByName("background_2").active = false;
}
if (index === undefined || index < 0) {
// 如果没有提供有效索引,隐藏所有内容节点
for (let i = 0; i < this.contentNodes.length; i++) {
if (this.contentNodes[i]) {
this.contentNodes[i].active = false;
}
}
// 重置选择索引
this.selectIndex = -1;
if (this.node3) {
this.node3.y = 218;
for (let i = 0; i < this.node3.childrenCount; i++) {
const child = this.node3.children[i];
child.active = true;
}
}
return;
}
// 检查索引是否在有效范围内
if (index >= this.contentNodes.length) {
console.log("无效索引");
return;
}
// 检查是否与上次点击的是同一个选项
if (this.selectIndex === index) {
// 如果是同一个选项,则隐藏对应的内容节点
if (this.contentNodes[index]) {
this.contentNodes[index].active = false;
}
this.selectIndex = -1;
if (this.node3) {
this.node3.y = 218;
}
} else {
// 隐藏所有内容节点
for (let i = 0; i < this.contentNodes.length; i++) {
if (this.contentNodes[i]) {
this.contentNodes[i].active = false;
}
}
// 显示当前点击的内容节点
if (this.contentNodes[index]) {
this.contentNodes[index].active = true;
}
// 更新选择索引为当前索引
this.selectIndex = index;
this.recordMenu = clickedMenuBtn;
}
}
onJushiClick(event, customData) {
if (this.currentJushi) {
if (this.currentJushi.active) {
this.currentJushi.active = false;
} else {
this.currentJushi.active = true;
}
}
}
onCloseClick(event, customData) {
console.log("onCloseClick", event, customData);
this.node.destroy();
}
onUpClick(event, customData) {
console.log("onUpClick", event, customData);
// 获取当前玩家的名称、头像和当前关卡等级
const playerName = cc.fx.GameConfig.GM_INFO.username;
let playerAvatar = cc.fx.GameConfig.GM_INFO.useravatar;
if (cc.fx.GameConfig.GM_INFO.useravatarIcon.length > 10) {
console.log("获取头像链接:", cc.fx.GameConfig.GM_INFO.useravatarIcon);
playerAvatar = cc.fx.GameConfig.GM_INFO.useravatarIcon;
}
const currentLevel = cc.fx.GameConfig.GM_INFO.level;
// 获取留言内容
const messageContent = this.selectLabel.string; // 使用当前显示的消息内容
let data = {
name: playerName,
head: playerAvatar,
content: messageContent,
};
// 输出信息到控制台
console.log("玩家名称:", playerName);
console.log("玩家头像:", playerAvatar);
console.log("当前关卡等级:", currentLevel);
console.log("留言内容:", messageContent);
console.log("lick 0"); // 按照要求添加
Utils.setCommentInfo((res) => {
if (res.code == 1) {
// cc.fx.GameTool.shushu_Track("comment", data);
// cc.fx.GameTool.typingAni(this.selectLabel, messageContent, () => {
// this.onCloseClick(null, null);
// }, this.node);
console.log("留言成功", res.msg);
MiniGameSdk.API.showToast(res.msg);
}
}, data, this._pos)
// 删除当前节点
// this.node.destroy();
}
updateDisplay() {
if (this.selectLabel) {
this.selectLabel.string = this.getFinalResult();
}
}
getFinalResult(): string {
if (!this.originalTemplate) {
return '';
}
let result = this.originalTemplate;
let matchIndex = 0;
result = result.replace(/\{0\}/g, () => {
// 如果该位置有设置的值,就用设置的值
if (this.columnValues.has(matchIndex)) {
matchIndex++;
return this.columnValues.get(matchIndex - 1);
}
// 否则保持占位符
matchIndex++;
return "{0}";
});
return result;
}
setColumnValue(columnIndex: number, value: string) {
if (columnIndex < 0 || columnIndex >= this.placeholderCount) {
cc.warn("列索引超出范围:", columnIndex);
return;
}
this.columnValues.set(columnIndex, value);
this.updateDisplay(); // 实时更新显示
}
// 清除指定列的值
clearColumnValue(columnIndex: number) {
this.columnValues.delete(columnIndex);
this.updateDisplay();
}
onShowLevelWord() {
let levelWordNode = this.node.getChildByName("levelWordNode");
let showNum = 3;
// 检查是否存在labelContainer节点如果没有则创建
let labelContainer = levelWordNode.getChildByName("labelContainer");
if (!labelContainer) {
// 如果场景中没有预设的labelContainer节点则创建一个新的
labelContainer = new cc.Node("labelContainer");
labelContainer.parent = levelWordNode;
labelContainer.zIndex = 100; // 确保label在正确层级
}
let comment = null;
// let commentData = [{
// content: "首先,方块吧",
// head: "https://thirdwx.qlogo.cn/mmopen/vi_32/oCAezhMhThqmsb7ovGhC4ZDU7QoBCYqbLlAJqOofpzkCSOzqmzlXVqVc87ib1Oo9f6HOgbFLSJq83AIefT0M7xTkaE3Jb0kaFKRlYkhJol6M/132",
// index: 0,
// like: 2310,
// name: "卡越来越",
// uid: "68d0edf0cc79706182bda53f"
// }, {
// content: "首先2*2方块吧",
// head: "https://thirdwx.qlogo.cn/mmopen/vi_32/oCAezhMhThqmsb7ovGhC4ZDU7QoBCYqbLlAJqOofpzkCSOzqmzlXVqVc87ib1Oo9f6HOgbFLSJq83AIefT0M7xTkaE3Jb0kaFKRlYkhJol6M/132",
// index: 0,
// like: 110,
// name: "卡越来越",
// uid: "68d0edf0cc79706182bda53f"
// }, {
// content: "首先1x3方块吧",
// head: "https://thirdwx.qlogo.cn/mmopen/vi_32/oCAezhMhThqmsb7ovGhC4ZDU7QoBCYqbLlAJqOofpzkCSOzqmzlXVqVc87ib1Oo9f6HOgbFLSJq83AIefT0M7xTkaE3Jb0kaFKRlYkhJol6M/132",
// index: 0,
// like: 120,
// name: "卡越来越",
// uid: "68d0edf0cc79706182bda53f"
// }, {
// content: "首先,方块吧",
// head: "https://thirdwx.qlogo.cn/mmopen/vi_32/oCAezhMhThqmsb7ovGhC4ZDU7QoBCYqbLlAJqOofpzkCSOzqmzlXVqVc87ib1Oo9f6HOgbFLSJq83AIefT0M7xTkaE3Jb0kaFKRlYkhJol6M/132",
// index: 0,
// like: 0,
// name: "卡越来越",
// uid: "68d0edf0cc79706182bda53f"
// }, {
// content: "首先,方块吧",
// head: "https://thirdwx.qlogo.cn/mmopen/vi_32/oCAezhMhThqmsb7ovGhC4ZDU7QoBCYqbLlAJqOofpzkCSOzqmzlXVqVc87ib1Oo9f6HOgbFLSJq83AIefT0M7xTkaE3Jb0kaFKRlYkhJol6M/132",
// index: 0,
// like: 0,
// name: "卡越来越",
// uid: "68d0edf0cc79706182bda53f"
// }];
// let res = { code: 1, data: commentData, msg: "成功" }
Utils.getLikeInfo((res) => {
console.log("获取点赞信息", res);
comment = res.data.comment;
const likedDataKey = 'liked_data';
let likedData = cc.sys.localStorage.getItem(likedDataKey);
likedData = likedData ? JSON.parse(likedData) : { level: null, pos: null, likedIds: [] };
console.log("从本地获取点赞信息", likedData);
// 检查当前关卡等级和pos是否与缓存中的匹配
const currentLevel = cc.fx.GameConfig.GM_INFO.level;
const currentPos = this._pos;
const shouldUseCachedIds = likedData.level === currentLevel && likedData.pos === currentPos;
// comment = res.data;
// if (comment.length <= showNum) {
for (let i = 0; i < comment.length; i++) {
let node = levelWordNode.getChildByName("node" + (i + 1));
console.log("node", node)
// 检查节点是否存在
if (node) {
node.active = false;
if (i < showNum) {
node.active = true;
// 为每个node添加文本显示但将Label放在单独的容器中以优化合批
let labelNode = labelContainer.getChildByName("labelNode" + i);
// 检查menuLabel是否存在再实例化
if (!labelNode && this.menuLabel) {
labelNode = cc.instantiate(this.menuLabel);
if (labelNode) {
labelNode.name = "labelNode" + i;
labelContainer.addChild(labelNode);
}
} else if (!labelNode && !this.menuLabel) {
console.warn(`menuLabel prefab is not assigned, creating label node directly`);
// 如果没有预制体直接创建一个带Label组件的节点
labelNode = new cc.Node();
let labelComponent = labelNode.addComponent(cc.Label);
// 设置统一的字体和样式以优化合批
// labelComponent.fontSize = 40;
// labelComponent.lineHeight = 45;
// labelComponent.enableBold = true;
labelComponent.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
labelComponent.verticalAlign = cc.Label.VerticalAlign.CENTER;
labelComponent.overflow = cc.Label.Overflow.NONE;
// 重要设置CHAR模式以优化合批
labelComponent.cacheMode = cc.Label.CacheMode.CHAR;
labelNode.name = "labelNode" + i;
labelContainer.addChild(labelNode);
}
// 检查labelNode是否存在再设置属性
if (labelNode) {
// 设置label位置与对应的node对齐
labelNode.active = true;
// 使用node的世界坐标转换为labelContainer的局部坐标
labelNode.position = labelContainer.convertToNodeSpaceAR(node.parent.convertToWorldSpaceAR(node.position));
// 如果需要显示特定文本,可以在这里设置
let labelComponent = labelNode.getComponent(cc.Label);
if (labelComponent) {
// 重要设置CHAR模式以优化合批
labelComponent.cacheMode = cc.Label.CacheMode.CHAR;
// 设置统一的文本内容,如果需要不同内容,请确保使用相同的字体资源
// labelComponent.string = "文本内容";
console.log("comment[i].content", comment[i].content);
labelComponent.string = comment[i].content;
// 确保所有Label使用相同的字体和样式设置
// labelComponent.font = null; // 清除可能存在的字体资源引用
// labelComponent.useSystemFont = true;
// labelComponent.fontSize = 60;
// labelComponent.lineHeight = 60;
// labelComponent.enableBold = true;
labelComponent.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
labelComponent.verticalAlign = cc.Label.VerticalAlign.CENTER;
labelComponent.overflow = cc.Label.Overflow.NONE;
}
}
// 获取当前节点下的likeBtn
let likeBtn = node.getChildByName("likeBtn");
if (likeBtn) {
// 移除之前的点击事件监听器,避免重复添加
likeBtn.off(cc.Node.EventType.TOUCH_END);
// 查找或创建点赞数显示标签
let likeLabel = node.getChildByName("likeLabel");
if (!likeLabel) {
// 创建点赞数显示标签
likeLabel = new cc.Node("likeLabel");
let labelComponent = likeLabel.addComponent(cc.Label);
// 设置标签样式
labelComponent.string = comment[i].like.toString();
labelComponent.fontSize = 30;
labelComponent.lineHeight = 32;
labelComponent.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
labelComponent.verticalAlign = cc.Label.VerticalAlign.CENTER;
// 设置颜色
likeLabel.color = cc.Color.BLACK;
likeLabel.anchorX = 0;
likeLabel.anchorY = 0.5;
// 添加到节点
node.addChild(likeLabel);
// 设置位置通常在likeBtn旁边
likeLabel.setPosition(likeBtn.x + likeBtn.width / 2 + 20, likeBtn.y); // 根据实际需求调整位置
} else {
// 如果标签已存在,更新显示的点赞数
let labelComponent = likeLabel.getComponent(cc.Label);
if (labelComponent) {
labelComponent.string = comment[i].like.toString();
}
}
// 检查当前评论ID是否已经在本地缓存的已点赞列表中
const currentCommentId = comment[i].index; // 使用index作为唯一标识
const isAlreadyLiked = shouldUseCachedIds && likedData.likedIds.includes(currentCommentId);
if (isAlreadyLiked) {
// 如果已经点赞过,设置按钮为不可点击状态
likeBtn['_hasLiked'] = true;
console.log(`节点 ${i + 1} 的likeBtn已点赞过设置为不可点击状态`);
} else {
// 如果没有点赞过,添加点击事件
likeBtn['_hasLiked'] = false;
likeBtn.on(cc.Node.EventType.TOUCH_END, (event) => {
if (likeBtn['_hasLiked']) {
console.log(`节点 ${i + 1} 的likeBtn已经点赞不可再次点击`);
return; // 如果已经点赞,则不执行后续操作
}
console.log(`调用事件信息: 节点 ${i + 1} 的likeBtn被点击评论内容: ${comment[i].content}, 用户: ${comment[i].name}`);
// 这里可以添加点赞逻辑
this.handleLikeClick(i, comment[i], likeLabel, likeBtn, currentCommentId, currentLevel, currentPos);
}, this);
}
// 设置按钮的ID以便在后续处理中识别
likeBtn['_commentId'] = currentCommentId;
} else {
console.warn(`节点 ${i + 1} 下未找到名为 likeBtn 的子节点`);
}
} else {
// 如果节点超出显示范围隐藏其对应的label
let labelNode = labelContainer.getChildByName("labelNode" + i);
if (labelNode) {
labelNode.active = false;
}
// 检查超出范围的节点是否也有likeBtn也需要处理
let likeBtn = node.getChildByName("likeBtn");
if (likeBtn) {
likeBtn.off(cc.Node.EventType.TOUCH_END); // 移除可能存在的事件监听器
likeBtn.active = false; // 隐藏超出范围的点赞按钮
}
}
} else {
console.warn(`Node with name "node${i}" not found in levelWordNode`);
}
}
// }
}, this._pos)
}
// 添加点赞点击处理函数
private handleLikeClick(index: number, commentData: any, likeLabel: cc.Node, likeBtn: cc.Node, commentId: number, level: number, pos: number) {
console.log(`点赞按钮被点击,索引: ${index}, 评论数据:`, commentData);
Utils.setLikeInfo((res) => {
if (res.code === 1) {
commentData.like += 1;
let labelComponent = likeLabel.getComponent(cc.Label);
if (labelComponent) {
labelComponent.string = commentData.like.toString();
}
// 设置按钮为已点赞状态,不可再次点击
likeBtn['_hasLiked'] = true;
likeBtn.off(cc.Node.EventType.TOUCH_END); // 移除点击事件
// 更新本地缓存中的已点赞数据
const likedDataKey = 'liked_data';
let likedData = cc.sys.localStorage.getItem(likedDataKey);
likedData = likedData ? JSON.parse(likedData) : { level: null, pos: null, likedIds: [] };
// 更新关卡等级和pos
likedData.level = level;
likedData.pos = pos;
// 添加当前ID到已点赞列表如果不存在
if (!likedData.likedIds.includes(commentId)) {
likedData.likedIds.push(commentId);
}
// 保存到本地缓存
cc.sys.localStorage.setItem(likedDataKey, JSON.stringify(likedData));
console.log("点赞成功!");
} else {
console.log("点赞失败!");
}
}, this._pos, index);
}
onUpLikeClick(event, customData) {
console.log("onUpClick", event, customData);
// 获取当前玩家的名称、头像和当前关卡等级
const playerName = cc.fx.GameConfig.GM_INFO.username;
const playerAvatar = cc.fx.GameConfig.GM_INFO.useravatar;
const currentLevel = cc.fx.GameConfig.GM_INFO.level;
// 获取留言内容
const messageContent = this.selectLabel.string; // 使用当前显示的消息内容
let likeNum = 0;
let data = {
name: playerName,
head: playerAvatar,
content: messageContent,
like: likeNum,
};
let pos = 1;
let index = 1;
// 输出信息到控制台
// console.log("玩家名称:", playerName);
// console.log("玩家头像:", playerAvatar);
// console.log("当前关卡等级:", currentLevel);
// console.log("留言内容:", messageContent);
// console.log("lick 0"); // 按照要求添加
Utils.setLikeInfo((res) => {
if (res.code == 1) {
// cc.fx.GameTool.shushu_Track("comment", data);
// cc.fx.GameTool.typingAni(this.selectLabel, messageContent, () => {
// this.onCloseClick(null, null);
// }, this.node);
}
}, pos, index)
// 删除当前节点
// this.node.destroy();
}
// 清除所有
clearAll() {
this.columnValues.clear();
this.updateDisplay();
}
}