using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LC.Newtonsoft.Json; using TapTap.Common; using TapTap.Common.Internal.Utils; using TapTap.Login.Internal; using TapTap.Login.Internal.Http; using UnityEngine; namespace TapTap.Login.Standalone { public class TapLoginStandalone : ITapLoginPlatform { private static bool isOverseas = false; internal static bool IsOverseas => isOverseas; public void Init(string clientID) { } public void Init(string clientID, bool isCn, bool roundCorner) { isOverseas = !isCn; } public void ChangeConfig(bool roundCorner, bool isPortrait) { } public Task FetchProfile() { return LoginHelper.GetProfile(); } public Task GetProfile() { return LoginHelper.GetProfile(); } public Task GetAccessToken() { return LoginHelper.GetAccessToken(); } public Task Authorize(string[] permissions = null) { List allPermissions = MergePerssions(permissions); return AuthorizeInternal(allPermissions); } public Task Login() { return Login(new string[] {}); } private List MergePerssions(string[] permissions){ HashSet allPermissions = new HashSet(); allPermissions.Add(TapLogin.TAP_LOGIN_SCOPE_PUBLIC_PROFILE); if (permissions != null) { allPermissions.UnionWith(permissions); } if (TapLogin.DefaultPermissions != null) { allPermissions.UnionWith(TapLogin.DefaultPermissions); } if (TapCommon.Config.RegionType == RegionType.IO) { allPermissions.Remove(TapLogin.TAP_LOGIN_SCOPE_COMPLIANCE); } return allPermissions.ToList(); } public async Task Login(string[] permissions) { List allPermissions = MergePerssions(permissions); AccessToken token = await AuthorizeInternal(allPermissions); try { ProfileData profileData = await LoginService.GetProfile(TapTapSdk.ClientId, token); Profile profile = ConvertToProfile(profileData); SaveTapUser(token, profile); string message = string.Format(LoginLanguage.GetCurrentLang().LoginNotice(), profile.name); Texture avatar = await ImageUtils.LoadImage(profile.avatar); UI.UIManager.Instance.OpenToast(false, message, icon: avatar); } catch (TapException e) { throw e; } catch (Exception) { throw new TapException((int) TapErrorCode.ERROR_CODE_UNDEFINED, "UnKnow Error"); } return token; } public async Task Login(TapLoginPermissionConfig config) { Tuple result = await AuthorizeInternal(config); SaveTapUser(result.Item1, result.Item2); return result.Item1; } public void Logout() { LoginHelper.Logout(); } public Task GetTestQualification() { return Task.FromResult(false); } private Task AuthorizeInternal(IEnumerable permissions) { TaskCompletionSource tcs = new TaskCompletionSource(); LoginPanelController.OpenParams openParams = new LoginPanelController.OpenParams { ClientId = TapTapSdk.ClientId, Permissions = new HashSet(permissions).ToArray(), OnAuth = tokenData => { if (tokenData == null) { tcs.TrySetException(new TapException((int) TapErrorCode.ERROR_CODE_UNDEFINED, "UnKnow Error")); } else { // 将 TokenData 转化为 AccessToken AccessToken accessToken = ConvertToAccessToken(tokenData); tcs.TrySetResult(accessToken); } }, OnError = e => { tcs.TrySetException(e); }, OnClose = () => { tcs.TrySetException( new TapException((int) TapErrorCode.ERROR_CODE_LOGIN_CANCEL, "Login Cancel")); } }; TapTap.UI.UIManager.Instance.OpenUI("Prefabs/TapLogin/LoginPanel", openParams); return tcs.Task; } private Task> AuthorizeInternal(TapLoginPermissionConfig config) { TaskCompletionSource> tcs = new TaskCompletionSource>(); LoginWithPermissionsPanelController.OpenParams openParams = new LoginWithPermissionsPanelController.OpenParams { ClientId = TapTapSdk.ClientId, Name = config.Name, Permissions = config.Permissions, OnAuth = (tokenData, profileData) => { // 将 TokenData 转化为 AccessToken AccessToken accessToken = ConvertToAccessToken(tokenData); Profile profile = ConvertToProfile(profileData); tcs.TrySetResult(new Tuple(accessToken, profile)); } }; TapTap.UI.UIManager.Instance.OpenUI("Prefabs/TapLogin/LoginWithPermissionPanel", openParams); return tcs.Task; } private static AccessToken ConvertToAccessToken(TokenData tokenData) { return new AccessToken { kid = tokenData.Kid, accessToken = tokenData.Token, tokenType = tokenData.TokenType, macKey = tokenData.MacKey, macAlgorithm = tokenData.MacAlgorithm, scopeSet = tokenData.Scopes }; } private static Profile ConvertToProfile(ProfileData profileData) { return new Profile { openid = profileData.OpenId, unionid = profileData.UnionId, name = profileData.Name, avatar = profileData.Avatar, gender = profileData.Gender }; } private static void SaveTapUser(AccessToken accessToken, Profile profile) { DataStorage.SaveString("taptapsdk_accesstoken", JsonConvert.SerializeObject(accessToken)); DataStorage.SaveString("taptapsdk_profile", JsonConvert.SerializeObject(profile)); } } }