Config.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.IO;
  3. using System.Globalization;
  4. using System.Threading.Tasks;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using LC.Newtonsoft.Json;
  8. using TapTap.AntiAddiction.Model;
  9. using TapTap.Common;
  10. namespace TapTap.AntiAddiction.Internal
  11. {
  12. internal static class Config
  13. {
  14. static readonly string CONFIG_SDK_PATH = "Config/anti-addiction";
  15. internal static readonly string ANTI_ADDICTION_DIR = "tap-anti-addiction";
  16. static readonly string CONFIG_FILENAME = "config";
  17. static readonly string UPLOAD_USER_ACTION = "1";
  18. static readonly int USEABLE_HEALTH_REMINDER_INDEX = 1;
  19. static readonly int MINOR_UNPLAYABLE_HEALTH_REMINDER_INDEX = 5;
  20. static readonly int MINOR_PLAYABLE_HEALTH_REMINDER_INDEX = 7;
  21. static readonly int CHINA_IDENTIFY_INPUT_TIP_INDEX = 3;
  22. static readonly int CHINA_IDENTIFY_INPUT_BLOCK_TIP_INDEX = 4;
  23. static readonly int CHINA_IDENTIFY_INPUT_FORMAT_ERROR_TIP_INDEX = 5;
  24. private const string TIME_FROMAT = "HH:mm";
  25. internal static readonly Persistence persistence = new Persistence(Path.Combine(Application.persistentDataPath,
  26. ANTI_ADDICTION_DIR, CONFIG_FILENAME));
  27. static AntiAddictionConfigResult current;
  28. static Dictionary<int, HealthReminderTip> healthReminderTips;
  29. static Dictionary<int, PayLimitTip> payLimitTips;
  30. static Dictionary<int, AuthIdentifyTip> authIdentifyTips;
  31. internal static string healthPayTipButtonText;
  32. internal static AntiAddictionConfigResult Current
  33. {
  34. get => current;
  35. private set => current = value;
  36. }
  37. internal static async Task Fetch()
  38. {
  39. // 从服务端加载
  40. try
  41. {
  42. Current = await Network.FetchConfig();
  43. if (IsValid())
  44. {
  45. await persistence.Save(Current);
  46. return;
  47. }
  48. else
  49. {
  50. LoadFromBuiltin();
  51. }
  52. }
  53. catch (Exception e)
  54. {
  55. TapLogger.Error(e);
  56. }
  57. // 从设备加载
  58. try
  59. {
  60. Current = await persistence.Load<AntiAddictionConfigResult>();
  61. if (Current != null)
  62. return;
  63. }
  64. catch (Exception e)
  65. {
  66. TapLogger.Error(e);
  67. }
  68. LoadFromBuiltin();
  69. }
  70. private static void LoadFromBuiltin()
  71. {
  72. // 从 SDK 中加载
  73. try
  74. {
  75. TextAsset textAsset = Resources.Load(CONFIG_SDK_PATH) as TextAsset;
  76. if (textAsset != null)
  77. Current = JsonConvert.DeserializeObject<AntiAddictionConfigResult>(textAsset.text);
  78. }
  79. catch (Exception e)
  80. {
  81. TapLogger.Error(e);
  82. }
  83. }
  84. private static bool IsValid()
  85. {
  86. if (current == null) return false;
  87. if (current.UIConfig == null) return false;
  88. return true;
  89. }
  90. internal static void OnFetched()
  91. {
  92. if (current == null) return;
  93. // 梳理数据,方便查找
  94. healthPayTipButtonText = current.UIConfig?.PaymentInfo?.buttonConfirm;
  95. healthReminderTips = new Dictionary<int, HealthReminderTip>();
  96. if (current.UIConfig?.HealthReminderTips != null)
  97. {
  98. foreach (HealthReminderTips tips in current.UIConfig.HealthReminderTips)
  99. {
  100. if (tips.AccountType == USEABLE_HEALTH_REMINDER_INDEX)
  101. {
  102. foreach (HealthReminderTip tip in tips.Tips)
  103. {
  104. healthReminderTips.Add(tip.Type, tip);
  105. }
  106. }
  107. }
  108. }
  109. payLimitTips = new Dictionary<int, PayLimitTip>();
  110. if (current.UIConfig?.PayLimitTips != null)
  111. {
  112. foreach (PayLimitTip tip in current.UIConfig.PayLimitTips)
  113. {
  114. payLimitTips.Add(tip.AccountType, tip);
  115. }
  116. }
  117. authIdentifyTips = new Dictionary<int, AuthIdentifyTip>();
  118. if (current.UIConfig?.AuthIdentifyTips != null)
  119. {
  120. foreach (var tip in current.UIConfig.AuthIdentifyTips)
  121. {
  122. authIdentifyTips.Add(tip.AuthIdentifyType, tip);
  123. }
  124. }
  125. }
  126. internal static bool NeedUploadUserAction => Current?.UploadUserAction == UPLOAD_USER_ACTION;
  127. internal static HealthReminderTip GetMinorUnplayableHealthReminderTip()
  128. {
  129. if (healthReminderTips.TryGetValue(MINOR_UNPLAYABLE_HEALTH_REMINDER_INDEX, out HealthReminderTip tip))
  130. {
  131. return tip;
  132. }
  133. return null;
  134. }
  135. internal static HealthReminderTip GetMinorUnplayableHealthReminderTipVietnam()
  136. {
  137. var healthInfo = Current.UIConfig.HealthReminderVietnam;
  138. if (healthInfo != null)
  139. {
  140. return new HealthReminderTip
  141. {
  142. Type = 1,
  143. Title = healthInfo.title,
  144. Content = healthInfo.description,
  145. };
  146. }
  147. return null;
  148. }
  149. internal static AuthIdentifyTip GetInputIdentifyTip()
  150. {
  151. if (authIdentifyTips.TryGetValue(CHINA_IDENTIFY_INPUT_TIP_INDEX, out AuthIdentifyTip tip))
  152. {
  153. return tip;
  154. }
  155. return null;
  156. }
  157. internal static AuthIdentifyTip GetInputIdentifyFormatErrorTip()
  158. {
  159. if (authIdentifyTips.TryGetValue(CHINA_IDENTIFY_INPUT_FORMAT_ERROR_TIP_INDEX, out AuthIdentifyTip tip))
  160. {
  161. return tip;
  162. }
  163. return null;
  164. }
  165. /// <summary>
  166. /// 认证中提示(因为中宣部认证无响应)
  167. /// </summary>
  168. /// <returns></returns>
  169. internal static AuthIdentifyTip GetInputIdentifyBlockingTip()
  170. {
  171. if (authIdentifyTips.TryGetValue(CHINA_IDENTIFY_INPUT_BLOCK_TIP_INDEX, out AuthIdentifyTip tip))
  172. {
  173. return tip;
  174. }
  175. return null;
  176. }
  177. internal static HealthReminderTip GetMinorPlayableHealthReminderTip()
  178. {
  179. if (healthReminderTips.TryGetValue(MINOR_PLAYABLE_HEALTH_REMINDER_INDEX, out HealthReminderTip tip))
  180. {
  181. return tip;
  182. }
  183. return null;
  184. }
  185. internal static DateTimeOffset StrictStartTime =>
  186. DateTimeOffset.ParseExact(current.ChildProtectedConfig.NightStrictStart,
  187. TIME_FROMAT, CultureInfo.InvariantCulture);
  188. internal static DateTimeOffset StrictEndTime =>
  189. DateTimeOffset.ParseExact(current.ChildProtectedConfig.NightStrictEnd,
  190. TIME_FROMAT, CultureInfo.InvariantCulture);
  191. }
  192. }