NG_/Assets/Scripts/Data/Controller/NoticeListController.cs
2024-12-13 19:40:05 +08:00

84 lines
2.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
// 公告列表
public class NoticeListController : MonoBehaviour
{
private Transform dataView; // 显示对象
private HttpRequestHandler httpRequestHandler;
private List<NoticeData> noticeDatas;
private NoticeData noticeData; // 最近一条公告数据
private NoticeData lastData; // 上一次的最近一条公告数据
private string httpUrl=HttpRequestHandler.httpIp+"/system/notice/listApi";
private string encryptText; // 加密文本
public static List<string> detailsMessageList=new List<string>();
void OnEnable()
{
dataView = transform;
httpRequestHandler = gameObject.AddComponent<HttpRequestHandler>();
encryptText = AESHelper.Encrypt("/system/notice/listApi");
StartCoroutine(UpdateDataSource());
}
public IEnumerator UpdateDataSource()
{
while (true)
{
// 发起一个 POST 请求
string jsonData = "{\"encryptText\":\"" + encryptText + "\"}";
StartCoroutine(httpRequestHandler.PostRequest(httpUrl, jsonData, OnPostSuccess, OnError));
yield return new WaitForSeconds(4f);
}
}
// POST 请求成功回调
private void OnPostSuccess(string response)
{
Debug.Log("POST Success 信息列表: " + DateTime.Now);
ReturnInfo<List<NoticeData>> returnInfo = JsonUtility.FromJson<ReturnInfo<List<NoticeData>>>(response);
noticeDatas = returnInfo.data;
if (noticeDatas.Count > 0)
{
noticeData = noticeDatas[0]; // 获取最新的公告数据
}
else
{
return;
}
UpdateDataView();
}
// 请求失败的回调
private void OnError(string error)
{
Debug.LogError("Request Error: " + error);
}
// 更新填充数据
private void UpdateDataView()
{
// 通知标题
dataView.GetChild(0).GetComponent<TextMeshProUGUI>().text =
TextFormat.ExtractTextFromHtml(noticeData.noticeTitle);
// 创建时间
dataView.GetChild(1).GetComponent<TextMeshProUGUI>().text =
TextFormat.DateTimeFormat(noticeData.createTime);
// 公告内容
dataView.GetChild(2).GetComponent<TextMeshProUGUI>().text =
TextFormat.ExtractTextFromHtml(noticeData.noticeContent);
}
}