Verification.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using TapTap.AntiAddiction.Model;
  6. using TapTap.Common;
  7. namespace TapTap.AntiAddiction.Internal
  8. {
  9. internal static class Verification
  10. {
  11. internal const int AGE_LIMIT_UNKNOWN = -1;
  12. internal const int AGE_LIMIT_CHILD = 0;
  13. internal const int AGE_LIMIT_TEEN = 8;
  14. internal const int AGE_LIMIT_YOUNG = 16;
  15. internal const int AGE_LIMIT_ADULT = 18;
  16. internal static readonly string HOST = "https://tds-real-name.tapapis.cn";
  17. static readonly string VERIFICATION_FILENAME = "verification";
  18. static readonly bool DEFAULT_VERIFIED = false;
  19. static readonly bool DEFAULT_VERIFING = false;
  20. static readonly bool DEFAULT_ADULT = false;
  21. static LocalVerification current;
  22. static Persistence persistence;
  23. internal static async Task Fetch(string userId)
  24. {
  25. string filename = Tool.EncryptString(userId);
  26. persistence = new Persistence(Path.Combine(Application.persistentDataPath,
  27. Config.ANTI_ADDICTION_DIR,
  28. VERIFICATION_FILENAME,
  29. filename));
  30. try
  31. {
  32. VerificationResult result = await Network.FetchVerification();
  33. await Save(userId, TapTapAntiAddictionManager.AntiAddictionConfig.region, result);
  34. }
  35. catch (Exception e)
  36. {
  37. TapLogger.Error(e);
  38. LocalVerification localVerification = await persistence.Load<LocalVerification>();
  39. if (localVerification == null)
  40. {
  41. // 当本地不包含认证信息时,抛出异常
  42. throw;
  43. }
  44. if ((int)localVerification.Region == 0)
  45. {
  46. localVerification.Region = Region.China;
  47. }
  48. current = localVerification;
  49. }
  50. }
  51. internal static async Task<VerificationResult> VerifyKycAsync(string userId, string verificationInfo)
  52. {
  53. var tcs = new TaskCompletionSource<VerificationResult>();
  54. var filename = Tool.EncryptString(userId);
  55. persistence = new Persistence(Path.Combine(Application.persistentDataPath,
  56. Config.ANTI_ADDICTION_DIR,
  57. VERIFICATION_FILENAME,
  58. filename));
  59. try
  60. {
  61. var rsaKey = TapTapAntiAddictionManager.GetRsaPublicKey();
  62. VerificationResult result = await Network.VerifyKycAsync(verificationInfo, rsaKey);
  63. tcs.SetResult(result);
  64. await Save(verificationInfo, TapTapAntiAddictionManager.AntiAddictionConfig.region, result);
  65. }
  66. catch (Exception e)
  67. {
  68. tcs.SetException(e);
  69. TapLogger.Error(e);
  70. LocalVerification localVerification = await persistence.Load<LocalVerification>();
  71. if (localVerification == null)
  72. {
  73. // 当本地不包含认证信息时,抛出异常
  74. throw;
  75. }
  76. if ((int)localVerification.Region == 0)
  77. {
  78. localVerification.Region = Region.China;
  79. }
  80. current = localVerification;
  81. }
  82. return await tcs.Task;
  83. }
  84. internal static async Task Save(string userId, Region region, VerificationResult verification)
  85. {
  86. current = new LocalVerification(verification)
  87. {
  88. UserId = userId,
  89. Region = region,
  90. };
  91. await persistence.Save(current);
  92. }
  93. internal static void Logout()
  94. {
  95. if (IsVerified && !IsAdult)
  96. {
  97. #pragma warning disable CS4014
  98. Network.CheckPlayable();
  99. #pragma warning restore CS4014
  100. }
  101. persistence = null;
  102. current = null;
  103. }
  104. internal static string GetCurrentToken()
  105. {
  106. return current?.AntiAddictionToken;
  107. }
  108. /// <summary>
  109. /// 是否已认证
  110. /// </summary>
  111. internal static bool IsVerified => current?.IsVerified ?? DEFAULT_VERIFIED;
  112. /// <summary>
  113. /// 是否在认证中
  114. /// </summary>
  115. internal static bool IsVerifing => current?.IsVerifing ?? DEFAULT_VERIFING;
  116. /// <summary>
  117. /// 是否是成年人
  118. /// </summary>
  119. internal static bool IsAdult => current?.IsAdult ?? DEFAULT_ADULT;
  120. /// <summary>
  121. /// 年龄级别
  122. /// </summary>
  123. internal static int AgeLimit => current?.AgeLimit ?? AGE_LIMIT_CHILD;
  124. }
  125. }