using System; using System.Collections; using System.Collections.Generic; using DefaultNamespace; using UnityEngine; using WXGame.Network; using Cysharp.Threading.Tasks; using TMPro; using UnityEngine.UI; /// /// 首页组件 /// 负责处理首页相关的用户信息获取和显示 /// public class HomeComponent : MonoBehaviour { /// /// 当前用户信息 /// private UserInfo currentUserInfo; public TMP_Text userid; public TMP_Text username; public TMP_Text user_gold_coin; public TMP_Text user_star_num; public RawImage avatarImage; private void Start() { GetMyInfo(); } /// /// 获取我的用户信息 /// 使用UserDataNetworkManager进行网络请求和数据解析 /// 若请求成功且返回数据有效,则更新当前用户信息并处理 /// async void GetMyInfo() { try { Debug.Log("开始获取用户信息..."); // 使用UserDataNetworkManager获取用户信息 UserInfoResponse response = await UserDataNetworkManager.Instance.GetMyInfo(); if (response != null && response.data != null) { // 保存用户信息 currentUserInfo = response.data; // 处理用户信息 ProcessUserInfo(currentUserInfo); Debug.Log("用户信息获取成功!"); } else { Debug.LogError("获取到的用户信息为空"); } } catch (Exception ex) { Debug.LogError($"获取用户信息时发生错误: {ex.Message}"); // 可以在这里添加错误处理逻辑,比如显示错误提示给用户 } } /// /// 处理用户信息 /// 在这里可以添加具体的业务逻辑,比如更新UI显示 /// /// 用户信息数据 private void ProcessUserInfo(UserInfo userInfo) { if (userInfo == null) { Debug.LogWarning("用户信息为空,无法处理"); return; } Debug.Log($"=== 用户信息详情 ==="); Debug.Log($"用户ID: {userInfo.user_id}"); Debug.Log($"账户ID: {userInfo.user_account_id}"); Debug.Log($"昵称: {userInfo.user_username}"); Debug.Log($"荣誉等级: {userInfo.user_honor_level}"); Debug.Log($"金币: {userInfo.user_gold_coin}"); Debug.Log($"星砂: {userInfo.user_star_num}"); Debug.Log($"贡献值: {userInfo.user_contribute_num}"); Debug.Log($"活跃值: {userInfo.user_live_num}"); Debug.Log($"收藏值: {userInfo.user_collect_num}"); Debug.Log($"头像: {userInfo.user_image}"); Debug.Log($"手机号: {userInfo.user_phone}"); Debug.Log($"在线状态: {(userInfo.user_online == 1 ? "在线" : "离线")}"); Debug.Log($"用户状态: {(userInfo.user_status == 1 ? "正常" : "异常")}"); Debug.Log($"注册时间: {userInfo.user_time}"); Debug.Log($"最后登录: {userInfo.user_lastlogin_time}"); Debug.Log($"=================="); userid.text = "ID " + userInfo.user_id.ToString(); username.text = userInfo.user_username; user_gold_coin.text = "金币 " + userInfo.user_gold_coin.ToString(); user_star_num.text = "星砂 " + userInfo.user_star_num.ToString(); DownloadAvatarAsync(userInfo.user_image); // 在这里可以添加更多业务逻辑 // 例如:更新UI显示、保存到本地存储、触发其他系统等 } 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}"); } } /// /// 获取当前用户信息 /// 供其他组件调用 /// /// 当前用户信息,如果未获取到则返回null public UserInfo GetCurrentUserInfo() { return currentUserInfo; } /// /// 刷新用户信息 /// 重新从服务器获取最新的用户信息 /// public void RefreshUserInfo() { GetMyInfo(); } }