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 { /// /// 安全学习页面控制器 - 仅显示题目,不包含答题逻辑 /// public class SafetylearningPageCtrl : MonoBehaviour { public static SafetylearningPageCtrl Instance { get; private set; } /// /// 0引导 1视屏学习 2测试 3完成 /// public GameObject[] sonPages; private GameObject nowPage; public GameObject topUI; // 题目数据 private List questions = new List(); 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); } /// /// 从服务器加载全部题目 /// public async void LoadQuestions() { LoadingUI.Show(); try { // 调用获取题目列表接口,获取全部题目 var response = await NetworkCtrl.Instance.Get("/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"); } } /// /// 完成考试 /// public async void ExamCompletion() { LoadingUI.Show(); try { var response = await NetworkCtrl.Instance.Post("/api/v1/exam/completion"); LoadingUI.Hide(); ResponseCodeHandler.HandleResponse(response, onSuccess: (data) => { DataManager.Instance.userInfo.exam_completed=true; 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().Init(videoUrl); questions.RemoveAt(0); } public void ShowTestPage() { nowPage.SetActive(false); nowPage = sonPages[2]; nowPage.SetActive(true); nowPage.GetComponent().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); } } }