123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using LitJson;
- using System;
- using UnityEngine;
- namespace SC.XR.Unity.Module_PlatformAccount
- {
- public class Module_PlatformAccount : MonoBehaviour
- {
- private static Module_PlatformAccount instance;
- private static object lockObj = new object();
- public static Module_PlatformAccount Instance
- {
- get
- {
- if (instance == null)
- {
- lock (lockObj)
- {
- if (PlatformAccountMgr.Instance.PlatformAccountConfig.module_PlatformAccount != null)
- {
- instance = Instantiate<Module_PlatformAccount>(PlatformAccountMgr.Instance.PlatformAccountConfig.module_PlatformAccount);
- }
- else
- {
- Module_PlatformAccount singleton = Resources.Load<Module_PlatformAccount>("Prefabs/Module_PlatformAccount");
- instance = Instantiate<Module_PlatformAccount>(singleton);
- }
- }
- }
- return instance;
- }
- }
- public Action<string> NetWorkError;
- protected virtual void Awake()
- {
- instance = this;
- NetWorkError += NetWorkErrorCallBack;
- API_Module_PlatformAccount.RegistLoginSuccessCallback(LoginSuccessCallBack);
- }
- protected virtual void OnEnable()
- {
-
- }
- protected virtual void Start()
- {
- Display();
- }
- protected virtual void OnDestroy()
- {
- NetWorkError -= NetWorkErrorCallBack;
- API_Module_PlatformAccount.UnRegistLoginSuccessCallback(LoginSuccessCallBack);
- }
- public virtual void Display()
- {
- Init();
- API_Module_PlatformAccount.CheckUserState(LoginMgr.Instance.serverConfig.channel, CheckUserStateCallBack, NetWorkError);
- }
- public virtual void Init()
- {
- LoginForms.Instance?.Init();
- ForgetForms.Instance?.Init();
- ErrorForms.Instance?.Init();
- }
- public void Logout(string channel = "quli", Action<string> callback = null, Action<string> error = null)
- {
- API_Module_PlatformAccount.AccountLogout(
- LoginMgr.Instance.userData.token,
- LoginMgr.Instance.userData.unionId,
- channel,
- (string json) => { LogoutCallBack(json); callback?.Invoke(json); },
- (string json) => { NetWorkError(json); error?.Invoke(json); });
- }
- public void CheckUserStateCallBack(bool valid)
- {
- if (valid)
- {
- LoginForms.Instance?.apLoginForms.Hiding();
- JsonData data = new JsonData();
- data["code"] = 200;
- data["message"] = "登录成功";
- data["data"] = "";
- string json = JsonMapper.ToJson(data);
- LoginMgr.Instance.LoginSuccessCallBack?.Invoke(json);
- }
- else
- {
- LoginForms.Instance?.apLoginForms.Display();
- }
- }
- public virtual void LoginSuccessCallBack(string json)
- {
- LoginForms.Instance?.logoutForms?.Display();
- }
- public virtual void NetWorkErrorCallBack(string error)
- {
- if (ErrorForms.Instance == null) return;
- HideAllForms();
- ErrorForms.Instance.networkErrorForms.Display();
- }
- public void LogoutCallBack(string json)
- {
- JsonData data = JsonMapper.ToObject(json);
- int code = (int)data["code"];
- switch (code)
- {
- case 200:
- HideAllForms();
- LoginForms.Instance.apLoginForms.Display();
- break;
- }
- }
- private void HideAllForms()
- {
- if (LoginForms.Instance)
- foreach (var item in LoginForms.Instance.GetComponentsInChildren<BaseUIForms>())
- {
- if (item != LoginForms.Instance)
- {
- if (item is SCInputFieldForms) return;
- item.Hiding();
- }
- }
- if (ForgetForms.Instance)
- foreach (var item in ForgetForms.Instance.GetComponentsInChildren<BaseUIForms>())
- {
- if (item != ForgetForms.Instance)
- {
- if (item is SCInputFieldForms) return;
- item.Hiding();
- }
- }
- if (ErrorForms.Instance)
- {
- foreach (var item in ErrorForms.Instance.GetComponentsInChildren<BaseUIForms>())
- {
- if (item != ErrorForms.Instance)
- {
- if (item is SCInputFieldForms) return;
- item.Hiding();
- }
- }
- }
- }
- }
- }
|