添加TapTap登录
This commit is contained in:
@@ -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