Files
5.24BuYu/Assets/MyGame/Scripts/Net/APIManager.cs
T
2026-08-01 15:27:12 +08:00

403 lines
18 KiB
C#

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<APIType, string> dic_API = new Dictionary<APIType, string>();
//public const string baseURL = "http://game5241.wzaxr.top";
#if ENABLE_LOG_FILE
public const string baseURL = "http://47.109.132.12";
#elif UNITY_EDITOR
//public const string baseURL = "http://8.137.163.153";
public const string baseURL = "http://47.109.132.12";
#else
//public const string baseURL = "http://47.109.132.12";
public const string baseURL = "http://47.109.34.50";
#endif
public 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";
// ---------------- ActivityConfig ----------------
dic_API[APIType.ActivityConfig_Get] = "/api/ActivityConfig";
dic_API[APIType.ActivityConfig_DiamondShopList] = "/api/ActivityConfig/diamondShopList";
dic_API[APIType.ActivityConfig_GoldShopList] = "/api/ActivityConfig/goldShopList";
dic_API[APIType.ActivityConfig_ReliefConfig] = "/api/ActivityConfig/reliefConfig";
dic_API[APIType.ActivityConfig_SignConfigs] = "/api/ActivityConfig/signConfigs";
dic_API[APIType.ActivityConfig_RegisterConfig] = "/api/ActivityConfig/registerConfig";
dic_API[APIType.ActivityConfig_BattleOrderConfig] = "/api/ActivityConfig/battleOrderConfig";
dic_API[APIType.ActivityConfig_FishingReturnConfig] = "/api/ActivityConfig/fishingReturnConfig";
dic_API[APIType.ActivityConfig_RankingRewardConfig] = "/api/ActivityConfig/rankingRewardConfig";
// ---------------- 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";
dic_API[APIType.Hall_ReceiveAllTaskRewards] = "/api/Hall/ReceiveAllTaskRewards";
// ---------------- BattleOrder ----------------
dic_API[APIType.Hall_GetBattleOrder] = "/api/Hall/GetBattleOrder";
dic_API[APIType.Hall_ReceiveAllBattleOrderRewards] = "/api/Hall/ReceiveAllBattleOrderRewards";
dic_API[APIType.Hall_ReceiveBattleOrderLevelReward] = "/api/Hall/ReceiveBattleOrderLevelReward";
dic_API[APIType.Hall_BuyBattleOrderLevel] = "/api/Hall/BuyBattleOrderLevel";
dic_API[APIType.Hall_BuyBattleOrderAdvance] = "/api/Hall/BuyBattleOrderAdvance";
// ---------------- ScratchCard ----------------
dic_API[APIType.Hall_ScratchCardDraw] = "/api/ScratchCard/draw";
// ---------------- 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_UpdateAvatarFrame] = "/api/User/UpdateAvatarFrame";
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";
// ---------------- Rank ----------------
dic_API[APIType.Rank_GetUserList] = "/api/User/GetUserList";
dic_API[APIType.Hall_GetRanking] = "/api/Hall/GetRanking";
dic_API[APIType.Hall_GetChessRoomPartitionCount] = "/api/Hall/GetChessRoomPartitionCount";
dic_API[APIType.Hall_SendBroadcast] = "/api/Hall/SendBroadcast";
dic_API[APIType.Room_Assign] = "/api/Room/Assign";
dic_API[APIType.Room_QuickJoin] = "/api/Room/QuickJoin";
dic_API[APIType.Hall_UseVipExp] = "/api/Hall/UseVipExp";
// ---------------- 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}";
dic_API[APIType.Customer_MyConversations] = "/customerapi/Customer/my-conversations";
// ---------------- 补充:暂未使用但已注册,方便后续接入 ----------------
dic_API[APIType.Login_LoginByPassword] = "/api/Login/LoginByPassword";
dic_API[APIType.User_GetUserAssets] = "/api/User/GetUserAssets";
dic_API[APIType.User_GetFishingReturnInfo] = "/api/User/GetFishingReturnInfo";
dic_API[APIType.User_ClaimFishingReturn] = "/api/User/ClaimFishingReturn";
dic_API[APIType.ActivityConfig_LotteryConfig] = "/api/ActivityConfig/lotteryConfig";
dic_API[APIType.Lottery_Config] = "/api/Lottery/Config";
dic_API[APIType.Lottery_Draw] = "/api/Lottery/Draw";
dic_API[APIType.Lottery_UserStatus] = "/api/Lottery/UserStatus";
dic_API[APIType.Lottery_DrawnPrizes] = "/api/Lottery/DrawnPrizes";
dic_API[APIType.SummonCard_Use] = "/api/SummonCard/use";
dic_API[APIType.Dict_GetByCode] = "/api/Dict/GetByCode";
dic_API[APIType.Dict_GetListByCode] = "/api/Dict/GetListByCode";
dic_API[APIType.Hall_GetBattleOrderLevelStatus] = "/api/Hall/GetBattleOrderLevelStatus";
}
// ---------------- 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)
{
#if UNITY_EDITOR
Debug.Log("url----" + url);
#endif
// 将 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);
#if UNITY_EDITOR
Debug.Log("url----" + url + "--" + "Json-" + json + "res--" + JsonConvert.SerializeObject(res));
#endif
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;
#if UNITY_EDITOR
Debug.Log("登录成功 Token" + token);
#endif
}
if (res.code == 0)
{
action?.Invoke(res.data);
}
else
{
ErrorHandle(res.code, res.info);
}
}
else
{
#if UNITY_EDITOR
Debug.LogError(req.error);
#endif
}
}
}
// ---------------- 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);
#if UNITY_EDITOR
Debug.Log("url----" + url + "--" + "Json-" + json + "res--" + JsonConvert.SerializeObject(res));
#else
Debug.Log("url----" + url + "--" + "Json-" + json + "res--" + JsonConvert.SerializeObject(res));
#endif
// 自动保存 token
if (typeof(T) == typeof(UserToken))
{
var ut = res.data as UserToken;
if (ut != null) token = ut.token;
#if UNITY_EDITOR
Debug.Log("登录成功 Token" + token);
#endif
}
if (res.code == 0)
{
action?.Invoke(res.data);
}
else
{
ErrorHandle(res.code, res.info);
}
}
else
{
#if UNITY_EDITOR
Debug.LogError(req.error);
#endif
}
}
}
// ---------------- 调用入口 ----------------
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
{
#if UNITY_EDITOR
Debug.LogError("RequestBase 不是 RequestParamBase 或 RequestObjBase");
#endif
}
}
/// <summary>
/// 无参
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="apiType"></param>
/// <param name="action"></param>
public static void Response<T>(APIType apiType, Action<T> action = null)
{
string url = baseURL + dic_API[apiType];
GetSend<T>(url, null, action);
}
public static void Log(string json)
{
#if UNITY_EDITOR
Debug.Log(json);
#endif
}
public static void ErrorHandle(int code, string message)
{
#if UNITY_EDITOR
Debug.LogError("错误代码:" + code + "错误信息:" + message);
#endif
//这里之后补弹窗
UIManager.Instance.ShowBoardError(message);
}
}
}