[M] all functional

This commit is contained in:
2024-08-16 00:56:24 +08:00
parent c355784122
commit 1a0af9d725
14 changed files with 1278 additions and 171 deletions

View File

@@ -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");
}
}
}
}

View File

@@ -1,61 +0,0 @@
using System;
using asap.core;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace tysdk
{
public partial class TYSdkFacade : MonoBehaviour
{
/*================================================
_ ____
/ \ | _ \
/ _ \ | | | |
/ ___ \| |_| |
/_/ \_\____/
=================================================*/
public void InitAdCallback(string jstr)
{
try{
var jobj = JObject.Parse(jstr);
if(jobj["code"].ToString() == "0")
UnityEngine.Debug.Log($"[TYSdkFacade] InitAdCallback Success");
else
UnityEngine.Debug.LogError($"[TYSdkFacade] InitAdCallback Failed ");
}
catch(Exception e)
{
UnityEngine.Debug.LogError($"[TYSdkFacade] InitAdCallback error: {e.Message}");
}
}
public void LoadAdCallback(string jstr)
{
try
{
var adLoadinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<ADLoadInfo>(jstr);
GContext.Publish(adLoadinfo);
}
catch (Exception e)
{
UnityEngine.Debug.LogError($"[TYSdkFacade] LoadAdCallback error: {e.Message}");
}
}
public void ShowAdCallback(string jstr)
{
try
{
var adShowInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<ShowADInfo>(jstr);
GContext.Publish(adShowInfo);
}
catch (Exception e)
{
UnityEngine.Debug.LogError($"[TYSdkFacade] ShowAdCallback error: {e.Message}");
}
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 179ddafea9354cadbfabca342e552139
timeCreated: 1722855804

View File

@@ -18,18 +18,26 @@ namespace tysdk
|___/
=================================================*/
public async Task<PaymentInfo> Pay(string prodId, string prodPrice, string prodName, int count, string pType)
//public async Task<PaymentInfo> Pay(string prodId, string prodPrice, string prodName, int count, string pType, string price_amount_micros =null)
public async Task<PaymentInfo> Pay(SKUDetail prod, int count)
{
if (!IsLoggedIn) return new PaymentInfo() { code = "-1", msg = "未登录" };
var orderId = System.Guid.NewGuid().ToString();
var extraDic = new Dictionary<string, object>() {
Dictionary<string, object> extraDic = new Dictionary<string, object>() {
{"paytime" , System.DateTime.UtcNow.Ticks},
{"sum" , float.Parse(prodPrice) * count},
{"sum" , prod.ProdPrice * count},
};
string extraInfo = Newtonsoft.Json.JsonConvert.SerializeObject(extraDic);
//to base64
extraInfo = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(extraInfo));
var prodId = prod.ProdID;
var prodPrice = prod.price;
var prodName = prod.title;
var pType = "googleiab.global.app";
#if UNITY_EDITOR
@@ -44,6 +52,7 @@ namespace tysdk
price = prodPrice
};
return result;
#elif UNITY_ANDROID
@@ -52,7 +61,10 @@ namespace tysdk
{
taskSource.SetResult((PaymentInfo)callback);
});
//UnityBridgeFunc.UnityKnow(prodId, prodName, count.ToString(), orderId, extraInfo);
UnityBridgeFunc.UnityKnowNew(prodId, prodPrice, prodName, count.ToString(), orderId, extraInfo,pType);
var result = await taskSource.Task;
result.count = count;
result.orderId = orderId;

View File

@@ -23,6 +23,11 @@ namespace tysdk
public int code;
}
public class LinkCheckInfo : ITYSdkCallback
{
public int code;
}
public class PaymentInfo : ITYSdkCallback
{
public bool isSuccess => code == "0";
@@ -56,7 +61,7 @@ namespace tysdk
LocaleCurrencyCode = x["LocaleCurrencyCode"].ToString(),
localeCurrencySymbol = x["localeCurrencySymbol"].ToString(),
localizedDescription = x["localizedDescription"].ToString(),
localizedTitle = x["localizedTitle"].ToString()
title = x["localizedTitle"].ToString()
};
}).ToList();
products = prodList;
@@ -81,6 +86,10 @@ namespace tysdk
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
public string ProdPriceStr => price;
public float ProdPrice => float.Parse(price_amount_micros) / 1000000;
public string ProdID => ourProductId;
}
#elif UNITY_IOS
public class SKUDetail
@@ -90,7 +99,12 @@ namespace tysdk
public string LocaleCurrencyCode;
public string localeCurrencySymbol;
public string localizedDescription;
public string localizedTitle;
public string title;
public string type;
public string ProdPriceStr => price;
public float ProdPrice => float.Parse(price);
public string ProdID => productId;
public override string ToString()
{
@@ -104,41 +118,4 @@ namespace tysdk
{
public bool isAccepted;
}
public class ADLoadInfo
{
public bool isSuccess => code == "0";
public string code;
public string errStr;
public override string ToString()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
}
public enum EShowAdState : byte
{
StartShow = 1,
Reward = 2,
Loading = 3,
LoadFaild = 4,
ShowFaild = 5,
Closed = 6,
Finished = 7,
TimeOut = 8
}
public class ShowADInfo
{
public string code;
public string message;
public string reward;
public EShowAdState state => (EShowAdState)int.Parse(code);
public override string ToString()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
}
}

View File

@@ -18,6 +18,9 @@ namespace tysdk
//登录
public static void UnityLoginByTokenFun(string token){}
public static void UnityLogin(EAccoutType accoutType){}
public static void LinkAccount(EAccoutType accoutType){}
public static void UnlinkAccount(EAccoutType accoutType){}
public static void LinkCheck(EAccoutType accoutType){}
public static void UnityGetIdentityFun(string type){}
@@ -27,8 +30,9 @@ namespace tysdk
string productCount, string prodorderId, string appInfo, string pType) { }
//支付
public static void UnityKnow(string userId, string productId, string productPrice, string productName,
string productCount, string prodorderId, string appInfo){}
public static void UnityKnow(String productId, String productName, String productCount,
String prodorderId, String appInfo) {}
//获取商品列表
public static void GetSKUList() { }
@@ -81,11 +85,9 @@ namespace tysdk
public static void UnityLogin(EAccoutType accoutType)
{
var typeName = GetLoginTypeName(accoutType);
Debug.LogError("typeName:::" + typeName);
using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
{
sdkManager.CallStatic("UnityLogin", typeName);
sdkManager.CallStatic("UnityLogin", accoutType.ToString());
}
}
@@ -99,7 +101,36 @@ namespace tysdk
public static void UnityLogOutByChannel(EAccoutType accoutType)
{
sdkManager.CallStatic("UnityLogOutByChannel", accoutType.ToString());
using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
{
sdkManager.CallStatic("UnityLogOutByChannel", accoutType.ToString());
}
}
public static void LinkAccount(EAccoutType accoutType)
{
using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
{
string userId = TYSdkFacade.TYAccountInfo.strUserId;
sdkManager.CallStatic("LinkAccount", accoutType.ToString(), userId);
}
}
public static void UnlinkAccount(EAccoutType accoutType)
{
using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
{
sdkManager.CallStatic("UnlinkAccount", accoutType.ToString());
}
}
public static void LinkCheck(EAccoutType accoutType)
{
using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
{
sdkManager.CallStatic("LinkCheck", accoutType.ToString());
}
}
//支付new
@@ -113,20 +144,21 @@ namespace tysdk
productCount, prodorderId, appInfo,pType);
}
}
//支付
public static void UnityKnow(string userId, string productId, string productPrice, string productName,
string productCount, string prodorderId, string appInfo)
public static void UnityKnow(String productId, String productName, String productCount, String prodorderId, String appInfo)
{
using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
{
sdkManager.CallStatic("UnityKnow",userId, productId, productPrice, productName,
productCount, prodorderId, appInfo);
sdkManager.CallStatic("UnityKnow", productId, productName, productCount, prodorderId, appInfo);
}
}
//获取商品列表
public static void GetSKUList()
{
UnityEngine.Debug.Log("UnityBridgeFunc.GetSKUList()");
using(var sdkManager = new AndroidJavaClass(SDK_CLASS))
{
sdkManager.CallStatic("thirdExtend");
@@ -231,6 +263,54 @@ namespace tysdk
[DllImport("__Internal")]
private static extern void UnityLogoutGuest();
public static void LinkAccount(EAccoutType accoutType)
{
switch (accoutType)
{
case EAccoutType.hwGoogle:
LinkGoogle();
break;
case EAccoutType.hwFacebook:
LinkFacebook();
break;
}
}
public static void UnlinkAccount(EAccoutType accoutType)
{
switch (accoutType)
{
case EAccoutType.hwGoogle:
UnlinkGoogle();
break;
case EAccoutType.hwFacebook:
UnlinkFacebook();
break;
}
}
[DllImport("__Internal")] private static extern void LinkGoogle();
[DllImport("__Internal")] private static extern void UnlinkGoogle();
[DllImport("__Internal")] private static extern void LinkFacebook();
[DllImport("__Internal")] private static extern void UnlinkFacebook();
public static void LinkCheck(EAccoutType accoutType)
{
switch (accoutType)
{
case EAccoutType.hwGoogle:
IsLinkedGoogle();
break;
case EAccoutType.hwFacebook:
IsLinkedFacebook();
break;
}
}
[DllImport("__Internal")] private static extern void IsLinkedGoogle();
[DllImport("__Internal")] private static extern void IsLinkedFacebook();
//支付
[DllImport("__Internal")]
public static extern void UnityKnow(string userId, string productId, string productPrice, string productName,