using System; using HttpApi; using Plugins.CxShine.net; using Plugins.CxShine.Singleton; using UI.Common; using UnityEngine; using UnityEngine.Networking; namespace Api { public class ApiComp : UnitySingleton { private static string ApiSiteUrl() { return Constants.isLocal() ? "http://127.0.0.01:8888" : "http://india.cxhy.cn"; // return Constants.isLocal() ? "http://127.0.0.01:8888" : "http://192.168.110.70:8888"; } // 请求服务器接口 private void HttpPostServer(string requestUrl, ModelApiRequest request, Action action, Action serverErrorAction) { // var postData = JsonUtility.ToJson(request); // Debug.Log(requestUrl + "request body:" + postData); var ie = CxHttp.HttpPost(requestUrl, postData, text => { // Debug.Log("api resp " + text); var bean = JsonUtility.FromJson(text); action?.Invoke(bean); }, error => { var msg = "网络错误:" + error; serverErrorAction?.Invoke(error.GetHashCode(), msg); }); try { StartCoroutine(ie); } catch (Exception e) { Debug.LogError(e); } } // 静态对象 public void HttpGetJsonObj(string requestUrl, Action successAction, Action errorAction) { HttpGetText(requestUrl, text => { var bean = JsonUtility.FromJson(text); successAction?.Invoke(bean); }, errorAction); } // 静态数组 public void HttpGetJsonList(string requestUrl, Action successAction, Action errorAction) { HttpGetText(requestUrl, text => { // 如果T是list类型,需要包装helper, var bean = JsonHelper.getJsonArray(text); successAction?.Invoke(bean); }, errorAction); } // 静态文本 public void HttpGetText(string requestUrl, Action successAction, Action errorAction) { var a = CxHttp.HttpGet(requestUrl, successAction, error => { errorCommonProcess(error); errorAction?.Invoke(error); }); StartCoroutine(a); } public void errorCommonProcess(UnityWebRequest.Result result) { } private void Api(ModelApiRequest request, Action action, Action serverErrorAction) { request.buildEncryptRequest(); string url = ApiSiteUrl() + "/epi"; HttpPostServer(url, request, resp => { if (NetErrorCode.Success.GetHashCode() == resp.code) { action?.Invoke(resp.data); } else { TipsComp.ShowTips(resp.err); // 这里才是真正的业务逻辑相关错误 Debug.LogError( url + "服务器Api错误" + resp.code + resp.err + "postData:" + JsonUtility.ToJson(request)); serverErrorAction?.Invoke(resp.code, resp.err); } }, serverErrorAction); } private void ApiNotSession(string path, ModelApiRequest req, Action success, Action errAction) { string url = ApiSiteUrl() + path; HttpPostServer(url, req, resp => { if (NetErrorCode.Success.GetHashCode() == resp.code) { success?.Invoke(resp.data); } else { TipsComp.ShowTips(resp.err); // 这里才是真正的业务逻辑相关错误 Debug.LogError( url + "服务器Api错误" + resp.code + resp.err + "postData:" + JsonUtility.ToJson(req)); errAction?.Invoke(resp.code, resp.err); } }, errAction); } public void GetOnlinePlayers(OnlinePlayerType type, Action dataAction, Action serverErrorAction) { var req = new ModelApiRequest(); switch (type) { case OnlinePlayerType.All: req.api = "playersAll"; break; case OnlinePlayerType.Friends: req.api = "playersFriend"; break; case OnlinePlayerType.India: req.api = "playersIndia"; break; } Api(req, dataAction, serverErrorAction); } public void EndMessage(string msgID) { var req = new ModelApiRequest { api = "msgEnd", msgId = msgID, }; Api(req, null, null); } public void GetMsgList(Action action, Action serverErrorAction) { if (AccountManager.Instance.isLogin()) { var req = new ModelApiRequest { api = "msgList", }; Api(req, action, serverErrorAction); } } public void AgreeBattle(string battleSession) { var req = new ModelApiRequest { api = "agreeBattle", battleSession = battleSession, }; Api(req, null, null); } public void InviteBattleToPlayer(int playerID, Action action, Action errAction) { var req = new ModelApiRequest { api = "inviteBattle", sendToPlayer = playerID }; Api(req, action, errAction); } public void ClearMsgs() { var req = new ModelApiRequest { api = "msgClear", }; Api(req, null, null); } public void QuerySelfInfo(Action action, Action errAction) { var req = new ModelApiRequest { api = "querySelfInfo", }; Api(req, action, errAction); } public void QueryPlayerInfo(int playerId, Action action, Action o) { var req = new ModelApiRequest { api = "queryPlayerInfo", queryPlayerId = playerId, }; Api(req, action, o); } public void PlayerReady(string battleSession, int cockId, int diamond) { var req = new ModelApiRequest { api = "playerReady", battleSession = battleSession, cockId = cockId, diamond = diamond }; Api(req, null, null); } public void MsgInBattle(string battleSession, MsgContent msgContent) { var req = new ModelApiRequest { api = "msgInBattle", battleSession = battleSession, msgContent = msgContent }; Api(req, null, null); } public void PlayerCocks(Action action, Action errorAction) { var req = new ModelApiRequest { api = "playerCocks", }; Api(req, action, errorAction); } public void LoadConfig(Action action, Action errAction) { var req = new ModelApiRequest { api = "config", }; Api(req, data => { ConfigManager.Instance.setupCockTypes(data.cockTypes); action?.Invoke(); }, errAction); } public void cockMarket(Action action, Action o) { var req = new ModelApiRequest { api = "cockMarket", }; Api(req, action, o); } public void BuyCock(int marketCockMarketId, Action success, Action errAction) { var req = new ModelApiRequest { api = "buyCock", marketId = marketCockMarketId }; Api(req, success, errAction); } public void UpdateSelfInfo(string avatar, string updateName, Action success, Action errAction) { var req = new ModelApiRequest { api = "updateSelfInfo", avatar = avatar, name = updateName }; Api(req, success, errAction); } public void NotSessionSendVerifyCode(string phone, Action success, Action errAction) { var req = new ModelApiRequest { phone = phone, }; string path = "/sendVerifyCode"; ApiNotSession(path, req, success, errAction); } public void NotSessionLoginVerifyCode(string phone, string code, bool isIndia, Action success, Action errAction) { var req = new ModelApiRequest { phone = phone, code = code, isIndia = isIndia, }; string path = "/loginVerifyCode"; ApiNotSession(path, req, success, errAction); } public void GetBattleDetail(string battleSession, Action success, Action errAction) { var req = new ModelApiRequest { api = "battleInfo", battleSession = battleSession }; Api(req, (resp) => { success(resp.battleInfo); }, errAction); } public void playerQuitGame(string battleSession, Action success, Action errAction) { var req = new ModelApiRequest { api = "playerQuitGame", battleSession = battleSession }; Api(req, success, errAction); } public void playerRank(bool isIndia, Action success, Action errAction) { var req = new ModelApiRequest { api = "playerRank", isIndia = isIndia, }; Api(req, success, errAction); } } }