123 lines
3.8 KiB
C#
123 lines
3.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using Sirenix.OdinInspector;
|
||
using TapSDK.Core;
|
||
using TapSDK.Login;
|
||
using UnityEngine;
|
||
|
||
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;
|
||
|
||
/// <summary>
|
||
/// TapTap 登录成功并获取 AccessToken 后触发,参数为 TapTapAccount 和 AccessToken。
|
||
/// AuthService 可订阅此事件在通用成功流程之前拦截 token 进行 API 调用。
|
||
/// </summary>
|
||
public event Action<TapTapAccount, AccessToken> OnLoginWithToken;
|
||
|
||
private bool _initialized;
|
||
|
||
private void Awake()
|
||
{
|
||
if (Instance != null)
|
||
{
|
||
Destroy(gameObject);
|
||
return;
|
||
}
|
||
|
||
Instance = this;
|
||
DontDestroyOnLoad(gameObject);
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
InitializeTapTapSDK();
|
||
}
|
||
|
||
private void InitializeTapTapSDK()
|
||
{
|
||
if (_initialized) return;
|
||
|
||
// 核心配置 详细参数见 [TapTapSDK]
|
||
TapTapSdkOptions coreOptions = new TapTapSdkOptions()
|
||
{
|
||
clientId = "hkbfpbh2jv2kbxupmo",
|
||
clientToken = "Cry8OrA9EmfAetgu1RduPrWntgba2Qt44uC5tfEB",
|
||
region = TapTapRegionType.Overseas,
|
||
preferredLanguage = TapTapLanguageType.en,
|
||
enableLog = true
|
||
};
|
||
// TapSDK 初始化
|
||
|
||
TapTapSDK.Init(coreOptions);
|
||
_initialized = true;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发起 TapTap 登录,由 UI 按钮调用。
|
||
/// 登录结果通过事件 OnLoginSuccess / OnLoginCanceled / OnLoginFailed 通知。
|
||
/// </summary>
|
||
public async void StartTapTapLogin()
|
||
{
|
||
// 确保 SDK 已初始化
|
||
if (!_initialized)
|
||
{
|
||
InitializeTapTapSDK();
|
||
}
|
||
|
||
try
|
||
{
|
||
// 定义授权范围
|
||
List<string> scopes = new List<string>
|
||
{
|
||
TapTapLogin.TAP_LOGIN_SCOPE_PUBLIC_PROFILE,
|
||
TapTapLogin.TAP_LOGIN_SCOPE_EMAIL
|
||
};
|
||
// 发起 Tap 登录
|
||
var account = await TapTapLogin.Instance.LoginWithScopes(scopes.ToArray());
|
||
Debug.Log($"TapTap 登录成功,用户 ID:{account.openId},name:{account.name}");
|
||
OnLoginWithToken?.Invoke(account, account.accessToken);
|
||
LoginCacheManager.SaveFromTapTapAccount(account);
|
||
OnLoginSuccess?.Invoke(account);
|
||
}
|
||
catch (TaskCanceledException)
|
||
{
|
||
Debug.Log("用户取消 TapTap 登录");
|
||
OnLoginCanceled?.Invoke();
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
Debug.LogError($"TapTap 登录失败:{exception}");
|
||
OnLoginFailed?.Invoke(exception.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 登出 TapTap
|
||
/// </summary>
|
||
public void Logout()
|
||
{
|
||
TapTapLogin.Instance.Logout();
|
||
Debug.Log("TapTap 已登出");
|
||
}
|
||
}
|
||
} |