Files
5.24BuYu/Assets/MyGame/Scripts/Net/APIManager.cs
T

339 lines
14 KiB
C#
Raw Normal View History

2026-02-03 11:35:19 +08:00
using Assets.MyGame.Scripts.Net.API;
using Assets.MyGame.Scripts.Net.APIRequest;
2026-05-18 18:39:11 +08:00
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Ocsp;
2026-02-03 11:35:19 +08:00
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<APIType, string> dic_API = new Dictionary<APIType, string>();
2026-02-13 19:23:01 +08:00
//public const string baseURL = "http://game5241.wzaxr.top";
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-06-18 19:33:39 +08:00
//public const string baseURL = "http://8.137.163.153";
public const string baseURL = "http://8.137.8.46";
2026-05-27 19:47:06 +08:00
2026-05-18 18:39:11 +08:00
#else
2026-06-19 18:18:04 +08:00
public const string baseURL = "http://8.137.8.46";
//public const string baseURL = "http://8.137.163.153";
2026-06-13 01:25:41 +08:00
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
static string token;
2026-03-27 15:18:35 +08:00
2026-02-03 11:35:19 +08:00
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";
2026-04-09 18:30:42 +08:00
dic_API[APIType.Login_ResetPassWord] = "/api/Login/ResetPassword";
2026-02-03 11:35:19 +08:00
// ---------------- 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";
2026-04-10 18:30:56 +08:00
// ---------------- 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";
2026-04-14 18:30:34 +08:00
// ---------------- 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}";
2026-02-03 11:35:19 +08:00
}
// ---------------- GET ----------------
static void GetSend<T>(string url, RequestParamBase request_Params, Action<T> action)
{
UIManager.Instance.StartCoroutine(IEGetSend<T>(url, request_Params, action));
}
static IEnumerator IEGetSend<T>(string url, RequestParamBase paramObj, Action<T> action)
{
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-02-03 11:35:19 +08:00
Debug.Log("url----" + url);
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
// 将 RequestParamBase 转成 GET 参数
if (paramObj != null)
{
var fields = paramObj.GetType().GetFields();
List<string> paramList = new List<string>();
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<APIResponse<T>>(json);
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-04-23 17:24:31 +08:00
Debug.Log("url----" + url+"--"+ "Json-"+json+"res--"+JsonConvert.SerializeObject(res));
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
// 自动保存 token
if (typeof(T) == typeof(UserToken))
{
var ut = res.data as UserToken;
if (ut != null) token = ut.token;
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-02-03 11:35:19 +08:00
Debug.Log("微信登录成功 Token" + token);
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
}
2026-03-27 15:18:35 +08:00
if (res.code == 0)
2026-02-03 11:35:19 +08:00
{
action?.Invoke(res.data);
}
else
{
ErrorHandle(res.code, res.info);
}
2026-03-27 15:18:35 +08:00
2026-02-03 11:35:19 +08:00
}
else
{
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-02-03 11:35:19 +08:00
Debug.LogError(req.error);
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
}
}
}
// ---------------- POST ----------------
static void PostSend<T>(string url, APIRequest.RequestObjBase body, Action<T> action)
{
UIManager.Instance.StartCoroutine(IEPostSend<T>(url, body, action));
}
static IEnumerator IEPostSend<T>(string url, APIRequest.RequestObjBase body, Action<T> 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<APIResponse<T>>(json);
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-04-23 17:24:31 +08:00
Debug.Log("url----" + url + "--" + "Json-" + json + "res--" + JsonConvert.SerializeObject(res));
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
if (res.code == 0)
{
action?.Invoke(res.data);
}
else
{
ErrorHandle(res.code, res.info);
}
2026-03-27 15:18:35 +08:00
2026-02-03 11:35:19 +08:00
}
else
{
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-02-03 11:35:19 +08:00
Debug.LogError(req.error);
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
}
}
}
// ---------------- 调用入口 ----------------
public static void Response<T>(APIType aPIType, RequestBase request, Action<T> action = null)
{
string url = baseURL + dic_API[aPIType];
if (request is RequestParamBase param)
{
GetSend<T>(url, param, action);
}
else if (request is APIRequest.RequestObjBase obj)
{
PostSend<T>(url, obj, action);
}
else
{
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-02-03 11:35:19 +08:00
Debug.LogError("RequestBase 不是 RequestParamBase 或 RequestObjBase");
2026-05-18 18:39:11 +08:00
#endif
2026-02-03 11:35:19 +08:00
}
}
/// <summary>
/// 无参
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="apiType"></param>
/// <param name="action"></param>
2026-03-27 15:18:35 +08:00
public static void Response<T>(APIType apiType, Action<T> action = null)
2026-02-03 11:35:19 +08:00
{
string url = baseURL + dic_API[apiType];
2026-03-27 15:18:35 +08:00
GetSend<T>(url, null, action);
2026-02-03 11:35:19 +08:00
}
public static void Log(string json)
{
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
Debug.Log(json);
#endif
2026-02-03 11:35:19 +08:00
}
2026-03-27 15:18:35 +08:00
public static void ErrorHandle(int code, string message)
2026-02-03 11:35:19 +08:00
{
2026-03-27 15:18:35 +08:00
2026-05-18 18:39:11 +08:00
#if UNITY_EDITOR
2026-03-27 15:18:35 +08:00
Debug.LogError("错误代码:" + code + "错误信息:" + message);
2026-05-18 18:39:11 +08:00
#endif
2026-03-27 15:18:35 +08:00
//这里之后补弹窗
2026-04-03 18:32:19 +08:00
UIManager.Instance.ShowBoardError("错误代码:" + code + "错误信息:" + message);
2026-02-03 11:35:19 +08:00
}
}
}