ChinaAntiAddictionWorker.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Net.WebSockets;
  5. using System.Threading.Tasks;
  6. using TapSDK.UI;
  7. using TapTap.AntiAddiction.Internal;
  8. using TapTap.AntiAddiction.Model;
  9. using TapTap.Common;
  10. using UnityEngine;
  11. namespace TapTap.AntiAddiction
  12. {
  13. public sealed class ChinaAntiAddictionWorker : BaseAntiAddictionWorker
  14. {
  15. #region Abstract Override
  16. public override Region Region => Region.China;
  17. private readonly string rsaPublicKey =
  18. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1pM6yfulomBTXWKiQT5gK9fY4hq11Kv8D+ewum25oPGReuEn6dez7ogA8bEyQlnYYUoEp5cxYPBbIxJFy7q1qzQhTFphuFzoC1x7DieTvfZbh+b60psEottrCD8M0Pa3h44pzyIp5U5WRpxRcQ9iULolGLHZXJr9nW6bpOsyEIFG5tQ7qCBj8HSFoNBKZH+5Cwh3j5cjmyg55WdJTimg9ysbbwZHYmI+TFPuGo/ckHT6j4TQLCmmxI8Qf5pycn3/qJWFhjx/y8zaxgn2hgxbma8hyyGRCMnhM5tISYQv4zlQF+5RashvKa2zv+FHA5DALzIsGXONeTxk6TSBalX5gQIDAQAB";
  19. internal override string RsaPublicKey => rsaPublicKey;
  20. private TaptapAntiAddictionIDInputController idInputPanel;
  21. protected override async Task<int> CompleteVerificationWindowAsync()
  22. {
  23. var userId = TapTapAntiAddictionManager.UserId;
  24. do
  25. {
  26. try
  27. {
  28. var result = await OpenVerificationPanelCn();
  29. await Verification.Save(userId, Region.China, result);
  30. var tcs = new TaskCompletionSource<int>();
  31. // 如果在实名认证界面得到是未认证的数据,则弹出认证中的弹框
  32. if (Verification.IsVerifing)
  33. {
  34. var tip = Config.GetInputIdentifyBlockingTip();
  35. Action onOk = () =>
  36. {
  37. Debug.Log("因为认证中,退出游戏!");
  38. Application.Quit();
  39. };
  40. TapTapAntiAddictionUIKit.OpenHealthPaymentPanel(tip.Title, tip.Content, tip.PositiveButtonText, onOk);
  41. }
  42. else
  43. {
  44. if (Verification.IsVerified)
  45. TapTapAntiAddictionUIKit.OpenVerifyFinishPanel(()=> tcs.TrySetResult(0));
  46. else
  47. {
  48. idInputPanel.ShowErrorTip("认证未通过,请提交真实信息");
  49. continue;
  50. }
  51. }
  52. return await tcs.Task;
  53. }
  54. catch (TaskCanceledException)
  55. {
  56. TapLogger.Debug("Close verification panel.");
  57. // 返回关闭实名制窗口
  58. return StartUpResult.REAL_NAME_STOP;
  59. }
  60. catch (Exception e)
  61. {
  62. // 其他异常
  63. TapLogger.Error(e);
  64. if (e.Message.Contains("Verification Failed"))
  65. {
  66. await TapTapAntiAddictionUIKit.ShowRetryDialog(Config.GetInputIdentifyFormatErrorTip()?.Content);
  67. }
  68. else
  69. {
  70. if (e is HttpRequestException || e is WebException)
  71. {
  72. idInputPanel.ShowErrorTip(TapTapAntiAddictionManager.LocalizationItems.Current
  73. .NetError);
  74. continue;
  75. }
  76. if (e is AntiAddictionException aae)
  77. {
  78. if (aae.Code == 3)
  79. {
  80. idInputPanel.ShowErrorTip(aae.Message);
  81. continue;
  82. }
  83. if (aae.Code >= (int)HttpStatusCode.InternalServerError)
  84. {
  85. idInputPanel.ShowErrorTip("请求出错");
  86. continue;
  87. }
  88. }
  89. if (e.Message.Contains("Interval server error."))
  90. {
  91. idInputPanel.ShowErrorTip("请求出错");
  92. continue;
  93. }
  94. idInputPanel.ShowErrorTip(TapTapAntiAddictionManager.LocalizationItems.Current.NoVerification);
  95. await Task.Yield();
  96. }
  97. }
  98. }
  99. while (true);
  100. }
  101. internal override async Task<bool> IsStandaloneEnabled()
  102. {
  103. return await StandaloneChina.Enabled();
  104. }
  105. protected override PlayableResult CheckOfflineMinorPlayable()
  106. {
  107. // 未成年人
  108. if (IsGameTime())
  109. {
  110. // 可玩:节假日并且是游戏时间
  111. HealthReminderTip playableTip = Config.GetMinorPlayableHealthReminderTip();
  112. // 计算时间
  113. DateTimeOffset gameEndTime = Config.StrictStartTime;
  114. TimeSpan remain = gameEndTime - DateTimeOffset.Now;
  115. return new PlayableResult
  116. {
  117. RestrictType = PlayableResult.NIGHT_STRICT,
  118. CanPlay = true,
  119. Title = playableTip?.Title,
  120. Content = playableTip?.Content,
  121. RemainTime = Convert.ToInt32(remain.TotalSeconds)
  122. };
  123. }
  124. // 不可玩:未成年人不在可玩时间
  125. HealthReminderTip unplayableTip = Config.GetMinorUnplayableHealthReminderTip();
  126. return new PlayableResult
  127. {
  128. RestrictType = PlayableResult.NIGHT_STRICT,
  129. CanPlay = false,
  130. Title = unplayableTip?.Title,
  131. Content = unplayableTip?.Content,
  132. };
  133. }
  134. #endregion
  135. /// <summary>
  136. /// 是否在允许游戏时间段
  137. /// </summary>
  138. private bool IsGameTime()
  139. {
  140. DateTimeOffset strictStart = Config.StrictStartTime;
  141. DateTimeOffset strictEnd = Config.StrictEndTime;
  142. bool playable;
  143. DateTimeOffset now = DateTimeOffset.Now;
  144. if (strictEnd > strictStart)
  145. {
  146. // 可玩时间分段
  147. DateTimeOffset today = new DateTimeOffset(DateTime.Today);
  148. DateTimeOffset tomorrow = today.AddDays(1);
  149. playable = (now > today && now < strictStart) ||
  150. (now > strictEnd && now < tomorrow);
  151. }
  152. else
  153. {
  154. playable = now > strictEnd && now < strictStart;
  155. }
  156. return playable;
  157. }
  158. /// <summary>
  159. /// 打开中国实名制窗口
  160. /// </summary>
  161. /// <param name="name"></param>
  162. /// <param name="idNumber"></param>
  163. /// <returns></returns>
  164. private Task<VerificationResult> OpenVerificationPanelCn()
  165. {
  166. var tcs = new TaskCompletionSource<VerificationResult>();
  167. var path = AntiAddictionConst.GetPrefabPath(AntiAddictionConst.ID_NUMBER_INPUT_PANEL_NAME,
  168. false);
  169. idInputPanel =
  170. UIManager.Instance.OpenUI<TaptapAntiAddictionIDInputController>(path);
  171. if (idInputPanel != null)
  172. {
  173. idInputPanel.OnVerified = (verification) => tcs.TrySetResult(verification);
  174. idInputPanel.OnException = (e) =>
  175. {
  176. if (e is HttpRequestException || e is WebException)
  177. {
  178. tcs.TrySetException(e);
  179. }
  180. else
  181. {
  182. if (e is AntiAddictionException aae)
  183. {
  184. // code == 3 代表身份证号码非法
  185. if (aae.Code == (int)HttpStatusCode.Forbidden || aae.Code == 3 ||
  186. aae.Code >= (int)HttpStatusCode.InternalServerError)
  187. tcs.TrySetException(e);
  188. }
  189. else
  190. {
  191. if (e.Message.Contains("Interval server error."))
  192. tcs.TrySetException(e);
  193. else
  194. tcs.TrySetException(new Exception("China Verification Failed"));
  195. }
  196. }
  197. };
  198. idInputPanel.OnClosed = () => tcs.TrySetCanceled();
  199. }
  200. return tcs.Task;
  201. }
  202. }
  203. }