328 lines
11 KiB
C#
328 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using asap.core;
|
|
using Newtonsoft.Json.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace tysdk
|
|
{
|
|
public class TYSdkFacade : MonoBehaviour
|
|
{
|
|
public class AccountInfo
|
|
{
|
|
public int userId;
|
|
public string token;
|
|
public string jwtToken;
|
|
public string strUserId => userId.ToString();
|
|
public bool IDVerified;
|
|
}
|
|
|
|
private static AccountInfo _accountInfo;
|
|
public AccountInfo TYAccountInfo => _accountInfo;
|
|
|
|
private static IDictionary<string, Action<ITYSdkCallback>> callbacks = new Dictionary<string, Action<ITYSdkCallback>>();
|
|
|
|
private static TYSdkFacade _instance;
|
|
public static TYSdkFacade Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new GameObject("TYSdkFacade").AddComponent<TYSdkFacade>();
|
|
DontDestroyOnLoad(_instance.gameObject);
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
/*================================================
|
|
|
|
_ _
|
|
/ \ ___ ___ ___ _ _ _ __ | |_
|
|
/ _ \ / __/ __/ _ \| | | | '_ \| __|
|
|
/ ___ \ (_| (_| (_) | |_| | | | | |_
|
|
/_/ \_\___\___\___/ \__,_|_| |_|\__|
|
|
|
|
=================================================*/
|
|
|
|
|
|
public bool IsLoggedIn => _accountInfo != null;
|
|
|
|
public void LogOut()
|
|
{
|
|
UnityBridgeFunc.UnityLoginOut();
|
|
_accountInfo = null;
|
|
}
|
|
|
|
public void logOutByChannel()
|
|
{
|
|
UnityBridgeFunc.UnityLoginOut();
|
|
_accountInfo = null;
|
|
}
|
|
|
|
public async Task<LoginInfo> Login(EAccoutType accoutType)
|
|
{
|
|
#if UNITY_EDITOR
|
|
await Task.Yield();
|
|
var deviceId = UnityEngine.Device.SystemInfo.deviceUniqueIdentifier.Split('-')[0].Substring(0,8);
|
|
|
|
var userId = (int)ulong.Parse(deviceId, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
_accountInfo = new AccountInfo()
|
|
{
|
|
userId = userId,
|
|
};
|
|
|
|
return new LoginInfo()
|
|
{
|
|
isSuccess = true,
|
|
userId = userId
|
|
};
|
|
|
|
#elif UNITY_ANDROID || UNITY_IOS
|
|
|
|
var taskSource = new TaskCompletionSource<LoginInfo>();
|
|
callbacks.Add("LoginResult", (ITYSdkCallback callback) =>
|
|
{
|
|
taskSource.SetResult((LoginInfo)callback);
|
|
});
|
|
|
|
UnityBridgeFunc.UnityLogin(accoutType);
|
|
|
|
return await taskSource.Task;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
public void LoginResult(string json)
|
|
{
|
|
Debug.Log("[TYSdkFacade] Login callback:");
|
|
var result = new LoginInfo();
|
|
var data = JObject.Parse(json);
|
|
var code = (int)data["code"];
|
|
result.code = code;
|
|
|
|
if (code == 0)
|
|
{
|
|
var loginData = data["respObj"]["result"];
|
|
|
|
_accountInfo = new AccountInfo()
|
|
{
|
|
userId = (int)loginData["userId"],
|
|
token = (string)loginData["token"],
|
|
jwtToken = (string)loginData["jwttoken"],
|
|
};
|
|
|
|
|
|
result.isSuccess = true;
|
|
result.userId = _accountInfo.userId;
|
|
SetUserInfo();
|
|
}
|
|
|
|
if (callbacks.TryGetValue("LoginResult", out var action))
|
|
{
|
|
action.Invoke(result);
|
|
callbacks.Remove("LoginResult");
|
|
}
|
|
}
|
|
|
|
private void SetUserInfo()
|
|
{
|
|
if (_accountInfo == null) return;
|
|
var strUserId = _accountInfo.strUserId;
|
|
|
|
UnityBridgeFunc.SetGaUserInfo(strUserId);
|
|
UnityBridgeFunc.UnitySetAdBoxUserInfo(strUserId);
|
|
UnityBridgeFunc.UnityInitADSConfig();
|
|
}
|
|
|
|
/*================================================
|
|
|
|
_____ _ _____ _
|
|
| ____|_ _____ _ __ | |_ |_ _| __ __ _ ___| | __
|
|
| _| \ \ / / _ \ '_ \| __| | || '__/ _` |/ __| |/ /
|
|
| |___ \ V / __/ | | | |_ | || | | (_| | (__| <
|
|
|_____| \_/ \___|_| |_|\__| |_||_| \__,_|\___|_|\_\
|
|
|
|
=================================================*/
|
|
|
|
public void EventTrack(int type, string name, string content)
|
|
{
|
|
#if UNITY_IOS || UNITY_ANDROID
|
|
UnityBridgeFunc.GAReportParams(type, name, content);
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
/*================================================
|
|
|
|
____ _
|
|
| _ \ __ _ _ _ _ __ ___ ___ _ __ | |_
|
|
| |_) / _` | | | | '_ ` _ \ / _ \ '_ \| __|
|
|
| __/ (_| | |_| | | | | | | __/ | | | |_
|
|
|_| \__,_|\__, |_| |_| |_|\___|_| |_|\__|
|
|
|___/
|
|
=================================================*/
|
|
|
|
//TODO
|
|
public async Task<PaymentInfo> Pay(string prodId, string prodPrice, string prodName, int count, string orderId, string extraInfo)
|
|
{
|
|
if (!IsLoggedIn) return new PaymentInfo() { code = "-1", msg = "未登录" };
|
|
|
|
#if UNITY_EDITOR
|
|
await Task.Yield();
|
|
var result = new PaymentInfo()
|
|
{
|
|
code = "0",
|
|
msg = "success",
|
|
count = count,
|
|
orderId = orderId,
|
|
productId = prodId,
|
|
price = prodPrice
|
|
};
|
|
|
|
return result;
|
|
#elif UNITY_ANDROID
|
|
var taskSource = new TaskCompletionSource<PaymentInfo>();
|
|
callbacks.Add("PayResult", (ITYSdkCallback callback) =>
|
|
{
|
|
taskSource.SetResult((PaymentInfo)callback);
|
|
});
|
|
UnityBridgeFunc.UnityKnow(_accountInfo.strUserId, prodId, prodPrice, prodName, count.ToString(), orderId, extraInfo);
|
|
var result = await taskSource.Task;
|
|
result.count = count;
|
|
result.orderId = orderId;
|
|
result.productId = prodId;
|
|
result.price = prodPrice;
|
|
return result;
|
|
#elif UNITY_IOS
|
|
var taskSource = new TaskCompletionSource<PaymentInfo>();
|
|
callbacks.Add("PayResult", (ITYSdkCallback callback) =>
|
|
{
|
|
taskSource.SetResult((PaymentInfo)callback);
|
|
});
|
|
UnityBridgeFunc.UnityKnow(_accountInfo.strUserId, prodId, prodPrice, prodName, count.ToString(), orderId, extraInfo);
|
|
var result = await taskSource.Task;
|
|
result.count = count;
|
|
result.orderId = orderId;
|
|
result.productId = prodId;
|
|
result.price = prodPrice;
|
|
return result;
|
|
#endif
|
|
}
|
|
|
|
//支付的回调
|
|
public void PayResult(string json)
|
|
{
|
|
Debug.Log("[TYSdkFacade] pay callback:");
|
|
var jresult = JObject.Parse(json);
|
|
var result = new PaymentInfo();
|
|
result.code = jresult["code"].ToString();
|
|
result.msg = jresult["errStr"].ToString();
|
|
|
|
if (callbacks.TryGetValue("PayResult", out var action))
|
|
{
|
|
action.Invoke(result);
|
|
callbacks.Remove("PayResult");
|
|
}
|
|
}
|
|
|
|
/*================================================
|
|
|
|
_ _____ _____
|
|
/ \|_ _|_ _|
|
|
/ _ \ | | | |
|
|
/ ___ \| | | |
|
|
/_/ \_\_| |_|
|
|
|
|
=================================================*/
|
|
|
|
public bool IsAttAccepted()
|
|
{
|
|
return UnityBridgeFunc.GetATT() == 1;
|
|
}
|
|
|
|
public async Task<ATTInfo> RequestATT()
|
|
{
|
|
#if UNITY_EDITOR || UNITY_ANDROID
|
|
await Task.Yield();
|
|
|
|
return new ATTInfo(){isAccepted = true};
|
|
#elif UNITY_IOS
|
|
var taskSource = new TaskCompletionSource<ATTInfo>();
|
|
callbacks.Add("RequestATT", (ITYSdkCallback callback) =>
|
|
{
|
|
taskSource.SetResult((ATTInfo)callback);
|
|
});
|
|
UnityBridgeFunc.RequestATT();
|
|
return await taskSource.Task;
|
|
#endif
|
|
}
|
|
|
|
public void RequestATTResult(string r)
|
|
{
|
|
if (callbacks.TryGetValue("RequestATT", out var action))
|
|
{
|
|
action.Invoke(new ATTInfo(){isAccepted = r == "1"});
|
|
callbacks.Remove("RequestATT");
|
|
}
|
|
}
|
|
|
|
/*================================================
|
|
|
|
_ ____
|
|
/ \ | _ \
|
|
/ _ \ | | | |
|
|
/ ___ \| |_| |
|
|
/_/ \_\____/
|
|
|
|
=================================================*/
|
|
|
|
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}");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|