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

196 lines
6.2 KiB
C#
Raw Permalink 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 Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using WXGame.Network;
namespace DefaultNamespace.Component
{
public class FriendAgreeItemComponent : MonoBehaviour
{
[Header("UI组件引用")] [SerializeField] private RawImage avatarImage; // 头像图片组件
[SerializeField] private TMP_Text usernameText; // 用户名文本组件
[SerializeField] private Button agreeBt; // 同意按钮
[SerializeField] private Button refuseBt; // 拒绝按钮
[Header("调试设置")] [SerializeField] private bool enableDebugLog = true; // 是否启用调试日志
// 私有字段
private MyFriendReviewDataItem friendData; // 好友数据
/// <summary>
/// 初始化好友项数据
/// </summary>
/// <param name="data">好友数据</param>
public void InitializeFriendItem(MyFriendReviewDataItem data)
{
if (data == null)
{
Debug.Log("好友数据为空,无法初始化");
return;
}
friendData = data;
Debug.Log($"开始初始化好友项: {data.user_username}");
// 设置用户名和等级
SetUserInfo(data);
// 异步下载头像
DownloadAvatarAsync(data.user_image).Forget();
// 添加按钮点击事件
agreeBt.onClick.AddListener(delegate
{
Debug.Log($"同意好友请求: {data.user_username}");
// 调用同意好友请求的方法
CheckFriend(friendData.friend_id, "2");
});
refuseBt.onClick.AddListener(delegate
{
Debug.Log($"拒绝好友请求: {data.user_username}");
// 调用拒绝好友请求的方法
CheckFriend(friendData.friend_id, "3");
});
}
/// <summary>
/// 同意拒绝好友请求
/// </summary>
private async void CheckFriend(int friend_id, string state)
{
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetCheckFriend(),
signParams: new Dictionary<string, object>()
{
{ "friend_id", friend_id},
{ "status", state }//2同意 3拒绝
}
);
Debug.Log($"同意拒绝好友请求结果: {result.ResponseText}");
}
/// <summary>
/// 设置用户信息(用户名和等级)
/// </summary>
/// <param name="data">好友数据</param>
private void SetUserInfo(MyFriendReviewDataItem data)
{
try
{
// 设置用户名
if (usernameText != null)
{
usernameText.text = string.IsNullOrEmpty(data.user_username) ? "未知用户" : data.user_username;
Debug.Log($"设置用户名: {usernameText.text}");
}
else
{
Debug.Log("用户名文本组件未设置");
}
}
catch (Exception ex)
{
Debug.Log($"设置用户信息时发生异常: {ex.Message}");
}
}
/// <summary>
/// 异步下载头像
/// </summary>
/// <param name="imageUrl">头像URL</param>
/// <returns>UniTask</returns>
private async UniTask DownloadAvatarAsync(string imageUrl)
{
try
{
// 检查URL是否有效
if (string.IsNullOrEmpty(imageUrl))
{
Debug.Log("头像URL为空使用默认头像");
return;
}
Debug.Log($"开始下载头像: {imageUrl}");
// 使用WebRequestManager下载图片
Texture2D texture = await WebRequestManager.Instance.DownloadImageAsync(imageUrl);
if (texture != null)
{
// 设置头像
SetAvatarTexture(texture);
Debug.Log($"头像设置完成,尺寸: {texture.width}x{texture.height}");
}
else
{
Debug.Log("头像下载失败,使用默认头像");
}
}
catch (Exception ex)
{
Debug.Log($"下载头像时发生异常: {ex.Message}");
}
}
/// <summary>
/// 设置头像纹理
/// </summary>
/// <param name="texture">要设置的纹理</param>
private void SetAvatarTexture(Texture2D texture)
{
try
{
if (avatarImage != null && texture != null)
{
avatarImage.texture = texture;
Debug.Log("头像纹理设置成功");
}
else
{
Debug.Log("头像图片组件未设置或纹理为空");
}
}
catch (Exception ex)
{
Debug.Log($"设置头像纹理时发生异常: {ex.Message}");
}
}
/// <summary>
/// 获取当前好友数据
/// </summary>
/// <returns>好友数据</returns>
public MyFriendReviewDataItem GetFriendData()
{
return friendData;
}
/// <summary>
/// 清理图片缓存已移除使用WebRequestManager的简单缓存
/// </summary>
public static void ClearCache()
{
Debug.Log("[FriendItemComponent] 缓存清理功能已移除使用WebRequestManager的简单缓存");
}
/// <summary>
/// 获取缓存统计信息已移除使用WebRequestManager的简单缓存
/// </summary>
/// <returns>缓存统计信息</returns>
public static string GetCacheStats()
{
return "使用WebRequestManager的简单缓存";
}
}
}