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(PlatformAccountMgr.Instance.PlatformAccountConfig.module_PlatformAccount); } else { Module_PlatformAccount singleton = Resources.Load("Prefabs/Module_PlatformAccount"); instance = Instantiate(singleton); } } } return instance; } } public Action 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 callback = null, Action 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()) { if (item != LoginForms.Instance) { if (item is SCInputFieldForms) return; item.Hiding(); } } if (ForgetForms.Instance) foreach (var item in ForgetForms.Instance.GetComponentsInChildren()) { if (item != ForgetForms.Instance) { if (item is SCInputFieldForms) return; item.Hiding(); } } if (ErrorForms.Instance) { foreach (var item in ErrorForms.Instance.GetComponentsInChildren()) { if (item != ErrorForms.Instance) { if (item is SCInputFieldForms) return; item.Hiding(); } } } } } }