using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Cysharp.Threading.Tasks; using TMPro; using WXGame.Network; namespace DefaultNamespace.Component { /// /// 搜索好友项组件 /// 用于显示搜索结果中的好友信息 /// public class SearchFriendItemComponent : MonoBehaviour { [Header("UI组件引用")] [SerializeField] private RawImage avatarImage; // 头像图片组件 [SerializeField] private TMP_Text usernameText; // 用户名文本组件 [SerializeField] private TMP_Text levelText; // 等级文本组件 [SerializeField] private Button addFriendButton; // 添加好友按钮 [SerializeField] private TMP_Text addFriendButtonText; // 添加好友按钮文本 // [SerializeField] private TMP_Text statusText; // 状态文本(是否已是好友) // [Header("调试设置")] [SerializeField] private bool enableDebugLog = true; // 是否启用调试日志 // 私有字段 private SearchFriendData searchFriendData; // 搜索好友数据 /// /// 初始化搜索好友项数据 /// /// 搜索好友数据 public void InitializeSearchFriendItem(SearchFriendData data) { if (data == null) { Debug.Log("搜索好友数据为空,无法初始化"); return; } searchFriendData = data; Debug.Log($"开始初始化搜索好友项: {data.user_username}"); // 设置用户信息 SetUserInfo(data); // 设置好友状态 // SetFriendStatus(data.have); // 设置添加好友按钮事件 SetupAddFriendButton(); // 异步下载头像 DownloadAvatarAsync(data.user_image).Forget(); } /// /// 设置用户信息(用户名和等级) /// /// 搜索好友数据 private void SetUserInfo(SearchFriendData data) { try { // 设置用户名 if (usernameText != null) { usernameText.text = string.IsNullOrEmpty(data.user_username) ? "未知用户" : data.user_username; Debug.Log($"设置用户名: {usernameText.text}"); } else { Debug.Log("用户名文本组件未设置"); } // 设置等级 if (levelText != null) { levelText.text = $"等级: {data.user_honor_level}"; Debug.Log($"设置等级: {levelText.text}"); } else { Debug.Log("等级文本组件未设置"); } } catch (Exception ex) { Debug.Log($"设置用户信息时发生异常: {ex.Message}"); } } // /// // /// 设置好友状态 // /// // /// 是否已是好友(0-不是好友,1-已是好友) // private void SetFriendStatus(int have) // { // try // { // if (statusText != null) // { // if (have == 1) // { // statusText.text = "已是好友"; // statusText.color = Color.green; // } // else // { // statusText.text = "可添加"; // statusText.color = Color.blue; // } // Debug.Log($"设置好友状态: {statusText.text}"); // } // // // 设置添加好友按钮状态 // if (addFriendButton != null) // { // addFriendButton.interactable = (have == 0); // Debug.Log($"设置添加好友按钮可交互状态: {addFriendButton.interactable}"); // } // } // catch (Exception ex) // { // Debug.Log($"设置好友状态时发生异常: {ex.Message}"); // } // } /// /// 设置添加好友按钮事件 /// private void SetupAddFriendButton() { try { if (addFriendButton != null) { // 移除之前的事件监听器 addFriendButton.onClick.RemoveAllListeners(); // 添加新的事件监听器 addFriendButton.onClick.AddListener(OnAddFriendButtonClick); Debug.Log("添加好友按钮事件设置完成"); } else { Debug.Log("添加好友按钮未设置"); } } catch (Exception ex) { Debug.Log($"设置添加好友按钮事件时发生异常: {ex.Message}"); } } /// /// 添加好友按钮点击事件 /// private void OnAddFriendButtonClick() { try { if (searchFriendData == null) { Debug.Log("搜索好友数据为空,无法添加好友"); return; } Debug.Log($"点击添加好友: {searchFriendData.user_username} (ID: {searchFriendData.user_id})"); // 禁用按钮防止重复点击 if (addFriendButton != null) { addFriendButton.interactable = false; } // 更新按钮文本 if (addFriendButtonText != null) { addFriendButtonText.text = "添加中"; } // 调用添加好友的方法 AddFriendAsync().Forget(); } catch (Exception ex) { Debug.Log($"添加好友按钮点击事件处理异常: {ex.Message}"); } } /// /// 异步添加好友 /// /// UniTask private async UniTask AddFriendAsync() { try { Debug.Log($"开始添加好友: {searchFriendData.user_username} (ID: {searchFriendData.user_id})"); // 调用添加好友API var result = await WebRequestManager.Instance.PostRequestAsync( url: Apis.GetAddFriend(), signParams: new Dictionary() { { "other_uid", searchFriendData.user_id.ToString() }, } ); if (result.ResponseCode == 200) { Debug.Log($"添加好友请求成功!状态码: {result.ResponseCode}"); Debug.Log($"响应内容: {result.ResponseText}"); // 解析响应(假设返回标准格式:{"code":0,"msg":"","data":null}) try { var response = JsonUtility.FromJson(result.ResponseText); if (response != null && response.code == 0) { // 添加好友成功 Debug.Log($"成功添加好友: {searchFriendData.user_username}"); // 更新UI状态 if (addFriendButtonText != null) { addFriendButtonText.text = "已发送"; } // 禁用按钮,防止重复添加 if (addFriendButton != null) { addFriendButton.interactable = false; } Debug.Log($"好友 {searchFriendData.user_username} 添加请求发送成功!"); } else if (response != null && response.code == 1001) { // 根据msg字段判断具体状态 string msg = response.msg ?? ""; if (msg.Contains("已经是好友了")) { // 已经是好友了 Debug.Log($"用户 {searchFriendData.user_username} 已经是好友了"); // 更新UI状态为"已是好友" if (addFriendButtonText != null) { addFriendButtonText.text = "已是好友"; } // 禁用按钮,因为已经是好友了 if (addFriendButton != null) { addFriendButton.interactable = false; } Debug.Log($"好友 {searchFriendData.user_username} 已经是好友状态!"); } else if (msg.Contains("已申请")) { // 已申请好友 Debug.Log($"用户 {searchFriendData.user_username} 已申请好友"); // 更新UI状态为"已申请" if (addFriendButtonText != null) { addFriendButtonText.text = "已申请"; } // 禁用按钮,因为已经申请过了 if (addFriendButton != null) { addFriendButton.interactable = false; } Debug.Log($"好友 {searchFriendData.user_username} 已申请状态!"); } else { // code: 1001 但msg不明确,当作一般错误处理 Debug.LogWarning($"添加好友返回code: 1001,但msg不明确: {msg}"); // 恢复按钮状态 RestoreButtonState(); // 显示错误信息 if (addFriendButtonText != null) { addFriendButtonText.text = "添加失败"; } } } else { // API返回其他错误 string errorMsg = response?.msg ?? "未知错误"; Debug.LogWarning($"添加好友失败: {errorMsg} (错误码: {response?.code ?? -1})"); // 恢复按钮状态 RestoreButtonState(); // 显示错误信息 if (addFriendButtonText != null) { addFriendButtonText.text = "添加失败"; } } } catch (Exception parseEx) { Debug.LogError($"解析添加好友响应失败: {parseEx.Message}"); RestoreButtonState(); } } else { Debug.LogError($"添加好友请求失败: {result.ResponseCode}"); RestoreButtonState(); } } catch (Exception ex) { Debug.LogError($"添加好友时发生异常: {ex.Message}"); RestoreButtonState(); } } /// /// 恢复按钮状态 /// private void RestoreButtonState() { if (addFriendButton != null) { addFriendButton.interactable = true; } if (addFriendButtonText != null) { addFriendButtonText.text = "添加好友"; } } /// /// 异步下载头像 /// /// 头像URL /// UniTask 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}"); } } /// /// 设置头像纹理 /// /// 要设置的纹理 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}"); } } /// /// 获取当前搜索好友数据 /// /// 搜索好友数据 public SearchFriendData GetSearchFriendData() { return searchFriendData; } } }