Files
ichni_Official/Assets/Scripts/UI/LoginPage/LoginPage.cs
2026-06-15 18:18:16 +08:00

183 lines
5.3 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 IchniOnline.Online.Logic;
using IchniOnline.Online.Network.Models;
using UnityEngine;
using UnityEngine.UI;
namespace Ichni.UI
{
public class LoginPage : UIPageBase
{
[Header("Button References")]
public Button closeButton;
public Button tapTapButton;
[Header("Page References")]
public StartUIPage startUIPage;
private bool _isLoggingIn;
protected override void Awake()
{
base.Awake();
// 如果 mainCanvasGroup 没有在 Inspector 中赋值,递归查找子物体
if (mainCanvasGroup == null)
{
mainCanvasGroup = GetComponentInChildren<CanvasGroup>(true);
}
}
private void Start()
{
// 确保初始状态alpha=0, 不可交互, 不可射线检测
// Canvas 处于 inactive 状态,这些值在激活后生效)
if (mainCanvasGroup != null)
{
mainCanvasGroup.alpha = 0f;
mainCanvasGroup.interactable = false;
mainCanvasGroup.blocksRaycasts = false;
}
// 在 Start 中绑定按钮事件(确保序列化引用已就绪)
if (closeButton != null)
{
closeButton.onClick.AddListener(OnCloseClicked);
}
else
{
Debug.LogWarning("[LoginPage] closeButton 未赋值");
}
if (tapTapButton != null)
{
tapTapButton.onClick.AddListener(OnTapTapClicked);
}
else
{
Debug.LogWarning("[LoginPage] tapTapButton 未赋值");
}
// 在 Start 中订阅 AuthService 事件
SubscribeAuthEvents();
}
private void OnDestroy()
{
UnsubscribeAuthEvents();
}
private void OnEnable()
{
SubscribeAuthEvents();
}
private void OnDisable()
{
UnsubscribeAuthEvents();
}
#region IchniOnlineAuthService Event Subscription
private void SubscribeAuthEvents()
{
IchniOnlineAuthService.OnLoginSuccess -= OnAuthLoginSuccess;
IchniOnlineAuthService.OnLoginFailed -= OnAuthLoginFailed;
IchniOnlineAuthService.OnLoginCanceled -= OnAuthLoginCanceled;
IchniOnlineAuthService.OnLoginSuccess += OnAuthLoginSuccess;
IchniOnlineAuthService.OnLoginFailed += OnAuthLoginFailed;
IchniOnlineAuthService.OnLoginCanceled += OnAuthLoginCanceled;
}
private void UnsubscribeAuthEvents()
{
IchniOnlineAuthService.OnLoginSuccess -= OnAuthLoginSuccess;
IchniOnlineAuthService.OnLoginFailed -= OnAuthLoginFailed;
IchniOnlineAuthService.OnLoginCanceled -= OnAuthLoginCanceled;
}
#endregion
/// <summary>
/// 点击 Close 按钮:淡出登录页,恢复 StartPage 交互
/// </summary>
private void OnCloseClicked()
{
FadeOut(0.5f, false, RestoreStartPage);
}
/// <summary>
/// 点击 TapTap 按钮:通过 AuthService 发起 TapTap 登录
/// </summary>
private void OnTapTapClicked()
{
if (_isLoggingIn) return;
_isLoggingIn = true;
tapTapButton.interactable = false;
if (closeButton != null) closeButton.interactable = false;
Debug.Log("[LoginPage] 正在登录...");
IchniOnlineAuthService.LoginWithTapTap();
}
/// <summary>
/// AuthService 登录成功回调
/// </summary>
private void OnAuthLoginSuccess(LoginResponseDto response)
{
_isLoggingIn = false;
if (response == null || response.User == null)
{
Debug.Log("[LoginPage] 登录成功(无用户详情)");
FadeOut(0.5f, false, RestoreStartPage);
return;
}
Debug.Log($"[LoginPage] 登录成功,用户:{response.User.DisplayName}ID: {response.User.UserId}");
// 登录成功后淡出登录页
FadeOut(0.5f, false, RestoreStartPage);
}
/// <summary>
/// AuthService 登录失败回调
/// </summary>
private void OnAuthLoginFailed(string error)
{
_isLoggingIn = false;
RestoreButtons();
Debug.LogError($"[LoginPage] 登录失败:{error}");
}
/// <summary>
/// AuthService 登录取消回调
/// </summary>
private void OnAuthLoginCanceled()
{
_isLoggingIn = false;
RestoreButtons();
Debug.Log("[LoginPage] 用户取消了登录");
}
private void RestoreButtons()
{
if (tapTapButton != null) tapTapButton.interactable = true;
if (closeButton != null) closeButton.interactable = true;
}
/// <summary>
/// 恢复 StartPage 的交互能力
/// </summary>
private void RestoreStartPage()
{
if (startUIPage != null)
{
startUIPage.RestoreInteraction();
}
}
}
}