添加TapTap登录
This commit is contained in:
92
Assets/Scripts/Online/Logic/LoginCacheManager.cs
Normal file
92
Assets/Scripts/Online/Logic/LoginCacheManager.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using IchniOnline.Online.Models;
|
||||
using TapSDK.Login;
|
||||
using UnityEngine;
|
||||
|
||||
namespace IchniOnline.Online.Logic
|
||||
{
|
||||
/// <summary>
|
||||
/// 登录缓存的读写管理器。所有方法均为静态,可在任意位置调用。
|
||||
/// 使用 ES3 进行本地持久化。
|
||||
/// </summary>
|
||||
public static class LoginCacheManager
|
||||
{
|
||||
private const string ES3_KEY = "Ichni_LoginCache";
|
||||
|
||||
/// <summary>
|
||||
/// 本地是否存在有效的登录缓存
|
||||
/// </summary>
|
||||
public static bool HasCachedLogin
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!ES3.KeyExists(ES3_KEY)) return false;
|
||||
var data = ES3.Load<LoginCacheData>(ES3_KEY);
|
||||
return data != null && data.IsValid;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存的登录数据,无缓存返回 null
|
||||
/// </summary>
|
||||
public static LoginCacheData CachedData
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!ES3.KeyExists(ES3_KEY)) return null;
|
||||
return ES3.Load<LoginCacheData>(ES3_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将 TapTap 登录结果写入本地缓存
|
||||
/// </summary>
|
||||
public static void SaveFromTapTapAccount(TapTapAccount account)
|
||||
{
|
||||
if (account == null)
|
||||
{
|
||||
Debug.LogWarning("[LoginCacheManager] account 为 null,跳过缓存");
|
||||
return;
|
||||
}
|
||||
|
||||
var data = new LoginCacheData(
|
||||
account.openId,
|
||||
account.unionId,
|
||||
account.name,
|
||||
account.avatar,
|
||||
account.email
|
||||
);
|
||||
|
||||
ES3.Save(ES3_KEY, data);
|
||||
Debug.Log($"[LoginCacheManager] 已缓存登录数据,openId={data.openId}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入模拟数据(供编辑器工具使用)
|
||||
/// </summary>
|
||||
public static void SaveMockData()
|
||||
{
|
||||
var data = new LoginCacheData(
|
||||
"mock_openid_001",
|
||||
"mock_unionid_001",
|
||||
"MockUser",
|
||||
"",
|
||||
"mock@test.com"
|
||||
);
|
||||
|
||||
ES3.Save(ES3_KEY, data);
|
||||
Debug.Log($"[LoginCacheManager] 已写入模拟缓存数据");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除本地登录缓存
|
||||
/// </summary>
|
||||
public static void Clear()
|
||||
{
|
||||
if (ES3.KeyExists(ES3_KEY))
|
||||
{
|
||||
ES3.DeleteKey(ES3_KEY);
|
||||
Debug.Log("[LoginCacheManager] 已清除登录缓存");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Online/Logic/LoginCacheManager.cs.meta
Normal file
2
Assets/Scripts/Online/Logic/LoginCacheManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e7ca7506cce807c47b5a4b482eaa36a4
|
||||
@@ -1,18 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Ichni.RhythmGame;
|
||||
using Sirenix.OdinInspector;
|
||||
using TapSDK.Core;
|
||||
using TapSDK.Login;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Online.Logic
|
||||
namespace IchniOnline.Online.Logic
|
||||
{
|
||||
public class ThirdPartyServiceManager:SerializedMonoBehaviour
|
||||
{
|
||||
public static ThirdPartyServiceManager Instance { get; private set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// TapTap 登录成功时触发,参数为登录获得的 TapTapAccount
|
||||
/// </summary>
|
||||
public event Action<TapTapAccount> OnLoginSuccess;
|
||||
|
||||
/// <summary>
|
||||
/// TapTap 登录被用户取消时触发
|
||||
/// </summary>
|
||||
public event Action OnLoginCanceled;
|
||||
|
||||
/// <summary>
|
||||
/// TapTap 登录失败时触发,参数为异常信息
|
||||
/// </summary>
|
||||
public event Action<string> OnLoginFailed;
|
||||
|
||||
private bool _initialized;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null)
|
||||
@@ -27,6 +43,13 @@ namespace Online.Logic
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InitializeTapTapSDK();
|
||||
}
|
||||
|
||||
private void InitializeTapTapSDK()
|
||||
{
|
||||
if (_initialized) return;
|
||||
|
||||
// 核心配置 详细参数见 [TapTapSDK]
|
||||
TapTapSdkOptions coreOptions = new TapTapSdkOptions()
|
||||
{
|
||||
@@ -37,12 +60,22 @@ namespace Online.Logic
|
||||
enableLog = true
|
||||
};
|
||||
// TapSDK 初始化
|
||||
TapTapSDK.Init(coreOptions);
|
||||
TapTapLoginTest();
|
||||
TapTapSDK.Init(coreOptions);
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
async Task TapTapLoginTest()
|
||||
/// <summary>
|
||||
/// 发起 TapTap 登录,由 UI 按钮调用。
|
||||
/// 登录结果通过事件 OnLoginSuccess / OnLoginCanceled / OnLoginFailed 通知。
|
||||
/// </summary>
|
||||
public async void StartTapTapLogin()
|
||||
{
|
||||
// 确保 SDK 已初始化
|
||||
if (!_initialized)
|
||||
{
|
||||
InitializeTapTapSDK();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 定义授权范围
|
||||
@@ -52,17 +85,30 @@ namespace Online.Logic
|
||||
TapTapLogin.TAP_LOGIN_SCOPE_EMAIL
|
||||
};
|
||||
// 发起 Tap 登录
|
||||
var userInfo = await TapTapLogin.Instance.LoginWithScopes(scopes.ToArray());
|
||||
Debug.Log($"登录成功,当前用户 ID:{JsonUtility.ToJson(userInfo.accessToken)}");
|
||||
var account = await TapTapLogin.Instance.LoginWithScopes(scopes.ToArray());
|
||||
Debug.Log($"TapTap 登录成功,用户 ID:{account.openId},name:{account.name}");
|
||||
LoginCacheManager.SaveFromTapTapAccount(account);
|
||||
OnLoginSuccess?.Invoke(account);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
Debug.Log("用户取消登录");
|
||||
Debug.Log("用户取消 TapTap 登录");
|
||||
OnLoginCanceled?.Invoke();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Debug.Log($"登录失败,出现异常:{exception}");
|
||||
Debug.LogError($"TapTap 登录失败:{exception}");
|
||||
OnLoginFailed?.Invoke(exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登出 TapTap
|
||||
/// </summary>
|
||||
public void Logout()
|
||||
{
|
||||
TapTapLogin.Instance.Logout();
|
||||
Debug.Log("TapTap 已登出");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user