killapp/Assets/Scripts/UI/Pages/Safetylearning/SafetylearningPageCtrl.cs
“虞渠成” a30c334bb5 设备绑定
2026-04-28 16:35:51 +08:00

143 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Kill.Managers;
using Kill.Network;
using Kill.UI.Components;
using UnityEngine;
using UnityEngine.UI;
namespace Kill.UI.Pages
{
/// <summary>
/// 安全学习页面控制器 - 仅显示题目,不包含答题逻辑
/// </summary>
public class SafetylearningPageCtrl : MonoBehaviour
{
public static SafetylearningPageCtrl Instance { get; private set; }
/// <summary>
/// 0引导 1视屏学习 2测试 3完成
/// </summary>
public GameObject[] sonPages;
private GameObject nowPage;
public GameObject topUI;
// 题目数据
private List<QuestionInfo> questions = new List<QuestionInfo>();
private int currentQuestionIndex = 0;
void Awake()
{
Instance = this;
}
void Start()
{
foreach (var page in sonPages)
{
page.SetActive(false);
}
nowPage = sonPages[0];
nowPage.SetActive(true);
topUI.SetActive(true);
// 加载题目
LoadQuestions();
UIManager.Instance.RegisterBackAction(BackAction);
}
/// <summary>
/// 从服务器加载全部题目
/// </summary>
public async void LoadQuestions()
{
LoadingUI.Show();
try
{
// 调用获取题目列表接口,获取全部题目
var response = await NetworkCtrl.Instance.Get<QuestionListResponse>("/api/v1/exam/questions");
LoadingUI.Hide();
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
questions = data.data.list;
Debug.Log($"加载题目成功,共 {questions.Count} 道");
},
onError:(code,message)=>
{
Debug.LogError($"加载题目失败: {message}");
ToastUI.Show("100062");
}
);
}
catch (Exception ex)
{
LoadingUI.Hide();
Debug.LogError($"加载题目异常: {ex.Message}");
ToastUI.Show("100062");
}
}
/// <summary>
/// 完成考试
/// </summary>
public async void ExamCompletion()
{
LoadingUI.Show();
try
{
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/exam/completion");
LoadingUI.Hide();
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
Debug.Log($"上传成功");
}
);
}
catch (Exception ex)
{
LoadingUI.Hide();
}
}
public void ShowVideoPage()
{
nowPage.SetActive(false);
nowPage = sonPages[1];
nowPage.SetActive(true);
string videoUrl=questions[0].videoUrl;
nowPage.GetComponent<SafetylearningVideoPage>().Init(videoUrl);
questions.RemoveAt(0);
}
public void ShowTestPage()
{
nowPage.SetActive(false);
nowPage = sonPages[2];
nowPage.SetActive(true);
nowPage.GetComponent<SafetylearningTestPage>().Init(questions);
}
public void ShowFinishPage()
{
ExamCompletion();
topUI.SetActive(false);
nowPage.SetActive(false);
nowPage = sonPages[3];
nowPage.SetActive(true);
}
public void BackToHomePage()
{
UIManager.Instance.OpenPage(UIManager.PageName.homePage,null,true);
}
public void ConnectDevice()
{
Debug.Log("链接设备");
}
public void BackAction()
{
UIManager.Instance.OpenPage(UIManager.PageName.homePage,null,true);
}
}
}