Module_PlatformAccount.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using LitJson;
  2. using System;
  3. using UnityEngine;
  4. namespace SC.XR.Unity.Module_PlatformAccount
  5. {
  6. public class Module_PlatformAccount : MonoBehaviour
  7. {
  8. private static Module_PlatformAccount instance;
  9. private static object lockObj = new object();
  10. public static Module_PlatformAccount Instance
  11. {
  12. get
  13. {
  14. if (instance == null)
  15. {
  16. lock (lockObj)
  17. {
  18. if (PlatformAccountMgr.Instance.PlatformAccountConfig.module_PlatformAccount != null)
  19. {
  20. instance = Instantiate<Module_PlatformAccount>(PlatformAccountMgr.Instance.PlatformAccountConfig.module_PlatformAccount);
  21. }
  22. else
  23. {
  24. Module_PlatformAccount singleton = Resources.Load<Module_PlatformAccount>("Prefabs/Module_PlatformAccount");
  25. instance = Instantiate<Module_PlatformAccount>(singleton);
  26. }
  27. }
  28. }
  29. return instance;
  30. }
  31. }
  32. public Action<string> NetWorkError;
  33. protected virtual void Awake()
  34. {
  35. instance = this;
  36. NetWorkError += NetWorkErrorCallBack;
  37. API_Module_PlatformAccount.RegistLoginSuccessCallback(LoginSuccessCallBack);
  38. }
  39. protected virtual void OnEnable()
  40. {
  41. }
  42. protected virtual void Start()
  43. {
  44. Display();
  45. }
  46. protected virtual void OnDestroy()
  47. {
  48. NetWorkError -= NetWorkErrorCallBack;
  49. API_Module_PlatformAccount.UnRegistLoginSuccessCallback(LoginSuccessCallBack);
  50. }
  51. public virtual void Display()
  52. {
  53. Init();
  54. API_Module_PlatformAccount.CheckUserState(LoginMgr.Instance.serverConfig.channel, CheckUserStateCallBack, NetWorkError);
  55. }
  56. public virtual void Init()
  57. {
  58. LoginForms.Instance?.Init();
  59. ForgetForms.Instance?.Init();
  60. ErrorForms.Instance?.Init();
  61. }
  62. public void Logout(string channel = "quli", Action<string> callback = null, Action<string> error = null)
  63. {
  64. API_Module_PlatformAccount.AccountLogout(
  65. LoginMgr.Instance.userData.token,
  66. LoginMgr.Instance.userData.unionId,
  67. channel,
  68. (string json) => { LogoutCallBack(json); callback?.Invoke(json); },
  69. (string json) => { NetWorkError(json); error?.Invoke(json); });
  70. }
  71. public void CheckUserStateCallBack(bool valid)
  72. {
  73. if (valid)
  74. {
  75. LoginForms.Instance?.apLoginForms.Hiding();
  76. JsonData data = new JsonData();
  77. data["code"] = 200;
  78. data["message"] = "登录成功";
  79. data["data"] = "";
  80. string json = JsonMapper.ToJson(data);
  81. LoginMgr.Instance.LoginSuccessCallBack?.Invoke(json);
  82. }
  83. else
  84. {
  85. LoginForms.Instance?.apLoginForms.Display();
  86. }
  87. }
  88. public virtual void LoginSuccessCallBack(string json)
  89. {
  90. LoginForms.Instance?.logoutForms?.Display();
  91. }
  92. public virtual void NetWorkErrorCallBack(string error)
  93. {
  94. if (ErrorForms.Instance == null) return;
  95. HideAllForms();
  96. ErrorForms.Instance.networkErrorForms.Display();
  97. }
  98. public void LogoutCallBack(string json)
  99. {
  100. JsonData data = JsonMapper.ToObject(json);
  101. int code = (int)data["code"];
  102. switch (code)
  103. {
  104. case 200:
  105. HideAllForms();
  106. LoginForms.Instance.apLoginForms.Display();
  107. break;
  108. }
  109. }
  110. private void HideAllForms()
  111. {
  112. if (LoginForms.Instance)
  113. foreach (var item in LoginForms.Instance.GetComponentsInChildren<BaseUIForms>())
  114. {
  115. if (item != LoginForms.Instance)
  116. {
  117. if (item is SCInputFieldForms) return;
  118. item.Hiding();
  119. }
  120. }
  121. if (ForgetForms.Instance)
  122. foreach (var item in ForgetForms.Instance.GetComponentsInChildren<BaseUIForms>())
  123. {
  124. if (item != ForgetForms.Instance)
  125. {
  126. if (item is SCInputFieldForms) return;
  127. item.Hiding();
  128. }
  129. }
  130. if (ErrorForms.Instance)
  131. {
  132. foreach (var item in ErrorForms.Instance.GetComponentsInChildren<BaseUIForms>())
  133. {
  134. if (item != ErrorForms.Instance)
  135. {
  136. if (item is SCInputFieldForms) return;
  137. item.Hiding();
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }