using Assets.MyGame.Scripts.Net.API; using Assets.MyGame.Scripts.Net.APIRequest; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Security.Cryptography; using System.Text; using UnityEngine; using UnityEngine.Networking; namespace Assets.MyGame.Scripts.Net { public static class APIManager { public static Dictionary dic_API = new Dictionary(); //public const string baseURL = "http://game5241.wzaxr.top"; public const string baseURL = "http://8.137.163.153"; static string token; static APIManager() { // ---------------- GameConfig ---------------- dic_API[APIType.GameConfig_Init] = "/api/GameConfig/init"; dic_API[APIType.GameConfig_Get] = "/api/GameConfig"; dic_API[APIType.GameConfig_Update] = "/api/GameConfig/Update"; // ---------------- Hall ---------------- dic_API[APIType.Hall_SaveSafeBox] = "/api/Hall/SaveSafeBox"; dic_API[APIType.Hall_GetSafeBox] = "/api/Hall/GetSafeBox"; dic_API[APIType.Hall_SetSafeBoxPassword] = "/api/Hall/SetSafeBoxPassword"; dic_API[APIType.Hall_ResetSafeBoxPassword] = "/api/Hall/ResetSafeBoxPassword"; dic_API[APIType.Hall_GetRelief] = "/api/Hall/GetRelief"; dic_API[APIType.Hall_SynthesisFragment] = "/api/Hall/SynthesisFragment"; dic_API[APIType.Hall_BuyMonthCard] = "/api/Hall/BuyMonthCard"; dic_API[APIType.Hall_BuyProp] = "/api/Hall/BuyProp"; dic_API[APIType.Hall_BuyGold] = "/api/Hall/BuyGold"; dic_API[APIType.Hall_BuyDiamond] = "/api/Hall/BuyDiamond"; dic_API[APIType.Hall_SignIn] = "/api/Hall/SignIn"; dic_API[APIType.Hall_ChangeCannon] = "/api/Hall/ChangeCannon"; dic_API[APIType.Hall_ChangeWing] = "/api/Hall/ChangeWing"; dic_API[APIType.Hall_ChangeGodWar] = "/api/Hall/ChangeGodWar"; dic_API[APIType.Hall_GetVipDayGift] = "/api/Hall/GetVipDayGift"; dic_API[APIType.Hall_GetVipGift] = "/api/Hall/GetVipGift"; dic_API[APIType.Hall_GetMonthCardDayGift] = "/api/Hall/GetMonthCardDayGift"; dic_API[APIType.Hall_GetMonthCardGift] = "/api/Hall/GetMonthCardGift"; dic_API[APIType.Hall_GetUserTaskList] = "/api/Hall/GetUserTaskList"; dic_API[APIType.Hall_DistributeRewards] = "/api/Hall/DistributeRewards"; // ---------------- HubsReceive ---------------- dic_API[APIType.Hubs_GetRoomList] = "/hubs/GetRoomList"; dic_API[APIType.Hubs_JoinRoom] = "/hubs/JoinRoom"; dic_API[APIType.Hubs_SyncGameData] = "/hubs/SyncGameData"; dic_API[APIType.Hubs_ShootBullet] = "/hubs/ShootBullet"; dic_API[APIType.Hubs_UseProp] = "/hubs/UseProp"; dic_API[APIType.Hubs_KillFish] = "/hubs/KillFish"; dic_API[APIType.Hubs_LeaveRoom] = "/hubs/LeaveRoom"; dic_API[APIType.Hubs_ChangeGunMultiple] = "/hubs/ChangeGunMultiple"; // ---------------- HubsSend ---------------- dic_API[APIType.Send_BindSuccess] = "/send/BindSuccess"; dic_API[APIType.Send_ErrorMessage] = "/send/ErrorMessage"; dic_API[APIType.Send_GetRoomListResult] = "/send/GetRoomListResult"; dic_API[APIType.Send_KillNotice] = "/send/KillNotice"; dic_API[APIType.Send_RoomChange] = "/send/RoomChange"; dic_API[APIType.Send_RoomDisconnectNotice] = "/send/RoomDisconnectNotice"; dic_API[APIType.Send_RoomJoinNotice] = "/send/RoomJoinNotice"; dic_API[APIType.Send_RoomKillNotice] = "/send/RoomKillNotice"; dic_API[APIType.Send_ChangeGunMultipleNotice] = "/send/ChangeGunMultipleNotice"; dic_API[APIType.Send_RoomLeaveNotice] = "/send/RoomLeaveNotice"; dic_API[APIType.Send_UsePropNotice] = "/send/UsePropNotice"; dic_API[APIType.Send_SyncGameData] = "/send/SyncGameData"; // ---------------- ImageUpload ---------------- dic_API[APIType.ImageUpload_Upload] = "/api/ImageUpload/upload"; // ---------------- Login ---------------- dic_API[APIType.Login_GetAccessToken] = "/api/Login/GetAccessToken"; dic_API[APIType.Login_GetSessionToken] = "/api/Login/GetSessionToken"; dic_API[APIType.Login_RegisterWX] = "/api/Login/registerWX"; dic_API[APIType.Login_Register] = "/api/Login/register"; dic_API[APIType.Login_SendMobileCode] = "/api/Login/SendMobileCode"; dic_API[APIType.Login_LoginWx] = "/api/Login/LoginWx"; dic_API[APIType.Login_Login] = "/api/Login/Login"; dic_API[APIType.Login_ResetPassWord] = "/api/Login/ResetPassword"; // ---------------- Notice ---------------- dic_API[APIType.Notice_Add] = "/api/Notice/add"; dic_API[APIType.Notice_Update] = "/api/Notice/update"; dic_API[APIType.Notice_Delete] = "/api/Notice/delete"; dic_API[APIType.Notice_List] = "/api/Notice/list"; dic_API[APIType.Notice_Get] = "/api/Notice/get"; dic_API[APIType.Notice_GetNewOne] = "/api/Notice/get/newone"; // ---------------- System ---------------- dic_API[APIType.System_AddUser] = "/api/System/user/add"; // ---------------- BeautifulNumber ---------------- dic_API[APIType.BeautifulNumber_Available] = "/api/BeautifulNumber/available"; dic_API[APIType.BeautifulNumber_Check] = "/api/BeautifulNumber/check/{number}"; // ---------------- Feedback ---------------- dic_API[APIType.Feedback_Submit] = "/api/Feedback/Submit"; dic_API[APIType.Feedback_MyList] = "/api/Feedback/MyList"; dic_API[APIType.Feedback_Detail] = "/api/Feedback/Detail"; // ---------------- Hall(补充)---------------- dic_API[APIType.Hall_GetUserRewardList] = "/api/Hall/GetUserRewardList"; // ---------------- Login(补充)---------------- dic_API[APIType.Login_GetDateTime] = "/api/Login/GetDateTime"; // ---------------- User ---------------- dic_API[APIType.User_RealNameAuth] = "/api/User/RealNameAuth"; dic_API[APIType.User_UpdateUserName] = "/api/User/UpdateUserName"; dic_API[APIType.User_BindWX] = "/api/User/BindWX"; dic_API[APIType.User_BindMobile] = "/api/User/BindMobile"; dic_API[APIType.User_UpdateAvatar] = "/api/User/UpdateAvatar"; dic_API[APIType.User_GetUser] = "/api/User/GetUser"; dic_API[APIType.User_GetUserList] = "/api/User/GetUserList"; dic_API[APIType.User_SendEmail] = "/api/User/SendEmail"; dic_API[APIType.User_GetEmail] = "/api/User/GetEmail"; dic_API[APIType.User_RewardEmail] = "/api/User/RewardEmail"; dic_API[APIType.User_DeleteEmail] = "/api/User/DeleteEmail"; dic_API[APIType.User_ReadEmailMore] = "/api/User/ReadEmailMore"; dic_API[APIType.User_GetNoReadCount] = "/api/User/GetNoReadCount"; dic_API[APIType.User_GetEmailByPage] = "/api/User/GetEmailByPage"; // ---------------- Customer ---------------- dic_API[APIType.Customer_Start] = "/customerapi/Customer/start"; dic_API[APIType.Customer_Message] = "/customerapi/Customer/message"; dic_API[APIType.Customer_Messages] = "/customerapi/Customer/messages/{conversationId}"; dic_API[APIType.Customer_Conversations] = "/customerapi/Customer/conversations"; dic_API[APIType.Customer_End] = "/customerapi/Customer/end"; dic_API[APIType.Customer_Online] = "/customerapi/Customer/online"; dic_API[APIType.Customer_Status] = "/customerapi/Customer/status"; dic_API[APIType.Customer_ChatLog] = "/customerapi/Customer/chatlog/{conversationId}"; } // ---------------- GET ---------------- static void GetSend(string url, RequestParamBase request_Params, Action action) { UIManager.Instance.StartCoroutine(IEGetSend(url, request_Params, action)); } static IEnumerator IEGetSend(string url, RequestParamBase paramObj, Action action) { Debug.Log("url----" + url); // 将 RequestParamBase 转成 GET 参数 if (paramObj != null) { var fields = paramObj.GetType().GetFields(); List paramList = new List(); foreach (var f in fields) { object value = f.GetValue(paramObj); if (value != null) { string key = f.Name; string val = UnityWebRequest.EscapeURL(value.ToString()); paramList.Add($"{key}={val}"); } } if (paramList.Count > 0) { url += "?" + string.Join("&", paramList); } } using (UnityWebRequest req = UnityWebRequest.Get(url)) { req.SetRequestHeader("Content-Type", "application/json"); if (!string.IsNullOrEmpty(token)) req.SetRequestHeader("token", token); yield return req.SendWebRequest(); if (req.result == UnityWebRequest.Result.Success) { var json = req.downloadHandler.text; var res = JsonConvert.DeserializeObject>(json); Debug.Log("url----" + url+"--"+ "Json-"+json+"res--"+JsonConvert.SerializeObject(res)); // 自动保存 token if (typeof(T) == typeof(UserToken)) { var ut = res.data as UserToken; if (ut != null) token = ut.token; Debug.Log("微信登录成功 Token" + token); } if (res.code == 0) { action?.Invoke(res.data); } else { ErrorHandle(res.code, res.info); } } else { Debug.LogError(req.error); } } } // ---------------- POST ---------------- static void PostSend(string url, APIRequest.RequestObjBase body, Action action) { UIManager.Instance.StartCoroutine(IEPostSend(url, body, action)); } static IEnumerator IEPostSend(string url, APIRequest.RequestObjBase body, Action action) { string upBodyJson = JsonConvert.SerializeObject(body); byte[] buff = Encoding.UTF8.GetBytes(upBodyJson); using (UnityWebRequest req = new UnityWebRequest(url, "POST")) { req.SetRequestHeader("Content-Type", "application/json"); req.downloadHandler = new DownloadHandlerBuffer(); req.uploadHandler = new UploadHandlerRaw(buff); if (!string.IsNullOrEmpty(token)) req.SetRequestHeader("token", token); yield return req.SendWebRequest(); if (req.result == UnityWebRequest.Result.Success) { var json = req.downloadHandler.text; var res = JsonConvert.DeserializeObject>(json); Debug.Log("url----" + url + "--" + "Json-" + json + "res--" + JsonConvert.SerializeObject(res)); if (res.code == 0) { action?.Invoke(res.data); } else { ErrorHandle(res.code, res.info); } } else { Debug.LogError(req.error); } } } // ---------------- 调用入口 ---------------- public static void Response(APIType aPIType, RequestBase request, Action action = null) { string url = baseURL + dic_API[aPIType]; if (request is RequestParamBase param) { GetSend(url, param, action); } else if (request is APIRequest.RequestObjBase obj) { PostSend(url, obj, action); } else { Debug.LogError("RequestBase 不是 RequestParamBase 或 RequestObjBase"); } } /// /// 无参 /// /// /// /// public static void Response(APIType apiType, Action action = null) { string url = baseURL + dic_API[apiType]; GetSend(url, null, action); } public static void Log(string json) { Debug.Log(json); } public static void ErrorHandle(int code, string message) { Debug.LogError("错误代码:" + code + "错误信息:" + message); //这里之后补弹窗 UIManager.Instance.ShowBoardError("错误代码:" + code + "错误信息:" + message); } } }