NG_/Scripts/Data/Controller/ChemicalStorageController2.cs
2024-12-13 18:49:44 +08:00

115 lines
4.1 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;
public class ChemicalStorageController2 : MonoBehaviour,HttpController
{
private Transform dataView; // 显示对象
private HttpRequestHandler httpRequestHandler;
private int index = 0; // 数据下标
private List<ChemicalStorageData> chemicalStorageDatas;
private List<ChemicalStorageData> lastDatas; // 上一次的数据源
private string httpUrl=HttpRequestHandler.httpIp+"/chemical/chemicalOperation/listAll";
private string encryptText; // 加密文本
private bool firstLoad = true;
void OnEnable()
{
firstLoad = true;
dataView = transform;
httpRequestHandler = gameObject.AddComponent<HttpRequestHandler>();
encryptText = AESHelper.Encrypt("/chemical/chemicalOperation/listAll");
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<ChemicalStorageData>> returnInfo = JsonUtility.FromJson<ReturnInfo<List<ChemicalStorageData>>>(response);
chemicalStorageDatas = returnInfo.data;
if (firstLoad)
{
firstLoad = false;
UpdateDataView();
}
}
// 请求失败的回调
private void OnError(string error)
{
Debug.LogError("Request Error: " + error);
}
// 更新填充数据
private void UpdateDataView()
{
chemicalStorageDatas = chemicalStorageDatas.Where(obj => obj.operationType == "2").ToList();
int count = chemicalStorageDatas.Count;
print("危化品领用数量:"+count);
for (int i=0;i < dataView.childCount; i++)
{
// (index+i)%userDatas.Count()
// 当轮播图轮动多少次时列表的第一个数据就应该是数据源里的第多少个用i递增过去当超出数据源数量时使用%将多余的拿出去以获得正确的下标
// 领用人
dataView.GetChild(i).GetChild(0).GetChild(0).GetComponent<TextMeshProUGUI>().text =
chemicalStorageDatas[(index+i)%count].nickName == "" ? "无":chemicalStorageDatas[(index+i)%count].nickName;
// 试剂名
dataView.GetChild(i).GetChild(0).GetChild(1).GetComponent<TextMeshProUGUI>().text =
chemicalStorageDatas[(index+i)%count].chemicalName == ""? "无":chemicalStorageDatas[(index+i)%count].chemicalName;
// 重量
dataView.GetChild(i).GetChild(0).GetChild(2).GetComponent<TextMeshProUGUI>().text =
chemicalStorageDatas[(index+i)%count].weight == ""? "无":chemicalStorageDatas[(index+i)%count].weight;
// 用量
// 时间年月日
dataView.GetChild(i).GetChild(0).GetChild(4).GetComponent<TextMeshProUGUI>().text =
TextFormat.DateTimeFormat(chemicalStorageDatas[(index+i)%count].createTime);
// 时分
dataView.GetChild(i).GetChild(0).GetChild(5).GetComponent<TextMeshProUGUI>().text =
TextFormat.DateTimeFormat2(chemicalStorageDatas[(index+i)%count].createTime);
}
}
// 更新下标
public void UpdateIndex()
{
// 轮播图每次轮动下标增加,下标达到数据源数量最大值时清零
if (chemicalStorageDatas == null)
{
return;
}
if (index<chemicalStorageDatas.Count-1)
{
index++;
}
else
{
index = 0;
}
UpdateDataView();
}
}