AntiAddictionNewJob.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Threading.Tasks;
  3. using TapTap.AntiAddiction.Internal;
  4. using TapTap.AntiAddiction.Model;
  5. using CheckPayResult = Plugins.AntiAddictionUIKit.CheckPayResult;
  6. namespace TapTap.AntiAddiction
  7. {
  8. public sealed class AntiAddictionNewJob : IAntiAddictionJob
  9. {
  10. private Action<int, string> _externalCallback;
  11. public Action<int, string> ExternalCallback
  12. {
  13. get => _externalCallback;
  14. }
  15. public int AgeRange
  16. {
  17. get
  18. {
  19. if (!Verification.IsVerified) return -1;
  20. return Verification.AgeLimit;
  21. }
  22. }
  23. /// <summary>
  24. /// 剩余时间(单位:秒)
  25. /// </summary>
  26. public int RemainingTime
  27. {
  28. get
  29. {
  30. if (TapTapAntiAddictionManager.CurrentRemainSeconds == null) return 0;
  31. return TapTapAntiAddictionManager.CurrentRemainSeconds.Value;
  32. }
  33. }
  34. public int RemainingTimeInMinutes
  35. {
  36. get
  37. {
  38. int seconds = RemainingTime;
  39. if (seconds <= 0)
  40. return 0;
  41. return UnityEngine.Mathf.CeilToInt(seconds / 60.0f);
  42. }
  43. }
  44. public string CurrentToken
  45. {
  46. get
  47. {
  48. if (!Verification.IsVerified) return "";
  49. return Verification.GetCurrentToken();
  50. }
  51. }
  52. public void Init(AntiAddictionConfig config, Action<int, string> callback)
  53. {
  54. TapTapAntiAddictionManager.Init(config);
  55. _externalCallback = callback;
  56. }
  57. /// <summary>
  58. /// Async Method
  59. /// </summary>
  60. /// <param name="userId"></param>
  61. public async void Startup(string userId)
  62. {
  63. var code = await TapTapAntiAddictionManager.StartUp(userId);
  64. // -1 代表因为错误而退出
  65. if (code != StartUpResult.INTERNAL_ERROR)
  66. ExternalCallback?.Invoke(code, null);
  67. }
  68. public void Exit()
  69. {
  70. TapTapAntiAddictionManager.Logout();
  71. _externalCallback?.Invoke(StartUpResult.EXITED, null);
  72. }
  73. public void EnterGame()
  74. {
  75. TapTapAntiAddictionManager.EnterGame();
  76. }
  77. public void LeaveGame()
  78. {
  79. TapTapAntiAddictionManager.LeaveGame();
  80. }
  81. public async void CheckPayLimit(long amount, Action<CheckPayResult> handleCheckPayLimit, Action<string> handleCheckPayLimitException)
  82. {
  83. try
  84. {
  85. var payResult = await TapTapAntiAddictionManager.CheckPayLimit(amount);
  86. handleCheckPayLimit?.Invoke(new CheckPayResult()
  87. {
  88. // status 为 1 时可以支付
  89. status = payResult.Status ? 1 : 0,
  90. title = payResult.Title,
  91. description = payResult.Content
  92. });
  93. }
  94. catch (Exception e)
  95. {
  96. handleCheckPayLimitException?.Invoke(e.Message);
  97. }
  98. }
  99. public async void SubmitPayResult(long amount, Action handleSubmitPayResult, Action<string> handleSubmitPayResultException)
  100. {
  101. try
  102. {
  103. await TapTapAntiAddictionManager.SubmitPayResult(amount);
  104. handleSubmitPayResult?.Invoke();
  105. }
  106. catch (Exception e)
  107. {
  108. handleSubmitPayResultException?.Invoke(e.Message);
  109. }
  110. }
  111. public bool isStandalone()
  112. {
  113. var task = Task.Run(async () => await TapTapAntiAddictionManager.IsStandaloneEnabled());
  114. return task.Result;
  115. }
  116. public void SetRegion(Region region)
  117. {
  118. TapTapAntiAddictionManager.SetRegion(region);
  119. }
  120. }
  121. }