WX-Game1/Assets/Scripts/Component/MyFriendComponent.cs

355 lines
14 KiB
C#
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.

using System;
using System.Collections.Generic;
using MotionFramework;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using WXGame.Network;
namespace DefaultNamespace.Component
{
public class MyFriendComponent : MonoBehaviour
{
[SerializeField] private Transform contentParent;
[SerializeField] private GameObject friendItemPrefab;
public Button closeBt;
public Toggle[] Toggles;
public GameObject[] games;
public InputField inputField;
public Button searchBt;
public Transform addContentParent;
public GameObject addFriendItemPrefab;
public TMP_Text titleText;
async void Start()
{
// 为每个Toggle添加事件监听器修复闭包问题
for (int i = 0; i < Toggles.Length; i++)
{
// 创建局部变量捕获当前索引,避免闭包问题
int currentIndex = i;
Toggles[i].onValueChanged.AddListener((value) =>
{
// 只有当Toggle被激活时才执行逻辑避免在自动设置为false时重复触发
games[currentIndex].SetActive(value);
if (!value)
{
Debug.Log($"Toggle {Toggles[currentIndex].name} 被设置为false跳过处理");
return;
}
Debug.Log($"Toggle {Toggles[currentIndex].name} 被激活,开始处理");
// 清理之前的好友项
ClearFriendItems();
// 确保索引在有效范围内
if (currentIndex >= 0 && currentIndex < Toggles.Length)
{
// 设置标题文本为当前Toggle的名称
titleText.text = Toggles[currentIndex].name;
// // 确保游戏对象数组索引有效
// if (currentIndex < games.Length)
// {
// games[currentIndex].SetActive(value);
// Debug.Log($"激活游戏对象: {currentIndex}");
// }
// else
// {
// Debug.LogWarning($"游戏对象数组索引 {currentIndex} 超出范围,数组长度为 {games.Length}");
// }
// 根据索引执行相应的逻辑
switch (currentIndex)
{
case 0: // 第一个Toggle - 好友
Debug.Log("执行获取好友列表逻辑 (索引: 0)");
GetFriendList();
break;
case 1: // 第二个Toggle - 最近
Debug.Log("执行获取最近好友列表逻辑 (索引: 1)");
GetMyTalkFriend();
break;
default:
Debug.LogWarning($"未知的Toggle索引: {currentIndex}Toggle名称: {Toggles[currentIndex].name}");
break;
}
}
else
{
Debug.LogWarning($"Toggle索引 {currentIndex} 超出范围,数组长度为 {Toggles.Length}");
}
});
}
searchBt.onClick.AddListener(delegate { SearchFriend(); });
closeBt.onClick.AddListener(delegate { UIManager.Instance.HidePopup("我的好友列表"); });
GetFriendList();
}
/// <summary>
/// 搜索好友
/// </summary>
private async void SearchFriend()
{
// 检查输入是否为空
// if (string.IsNullOrEmpty(inputField.text))
// {
// Debug.Log("请输入好友用户名");
// return;
// }
// 清理之前的搜索结果
ClearSearchResults();
try
{
Debug.Log($"开始搜索好友: {inputField.text}");
// 发送搜索请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetSearchFriend(),
signParams: new Dictionary<string, object>()
{
{ "keyword", inputField.text },
{ "page", 1 }
}
);
if (result.ResponseCode == 200)
{
Debug.Log($"搜索好友请求成功!状态码: {result.ResponseCode}");
Debug.Log($"响应内容: {result.ResponseText}");
// 解析JSON响应
SearchFriendResponse searchResponse = JsonUtility.FromJson<SearchFriendResponse>(result.ResponseText);
if (searchResponse != null && searchResponse.code == 0)
{
Debug.Log($"搜索好友成功,找到 {searchResponse.data?.Length ?? 0} 个结果");
// 处理搜索结果
if (searchResponse.data != null && searchResponse.data.Length > 0)
{
Debug.Log($"开始创建 {searchResponse.data.Length} 个搜索结果项...");
foreach (var searchFriend in searchResponse.data)
{
// 实例化搜索好友项
var searchFriendItem = Instantiate(addFriendItemPrefab, addContentParent);
searchFriendItem.SetActive(true);
// 获取或添加SearchFriendItemComponent组件
var searchFriendItemComponent = searchFriendItem.GetComponent<SearchFriendItemComponent>();
if (searchFriendItemComponent == null)
{
searchFriendItemComponent = searchFriendItem.AddComponent<SearchFriendItemComponent>();
Debug.Log($"为搜索好友项 {searchFriend.user_username} 添加了SearchFriendItemComponent组件");
}
// 初始化搜索好友项数据
searchFriendItemComponent.InitializeSearchFriendItem(searchFriend);
Debug.Log($"搜索好友项创建完成: {searchFriend.user_username}, ID: {searchFriend.user_id}, 等级: {searchFriend.user_honor_level}, 已是好友: {searchFriend.have}");
}
Debug.Log("所有搜索好友项创建完成");
}
else
{
Debug.Log("未找到匹配的好友");
}
}
else
{
Debug.LogWarning($"搜索好友失败,状态码: {searchResponse?.code ?? -1}, 消息: {searchResponse?.msg ?? ""}");
}
}
else
{
Debug.LogError($"搜索好友请求失败: {result.ResponseCode}");
}
}
catch (System.Exception ex)
{
Debug.LogError($"搜索好友时发生异常: {ex.Message}");
}
}
/// <summary>
/// 获取好友列表
/// </summary>
private async void GetFriendList()
{
ClearFriendItems();
try
{
Debug.Log("开始获取好友列表...");
var friendResponse = await MotionEngine.GetModule<UserDataNetworkManager>().GetMyFriend();
// 检查响应状态码
if (friendResponse != null && friendResponse.code == 0)
{
Debug.Log($"成功获取好友列表,共 {friendResponse.data?.data?.Length ?? 0} 个好友");
Debug.Log($"当前页码: {friendResponse.data?.page ?? ""}, 总页数: {friendResponse.data?.allPage ?? 0}");
// 处理好友数据
if (friendResponse.data?.data != null && friendResponse.data.data.Length > 0)
{
Debug.Log($"开始创建 {friendResponse.data.data.Length} 个好友项...");
foreach (var friend in friendResponse.data.data)
{
// 实例化好友项
var friendItem = Instantiate(friendItemPrefab, contentParent);
friendItem.SetActive(true);
// 获取或添加FriendItemComponent组件
var friendItemComponent = friendItem.GetComponent<FriendItemComponent>();
if (friendItemComponent == null)
{
friendItemComponent = friendItem.AddComponent<FriendItemComponent>();
Debug.Log($"为好友项 {friend.user_username} 添加了FriendItemComponent组件");
}
// 初始化好友项数据(异步下载头像)
friendItemComponent.InitializeFriendItem(friend);
Debug.Log($"好友项创建完成: {friend.user_username}, ID: {friend.friend_id}, 等级: {friend.user_honor_level}");
}
Debug.Log("所有好友项创建完成,头像将异步下载");
}
else
{
Debug.Log("当前没有好友");
}
}
else
{
Debug.LogWarning($"获取好友列表失败,状态码: {friendResponse?.code ?? -1}, 消息: {friendResponse?.msg ?? ""}");
}
}
catch (System.Exception ex)
{
Debug.LogError($"获取好友列表时发生异常: {ex.Message}");
}
}
/// <summary>
/// 获取我的最近好友列表
/// </summary>
private async void GetMyTalkFriend()
{
ClearFriendItems();
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetMyTalkFriend(),
signParams: new Dictionary<string, string>()
{
{ "page", "1" },
}
);
FriendResponse friendResponse = JsonUtility.FromJson<FriendResponse>(result.ResponseText);
if (friendResponse != null && friendResponse.code == 0)
{
Debug.Log($"成功获取好友列表,共 {friendResponse.data?.data?.Length ?? 0} 个好友");
Debug.Log($"当前页码: {friendResponse.data?.page ?? ""}, 总页数: {friendResponse.data?.allPage ?? 0}");
// 处理好友数据
if (friendResponse.data?.data != null && friendResponse.data.data.Length > 0)
{
Debug.Log($"开始创建 {friendResponse.data.data.Length} 个好友项...");
foreach (var friend in friendResponse.data.data)
{
// 实例化好友项
var friendItem = Instantiate(friendItemPrefab, contentParent);
friendItem.SetActive(true);
// 获取或添加FriendItemComponent组件
var friendItemComponent = friendItem.GetComponent<FriendItemComponent>();
if (friendItemComponent == null)
{
friendItemComponent = friendItem.AddComponent<FriendItemComponent>();
Debug.Log($"为好友项 {friend.user_username} 添加了FriendItemComponent组件");
}
// 初始化好友项数据(异步下载头像)
friendItemComponent.InitializeFriendItem(friend);
Debug.Log($"好友项创建完成: {friend.user_username}, ID: {friend.friend_id}, 等级: {friend.user_honor_level}");
}
Debug.Log("所有好友项创建完成,头像将异步下载");
}
else
{
Debug.Log("当前没有好友");
}
}
else
{
Debug.LogWarning($"获取好友列表失败,状态码: {friendResponse?.code ?? -1}, 消息: {friendResponse?.msg ?? ""}");
}
}
private void ClearFriendItems()
{
// 清除所有子项
foreach (Transform child in contentParent.transform)
{
Destroy(child.gameObject);
}
}
/// <summary>
/// 清理搜索结果
/// </summary>
private void ClearSearchResults()
{
if (addContentParent == null)
{
Debug.Log("搜索结果父容器未设置");
return;
}
try
{
Debug.Log("开始清理搜索结果...");
// 清除所有搜索结果子项
foreach (Transform child in addContentParent.transform)
{
if (child.gameObject.activeSelf)
{
Destroy(child.gameObject);
}
}
Debug.Log("搜索结果清理完成");
}
catch (System.Exception ex)
{
Debug.LogError($"清理搜索结果时发生异常: {ex.Message}");
}
}
}
}