TaptapAntiAddictionIDInputController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using LC.Newtonsoft.Json;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using TapSDK.UI;
  7. using TapTap.AntiAddiction.Model;
  8. namespace TapTap.AntiAddiction.Internal {
  9. public class TaptapAntiAddictionIDInputController : BasePanelController
  10. {
  11. public Button closeButton;
  12. public Button submitButton;
  13. public InputField nameInputField;
  14. public InputField idNumberInputField;
  15. public Text titleText;
  16. public Text descriptionText;
  17. public Text buttonText;
  18. public GameObject errorNode;
  19. public Text errorTipText;
  20. internal Action<VerificationResult> OnVerified;
  21. internal Action<Exception> OnException;
  22. internal Action OnClosed;
  23. private bool _isSending;
  24. private bool isSending
  25. {
  26. get => _isSending;
  27. set
  28. {
  29. if (value != _isSending)
  30. {
  31. _isSending = value;
  32. if (_isSending)
  33. UIManager.Instance.OpenLoading();
  34. else
  35. UIManager.Instance.CloseLoading();
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// bind ugui components for every panel
  41. /// </summary>
  42. protected override void BindComponents()
  43. {
  44. closeButton = transform.Find("Root/CloseButton").GetComponent<Button>();
  45. submitButton = transform.Find("Root/SubmitButton").GetComponent<Button>();
  46. titleText = transform.Find("Root/TitleText").GetComponent<Text>();
  47. descriptionText = transform.Find("Root/ContentText").GetComponent<Text>();
  48. buttonText = submitButton.transform.Find("Text").GetComponent<Text>();
  49. nameInputField = transform.Find("Root/NameInput").GetComponent<InputField>();
  50. idNumberInputField = transform.Find("Root/IDNumInput").GetComponent<InputField>();
  51. errorNode = transform.Find("Root/Error").gameObject;
  52. errorTipText = errorNode.transform.Find("Text").GetComponent<Text>();
  53. }
  54. protected override void OnLoadSuccess()
  55. {
  56. base.OnLoadSuccess();
  57. closeButton.onClick.AddListener(OnCloseButtonClicked);
  58. submitButton.onClick.AddListener(OnConfirmButtonClicked);
  59. var config = Config.GetInputIdentifyTip();
  60. if (config != null)
  61. {
  62. titleText.text = config.Title;
  63. descriptionText.text = config.Content;
  64. buttonText.text = config.PositiveButtonText;
  65. }
  66. isSending = false;
  67. errorNode.gameObject.SetActive(false);
  68. }
  69. private bool Validate(out string name, out string idNumber, out string errorTip)
  70. {
  71. errorTip = null;
  72. name = nameInputField.text;
  73. idNumber = idNumberInputField.text;
  74. if (string.IsNullOrWhiteSpace(name))
  75. {
  76. errorTip = "请输入真实姓名";
  77. return false;
  78. }
  79. if (!IsIdNum(idNumber))
  80. {
  81. errorTip = "请输入真实的身份证号码";
  82. return false;
  83. }
  84. return true;
  85. }
  86. private async void OnConfirmButtonClicked()
  87. {
  88. if (isSending) return;
  89. string errorTip;
  90. string name;
  91. string idNumber;
  92. var validation = Validate(out name, out idNumber, out errorTip);
  93. if (validation)
  94. {
  95. var dateJson = new { name = name, idCard = idNumber };
  96. var json = JsonConvert.SerializeObject(dateJson, Formatting.Indented);
  97. try
  98. {
  99. isSending = true;
  100. var verificationResult = await Verification.VerifyKycAsync(TapTapAntiAddictionManager.UserId, json);
  101. isSending = false;
  102. OnVerified?.Invoke(verificationResult);
  103. // 2-认证失败
  104. if (verificationResult.Status != 2)
  105. Close();
  106. }
  107. catch (Exception e)
  108. {
  109. isSending = false;
  110. OnException?.Invoke(e);
  111. }
  112. }
  113. else
  114. {
  115. ShowErrorTip(errorTip);
  116. }
  117. }
  118. private void OnCloseButtonClicked()
  119. {
  120. OnClosed?.Invoke();
  121. Close();
  122. }
  123. /// <summary>
  124. /// 验证身份证号(https://cloud.tencent.com/developer/article/1860685)
  125. /// </summary>
  126. /// <param name="input"></param>
  127. /// <returns></returns>
  128. private static bool IsIdNum(string input)
  129. {
  130. return !string.IsNullOrWhiteSpace(input);
  131. // double iSum = 0;
  132. // // 18位验证
  133. // Regex rg = new Regex(@"^\d{17}(\d|x)$");
  134. // Match mc = rg.Match(input);
  135. // if (!mc.Success)
  136. // {
  137. // return false;
  138. // }
  139. //
  140. // // 生日验证
  141. // input = input.ToLower();
  142. // input = input.Replace("x", "a");
  143. // try
  144. // {
  145. // var year = input.Substring(6, 4);
  146. // var month = input.Substring(10, 2);
  147. // var day = input.Substring(12, 2);
  148. // DateTime.Parse(year + "-" + month + "-" + day);
  149. // }
  150. // catch
  151. // {
  152. // #if UNITY_EDITOR
  153. // Debug.LogErrorFormat("国内-防沉迷 身份证号非法出生日期");
  154. // #endif
  155. // return false;
  156. // }
  157. //
  158. // // 最后一位验证
  159. // for (int i = 17; i >= 0; i--)
  160. // {
  161. // iSum += (Math.Pow(2, i) % 11) *
  162. // int.Parse(input[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
  163. // }
  164. //
  165. // if (iSum % 11 != 1)
  166. // {
  167. // #if UNITY_EDITOR
  168. // Debug.LogErrorFormat("国内-防沉迷 身份证号非法尾号");
  169. // #endif
  170. // return false;
  171. // }
  172. //
  173. // return true;
  174. }
  175. public void ShowErrorTip(string content)
  176. {
  177. errorNode.gameObject.SetActive(true);
  178. errorTipText.text = content;
  179. }
  180. }
  181. }