274 lines
8.8 KiB
C#
274 lines
8.8 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using DefaultNamespace;
|
||
using UnityEngine;
|
||
using WXGame.Network;
|
||
using Cysharp.Threading.Tasks;
|
||
using DefaultNamespace.Component;
|
||
using MotionFramework;
|
||
using TMPro;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine.UI;
|
||
|
||
/// <summary>
|
||
/// 首页组件
|
||
/// 负责处理首页相关的用户信息获取和显示
|
||
/// </summary>
|
||
public class HomeComponent : MonoBehaviour
|
||
{
|
||
/// <summary>
|
||
/// 当前用户信息
|
||
/// </summary>
|
||
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;
|
||
|
||
public Transform friendItemPrefab;
|
||
public Transform contentParent;
|
||
|
||
|
||
private void Start()
|
||
{
|
||
GetMyInfo();
|
||
GetTodayHaveSign();
|
||
GetMyFriend();
|
||
}
|
||
|
||
private async void GetMyFriend()
|
||
{
|
||
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.gameObject.SetActive(true);
|
||
|
||
// 获取或添加FriendItemComponent组件
|
||
var friendItemComponent = friendItem.GetComponent<HomeFriendComponent>();
|
||
|
||
// 初始化好友项数据(异步下载头像)
|
||
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}");
|
||
}
|
||
}
|
||
|
||
private async void GetTodayHaveSign()
|
||
{
|
||
var response = await WebRequestManager.Instance.GetRequestAsync(
|
||
url: Apis.GetTodayHaveSign(),
|
||
enableSign: true
|
||
);
|
||
if (response != null && response.ResponseText != null)
|
||
{
|
||
TodayHaveSignData todayHaveSignData = JsonUtility.FromJson<TodayHaveSignData>(response.ResponseText);
|
||
//0就是今日未打卡 1就是今日已打卡
|
||
if (todayHaveSignData != null)
|
||
{
|
||
if (todayHaveSignData.data == 0)
|
||
{
|
||
await UniTask.Delay(1000);
|
||
UIManager.Instance.ShowPage("签到");
|
||
}
|
||
|
||
Debug.Log($"今日是否签到: {todayHaveSignData.data}");
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取我的用户信息
|
||
/// 使用UserDataNetworkManager进行网络请求和数据解析
|
||
/// 若请求成功且返回数据有效,则更新当前用户信息并处理
|
||
/// </summary>
|
||
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}");
|
||
// 可以在这里添加错误处理逻辑,比如显示错误提示给用户
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理用户信息
|
||
/// 在这里可以添加具体的业务逻辑,比如更新UI显示
|
||
/// </summary>
|
||
/// <param name="userInfo">用户信息数据</param>
|
||
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}");
|
||
}
|
||
}
|
||
|
||
/// <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>当前用户信息,如果未获取到则返回null</returns>
|
||
public UserInfo GetCurrentUserInfo()
|
||
{
|
||
return currentUserInfo;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刷新用户信息
|
||
/// 重新从服务器获取最新的用户信息
|
||
/// </summary>
|
||
public void RefreshUserInfo()
|
||
{
|
||
GetMyInfo();
|
||
}
|
||
} |