123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- 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<ApiComp>
- {
- 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<T>(string requestUrl, ModelApiRequest request, Action<T> action,
- Action<int, string> 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<T>(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<T>(string requestUrl, Action<T> successAction,
- Action<UnityWebRequest.Result> errorAction)
- {
- HttpGetText(requestUrl, text =>
- {
- var bean = JsonUtility.FromJson<T>(text);
- successAction?.Invoke(bean);
- }, errorAction);
- }
- // 静态数组
- public void HttpGetJsonList<T>(string requestUrl, Action<T[]> successAction,
- Action<UnityWebRequest.Result> errorAction)
- {
- HttpGetText(requestUrl, text =>
- {
- // 如果T是list类型,需要包装helper,
- var bean = JsonHelper.getJsonArray<T>(text);
- successAction?.Invoke(bean);
- }, errorAction);
- }
- // 静态文本
- public void HttpGetText(string requestUrl, Action<string> successAction,
- Action<UnityWebRequest.Result> 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<ResponseSuccessData> action,
- Action<int, string> serverErrorAction)
- {
- request.buildEncryptRequest();
- string url = ApiSiteUrl() + "/epi";
- HttpPostServer<ModelApiResponse>(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<ResponseSuccessData> success,
- Action<int, string> errAction)
- {
- string url = ApiSiteUrl() + path;
- HttpPostServer<ModelApiResponse>(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<ResponseSuccessData> dataAction,
- Action<int, string> 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<ResponseSuccessData> action,
- Action<int, string> 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<ResponseSuccessData> action,
- Action<int, string> 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<ResponseSuccessData> action, Action<int, string> errAction)
- {
- var req = new ModelApiRequest
- {
- api = "querySelfInfo",
- };
- Api(req, action, errAction);
- }
- public void QueryPlayerInfo(int playerId, Action<ResponseSuccessData> action, Action<int, String> 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<ResponseSuccessData> action, Action<int, string> errorAction)
- {
- var req = new ModelApiRequest
- {
- api = "playerCocks",
- };
- Api(req, action, errorAction);
- }
- public void LoadConfig(Action action, Action<int, string> errAction)
- {
- var req = new ModelApiRequest
- {
- api = "config",
- };
- Api(req, data =>
- {
- ConfigManager.Instance.setupCockTypes(data.cockTypes);
- action?.Invoke();
- }, errAction);
- }
- public void cockMarket(Action<ResponseSuccessData> action, Action<int, string> o)
- {
- var req = new ModelApiRequest
- {
- api = "cockMarket",
- };
- Api(req, action, o);
- }
- public void BuyCock(int marketCockMarketId, Action<ResponseSuccessData> success, Action<int, string> errAction)
- {
- var req = new ModelApiRequest
- {
- api = "buyCock",
- marketId = marketCockMarketId
- };
- Api(req, success, errAction);
- }
- public void UpdateSelfInfo(string avatar, string updateName, Action<ResponseSuccessData> success,
- Action<int, string> errAction)
- {
- var req = new ModelApiRequest
- {
- api = "updateSelfInfo",
- avatar = avatar,
- name = updateName
- };
- Api(req, success, errAction);
- }
- public void NotSessionSendVerifyCode(string phone, Action<ResponseSuccessData> success,
- Action<int, string> 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<ResponseSuccessData> success,
- Action<int, string> 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<BattleDetailListObj> success,
- Action<int, string> errAction)
- {
- var req = new ModelApiRequest
- {
- api = "battleInfo",
- battleSession = battleSession
- };
- Api(req, (resp) =>
- {
- success(resp.battleInfo);
- }, errAction);
- }
-
- }
- }
|