Network.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using TapTap.AntiAddiction.Internal.Http;
  6. using TapTap.AntiAddiction.Model;
  7. using UnityEngine;
  8. namespace TapTap.AntiAddiction.Internal
  9. {
  10. internal static class Network {
  11. static readonly string ChinaHost = "https://tds-tapsdk.cn.tapapis.com";
  12. static readonly string VietnamHost = "https://tds-account.intl.tapapis.com";
  13. private static AntiAddictionHttpClient
  14. HttpClient = new AntiAddictionHttpClient(ChinaHost);
  15. internal static void SetGameId(string gameId)
  16. {
  17. HttpClient.ChangeAddtionalHeader("X-LC-Id", gameId);
  18. }
  19. internal static void ChangeRegion(Region region)
  20. {
  21. HttpClient.ChangeAddtionalHeader("Accept-Language",
  22. (region == Region.Vietnam ? "vi-VN" : "zh-CN"));
  23. }
  24. internal static void ChangeHost()
  25. {
  26. string host;
  27. switch (TapTapAntiAddictionManager.AntiAddictionConfig.region)
  28. {
  29. case Region.Vietnam:
  30. host = VietnamHost;
  31. break;
  32. case Region.China:
  33. default:
  34. host = ChinaHost;
  35. break;
  36. }
  37. if (HttpClient != null)
  38. {
  39. Type httpClientType = typeof(AntiAddictionHttpClient);
  40. var hostFieldInfo = httpClientType.GetField("serverUrl", BindingFlags.NonPublic | BindingFlags.Instance);
  41. hostFieldInfo?.SetValue(HttpClient, host);
  42. }
  43. }
  44. /// <summary>
  45. /// 拉取配置并缓存在内存
  46. /// 没有持久化的原因是无法判断 SDK 自带与本地持久化版本的高低
  47. /// </summary>
  48. /// <returns></returns>
  49. internal static async Task<AntiAddictionConfigResult> FetchConfig()
  50. {
  51. string path = $"anti-addiction/v1/{TapTapAntiAddictionManager.AntiAddictionConfig.regionStr}/clients/{TapTapAntiAddictionManager.GameId}/configuration";
  52. AntiAddictionConfigResponse response = await HttpClient.Get<AntiAddictionConfigResponse>(path);
  53. return response.Result;
  54. }
  55. /// <summary>
  56. /// 拉取实名认证数据
  57. /// </summary>
  58. /// <returns></returns>
  59. internal static async Task<VerificationResult> FetchVerification()
  60. {
  61. string path = $"real-name/v1/{TapTapAntiAddictionManager.AntiAddictionConfig.regionStr}/clients/{TapTapAntiAddictionManager.GameId}/users/{TapTapAntiAddictionManager.UserId}";
  62. ServerVerificationResponse response = await HttpClient.Get<ServerVerificationResponse>(path);
  63. return response.Result;
  64. }
  65. /// <summary>
  66. /// 获取服务器时间
  67. /// </summary>
  68. /// <returns></returns>
  69. internal static async Task<long> FetchServerTime()
  70. {
  71. string path = $"anti-addiction/v1/server-time";
  72. ServerTimeResponse response = await HttpClient.Get<ServerTimeResponse>(path);
  73. return response.Result.Timestamp;
  74. }
  75. /// <summary>
  76. /// 检测身份信息是否通过
  77. /// </summary>
  78. /// <param name="json">包括了身份信息,每个国家的身份信息会不同</param>
  79. /// <param name="key"></param>
  80. /// <returns></returns>
  81. internal static async Task<VerificationResult> VerifyKycAsync(string json, string key)
  82. {
  83. var tcs = new TaskCompletionSource<VerificationResult>();
  84. string encryptStr = Tool.RsaEncrypt(json, key);
  85. if (string.IsNullOrEmpty(encryptStr))
  86. {
  87. tcs.TrySetException(new Exception("RSA Encrypt Failed"));
  88. }
  89. else
  90. {
  91. string path = $"real-name/v1/{TapTapAntiAddictionManager.AntiAddictionConfig.regionStr}/clients/{TapTapAntiAddictionManager.GameId}/users/{TapTapAntiAddictionManager.UserId}/manual";
  92. Dictionary<string, object> data = new Dictionary<string, object>
  93. {
  94. ["data"] = encryptStr
  95. };
  96. ServerVerificationResponse response = await HttpClient.Post<ServerVerificationResponse>(path, data: data);
  97. Debug.LogFormat($"Status: {response.Result.Status} AgeLimit: {response.Result.AgeLimit}");
  98. tcs.TrySetResult(response.Result);
  99. }
  100. return await tcs.Task;
  101. }
  102. /// <summary>
  103. /// 检测是否可玩
  104. /// </summary>
  105. /// <returns></returns>
  106. internal static async Task<PlayableResult> CheckPlayable()
  107. {
  108. string path = $"anti-addiction/v1/{TapTapAntiAddictionManager.AntiAddictionConfig.regionStr}/clients/{TapTapAntiAddictionManager.GameId}/users/{TapTapAntiAddictionManager.UserId}/playable";
  109. Dictionary<string, object> headers = GetAuthHeaders();
  110. PlayableResponse response = await HttpClient.Post<PlayableResponse>(path, headers: headers);
  111. #if UNITY_EDITOR
  112. Debug.LogFormat($"检查是否可玩结果: canPlay: {response.Result.CanPlay} remainTime: {response.Result.RemainTime} CostTime: {response.Result.CostTime} IsAdult: {response.Result.IsAdult} Content: {response.Result.Content}");
  113. #endif
  114. return response.Result;
  115. }
  116. /// <summary>
  117. /// 检测是否可充值
  118. /// </summary>
  119. /// <param name="amount"></param>
  120. /// <returns></returns>
  121. internal static async Task<PayableResult> CheckPayable(long amount)
  122. {
  123. string path = $"anti-addiction/v1/{TapTapAntiAddictionManager.AntiAddictionConfig.regionStr}/clients/{TapTapAntiAddictionManager.GameId}/users/{TapTapAntiAddictionManager.UserId}/payable";
  124. Dictionary<string, object> headers = GetAuthHeaders();
  125. Dictionary<string, object> data = new Dictionary<string, object>
  126. {
  127. { "amount", amount }
  128. };
  129. PayableResponse response = await HttpClient.Post<PayableResponse>(path, headers: headers, data: data);
  130. return response.Result;
  131. }
  132. /// <summary>
  133. /// 上传充值操作
  134. /// </summary>
  135. /// <param name="amount"></param>
  136. /// <returns></returns>
  137. internal static async Task SubmitPayment(long amount)
  138. {
  139. string path = $"anti-addiction/v1/{TapTapAntiAddictionManager.AntiAddictionConfig.regionStr}/clients/{TapTapAntiAddictionManager.GameId}/users/{TapTapAntiAddictionManager.UserId}/payments";
  140. Dictionary<string, object> headers = GetAuthHeaders();
  141. Dictionary<string, object> data = new Dictionary<string, object>
  142. {
  143. { "amount", amount }
  144. };
  145. await HttpClient.Post<SubmitPaymentResponse>(path, headers:headers, data: data);
  146. }
  147. internal static Dictionary<string, object> GetAuthHeaders()
  148. {
  149. string token = Verification.GetCurrentToken();
  150. if (string.IsNullOrEmpty(token))
  151. {
  152. return null;
  153. }
  154. return new Dictionary<string, object>
  155. {
  156. { "Authorization", token }
  157. };
  158. }
  159. }
  160. }