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

112 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
// 用户列表
public class UserListController : MonoBehaviour,HttpController
{
public Transform dataView; // Content对象
private HttpRequestHandler httpRequestHandler;
private int index = 0; // 数据下标
private List<UserData> userDatas;
private List<UserData> lastDatas; // 上一次的数据源
private bool firstLoad = true;
private string httpUrl=HttpRequestHandler.httpIp+"/system/user/listAllApi";
private string encryptText; // 加密文本
void OnEnable()
{
firstLoad = true;
httpRequestHandler = gameObject.AddComponent<HttpRequestHandler>();
encryptText = AESHelper.Encrypt("/system/user/listAllApi");
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<UserData>> returnInfo = JsonUtility.FromJson<ReturnInfo<List<UserData>>>(response);
userDatas = returnInfo.data;
if (firstLoad)
{
firstLoad = false;
UpdateDataView();
}
}
// 请求失败的回调
private void OnError(string error)
{
Debug.LogError("Request Error: " + error);
}
// 更新填充数据
private void UpdateDataView()
{
print("用户数量:"+userDatas.Count);
for (int i=0;i < dataView.childCount; i++)
{
// (index+i)%userDatas.Count()
// 当轮播图轮动多少次时列表的第一个数据就应该是数据源里的第多少个用i递增过去当超出数据源数量时使用%将多余的拿出去以获得正确的下标
// 用户名
dataView.GetChild(i).GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().text =
userDatas[(index+i)%userDatas.Count()].nickName == "" ? "无":userDatas[(index+i)%userDatas.Count()].nickName;
// 课题组
dataView.GetChild(i).GetChild(0).GetChild(1).GetComponent<TextMeshProUGUI>().text =
userDatas[(index+i)%userDatas.Count()].researches.Count == 0 ? "无":userDatas[(index+i)%userDatas.Count()].researches[0].name;
// 所属岗位
dataView.GetChild(i).GetChild(0).GetChild(2).GetComponent<TextMeshProUGUI>().text =
userDatas[(index+i)%userDatas.Count()].postDept.Count == 0 ? "无":userDatas[(index+i)%userDatas.Count()].postDept[0].postName;
// 序号
dataView.GetChild(i).GetChild(0).GetChild(3).GetChild(0).GetComponent<TextMeshProUGUI>().text =
((index+i)%userDatas.Count()+1).ToString();
}
}
// 更新下标
public void UpdateIndex()
{
// 轮播图每次轮动下标增加,下标达到数据源数量最大值时清零
if (userDatas == null)
{
return;
}
if (index<userDatas.Count-1)
{
index++;
}
else
{
index = 0;
}
UpdateDataView();
}
}