using UnityEngine; using System; using LitJson; namespace GFGGame { public class LoginProxy { private const string API_HI = "hi"; private const string API_LOGIN = "login"; private const string API_LOGIN_AS_VISITOR = "loginAsVisitor"; private const string API_REGISTER = "register"; public static void SayHi(Action callback) { //ViewManager.Show(ViewName.MODAL_STATUS_VIEW, "连接中..."); ViewManager.Show("连接中..."); Get(API_HI, (LoginResult result) => { ViewManager.Hide(); if(result.version == GameConst.SERVER_VERSION || Application.isEditor) { callback(result); } else { Alert.Show("版本已更新,请联系研发获取最新版本") .SetLeftButton(true, "知道了", (data) => { Application.Quit(); }); ; } }); } public static void Login(string account, string password) { ViewManager.Show("登录中..."); string data = JsonUtil.createJsonStr("account", account, "password", password); Post(API_LOGIN, data, (LoginResult result) => { ViewManager.Hide(); if(result == null || result.code == 0) { GameGlobal.isVisitor = false; GameController.OnLoginSuccess(result); } else { GameController.OnLoginFail(result); } }); } public static void LoginAsVisitor() { ViewManager.Show("登录中..."); long id = (long)PlayerPrefs.GetFloat(GameConst.VISITOR_ID_KEY, -1); string data = JsonUtil.createJsonStr("id", "" + id); Post(API_LOGIN_AS_VISITOR, data, (LoginResult result) => { ViewManager.Hide(); if(result == null || result.code == 0) { GameGlobal.isVisitor = true; GameController.OnLoginSuccess(result); } else { GameController.OnLoginFail(result); } }); } public static void Register(string account, string password, string name, string identityNum, string code) { ViewManager.Show("注册中..."); string data = JsonUtil.createJsonStr("account", account, "password", password, "name", name, "identityNum", identityNum, "code", code); Post(API_REGISTER, data, (LoginResult result) => { ViewManager.Hide(); if(result == null || result.code == 0) { GameGlobal.isVisitor = false; GameController.OnLoginSuccess(result); } else { GameController.OnLoginFail(result); } }); } private static void Get(string methodName, Action callback) { HttpTool.Instance.Get(GameGlobal.loginApiUrl, methodName, (string data) => { ResultHandler(data, callback); }); } private static void Post(string methodName, string jsonString, Action callback) { HttpTool.Instance.Post(GameGlobal.loginApiUrl, methodName, jsonString, (string data) => { ResultHandler(data, callback); }); } private static void ResultHandler(string data, Action callback) { LoginResult resultObj = null; if(data != null) { resultObj = JsonMapper.ToObject(data); } if(resultObj != null && resultObj.code != 0 && resultObj.message != null && resultObj.message.Length > 0) { PromptController.Instance.ShowFloatTextPrompt(resultObj.message); } if (callback != null) { callback(resultObj); } } } }