Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

204 righe
6.3 KiB

  1. using System;
  2. using System.Collections;
  3. using Cysharp.Threading.Tasks;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. using ETHotfix;
  8. public class LoginView : MonoBehaviour
  9. {
  10. public InputField PhoneNumberInputField;
  11. public InputField VerifyCodeInputField;
  12. public Text ErrorText;
  13. public Button SendCodeButton;
  14. public Text SendCodeButtonText;
  15. public Button LoginButton;
  16. public GameObject PhoneTrans;
  17. public GameObject loginBthTrans;
  18. public Button PhoneLoginButton;
  19. public Button WxLoginButton;
  20. public Button WxBindButton;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. SendCodeButton.onClick.AddListener(SendVerifyCode);
  25. LoginButton.onClick.AddListener(Login);
  26. PhoneLoginButton.onClick.AddListener(ClickPhoneLoginButton);
  27. WxLoginButton.onClick.AddListener(ClickWxLoginButton);
  28. WxBindButton.onClick.AddListener(ClickWxBindButton);
  29. string cachedPhoneNumber = PlayerPrefs.GetString("CachedPhoneNumber", "");
  30. string cachedWxUnionId = PlayerPrefs.GetString("CachedWxUnionId", "");
  31. if (!string.IsNullOrEmpty(cachedPhoneNumber) && !string.IsNullOrEmpty(cachedWxUnionId))
  32. {
  33. HttpManager.Instance.phoneNum = cachedPhoneNumber;
  34. HttpManager.Instance.wxUnionID = cachedWxUnionId;
  35. SceneManager.LoadScene("LoadingScreen");
  36. }
  37. else
  38. {
  39. PlayerPrefs.DeleteKey("CachedPhoneNumber");
  40. PlayerPrefs.DeleteKey("CachedWxUnionId");
  41. PlayerPrefs.Save();
  42. NativeBridgeUtil.Instance.loginCallback = WxloginCallback;
  43. }
  44. }
  45. public void OnDestroy()
  46. {
  47. NativeBridgeUtil.Instance.loginCallback = null;
  48. }
  49. public void WxloginCallback(string wxUnionID)
  50. {
  51. // 缓存手机号
  52. PlayerPrefs.SetString("CachedWxUnionId", wxUnionID);
  53. PlayerPrefs.Save();
  54. HttpManager.Instance.wxUnionID = wxUnionID;
  55. isWeChatAuthorized = true;
  56. CheckAuthorized();
  57. }
  58. private void SendVerifyCode()
  59. {
  60. SendVerifyCodeTask().Forget();
  61. }
  62. async UniTask SendVerifyCodeTask()
  63. {
  64. CodeResponse codeResponse = await HttpManager.Instance.RequestPost<CodeRequest, CodeResponse>("request-code", new CodeRequest(){phone = PhoneNumberInputField.text});
  65. if (codeResponse.message == "验证码已发送")
  66. {
  67. SendCodeButton.interactable = false;
  68. Countdown(30).Forget();
  69. }
  70. ErrorText.text = codeResponse.message;
  71. }
  72. private async UniTaskVoid Countdown(int duration)
  73. {
  74. // SendCodeButtonText.color = Color.gray;
  75. while (duration > 0)
  76. {
  77. SendCodeButtonText.text = $"发送验证码({duration})";
  78. await UniTask.Delay(TimeSpan.FromSeconds(1), ignoreTimeScale: false);
  79. duration--;
  80. }
  81. SendCodeButtonText.text = "发送验证码";
  82. SendCodeButton.interactable = true;
  83. // SendCodeButtonText.color = Color.black;
  84. }
  85. void Login()
  86. {
  87. LoginTask().Forget();
  88. }
  89. async UniTask LoginTask()
  90. {
  91. string phonenum = PhoneNumberInputField.text;
  92. LoginResponse loginResponse = await HttpManager.Instance.RequestPost<LoginRequest, LoginResponse>("verify-code", new LoginRequest(){phone = phonenum, code = VerifyCodeInputField.text});
  93. if (loginResponse.message == "验证成功")
  94. {
  95. // 缓存手机号
  96. PlayerPrefs.SetString("CachedPhoneNumber", phonenum);
  97. PlayerPrefs.Save();
  98. //ShowHomeView(phonenum);
  99. //SceneManager.LoadSceneAsync("LoadingScreen");
  100. HttpManager.Instance.phoneNum = phonenum;
  101. isPhoneAuthorized = true;
  102. CheckAuthorized();
  103. }
  104. else
  105. {
  106. ErrorText.text = loginResponse.message;
  107. }
  108. }
  109. async UniTask WxAndPhoneLoginTask()
  110. {
  111. string phonenum = PhoneNumberInputField.text;
  112. WxAndPhoneLoginResponse wxAndPhoneLoginResponse = await HttpManager.Instance.RequestPost<WxAndPhoneLoginRequest, WxAndPhoneLoginResponse>("wx-login", new WxAndPhoneLoginRequest() { uinid = HttpManager.Instance.wxUnionID, iphone_num = HttpManager.Instance.phoneNum });
  113. if (wxAndPhoneLoginResponse.code == 0)
  114. {
  115. SceneManager.LoadScene("LoadingScreen");
  116. }
  117. else
  118. {
  119. PlayerPrefs.DeleteKey("CachedPhoneNumber");
  120. PlayerPrefs.DeleteKey("CachedWxUnionId");
  121. PlayerPrefs.Save();
  122. HttpManager.Instance.wxUnionID = "";
  123. HttpManager.Instance.phoneNum = "";
  124. isWeChatAuthorized = false;
  125. isPhoneAuthorized = false;
  126. CheckAuthorized();
  127. }
  128. }
  129. private void CheckAuthorized()
  130. {
  131. if (isWeChatAuthorized && isPhoneAuthorized)
  132. {
  133. //SceneManager.LoadScene("LoadingScreen");
  134. WxAndPhoneLoginTask().Forget();
  135. }
  136. else if (isWeChatAuthorized && !isPhoneAuthorized)
  137. {
  138. WxBindButton.gameObject.SetActive(false);
  139. PhoneTrans.SetActive(true);
  140. loginBthTrans.SetActive(false);
  141. }
  142. else if (isPhoneAuthorized && !isWeChatAuthorized)
  143. {
  144. WxBindButton.gameObject.SetActive(true);
  145. PhoneTrans.SetActive(false);
  146. loginBthTrans.SetActive(false);
  147. }
  148. if (!isPhoneAuthorized && !isWeChatAuthorized)
  149. {
  150. WxBindButton.gameObject.SetActive(false);
  151. PhoneTrans.SetActive(false);
  152. loginBthTrans.SetActive(true);
  153. }
  154. }
  155. // 是否微信授权
  156. private bool isWeChatAuthorized = false;
  157. // 是否手机授权
  158. private bool isPhoneAuthorized = false;
  159. void ClickPhoneLoginButton()
  160. {
  161. WxBindButton.gameObject.SetActive(false);
  162. PhoneTrans.SetActive(true);
  163. loginBthTrans.SetActive(false);
  164. }
  165. void ClickWxLoginButton()
  166. {
  167. NativeBridgeUtil.Instance.WeChatLogin(1);
  168. }
  169. void ClickWxBindButton()
  170. {
  171. NativeBridgeUtil.Instance.WeChatLogin(1);
  172. }
  173. }