Files
ichni_Official/Assets/Scripts/UI/LoginPage/LoginPage.cs

183 lines
5.3 KiB
C#
Raw Normal View History

2026-06-15 14:54:30 +08:00
using System;
using IchniOnline.Online.Logic;
2026-06-15 18:18:16 +08:00
using IchniOnline.Online.Network.Models;
2026-06-15 14:54:30 +08:00
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 未赋值");
}
2026-06-15 18:18:16 +08:00
// 在 Start 中订阅 AuthService 事件
SubscribeAuthEvents();
2026-06-15 14:54:30 +08:00
}
private void OnDestroy()
{
2026-06-15 18:18:16 +08:00
UnsubscribeAuthEvents();
2026-06-15 14:54:30 +08:00
}
private void OnEnable()
{
2026-06-15 18:18:16 +08:00
SubscribeAuthEvents();
2026-06-15 14:54:30 +08:00
}
private void OnDisable()
{
2026-06-15 18:18:16 +08:00
UnsubscribeAuthEvents();
2026-06-15 14:54:30 +08:00
}
2026-06-15 18:18:16 +08:00
#region IchniOnlineAuthService Event Subscription
2026-06-15 14:54:30 +08:00
2026-06-15 18:18:16 +08:00
private void SubscribeAuthEvents()
2026-06-15 14:54:30 +08:00
{
2026-06-15 18:18:16 +08:00
IchniOnlineAuthService.OnLoginSuccess -= OnAuthLoginSuccess;
IchniOnlineAuthService.OnLoginFailed -= OnAuthLoginFailed;
IchniOnlineAuthService.OnLoginCanceled -= OnAuthLoginCanceled;
IchniOnlineAuthService.OnLoginSuccess += OnAuthLoginSuccess;
IchniOnlineAuthService.OnLoginFailed += OnAuthLoginFailed;
IchniOnlineAuthService.OnLoginCanceled += OnAuthLoginCanceled;
2026-06-15 14:54:30 +08:00
}
2026-06-15 18:18:16 +08:00
private void UnsubscribeAuthEvents()
2026-06-15 14:54:30 +08:00
{
2026-06-15 18:18:16 +08:00
IchniOnlineAuthService.OnLoginSuccess -= OnAuthLoginSuccess;
IchniOnlineAuthService.OnLoginFailed -= OnAuthLoginFailed;
IchniOnlineAuthService.OnLoginCanceled -= OnAuthLoginCanceled;
2026-06-15 14:54:30 +08:00
}
#endregion
/// <summary>
/// 点击 Close 按钮:淡出登录页,恢复 StartPage 交互
/// </summary>
private void OnCloseClicked()
{
FadeOut(0.5f, false, RestoreStartPage);
}
/// <summary>
2026-06-15 18:18:16 +08:00
/// 点击 TapTap 按钮:通过 AuthService 发起 TapTap 登录
2026-06-15 14:54:30 +08:00
/// </summary>
private void OnTapTapClicked()
{
if (_isLoggingIn) return;
_isLoggingIn = true;
tapTapButton.interactable = false;
if (closeButton != null) closeButton.interactable = false;
2026-06-15 18:18:16 +08:00
Debug.Log("[LoginPage] 正在登录...");
IchniOnlineAuthService.LoginWithTapTap();
2026-06-15 14:54:30 +08:00
}
/// <summary>
2026-06-15 18:18:16 +08:00
/// AuthService 登录成功回调
2026-06-15 14:54:30 +08:00
/// </summary>
2026-06-15 18:18:16 +08:00
private void OnAuthLoginSuccess(LoginResponseDto response)
2026-06-15 14:54:30 +08:00
{
_isLoggingIn = false;
2026-06-15 18:18:16 +08:00
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}");
2026-06-15 14:54:30 +08:00
// 登录成功后淡出登录页
FadeOut(0.5f, false, RestoreStartPage);
}
/// <summary>
2026-06-15 18:18:16 +08:00
/// AuthService 登录失败回调
2026-06-15 14:54:30 +08:00
/// </summary>
2026-06-15 18:18:16 +08:00
private void OnAuthLoginFailed(string error)
2026-06-15 14:54:30 +08:00
{
_isLoggingIn = false;
RestoreButtons();
2026-06-15 18:18:16 +08:00
Debug.LogError($"[LoginPage] 登录失败:{error}");
2026-06-15 14:54:30 +08:00
}
/// <summary>
2026-06-15 18:18:16 +08:00
/// AuthService 登录取消回调
2026-06-15 14:54:30 +08:00
/// </summary>
2026-06-15 18:18:16 +08:00
private void OnAuthLoginCanceled()
2026-06-15 14:54:30 +08:00
{
_isLoggingIn = false;
RestoreButtons();
2026-06-15 18:18:16 +08:00
Debug.Log("[LoginPage] 用户取消了登录");
2026-06-15 14:54:30 +08:00
}
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();
}
}
}
}