using Assets.MyGame.Scripts.Net; using Assets.MyGame.Scripts.Net.API.UserData_SubClass; using Assets.MyGame.Scripts.Net.APIRequest.Params; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class UIHome_BY_SafeBoxPanel : SubUIBase { public enum SafeBoxPenel { 设置二级密码 = 1, 存取 = 3, 记录 = 4, 修复密码 = 5 } [Header("面板")] public Button btn_Close_Switch1; // 设置二级密码面板关闭按钮 public Button btn_Close_Switch5; // 修复密码面板关闭按钮 public Dictionary dic_SafeBoxPanel = new Dictionary(); public SafeBoxPenel currPanel; public GameObject selectParent; // 存取和记录面板的父物体 public GameObject safeBoxPanel_Type1; // 设置二级密码面板 public GameObject safeBoxPanel_Type5; // 修复密码面板 [Header("设置密码")] public Button btn_EnterPassword; public TMP_InputField input_Password; public TMP_InputField input_PasswordRes; [Header("键盘")] // 只作为输入本次操作金币个数使用 public Button btn_KeyBoard_0; public Button btn_KeyBoard_1; public Button btn_KeyBoard_2; public Button btn_KeyBoard_3; public Button btn_KeyBoard_4; public Button btn_KeyBoard_5; public Button btn_KeyBoard_6; public Button btn_KeyBoard_7; public Button btn_KeyBoard_8; public Button btn_KeyBoard_9; public Button btn_KeyBoard_Retype; public Button btn_KeyBoard_Enter; [Header("保险箱主页面")] public Toggle toggle_Save; public Toggle toggle_Take; public Toggle toggle_Record; public Button btn_forgetPassword; public TextMeshProUGUI txt_CurrCoins; // 当前拥有金币 public TextMeshProUGUI txt_SaveCoins; // 保险箱余额 public TextMeshProUGUI txt_OpenatrCoins; // 本次操作金币数 public TMP_InputField input_SecondLevelPassword; // 取款时输入密码 public Button btn_SecondLevelPasswordForget; [Header("记录")] public Transform tra_Content; public GameObject obj_LinePrefab; // child 0时间 1类型 2数量 public ScrollRect scrollRect_Record; // 记录列表的ScrollRect,用于滚动加载 [Header("修复密码")] public Button btn_XFMMQR; public Button btn_XFMMYZM; public TMP_InputField input_XFMMYZM; public TMP_InputField input_XFMMNewPassword; public TMP_InputField input_XFMMNewRetypePassword; // 运行时 private string inputAmount = ""; private bool isDepositMode = true; // true=存, false=取 // 记录分页 private int recordPage; private const int RECORD_PAGE_SIZE = 100; private bool recordLoading; private bool recordHasMore = true; public override void Awake() { base.Awake(); // 注册面板字典 dic_SafeBoxPanel[SafeBoxPenel.设置二级密码] = safeBoxPanel_Type1; dic_SafeBoxPanel[SafeBoxPenel.修复密码] = safeBoxPanel_Type5; toggle_Save.onValueChanged.AddListener(OnToggleSave); toggle_Take.onValueChanged.AddListener(OnToggleTake); toggle_Record.onValueChanged.AddListener(OnToggleRecord); // 关闭按钮 btn_Close_Switch1.onClick.AddListener(() => ClosePanel(SafeBoxPenel.设置二级密码)); btn_Close_Switch5.onClick.AddListener(() => ClosePanel(SafeBoxPenel.修复密码)); // 键盘 btn_KeyBoard_0.onClick.AddListener(() => KeyInput("0")); btn_KeyBoard_1.onClick.AddListener(() => KeyInput("1")); btn_KeyBoard_2.onClick.AddListener(() => KeyInput("2")); btn_KeyBoard_3.onClick.AddListener(() => KeyInput("3")); btn_KeyBoard_4.onClick.AddListener(() => KeyInput("4")); btn_KeyBoard_5.onClick.AddListener(() => KeyInput("5")); btn_KeyBoard_6.onClick.AddListener(() => KeyInput("6")); btn_KeyBoard_7.onClick.AddListener(() => KeyInput("7")); btn_KeyBoard_8.onClick.AddListener(() => KeyInput("8")); btn_KeyBoard_9.onClick.AddListener(() => KeyInput("9")); btn_KeyBoard_Retype.onClick.AddListener(() => { inputAmount = ""; UpdateOperateText(); }); btn_KeyBoard_Enter.onClick.AddListener(OnOperateEnter); // 设置密码 btn_EnterPassword.onClick.AddListener(OnSetPassword); // 修复密码 btn_XFMMYZM.onClick.AddListener(OnResetPasswordYZM); btn_XFMMQR.onClick.AddListener(OnResetPasswordConfirm); // 忘记密码(跳转修复面板) btn_forgetPassword.onClick.AddListener(() => ShowPanel(SafeBoxPenel.修复密码)); btn_SecondLevelPasswordForget.onClick.AddListener(() => ShowPanel(SafeBoxPenel.修复密码)); } protected override void OnShow(object args = null) { base.OnShow(args); RefreshData(); } private void RefreshData() { var userId = GameManager.Instance?.user?.id; if (string.IsNullOrEmpty(userId)) return; APIRequestList.GetUser(userId, data => { if (data == null) return; GameManager.Instance.UpdataUserData(data); var safe = data.safeBox; string pwd = safe?.password; // 判断是否需要设置二级密码 if (string.IsNullOrEmpty(pwd)) { ShowPanel(SafeBoxPenel.设置二级密码); } else { ClosePanel(SafeBoxPenel.设置二级密码); ClosePanel(SafeBoxPenel.修复密码); txt_SaveCoins.text = safe.amount.ToString(); txt_CurrCoins.text = data.assets.gold.ToString(); inputAmount = ""; UpdateOperateText(); } }); } // ==================== 面板切换 ==================== private void ShowPanel(SafeBoxPenel panel) { if (dic_SafeBoxPanel.TryGetValue(panel, out var go) && go != null) go.SetActive(true); } private void ClosePanel(SafeBoxPenel panel) { if (dic_SafeBoxPanel.TryGetValue(panel, out var go) && go != null) go.SetActive(false); } // ==================== 设置密码 ==================== private void OnSetPassword() { string pwd = input_Password.text; string pwd2 = input_PasswordRes.text; if (string.IsNullOrEmpty(pwd) || pwd.Length < 6) { UIManager.Instance.ShowBoardError("密码长度至少6位"); return; } if (pwd != pwd2) { UIManager.Instance.ShowBoardError("两次密码不一致"); return; } APIRequestList.SetSafeBoxPassword(new SetSafeBoxPasswordParam { password = pwd }, result => { UIManager.Instance.ShowBoardError("密码设置成功"); RefreshData(); }); } // ==================== 键盘输入(存/取金额) ==================== private void KeyInput(string digit) { if (inputAmount.Length >= 9) return; // 限制9位数字 inputAmount += digit; UpdateOperateText(); } private void UpdateOperateText() { txt_OpenatrCoins.text = string.IsNullOrEmpty(inputAmount) ? "0" : inputAmount; } private void OnOperateEnter() { if (string.IsNullOrEmpty(inputAmount) || inputAmount == "0") { UIManager.Instance.ShowBoardError("请输入有效金额"); return; } long amount = long.Parse(inputAmount); if (isDepositMode) DoDeposit(amount); else DoWithdraw(amount); } // ==================== 存取操作 ==================== private void DoDeposit(long gold) { if (gold > GameManager.Instance.user.assets.gold) { UIManager.Instance.ShowBoardError("余额不足"); return; } UIManager.Instance.ShowTiShi($"确定要存入{gold}金币到保险箱吗?", () => { APIRequestList.SaveSafeBox(new SaveSafeBoxParam { gold = (int)gold }, result => { UIManager.Instance.ShowBoardError($"存入{gold}金币成功"); inputAmount = ""; RefreshData(); }); }); } private void DoWithdraw(long gold) { string pwd = input_SecondLevelPassword.text; if (string.IsNullOrEmpty(pwd)) { UIManager.Instance.ShowBoardError("请输入保险箱密码"); return; } UIManager.Instance.ShowTiShi($"确定要从保险箱取出{gold}金币吗?", () => { APIRequestList.GetSafeBox(new GetSafeBoxInfoParam { gold = gold, password = pwd }, result => { UIManager.Instance.ShowBoardError($"取出{gold}金币成功"); inputAmount = ""; input_SecondLevelPassword.text = ""; RefreshData(); }); }); } // ==================== 修复密码 ==================== private void OnResetPasswordYZM() { string mobile = GameManager.Instance.user?.BaseInfo?.mobile; if (string.IsNullOrEmpty(mobile)) { UIManager.Instance.ShowBoardError("未绑定手机号"); return; } APIRequestList.YZM(new YZMParam { mobile = mobile }, msg => { UIManager.Instance.ShowBoardError(msg ?? "验证码已发送"); }); } private void OnResetPasswordConfirm() { string code = input_XFMMYZM.text; string newPwd = input_XFMMNewPassword.text; string newPwd2 = input_XFMMNewRetypePassword.text; if (string.IsNullOrEmpty(code)) { UIManager.Instance.ShowBoardError("请输入验证码"); return; } if (string.IsNullOrEmpty(newPwd) || newPwd.Length < 6) { UIManager.Instance.ShowBoardError("密码长度至少6位"); return; } if (newPwd != newPwd2) { UIManager.Instance.ShowBoardError("两次密码不一致"); return; } APIRequestList.ResetSafeBoxPassword(new ResetSafeBoxPasswordParam { code = code, password = newPwd }, result => { UIManager.Instance.ShowBoardError("密码重置成功"); ClosePanel(SafeBoxPenel.修复密码); RefreshData(); }); } // ==================== Toggle点击:存/取模式切换 ==================== public void OnToggleSave(bool value) { if (value) { isDepositMode = true; inputAmount = ""; UpdateOperateText(); } } public void OnToggleTake(bool value) { if (value) { isDepositMode = false; inputAmount = ""; UpdateOperateText(); } } public void OnToggleRecord(bool value) { if (!value) return; // 重置分页 recordPage = 0; recordHasMore = true; for (int i = tra_Content.childCount - 1; i >= 0; i--) Destroy(tra_Content.GetChild(i).gameObject); LoadRecordPage(); // 滚动到底部时触发翻页 if (scrollRect_Record != null) { scrollRect_Record.onValueChanged.RemoveAllListeners(); scrollRect_Record.onValueChanged.AddListener(OnRecordScroll); } } private void OnRecordScroll(Vector2 pos) { // 滚动到最底部(normalizedPosition.y ≈ 0) if (!recordHasMore || recordLoading || pos.y > 0.05f) return; LoadRecordPage(); } private void LoadRecordPage() { if (recordLoading || !recordHasMore) return; recordLoading = true; // 第一层封装:保险箱记录专用参数 var req = new GetUserRewardListObj { keyWord = "", pageIndex = recordPage, pageSize = RECORD_PAGE_SIZE, userId = GameManager.Instance?.user?.id, type = 0, source = 0, currencyType = 2 // 金币保险箱 }; APIRequestList.GetUserRewardList(req, list => { recordLoading = false; if (list == null || list.Count == 0) { recordHasMore = false; if (tra_Content.childCount == 0) { var emptyLine = Instantiate(obj_LinePrefab, tra_Content); emptyLine.transform.GetChild(0).GetComponent().text = "暂无记录"; } return; } foreach (var item in list) { var line = Instantiate(obj_LinePrefab, tra_Content); var time = new System.DateTime(1970, 1, 1, 8, 0, 0).AddSeconds(item.createTime); line.transform.GetChild(0).GetComponent().text = time.ToString("MM-dd HH:mm"); string typeStr = item.changeCount >= 0 ? "收入" : "支出"; line.transform.GetChild(1).GetComponent().text = typeStr; line.transform.GetChild(2).GetComponent().text = Mathf.Abs(item.changeCount).ToString("F0"); } recordPage++; if (list.Count < RECORD_PAGE_SIZE) recordHasMore = false; }); } }