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 noticeDatas; private NoticeData noticeData; // 最近一条公告数据 private NoticeData lastData; // 上一次的最近一条公告数据 private string httpUrl=HttpRequestHandler.httpIp+"/system/notice/listApi"; private string encryptText; // 加密文本 public static List detailsMessageList=new List(); void OnEnable() { dataView = transform; httpRequestHandler = gameObject.AddComponent(); 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> returnInfo = JsonUtility.FromJson>>(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().text = TextFormat.ExtractTextFromHtml(noticeData.noticeTitle); // 创建时间 dataView.GetChild(1).GetComponent().text = TextFormat.DateTimeFormat(noticeData.createTime); // 公告内容 dataView.GetChild(2).GetComponent().text = TextFormat.ExtractTextFromHtml(noticeData.noticeContent); } }