ApiComp.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using HttpApi;
  3. using Plugins.CxShine.net;
  4. using Plugins.CxShine.Singleton;
  5. using UI.Common;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. namespace Api
  9. {
  10. public class ApiComp : UnitySingleton<ApiComp>
  11. {
  12. private static string ApiSiteUrl()
  13. {
  14. return Constants.isLocal() ? "http://127.0.0.01:8888" : "http://india.cxhy.cn";
  15. // return Constants.isLocal() ? "http://127.0.0.01:8888" : "http://192.168.110.70:8888";
  16. }
  17. // 请求服务器接口
  18. private void HttpPostServer<T>(string requestUrl, ModelApiRequest request, Action<T> action,
  19. Action<int, string> serverErrorAction)
  20. {
  21. //
  22. var postData = JsonUtility.ToJson(request);
  23. // Debug.Log(requestUrl + "request body:" + postData);
  24. var ie = CxHttp.HttpPost(requestUrl, postData, text =>
  25. {
  26. // Debug.Log("api resp " + text);
  27. var bean = JsonUtility.FromJson<T>(text);
  28. action?.Invoke(bean);
  29. }, error =>
  30. {
  31. var msg = "网络错误:" + error;
  32. serverErrorAction?.Invoke(error.GetHashCode(), msg);
  33. });
  34. try
  35. {
  36. StartCoroutine(ie);
  37. }
  38. catch (Exception e)
  39. {
  40. Debug.LogError(e);
  41. }
  42. }
  43. // 静态对象
  44. public void HttpGetJsonObj<T>(string requestUrl, Action<T> successAction,
  45. Action<UnityWebRequest.Result> errorAction)
  46. {
  47. HttpGetText(requestUrl, text =>
  48. {
  49. var bean = JsonUtility.FromJson<T>(text);
  50. successAction?.Invoke(bean);
  51. }, errorAction);
  52. }
  53. // 静态数组
  54. public void HttpGetJsonList<T>(string requestUrl, Action<T[]> successAction,
  55. Action<UnityWebRequest.Result> errorAction)
  56. {
  57. HttpGetText(requestUrl, text =>
  58. {
  59. // 如果T是list类型,需要包装helper,
  60. var bean = JsonHelper.getJsonArray<T>(text);
  61. successAction?.Invoke(bean);
  62. }, errorAction);
  63. }
  64. // 静态文本
  65. public void HttpGetText(string requestUrl, Action<string> successAction,
  66. Action<UnityWebRequest.Result> errorAction)
  67. {
  68. var a = CxHttp.HttpGet(requestUrl, successAction, error =>
  69. {
  70. errorCommonProcess(error);
  71. errorAction?.Invoke(error);
  72. });
  73. StartCoroutine(a);
  74. }
  75. public void errorCommonProcess(UnityWebRequest.Result result)
  76. {
  77. }
  78. private void Api(ModelApiRequest request, Action<ResponseSuccessData> action,
  79. Action<int, string> serverErrorAction)
  80. {
  81. request.buildEncryptRequest();
  82. string url = ApiSiteUrl() + "/epi";
  83. HttpPostServer<ModelApiResponse>(url, request, resp =>
  84. {
  85. if (NetErrorCode.Success.GetHashCode() == resp.code)
  86. {
  87. action?.Invoke(resp.data);
  88. }
  89. else
  90. {
  91. TipsComp.ShowTips(resp.err);
  92. // 这里才是真正的业务逻辑相关错误
  93. Debug.LogError(
  94. url + "服务器Api错误" + resp.code + resp.err + "postData:" + JsonUtility.ToJson(request));
  95. serverErrorAction?.Invoke(resp.code, resp.err);
  96. }
  97. }, serverErrorAction);
  98. }
  99. private void ApiNotSession(string path, ModelApiRequest req, Action<ResponseSuccessData> success,
  100. Action<int, string> errAction)
  101. {
  102. string url = ApiSiteUrl() + path;
  103. HttpPostServer<ModelApiResponse>(url, req, resp =>
  104. {
  105. if (NetErrorCode.Success.GetHashCode() == resp.code)
  106. {
  107. success?.Invoke(resp.data);
  108. }
  109. else
  110. {
  111. TipsComp.ShowTips(resp.err);
  112. // 这里才是真正的业务逻辑相关错误
  113. Debug.LogError(
  114. url + "服务器Api错误" + resp.code + resp.err + "postData:" + JsonUtility.ToJson(req));
  115. errAction?.Invoke(resp.code, resp.err);
  116. }
  117. }, errAction);
  118. }
  119. public void GetOnlinePlayers(OnlinePlayerType type, Action<ResponseSuccessData> dataAction,
  120. Action<int, string> serverErrorAction)
  121. {
  122. var req = new ModelApiRequest();
  123. switch (type)
  124. {
  125. case OnlinePlayerType.All:
  126. req.api = "playersAll";
  127. break;
  128. case OnlinePlayerType.Friends:
  129. req.api = "playersFriend";
  130. break;
  131. case OnlinePlayerType.India:
  132. req.api = "playersIndia";
  133. break;
  134. }
  135. Api(req, dataAction, serverErrorAction);
  136. }
  137. public void EndMessage(string msgID)
  138. {
  139. var req = new ModelApiRequest
  140. {
  141. api = "msgEnd",
  142. msgId = msgID,
  143. };
  144. Api(req, null, null);
  145. }
  146. public void GetMsgList(Action<ResponseSuccessData> action,
  147. Action<int, string> serverErrorAction)
  148. {
  149. if (AccountManager.Instance.isLogin())
  150. {
  151. var req = new ModelApiRequest
  152. {
  153. api = "msgList",
  154. };
  155. Api(req, action, serverErrorAction);
  156. }
  157. }
  158. public void AgreeBattle(string battleSession)
  159. {
  160. var req = new ModelApiRequest
  161. {
  162. api = "agreeBattle",
  163. battleSession = battleSession,
  164. };
  165. Api(req, null, null);
  166. }
  167. public void InviteBattleToPlayer(int playerID, Action<ResponseSuccessData> action,
  168. Action<int, string> errAction)
  169. {
  170. var req = new ModelApiRequest
  171. {
  172. api = "inviteBattle",
  173. sendToPlayer = playerID
  174. };
  175. Api(req, action, errAction);
  176. }
  177. public void ClearMsgs()
  178. {
  179. var req = new ModelApiRequest
  180. {
  181. api = "msgClear",
  182. };
  183. Api(req, null, null);
  184. }
  185. public void QuerySelfInfo(Action<ResponseSuccessData> action, Action<int, string> errAction)
  186. {
  187. var req = new ModelApiRequest
  188. {
  189. api = "querySelfInfo",
  190. };
  191. Api(req, action, errAction);
  192. }
  193. public void QueryPlayerInfo(int playerId, Action<ResponseSuccessData> action, Action<int, String> o)
  194. {
  195. var req = new ModelApiRequest
  196. {
  197. api = "queryPlayerInfo",
  198. queryPlayerId = playerId,
  199. };
  200. Api(req, action, o);
  201. }
  202. public void PlayerReady(string battleSession, int cockId, int diamond)
  203. {
  204. var req = new ModelApiRequest
  205. {
  206. api = "playerReady",
  207. battleSession = battleSession,
  208. cockId = cockId,
  209. diamond = diamond
  210. };
  211. Api(req, null, null);
  212. }
  213. public void MsgInBattle(string battleSession, MsgContent msgContent)
  214. {
  215. var req = new ModelApiRequest
  216. {
  217. api = "msgInBattle",
  218. battleSession = battleSession,
  219. msgContent = msgContent
  220. };
  221. Api(req, null, null);
  222. }
  223. public void PlayerCocks(Action<ResponseSuccessData> action, Action<int, string> errorAction)
  224. {
  225. var req = new ModelApiRequest
  226. {
  227. api = "playerCocks",
  228. };
  229. Api(req, action, errorAction);
  230. }
  231. public void LoadConfig(Action action, Action<int, string> errAction)
  232. {
  233. var req = new ModelApiRequest
  234. {
  235. api = "config",
  236. };
  237. Api(req, data =>
  238. {
  239. ConfigManager.Instance.setupCockTypes(data.cockTypes);
  240. action?.Invoke();
  241. }, errAction);
  242. }
  243. public void cockMarket(Action<ResponseSuccessData> action, Action<int, string> o)
  244. {
  245. var req = new ModelApiRequest
  246. {
  247. api = "cockMarket",
  248. };
  249. Api(req, action, o);
  250. }
  251. public void BuyCock(int marketCockMarketId, Action<ResponseSuccessData> success, Action<int, string> errAction)
  252. {
  253. var req = new ModelApiRequest
  254. {
  255. api = "buyCock",
  256. marketId = marketCockMarketId
  257. };
  258. Api(req, success, errAction);
  259. }
  260. public void UpdateSelfInfo(string avatar, string updateName, Action<ResponseSuccessData> success,
  261. Action<int, string> errAction)
  262. {
  263. var req = new ModelApiRequest
  264. {
  265. api = "updateSelfInfo",
  266. avatar = avatar,
  267. name = updateName
  268. };
  269. Api(req, success, errAction);
  270. }
  271. public void NotSessionSendVerifyCode(string phone, Action<ResponseSuccessData> success,
  272. Action<int, string> errAction)
  273. {
  274. var req = new ModelApiRequest
  275. {
  276. phone = phone,
  277. };
  278. string path = "/sendVerifyCode";
  279. ApiNotSession(path, req, success, errAction);
  280. }
  281. public void NotSessionLoginVerifyCode(string phone, string code, bool isIndia,
  282. Action<ResponseSuccessData> success,
  283. Action<int, string> errAction)
  284. {
  285. var req = new ModelApiRequest
  286. {
  287. phone = phone,
  288. code = code,
  289. isIndia = isIndia,
  290. };
  291. string path = "/loginVerifyCode";
  292. ApiNotSession(path, req, success, errAction);
  293. }
  294. public void GetBattleDetail(string battleSession, Action<BattleDetailListObj> success,
  295. Action<int, string> errAction)
  296. {
  297. var req = new ModelApiRequest
  298. {
  299. api = "battleInfo",
  300. battleSession = battleSession
  301. };
  302. Api(req, (resp) =>
  303. {
  304. success(resp.battleInfo);
  305. }, errAction);
  306. }
  307. public void playerQuitGame(string battleSession, Action<ResponseSuccessData> success,
  308. Action<int, string> errAction)
  309. {
  310. var req = new ModelApiRequest
  311. {
  312. api = "playerQuitGame",
  313. battleSession = battleSession
  314. };
  315. Api(req, success, errAction);
  316. }
  317. public void playerRank(bool isIndia, Action<ResponseSuccessData> success, Action<int, string> errAction)
  318. {
  319. var req = new ModelApiRequest
  320. {
  321. api = "playerRank",
  322. isIndia = isIndia,
  323. };
  324. Api(req, success, errAction);
  325. }
  326. }
  327. }