[M] all functional
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using asap.core;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -21,18 +22,44 @@ namespace tysdk
|
||||
public string token;
|
||||
public string jwtToken;
|
||||
public string strUserId => userId.ToString();
|
||||
public string channelstr;
|
||||
public EAccoutType channel => accoutTypeMap[channelstr];
|
||||
public string channelstr {
|
||||
set{
|
||||
channel = accoutTypeMap[value];
|
||||
linkedAccout.Add(channel);
|
||||
}
|
||||
}
|
||||
public HashSet<EAccoutType> linkedAccout = new HashSet<EAccoutType>();
|
||||
public EAccoutType channel {get; private set;}
|
||||
|
||||
private const string KEY_ACCOUNT_INFO = "KEY_ACCOUNT_INFO";
|
||||
|
||||
public void AddLinkedAccount(EAccoutType accoutType)
|
||||
{
|
||||
if (!linkedAccout.Contains(accoutType))
|
||||
{
|
||||
linkedAccout.Add(accoutType);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddLinkedAccount(string channel)
|
||||
{
|
||||
if(accoutTypeMap.TryGetValue(channel, out var accoutType))
|
||||
{
|
||||
if (!linkedAccout.Contains(accoutType))
|
||||
{
|
||||
linkedAccout.Add(accoutType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
JObject jInfo = new JObject();
|
||||
jInfo["channel"] = channelstr;
|
||||
jInfo["channel"] = accoutTypeMap.First(x => x.Value == channel).Key;
|
||||
jInfo["token"] = token;
|
||||
jInfo["userId"] = userId;
|
||||
jInfo["jwtToken"] = jwtToken;
|
||||
jInfo["linkedAccout"] = string.Join(",", linkedAccout.Select(x => x.ToString()));
|
||||
PlayerPrefs.SetString(KEY_ACCOUNT_INFO, jInfo.ToString());
|
||||
}
|
||||
|
||||
@@ -52,6 +79,7 @@ namespace tysdk
|
||||
token = (string)jInfo["token"],
|
||||
channelstr = (string)jInfo["channel"],
|
||||
jwtToken = (string)jInfo["jwtToken"],
|
||||
linkedAccout = jInfo["linkedAccout"].ToString().Split(',').Select(x => (EAccoutType)Enum.Parse(typeof(EAccoutType), x)).ToHashSet()
|
||||
};
|
||||
}
|
||||
else
|
||||
@@ -62,7 +90,7 @@ namespace tysdk
|
||||
}
|
||||
|
||||
private static AccountInfo _accountInfo;
|
||||
public AccountInfo TYAccountInfo => _accountInfo;
|
||||
public static AccountInfo TYAccountInfo => _accountInfo;
|
||||
|
||||
private static IDictionary<string, Action<ITYSdkCallback>> callbacks = new Dictionary<string, Action<ITYSdkCallback>>();
|
||||
|
||||
@@ -177,16 +205,32 @@ namespace tysdk
|
||||
{
|
||||
var loginData = data["respObj"]["result"];
|
||||
|
||||
_accountInfo = new AccountInfo()
|
||||
var userId = (int)loginData["userId"];
|
||||
var token = (string)loginData["token"];
|
||||
var jwtToken = (string)loginData["jwttoken"];
|
||||
var channelstr = (string)loginData["loginChannelTypeC"];
|
||||
|
||||
var savedInfo = AccountInfo.GetSavedAccoutInfo();
|
||||
if(savedInfo == null || savedInfo.userId != userId)
|
||||
{
|
||||
userId = (int)loginData["userId"],
|
||||
token = (string)loginData["token"],
|
||||
jwtToken = (string)loginData["jwttoken"],
|
||||
channelstr = (string)loginData["loginChannelTypeC"]
|
||||
};
|
||||
|
||||
_accountInfo.Save();
|
||||
_accountInfo = new AccountInfo()
|
||||
{
|
||||
userId = (int)loginData["userId"],
|
||||
token = (string)loginData["token"],
|
||||
jwtToken = (string)loginData["jwttoken"],
|
||||
channelstr = (string)loginData["loginChannelTypeC"]
|
||||
};
|
||||
|
||||
_accountInfo.Save();
|
||||
}
|
||||
else
|
||||
{
|
||||
savedInfo.token = token;
|
||||
savedInfo.jwtToken = jwtToken;
|
||||
savedInfo.AddLinkedAccount(channelstr);
|
||||
savedInfo.Save();
|
||||
_accountInfo = savedInfo;
|
||||
}
|
||||
|
||||
result.isSuccess = true;
|
||||
result.userId = _accountInfo.userId;
|
||||
@@ -207,5 +251,71 @@ namespace tysdk
|
||||
|
||||
UnityBridgeFunc.SetGaUserInfo(strUserId);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
return result.isSuccess;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否已经绑定
|
||||
/// <returns>
|
||||
/// 1 已经绑定
|
||||
/// 0 未绑定
|
||||
/// -1 失败
|
||||
/// </returns>
|
||||
/// </summary>
|
||||
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);
|
||||
});
|
||||
|
||||
UnityBridgeFunc.LinkCheck(accoutType);
|
||||
|
||||
var code = (await taskSource.Task).code;
|
||||
|
||||
if (code == 1 && !_accountInfo.linkedAccout.Contains(accoutType))
|
||||
{
|
||||
_accountInfo.linkedAccout.Add(accoutType);
|
||||
_accountInfo.Save();
|
||||
}
|
||||
|
||||
if(code == -1)
|
||||
{
|
||||
Debug.LogError("[TYSdkFacade] CheckLinkResult error");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user