371 lines
11 KiB
C#
371 lines
11 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using Kill.UI.Components;
|
||
using Kill.Network;
|
||
using Kill.Managers;
|
||
|
||
namespace Kill.UI.Pages
|
||
{
|
||
public class MessageListPage : MonoBehaviour
|
||
{
|
||
[Header("UI组件")]
|
||
public VerticalScrollLoader scrollLoader;
|
||
public Transform contentParent;
|
||
public GameObject messageItemPrefab;
|
||
public GameObject emptyTip;
|
||
public Text titleText;
|
||
public Button back;
|
||
|
||
[Header("分页设置")]
|
||
public int pageSize = 20;
|
||
public float loadThreshold = 0.8f;
|
||
|
||
[Header("测试模式")]
|
||
[Tooltip("启用测试模式,使用本地生成的测试数据")]
|
||
public bool useTestData = false;
|
||
|
||
private List<MessageData> messageList = new List<MessageData>();
|
||
private int currentPage = 1;
|
||
private bool isLoading = false;
|
||
private bool hasMoreData = true;
|
||
private MessageType currentType = MessageType.device;
|
||
|
||
private void Start()
|
||
{
|
||
// Start中不自动初始化,由外部调用Init(string type)传入类型
|
||
//Init("device");
|
||
back.onClick.RemoveAllListeners();
|
||
back.onClick.AddListener(Back);
|
||
UIManager.Instance.RegisterBackAction(Back);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化页面(带消息类型)
|
||
/// </summary>
|
||
/// <param name="type">消息类型:device/warning/other</param>
|
||
public void Init(MessageType type)
|
||
{
|
||
currentType = type;
|
||
|
||
// 设置页面标题
|
||
SetPageTitle(type.ToString());
|
||
|
||
// 设置滚动加载阈值
|
||
if (scrollLoader != null)
|
||
{
|
||
scrollLoader.triggerThreshold = loadThreshold;
|
||
scrollLoader.onLoadTriggered.AddListener(OnScrollToLoad);
|
||
scrollLoader.ResetLoader();
|
||
}
|
||
|
||
// 清空列表
|
||
ClearMessageList();
|
||
|
||
// 加载第一页
|
||
currentPage = 1;
|
||
hasMoreData = true;
|
||
LoadMessageList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据消息类型设置页面标题
|
||
/// </summary>
|
||
private void SetPageTitle(string type)
|
||
{
|
||
if (titleText == null) return;
|
||
|
||
string titleKey = type switch
|
||
{
|
||
"device" => "100243", // 设备消息
|
||
"warning" => "100244", // 警告消息
|
||
"announcement" => "100245", // 其他消息
|
||
_ => "100245" // 消息(默认)
|
||
};
|
||
|
||
titleText.text = LanguageManager.Instance.GetLanguage(titleKey);
|
||
}
|
||
|
||
// /// <summary>
|
||
// /// 切换消息类型筛选
|
||
// /// </summary>
|
||
// public void SwitchMessageType(string type)
|
||
// {
|
||
// if (currentType == type) return;
|
||
|
||
// currentType = type;
|
||
// currentPage = 1;
|
||
// hasMoreData = true;
|
||
// ClearMessageList();
|
||
|
||
// // 重置滚动加载器
|
||
// scrollLoader?.ResetLoader();
|
||
|
||
// LoadMessageList();
|
||
// }
|
||
|
||
/// <summary>
|
||
/// 清空消息列表
|
||
/// </summary>
|
||
private void ClearMessageList()
|
||
{
|
||
messageList.Clear();
|
||
|
||
// 删除所有子对象
|
||
if (contentParent != null)
|
||
{
|
||
for (int i = contentParent.childCount - 1; i >= 0; i--)
|
||
{
|
||
Destroy(contentParent.GetChild(i).gameObject);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 滚动到指定位置加载新消息
|
||
/// </summary>
|
||
private void OnScrollToLoad()
|
||
{
|
||
if (isLoading || !hasMoreData) return;
|
||
|
||
currentPage++;
|
||
LoadMessageList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载消息列表
|
||
/// </summary>
|
||
private async void LoadMessageList()
|
||
{
|
||
if (isLoading) return;
|
||
|
||
isLoading = true;
|
||
LoadingUI.Show();
|
||
|
||
try
|
||
{
|
||
var response = await FetchMessageList(currentPage, pageSize, currentType);
|
||
|
||
if (response != null && response.code == 200)
|
||
{
|
||
var data = response.data;
|
||
|
||
if (data != null && data.list != null && data.list.Count > 0)
|
||
{
|
||
// 添加新数据到列表
|
||
messageList.AddRange(data.list);
|
||
|
||
// 创建消息项UI
|
||
foreach (var message in data.list)
|
||
{
|
||
CreateMessageItem(message);
|
||
}
|
||
|
||
// 检查是否还有更多数据
|
||
hasMoreData = data.list.Count >= pageSize && data.total > messageList.Count;
|
||
|
||
// 更新空提示
|
||
if (emptyTip != null)
|
||
{
|
||
emptyTip.SetActive(false);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 没有更多数据了
|
||
hasMoreData = false;
|
||
|
||
// 如果是第一页且没有数据,显示空提示
|
||
if (currentPage == 1 && messageList.Count == 0)
|
||
{
|
||
if (emptyTip != null)
|
||
{
|
||
emptyTip.SetActive(true);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 通知滚动加载器加载完成
|
||
scrollLoader?.OnLoadComplete();
|
||
|
||
// 如果没有更多数据,设置全部加载完成
|
||
if (!hasMoreData)
|
||
{
|
||
scrollLoader?.SetAllDataLoaded();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 请求失败
|
||
hasMoreData = false;
|
||
scrollLoader?.OnLoadComplete();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"[MessageListPage] 加载消息列表失败: {ex.Message}");
|
||
hasMoreData = false;
|
||
scrollLoader?.OnLoadComplete();
|
||
}
|
||
finally
|
||
{
|
||
isLoading = false;
|
||
LoadingUI.Hide();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从服务器获取消息列表
|
||
/// </summary>
|
||
private async Task<MessageListResponse> FetchMessageList(int page, int pageSize, MessageType type)
|
||
{
|
||
try
|
||
{
|
||
|
||
if (type != MessageType.announcement)
|
||
{
|
||
var response = await NetworkCtrl.Instance.Get<MessageListResponse>(
|
||
$"/api/v1/notifications/list?page={page}&page_size={pageSize}&type={type.ToString()}"
|
||
);
|
||
if (response.IsSuccess)
|
||
{
|
||
return response.Data;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"[MessageListPage] 请求失败: {response.ErrorMessage}");
|
||
return null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var response = await NetworkCtrl.Instance.Get<AnnouncementListResponse>(
|
||
$"/api/v1/announcement/list?page={page}&limit={pageSize}"
|
||
);
|
||
if (response.IsSuccess)
|
||
{
|
||
List<MessageData> messageDatas = new List<MessageData>();
|
||
foreach (var an in response.Data.data.list)
|
||
{
|
||
MessageData md = new MessageData
|
||
{
|
||
id = an.id,
|
||
title = an.title,
|
||
type = "announcement",
|
||
is_read = an.is_read,
|
||
create_at = an.publish_time
|
||
};
|
||
messageDatas.Add(md);
|
||
|
||
}
|
||
MessageListResponse re = new MessageListResponse
|
||
{
|
||
code = response.Data.code,
|
||
message = response.Data.message,
|
||
data = new MessageListData()
|
||
};
|
||
re.data.limit = response.Data.data.limit;
|
||
re.data.page = response.Data.data.page;
|
||
re.data.total = response.Data.data.total;
|
||
re.data.list = messageDatas;
|
||
return re;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"[MessageListPage] 请求失败: {response.ErrorMessage}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"[MessageListPage] 请求异常: {ex.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 创建消息项UI
|
||
/// </summary>
|
||
private void CreateMessageItem(MessageData message)
|
||
{
|
||
if (messageItemPrefab == null || contentParent == null) return;
|
||
|
||
var item = Instantiate(messageItemPrefab, contentParent);
|
||
item.GetComponent<OneMessage>().Init(message);
|
||
}
|
||
|
||
|
||
|
||
private void OnDestroy()
|
||
{
|
||
if (scrollLoader != null)
|
||
{
|
||
scrollLoader.onLoadTriggered.RemoveListener(OnScrollToLoad);
|
||
}
|
||
}
|
||
public void Back()
|
||
{
|
||
UIManager.Instance.RegisterBackAction(GetComponentInParent<MessageTypeListPage>().Back);
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 消息列表响应数据
|
||
/// </summary>
|
||
[Serializable]
|
||
public class MessageListResponse
|
||
{
|
||
public int code;
|
||
public string message;
|
||
public MessageListData data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 消息列表数据
|
||
/// </summary>
|
||
[Serializable]
|
||
public class MessageListData
|
||
{
|
||
public int total;
|
||
public int pages;
|
||
public int limit;
|
||
public int page;
|
||
public List<MessageData> list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 消息列表响应数据
|
||
/// </summary>
|
||
[Serializable]
|
||
public class AnnouncementListResponse
|
||
{
|
||
public int code;
|
||
public string message;
|
||
public MessageListData data;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 消息列表数据
|
||
/// </summary>
|
||
[Serializable]
|
||
public class AnnouncementListData
|
||
{
|
||
public int total;
|
||
public int pages;
|
||
public int limit;
|
||
public int page;
|
||
public List<AnnouncementData> list;
|
||
}
|
||
|
||
}
|