[M] new call back
[wip]
This commit is contained in:
@@ -19,7 +19,7 @@ namespace tysdk
|
||||
public string jwtToken;
|
||||
public string strUserId => userId.ToString();
|
||||
public HashSet<EAccoutType> linkedAccout = new HashSet<EAccoutType>();
|
||||
public EAccoutType channel {get; set;}
|
||||
public EAccoutType channel { get; set; }
|
||||
public string userName;
|
||||
public string avatar;
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace tysdk
|
||||
{"ios13", EAccoutType.Apple}
|
||||
};
|
||||
|
||||
private static EAccoutType ConvertAccoutType(string accountType)
|
||||
private static EAccoutType ConvertAccoutType(string accountType)
|
||||
{
|
||||
if (accoutTypeMap.ContainsKey(accountType))
|
||||
{
|
||||
@@ -81,12 +81,12 @@ namespace tysdk
|
||||
linkedAccout = jInfo["linkedAccout"].ToString().Split(',').Select(x => ConvertAccoutType(x)).ToHashSet()
|
||||
};
|
||||
|
||||
if(jInfo.TryGetValue("userName", out var userName))
|
||||
if (jInfo.TryGetValue("userName", out var userName))
|
||||
{
|
||||
accountInfo.userName = (string) userName;
|
||||
accountInfo.userName = (string)userName;
|
||||
}
|
||||
|
||||
if(jInfo.TryGetValue("avatar", out var avatar))
|
||||
if (jInfo.TryGetValue("avatar", out var avatar))
|
||||
{
|
||||
accountInfo.avatar = (string)avatar;
|
||||
}
|
||||
@@ -103,8 +103,6 @@ namespace tysdk
|
||||
private static AccountInfo _accountInfo;
|
||||
public static AccountInfo TYAccountInfo => _accountInfo;
|
||||
|
||||
private static IDictionary<string, Action<ITYSdkCallback>> callbacks = new Dictionary<string, Action<ITYSdkCallback>>();
|
||||
|
||||
private static TYSdkFacade _instance;
|
||||
public static TYSdkFacade Instance
|
||||
{
|
||||
@@ -118,7 +116,6 @@ namespace tysdk
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
@@ -144,7 +141,7 @@ namespace tysdk
|
||||
public void Logout()
|
||||
{
|
||||
UnityEngine.Debug.Log("Logout");
|
||||
if(_accountInfo != null)
|
||||
if (_accountInfo != null)
|
||||
{
|
||||
UnityBridgeFunc.UnityLogOutByChannel(_accountInfo.channel);
|
||||
}
|
||||
@@ -157,7 +154,7 @@ namespace tysdk
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
await Task.Yield();
|
||||
var deviceId = UnityEngine.Device.SystemInfo.deviceUniqueIdentifier.Split('-')[0].Substring(0,8);
|
||||
var deviceId = UnityEngine.Device.SystemInfo.deviceUniqueIdentifier.Split('-')[0].Substring(0, 8);
|
||||
|
||||
var userId = (int)ulong.Parse(deviceId, System.Globalization.NumberStyles.HexNumber);
|
||||
|
||||
@@ -174,44 +171,53 @@ namespace tysdk
|
||||
|
||||
#elif UNITY_ANDROID || UNITY_IOS
|
||||
|
||||
var taskSource = new TaskCompletionSource<LoginInfo>();
|
||||
callbacks.Add("LoginResult", (ITYSdkCallback callback) =>
|
||||
{
|
||||
taskSource.SetResult((LoginInfo)callback);
|
||||
});
|
||||
|
||||
var task = TYSDKCallbackManager.Instance.RegisterCallback<LoginInfo>(TimeSpan.FromSeconds(30));
|
||||
|
||||
UnityBridgeFunc.UnityLogin(accoutType);
|
||||
|
||||
var loginInfo = await taskSource.Task;
|
||||
if(loginInfo.isSuccess)
|
||||
try
|
||||
{
|
||||
_accountInfo.channel = accoutType;
|
||||
_accountInfo.AddLinkedAccount(accoutType);
|
||||
_accountInfo.Save();
|
||||
var loginInfo = await task;
|
||||
if(loginInfo.isSuccess)
|
||||
{
|
||||
_accountInfo.channel = accoutType;
|
||||
_accountInfo.AddLinkedAccount(accoutType);
|
||||
_accountInfo.Save();
|
||||
}
|
||||
return loginInfo;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Debug.LogWarning($"[TYSdkFacade] Login failed: {e}");
|
||||
return new LoginInfo(){isSuccess = false};
|
||||
}
|
||||
return loginInfo;
|
||||
#endif
|
||||
}
|
||||
|
||||
public async Task<LoginInfo> LoginByToken()
|
||||
{
|
||||
var accoutInfo = AccountInfo.GetSavedAccoutInfo();
|
||||
if(accoutInfo == null) return new LoginInfo();
|
||||
if (accoutInfo == null) return new LoginInfo();
|
||||
|
||||
var taskSource = new TaskCompletionSource<LoginInfo>();
|
||||
callbacks.Add("LoginResult", (ITYSdkCallback callback) =>
|
||||
{
|
||||
taskSource.SetResult((LoginInfo)callback);
|
||||
});
|
||||
var task = TYSDKCallbackManager.Instance.RegisterCallback<LoginInfo>(TimeSpan.FromSeconds(30));
|
||||
|
||||
UnityBridgeFunc.UnityLoginByTokenFun(accoutInfo.token);
|
||||
|
||||
var loginInfo = await taskSource.Task;
|
||||
if(loginInfo.isSuccess)
|
||||
try
|
||||
{
|
||||
_accountInfo.Save();
|
||||
var loginInfo = await task;
|
||||
if (loginInfo.isSuccess)
|
||||
{
|
||||
_accountInfo.Save();
|
||||
}
|
||||
return loginInfo;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning($"[TYSdkFacade] Login by token failed: {e}");
|
||||
return new LoginInfo() { isSuccess = false };
|
||||
}
|
||||
return loginInfo;
|
||||
}
|
||||
|
||||
public void LoginResult(string json)
|
||||
@@ -231,7 +237,7 @@ namespace tysdk
|
||||
var jwtToken = (string)loginData["jwttoken"];
|
||||
|
||||
var savedInfo = AccountInfo.GetSavedAccoutInfo();
|
||||
if(savedInfo == null || savedInfo.userId != userId)
|
||||
if (savedInfo == null || savedInfo.userId != userId)
|
||||
{
|
||||
_accountInfo = new AccountInfo()
|
||||
{
|
||||
@@ -254,11 +260,7 @@ namespace tysdk
|
||||
SetUserInfo();
|
||||
}
|
||||
|
||||
if (callbacks.TryGetValue("LoginResult", out var action))
|
||||
{
|
||||
action.Invoke(result);
|
||||
callbacks.Remove("LoginResult");
|
||||
}
|
||||
TYSDKCallbackManager.Instance.TryTriggerCallback(result);
|
||||
}
|
||||
|
||||
private void SetUserInfo()
|
||||
@@ -269,42 +271,43 @@ namespace tysdk
|
||||
UnityBridgeFunc.SetGaUserInfo(strUserId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public async Task<bool> LinkAccount(EAccoutType accoutType)
|
||||
{
|
||||
await Task.Delay(300);
|
||||
return true;
|
||||
}
|
||||
#elif UNITY_IOS
|
||||
|
||||
public async Task<bool> LinkAccount(EAccoutType accoutType)
|
||||
#elif UNITY_IOS || UNITY_ANDROID
|
||||
public async Task<bool> LinkAccount(EAccoutType accoutType)
|
||||
{
|
||||
if (_accountInfo == null) return false;
|
||||
if( _accountInfo.linkedAccout.Contains(accoutType)) return true;
|
||||
|
||||
var taskSource = new TaskCompletionSource<LinkResult>();
|
||||
callbacks.Add("LinkAccoutResult", (ITYSdkCallback callback) =>
|
||||
{
|
||||
taskSource.SetResult((LinkResult)callback);
|
||||
});
|
||||
if (_accountInfo.linkedAccout.Contains(accoutType)) return true;
|
||||
|
||||
var task = TYSDKCallbackManager.Instance.RegisterCallback<LinkResult>();
|
||||
UnityBridgeFunc.LinkAccount(accoutType);
|
||||
|
||||
var result = await taskSource.Task;
|
||||
if (result.isSuccess)
|
||||
try
|
||||
{
|
||||
if(_accountInfo == null)
|
||||
_accountInfo = AccountInfo.GetSavedAccoutInfo();
|
||||
|
||||
if(_accountInfo != null)
|
||||
var result = await task;
|
||||
if (result.isSuccess)
|
||||
{
|
||||
_accountInfo.AddLinkedAccount(accoutType);
|
||||
_accountInfo.Save();
|
||||
}
|
||||
}
|
||||
if (_accountInfo == null)
|
||||
_accountInfo = AccountInfo.GetSavedAccoutInfo();
|
||||
|
||||
return result.isSuccess;
|
||||
if (_accountInfo != null)
|
||||
{
|
||||
_accountInfo.AddLinkedAccount(accoutType);
|
||||
_accountInfo.Save();
|
||||
}
|
||||
}
|
||||
return result.isSuccess;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning($"[TYSdkFacade] Link account failed: {e}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void LinkAccoutResult(string codestr)
|
||||
@@ -314,44 +317,8 @@ namespace tysdk
|
||||
isSuccess = codestr == "1"
|
||||
};
|
||||
|
||||
if (callbacks.TryGetValue("LinkAccoutResult", out var action))
|
||||
{
|
||||
action.Invoke(result);
|
||||
callbacks.Remove("LinkAccoutResult");
|
||||
}
|
||||
TYSDKCallbackManager.Instance.TryTriggerCallback(result);
|
||||
}
|
||||
|
||||
#elif UNITY_ANDROID
|
||||
|
||||
public async Task<bool> LinkAccount(EAccoutType accoutType)
|
||||
{
|
||||
if (_accountInfo == null) return false;
|
||||
if( _accountInfo.linkedAccout.Contains(accoutType)) return true;
|
||||
|
||||
var taskSource = new TaskCompletionSource<LoginInfo>();
|
||||
callbacks.Add("LoginResult", (ITYSdkCallback callback) =>
|
||||
{
|
||||
taskSource.SetResult((LoginInfo)callback);
|
||||
});
|
||||
|
||||
UnityBridgeFunc.LinkAccount(accoutType);
|
||||
|
||||
var result = await taskSource.Task;
|
||||
if (result.isSuccess)
|
||||
{
|
||||
if(_accountInfo == null)
|
||||
_accountInfo = AccountInfo.GetSavedAccoutInfo();
|
||||
|
||||
if(_accountInfo != null)
|
||||
{
|
||||
_accountInfo.AddLinkedAccount(accoutType);
|
||||
_accountInfo.Save();
|
||||
}
|
||||
}
|
||||
|
||||
return result.isSuccess;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
@@ -365,41 +332,40 @@ namespace tysdk
|
||||
public async Task<bool> LinkCheck(EAccoutType accoutType)
|
||||
{
|
||||
if (_accountInfo == null) return false;
|
||||
if( _accountInfo.linkedAccout.Contains(accoutType)) return true;
|
||||
|
||||
var taskSource = new TaskCompletionSource<LinkCheckInfo>();
|
||||
callbacks.Add("CheckLinkResult", (ITYSdkCallback callback) =>
|
||||
{
|
||||
taskSource.SetResult((LinkCheckInfo)callback);
|
||||
});
|
||||
if (_accountInfo.linkedAccout.Contains(accoutType)) return true;
|
||||
|
||||
var task = TYSDKCallbackManager.Instance.RegisterCallback<LinkCheckInfo>(TimeSpan.FromSeconds(30));
|
||||
UnityBridgeFunc.LinkCheck(accoutType);
|
||||
|
||||
var code = (await taskSource.Task).code;
|
||||
|
||||
if (code == 1 && !_accountInfo.linkedAccout.Contains(accoutType))
|
||||
try
|
||||
{
|
||||
_accountInfo.linkedAccout.Add(accoutType);
|
||||
_accountInfo.Save();
|
||||
}
|
||||
var result = await task;
|
||||
|
||||
if(code == -1)
|
||||
if (result.code == 1 && !_accountInfo.linkedAccout.Contains(accoutType))
|
||||
{
|
||||
_accountInfo.linkedAccout.Add(accoutType);
|
||||
_accountInfo.Save();
|
||||
}
|
||||
|
||||
if (result.code == -1)
|
||||
{
|
||||
Debug.LogError("[TYSdkFacade] CheckLinkResult error");
|
||||
}
|
||||
|
||||
return result.code == 1;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Debug.LogError("[TYSdkFacade] CheckLinkResult error");
|
||||
Debug.LogWarning($"[TYSdkFacade] CheckLinkResult error: {e}");
|
||||
return false;
|
||||
}
|
||||
|
||||
return code == 1;
|
||||
}
|
||||
|
||||
public void CheckLinkResult(string codestr)
|
||||
{
|
||||
if (callbacks.TryGetValue("CheckLinkResult", out var action))
|
||||
{
|
||||
var result = new LinkCheckInfo();
|
||||
result.code = int.Parse(codestr);
|
||||
action.Invoke(result);
|
||||
callbacks.Remove("CheckLinkResult");
|
||||
}
|
||||
var result = new LinkCheckInfo();
|
||||
result.code = int.Parse(codestr);
|
||||
TYSDKCallbackManager.Instance.TryTriggerCallback(result);
|
||||
}
|
||||
|
||||
public void Signout()
|
||||
@@ -411,7 +377,7 @@ namespace tysdk
|
||||
var tyurl = config.Get<string>("AGG_SERVER", defaultTyurl);
|
||||
|
||||
|
||||
string tycs = tyurl == defaultTyurl ?
|
||||
string tycs = tyurl == defaultTyurl ?
|
||||
"https://hwcsh.tygameworld.com/template/appCancel/note.html"
|
||||
:
|
||||
"https://customermanage-feature-test-web-test.tuyougame.cn/appCancel/note.html";
|
||||
|
||||
Reference in New Issue
Block a user