BaseAntiAddictionWorker.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Threading.Tasks;
  5. using TapSDK.UI;
  6. using TapTap.AntiAddiction.Internal;
  7. using TapTap.AntiAddiction.Model;
  8. using TapTap.Common;
  9. using UnityEngine;
  10. using Network = TapTap.AntiAddiction.Internal.Network;
  11. namespace TapTap.AntiAddiction
  12. {
  13. public abstract class BaseAntiAddictionWorker
  14. {
  15. protected AntiAddictionConfig config => TapTapAntiAddictionManager.AntiAddictionConfig;
  16. #region Abstract
  17. public abstract Region Region { get; }
  18. /// <summary>
  19. /// 传送身份验证等敏感信息时的key
  20. /// </summary>
  21. /// <param name="isRnd"></param>
  22. /// <returns></returns>
  23. internal abstract string RsaPublicKey { get; }
  24. /// <summary>
  25. /// 完成身份验证窗口
  26. /// </summary>
  27. /// <returns></returns>
  28. protected abstract Task<int> CompleteVerificationWindowAsync();
  29. /// <summary>
  30. /// 检查未成年人可玩性
  31. /// </summary>
  32. /// <returns></returns>
  33. protected abstract PlayableResult CheckOfflineMinorPlayable();
  34. /// <summary>
  35. /// 是否激活单机模式
  36. /// </summary>
  37. /// <returns></returns>
  38. internal abstract Task<bool> IsStandaloneEnabled();
  39. #endregion
  40. #region Virutal
  41. /// <summary>
  42. /// 检查可玩性后,已知成年人时的处理
  43. /// </summary>
  44. /// <param name="playable"></param>
  45. protected virtual void OnCheckedPlayableWithAdult(PlayableResult playable)
  46. {
  47. TryStartPoll();
  48. }
  49. /// <summary>
  50. /// 检查可玩性后,已知未成年人时的处理
  51. /// </summary>
  52. /// <param name="playable"></param>
  53. /// <returns></returns>
  54. protected virtual async Task<int> OnCheckedPlayableWithMinorAsync(PlayableResult playable)
  55. {
  56. var tcs = new TaskCompletionSource<int>();
  57. Action onOk;
  58. if (playable.CanPlay)
  59. {
  60. onOk = () =>
  61. {
  62. tcs.TrySetResult(StartUpResult.LOGIN_SUCCESS);
  63. TryStartPoll();
  64. };
  65. }
  66. else
  67. {
  68. tcs.TrySetResult(StartUpResult.PERIOD_RESTRICT);
  69. onOk = () =>
  70. {
  71. Application.Quit();
  72. };
  73. }
  74. Action onSwitchAccount = null;
  75. if (config.showSwitchAccount)
  76. {
  77. onSwitchAccount = () =>
  78. {
  79. Logout();
  80. AntiAddictionUIKit.ExternalCallback?.Invoke(StartUpResult.SWITCH_ACCOUNT, null);
  81. };
  82. }
  83. TapTapAntiAddictionUIKit.OpenHealthReminderPanel(playable, onOk, onSwitchAccount);
  84. return await tcs.Task;
  85. }
  86. /// <summary>
  87. /// 检查可玩性,如果出现异常就会用本地的计算方式
  88. /// </summary>
  89. /// <returns></returns>
  90. protected virtual async Task<PlayableResult> CheckPlayableAsyncWithFallback()
  91. {
  92. try
  93. {
  94. var playableResult = await CheckPlayableAsync();
  95. return playableResult;
  96. }
  97. catch (AntiAddictionException e)
  98. {
  99. if (e.Code == 401)
  100. {
  101. throw;
  102. }
  103. return CheckOfflinePlayable();
  104. }
  105. catch (Exception e)
  106. {
  107. TapLogger.Error(e);
  108. // 单机判断是否可玩
  109. return CheckOfflinePlayable();
  110. }
  111. }
  112. /// <summary>
  113. /// 检查可玩性
  114. /// </summary>
  115. /// <param name="shouldThrowException">当内部发生错误的时候,是否抛出异常.默认不抛出异常,就会按照本地规则计算playable</param>
  116. /// <returns></returns>
  117. protected virtual async Task<PlayableResult> CheckPlayableAsync()
  118. {
  119. try
  120. {
  121. if (!Config.NeedUploadUserAction) return CheckOfflinePlayable();
  122. var playableResult = await Network.CheckPlayable();
  123. return playableResult;
  124. }
  125. catch (Exception e)
  126. {
  127. TapLogger.Error(e);
  128. throw;
  129. }
  130. }
  131. /// <summary>
  132. /// 本地判断可玩性
  133. /// </summary>
  134. /// <returns></returns>
  135. protected virtual PlayableResult CheckOfflinePlayable()
  136. {
  137. // 成年人
  138. if (Verification.IsAdult)
  139. {
  140. // 可玩
  141. return new PlayableResult
  142. {
  143. RestrictType = PlayableResult.ADULT,
  144. CanPlay = true,
  145. };
  146. }
  147. return CheckOfflineMinorPlayable();
  148. }
  149. /// <summary>
  150. /// 是否需要开启轮询检查可玩性
  151. /// </summary>
  152. /// <returns></returns>
  153. protected virtual bool IsNeedStartPoll()
  154. {
  155. return !Verification.IsAdult || Config.NeedUploadUserAction;
  156. }
  157. /// <summary>
  158. /// 轮询时,检查可玩性判断
  159. /// </summary>
  160. /// <returns></returns>
  161. internal virtual async Task<PlayableResult> CheckPlayableOnPollingAsync()
  162. {
  163. try
  164. {
  165. var playable = await CheckPlayableAsyncWithFallback();
  166. if (!playable.CanPlay)
  167. {
  168. OnUnplayablePostPoll(playable);
  169. }
  170. return playable;
  171. }
  172. catch (AntiAddictionException e)
  173. {
  174. if (e.Code == 401) {
  175. // TapTapAntiAddictionManager.Logout();
  176. }
  177. return CheckOfflinePlayable();
  178. }
  179. }
  180. /// <summary>
  181. /// 轮询时,发现不可玩处理
  182. /// </summary>
  183. /// <param name="playable"></param>
  184. protected virtual void OnUnplayablePostPoll(PlayableResult playable)
  185. {
  186. Action onExitGame = Application.Quit;
  187. Action onSwitch = null;
  188. if (config.showSwitchAccount)
  189. {
  190. onSwitch = () => {
  191. Logout();
  192. AntiAddictionUIKit.ExternalCallback?.Invoke(StartUpResult.SWITCH_ACCOUNT, null);
  193. };
  194. }
  195. TapTapAntiAddictionUIKit.OpenHealthReminderPanel(playable, onExitGame, onSwitch);
  196. AntiAddictionUIKit.ExternalCallback?.Invoke(StartUpResult.PERIOD_RESTRICT, null);
  197. }
  198. /// <summary>
  199. /// 通过检查,发现不可支付时的处理
  200. /// </summary>
  201. /// <param name="payable"></param>
  202. protected virtual void OnCheckedUnpayable(PayableResult payable)
  203. {
  204. TapTapAntiAddictionUIKit.OpenHealthPaymentPanel(payable);
  205. }
  206. /// <summary>
  207. /// 获得配置后的处理
  208. /// </summary>
  209. internal virtual void OnConfigFetched()
  210. {
  211. Config.OnFetched();
  212. }
  213. /// <summary>
  214. /// 登出处理
  215. /// </summary>
  216. internal virtual void Logout()
  217. {
  218. Verification.Logout();
  219. AntiAddictionPoll.Logout();
  220. TapTapAntiAddictionManager.UserId = null;
  221. }
  222. #endregion
  223. #region Internal
  224. /// <summary>
  225. /// 获得实名信息
  226. /// </summary>
  227. internal async Task FetchVerificationAsync()
  228. {
  229. var userId = TapTapAntiAddictionManager.UserId;
  230. // 拉取服务端实名信息
  231. do
  232. {
  233. try
  234. {
  235. await Verification.Fetch(userId);
  236. UIManager.Instance.CloseLoading();
  237. break;
  238. }
  239. catch (HttpRequestException e)
  240. {
  241. TapLogger.Error(e);
  242. UIManager.Instance.CloseLoading();
  243. // 网络异常,则提示重新查询
  244. await TapTapAntiAddictionUIKit.ShowRetryDialog(TapTapAntiAddictionManager.LocalizationItems.Current.NetError);
  245. UIManager.Instance.OpenLoading();
  246. }
  247. catch (Exception e)
  248. {
  249. TapLogger.Error(e);
  250. UIManager.Instance.CloseLoading();
  251. var aae = e as AntiAddictionException;
  252. if (aae != null && aae.code == (int)HttpStatusCode.Unauthorized)
  253. {
  254. await TapTapAntiAddictionUIKit.ShowRetryDialog(TapTapAntiAddictionManager.LocalizationItems.Current.NoVerification);
  255. }
  256. else if (aae != null && aae.code >= (int)HttpStatusCode.InternalServerError)
  257. {
  258. await TapTapAntiAddictionUIKit.ShowRetryDialog(TapTapAntiAddictionManager.LocalizationItems.Current.NoVerification);
  259. }
  260. else if (e.Message.Contains("Interval server error."))
  261. {
  262. await TapTapAntiAddictionUIKit.ShowRetryDialog(TapTapAntiAddictionManager.LocalizationItems.Current.NoVerification);
  263. }
  264. else
  265. {
  266. await TapTapAntiAddictionUIKit.ShowRetryDialog(TapTapAntiAddictionManager.LocalizationItems.Current.NetError);
  267. }
  268. UIManager.Instance.OpenLoading();
  269. }
  270. }
  271. while (true);
  272. }
  273. /// <summary>
  274. /// 获得实名信息后的处理
  275. /// </summary>
  276. /// <returns></returns>
  277. internal async Task<int> OnVerificationFetched()
  278. {
  279. // 1. 如果没验证过,需要弹出验证窗口,并完成验证----------------------
  280. if (!Verification.IsVerified)
  281. {
  282. var result = await CompleteVerificationWindowAsync();
  283. if (result != 0)
  284. return result;
  285. }
  286. // 2. 检查可玩性
  287. return await ProcessPlayableAsync();
  288. }
  289. /// <summary>
  290. /// 检查可玩性
  291. /// </summary>
  292. /// <returns></returns>
  293. private async Task<int> ProcessPlayableAsync()
  294. {
  295. int tryCount = 0;
  296. do
  297. {
  298. try
  299. {
  300. tryCount++;
  301. PlayableResult playable = await CheckPlayableAsyncWithFallback();
  302. TapTapAntiAddictionManager.CurrentPlayableResult = playable;
  303. // 2.1 成年人-后处理
  304. if (playable.IsAdult)
  305. {
  306. OnCheckedPlayableWithAdult(playable);
  307. return StartUpResult.LOGIN_SUCCESS;
  308. }
  309. // 2.2 未成年人-后处理
  310. else
  311. {
  312. return await OnCheckedPlayableWithMinorAsync(playable);
  313. }
  314. }
  315. catch (Exception e)
  316. {
  317. var aae = e as AntiAddictionException;
  318. if (aae.Code == 401)
  319. {
  320. return StartUpResult.INTERNAL_ERROR;
  321. }
  322. if (Verification.IsVerified)
  323. {
  324. if (!Verification.IsAdult)
  325. await TapTapAntiAddictionUIKit.ShowRetryDialog(TapTapAntiAddictionManager.LocalizationItems.Current.NetError);
  326. else if (tryCount >= 3)
  327. {
  328. var playableResult = new PlayableResult
  329. {
  330. RestrictType = PlayableResult.ADULT,
  331. CanPlay = true,
  332. };
  333. TapTapAntiAddictionManager.CurrentPlayableResult = playableResult;
  334. OnCheckedPlayableWithAdult(playableResult);
  335. return StartUpResult.LOGIN_SUCCESS;
  336. }
  337. }
  338. }
  339. }while (true);
  340. }
  341. /// <summary>
  342. /// 尝试开启轮询检查
  343. /// </summary>
  344. protected void TryStartPoll()
  345. {
  346. if (IsNeedStartPoll())
  347. {
  348. AntiAddictionPoll.StartUp();
  349. }
  350. }
  351. /// <summary>
  352. /// 检查是否可以支付
  353. /// </summary>
  354. /// <param name="amount"></param>
  355. /// <returns></returns>
  356. internal async virtual Task<PayableResult> CheckPayableAsync(long amount)
  357. {
  358. PayableResult payable = await Network.CheckPayable(amount);
  359. if (!payable.Status)
  360. {
  361. OnCheckedUnpayable(payable);
  362. }
  363. return payable;
  364. }
  365. /// <summary>
  366. /// 提交充值结果
  367. /// </summary>
  368. /// <param name="amount"></param>
  369. /// <returns></returns>
  370. internal virtual Task SubmitPayResult(long amount)
  371. {
  372. return Network.SubmitPayment(amount);
  373. }
  374. /// <summary>
  375. /// 获取配置
  376. /// </summary>
  377. internal async Task FetchConfigAsync()
  378. {
  379. await Config.Fetch();
  380. }
  381. #endregion
  382. }
  383. }