部分接口初版

This commit is contained in:
HuangZiBo 2024-11-25 09:45:47 +08:00
parent f3db37ae71
commit 14ed69769c
2 changed files with 429 additions and 19 deletions

View File

@ -1,3 +1,4 @@
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@ -5,17 +6,32 @@ using UnityEngine;
public class Global : Singleton<Global>
{
public static Global global;
public server response;
//// 创建请求头,使用最新的 token
//public Dictionary<string, string> CreateHeaders()
//{
// if (string.IsNullOrEmpty(response.data.access_token))
// {
// Debug.LogWarning("尝试创建请求头时token 未设置。");
// return new Dictionary<string, string>();
// }
// return new Dictionary<string, string>
// {
// { "Authorization", response.data.access_token }
// };
//}
}
public class server : Response
{
public Data data;
//public string access_token;
}
@ -31,5 +47,116 @@ public class Data
public string companyName;
public string client_id;
public int expire_in;//tolingÊ£Óàʱ¼ä
public string isCreater;//是否是模板创建者Y是则跳蓝湖预定演练01页面开始创建模板N否则跳蓝狐回放1页面
}
//====================================================
public class TemplateListData: Response
{
public string[] data;
}
public class newTemplateData: Response
{
public string data;
}
//=============================================================
// 根数据类
public class QuerySceneData
{
public int code { get; set; }
public string msg { get; set; }
public List<Scene> data { get; set; }
}
// 场景信息类
public class Scene
{
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
public string description { get; set; }
public int suitIndustry { get; set; }
public string price { get; set; }
public int companyId { get; set; }
public string status { get; set; }
public string delFlag { get; set; }
public string remark { get; set; }
public string gameName { get; set; }
public string gameType { get; set; }
public string ossId { get; set; }
public List<File> fileList { get; set; }
public string gameStoreroom { get; set; }
public string industryName { get; set; }
public string companyName { get; set; }
}
// 文件信息类
public class File
{
public string ossId { get; set; }
public string name { get; set; }
public string url { get; set; }
public string originalName { get; set; }
public string fileSuffix { get; set; }
}
//=================================================================
public class DrillSubject : Response
{
[JsonProperty("data")]
public List<QueryDrillSubjectData> data; // 确保字段名与 JSON 中的匹配
}
public class QueryDrillSubjectData
{
public string id;
public string[] sceneIds;
public string sceneNames;
public string name;
public string suitVersion;
public string type;
public string description;
public string price;
public string companyId;
public string status;
public string delFlag;
public string remark;
public string gameName;
public string ossId;
public string fileList;
}
//===============================================================
public class RoleList
{
public int code; // 与 JSON 中的 "code" 字段对应
public string msg; // 与 JSON 中的 "msg" 字段对应
public List<RoleData> data; // 与 JSON 中的 "data" 字段对应,包含角色的列表
}
public class RoleData
{
public string id; // 与 JSON 中的 "id" 字段对应
public string roleName; // 与 JSON 中的 "roleName" 字段对应
public string sceneId; // 与 JSON 中的 "sceneId" 字段对应
public int gameName; // 与 JSON 中的 "gameName" 字段对应,注意这里是 int 类型
public string roleAttributions; // 与 JSON 中的 "roleAttributions" 字段对应
}
//===================================================================
public class BindPlayer:Response
{
public string data;
}
//=============================================================
public class BindNPC : Response
{
public string data;//++++++++++++++++++++++++++++++++++++
}
//====================================================
public class BindMaterial : Response
{
public string data;//++++++++++++++++++++++++++++++++++++
}
//===================================================
public class SubmitTemplate : Response
{
public string data;//++++++++++++++++++++++++++++++++++++
}

View File

@ -2,40 +2,323 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Threading.Tasks;
using UnityEditor.PackageManager;
using JetBrains.Annotations;
using System.Data;
public class auth_login
{
public string clientId = "e5cd7e4891bf95d1d19206ce24a7b32e";
public string grantType = "password";
public string userType = "company_user";
public string username = "13699802230";
public string password = "YYL2230!";
}
//public class TemplateList//可空
//{
// public string templateId="";
// public string templateName;
// public string sceneId;
// public string subjectId;
// public string queryType;
//}
public class createTemplate
{
public string templateName;
public string sceneId;
public string subjectId;
public string isTemplate;
public string mode;
}
//============================================================================================
public class login : MonoBehaviour
{
public string token;//登录返回的token
public void Start()
public Global Global;
auth_login auth_Login = new auth_login();
public string clientId = "e5cd7e4891bf95d1d19206ce24a7b32e";
// 创建请求头,使用最新的 token
public Dictionary<string, string> CreateHeaders()
{
loging();
//createTemplate();
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("尝试创建请求头时token 未设置。");
return new Dictionary<string, string>();
}
return new Dictionary<string, string>
{
{ "Authorization","Bearer "+token },
{"clientId", clientId }
};
}
public async void loging()
//=================================================================================================
public async void Start()
{
await loging();
SubmitTemplate();
BindNPC();
BindMaterial();
BindPlayer();
QueryDrillSubject();
TemplateList();
createTemplate();
QueryScene();
queryRoleList();
}
//登录
public async Task loging()
{
auth_login auth_Login = new auth_login();
Debug.Log(JsonUtility.ToJson(auth_Login));
string response = await web.SendRequest(web.URL + "/auth/login", "POST", JsonUtility.ToJson(auth_Login));
Debug.Log(response);
Debug.Log("登录" + response);
// 解析服务器返回的数据
server serverData = JsonConvert.DeserializeObject<server>(response);
//token = serverData.token;
//Debug.Log(setverData.msg);
token = serverData.data.access_token;
Debug.Log("===========" + serverData.data.isCreater);
}
//获取模板列表
public async void TemplateList()
{
string response = await web.SendRequest(web.URL + "/game/gameTemplate/list", "GET", "", CreateHeaders());
Debug.Log("获取模板列表" + response);
//解析服务器返回的数据
TemplateListData templateListData = JsonConvert.DeserializeObject<TemplateListData>(response);
Debug.Log("================" + templateListData.msg);
}
//新建模板
public async void createTemplate()
{
string response = await web.SendRequest(web.URL + "/game/gameTemplate/list", "GET");
Debug.Log(response);
string body = "{\"templateName\": \"11111\",\"sceneId\": \"11111\",\"subjectId\": \"111\",\"isTemplate\": \"111\",\"mode\": \"111\",\"playerList\": [{\"userId\": \"111\",\"roleId\": \"1111\",\"birthAreaId\": \"111\",\"chargeAreaId\": \"111\"}],\"npcList\": [{\"npcId\": \"111\",\"areaId\": \"111\",\"npcNum\": 0}],\"materialList\": [{\"materialId\": \"111\",\"num\": 0}]}";
string response = await web.SendRequest(web.URL + "/game/gameTemplate/add", "POST", body, CreateHeaders());
Debug.Log("新建模板列表" + response);
// 解析服务器返回的数据
newTemplateData newTemplateData = JsonConvert.DeserializeObject<newTemplateData>(response);
Debug.Log(newTemplateData.data);
}
//查询场景信息列表
public async void QueryScene()
{
// 发送请求并获取返回数据
string response = await web.SendRequest(web.URL + "/game/scene/list", "GET", "", CreateHeaders());
Debug.Log("查询场景信息列表" + response);
// 使用Newtonsoft.Json来反序列化JSON数据到QuerySceneData对象
QuerySceneData serverData = JsonConvert.DeserializeObject<QuerySceneData>(response);
// 打印整体响应数据的某些属性
if (serverData != null && serverData.data != null)
{
foreach (var scene in serverData.data)
{
Debug.Log("场景ID: " + scene.id);
Debug.Log("场景名称: " + scene.name);
Debug.Log("描述: " + scene.description);
Debug.Log("价格: " + scene.price);
Debug.Log("行业: " + scene.industryName);
if (scene.fileList != null)
{
foreach (var file in scene.fileList)
{
Debug.Log("文件名称: " + file.name);
Debug.Log("文件URL: " + file.url);
}
}
}
}
else
{
Debug.Log("解析服务器数据失败");
}
}
//查询演练科目信息列表
public async void QueryDrillSubject()
{
string response = await web.SendRequest(web.URL + "/game/subject/list", "GET", "", CreateHeaders());
Debug.Log("查询演练科目信息列表: " + response);
// 使用 Newtonsoft.Json 进行反序列化
DrillSubject drillSubject = JsonConvert.DeserializeObject<DrillSubject>(response);
// 检查反序列化结果
if (drillSubject == null)
{
Debug.LogError("Failed to deserialize JSON. DrillSubject is null.");
return;
}
if (drillSubject.data == null || drillSubject.data.Count == 0)
{
Debug.LogWarning("No subjects found in the returned data.");
return;
}
// 遍历反序列化后的 data 列表
foreach (var subject in drillSubject.data)
{
Debug.Log("ID: " + subject.id);
Debug.Log("Scene Names: " + subject.sceneNames);
Debug.Log("Name: " + subject.name);
Debug.Log("Suit Version: " + subject.suitVersion);
Debug.Log("Type: " + subject.type);
Debug.Log("Description: " + subject.description);
Debug.Log("Price: " + subject.price);
Debug.Log("Company ID: " + subject.companyId);
Debug.Log("Status: " + subject.status);
Debug.Log("Delete Flag: " + subject.delFlag);
Debug.Log("Remark: " + subject.remark);
Debug.Log("Game Name: " + subject.gameName);
Debug.Log("OSS ID: " + subject.ossId);
Debug.Log("File List: " + subject.fileList);
// 如果需要进一步处理字段,可以在这里进行
// 比如,将价格转换为数字计算,或根据特定类型执行逻辑
}
}
//角色列表
public async void queryRoleList()
{
string body = "{\"sceneId\": \"1845704588547301377\"}";
string response = await web.SendRequest(web.URL + "/game/role/list", "GET", body, CreateHeaders());
Debug.Log("查询角色列表: " + response);
RoleList serverData = JsonConvert.DeserializeObject<RoleList>(response);
// 检查反序列化结果
if (serverData == null)
{
Debug.LogError("Failed to deserialize JSON. RoleList is null.");
return;
}
if (serverData.data == null || serverData.data.Count == 0)
{
Debug.LogWarning("No role data found in the returned data.");
return;
}
// 遍历反序列化后的角色列表
foreach (var role in serverData.data)
{
Debug.Log("Role ID: " + role.id);
Debug.Log("Role Name: " + role.roleName);
Debug.Log("Scene ID: " + role.sceneId);
Debug.Log("Game Name: " + role.gameName);
Debug.Log("Role Attributions: " + role.roleAttributions);
}
}
//模板玩家绑定
public async void BindPlayer()
{
string templateId = "1111";
string userId = "1";
string roleId = "1";
string birthAreaId = "11";
string chargeAreaId = "2";
string body = $@"
{{
""templateId"": ""{templateId}"",
""playerList"": [
{{
""userId"": ""{userId}"",
""roleId"": ""{roleId}"",
""birthAreaId"": ""{birthAreaId}"",
""chargeAreaId"": ""{chargeAreaId}""
}}
]
}}";
string response = await web.SendRequest(web.URL + "/game/gameTemplate/player", "POST", body, CreateHeaders());
Debug.Log("模板玩家绑定: " + response);
BindPlayer serverData = JsonConvert.DeserializeObject<BindPlayer>(response);
}
//模板NPC绑定
public async void BindNPC()
{
string templateId = "1111";
string npcId = "1";
string areaId = "1";
int npcNum = 1;
//string chargeAreaId = "2";
string body = $@"
{{
""templateId"": ""{templateId}"",
""npcList"": [
{{
""npcId"": ""{npcId}"",
""areaId"": ""{areaId}"",
""npcNum"": ""{npcNum}""
}}
]
}}";
string response = await web.SendRequest(web.URL + "/game/gameTemplate/npc", "POST", body, CreateHeaders());
Debug.Log("模板NPC绑定: " + response);
BindNPC serverData = JsonConvert.DeserializeObject<BindNPC>(response);
}
//模板设备绑定
public async void BindMaterial()
{
string templateId = "1111";
string materialId = "1";
int num = 0;
//string chargeAreaId = "2";
string body = $@"
{{
""templateId"": ""{templateId}"",
""materialList"": [
{{
""materialId"": ""{materialId}"",
""num"": ""{num}""
}}
]
}}";
string response = await web.SendRequest(web.URL + "/game/gameTemplate/material", "POST", body, CreateHeaders());
Debug.Log("模板设备绑定: " + response);
BindMaterial serverData = JsonConvert.DeserializeObject<BindMaterial>(response);
if (serverData.data == null)
{
Debug.Log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
Debug.Log(serverData.data);
}
//提交预定演练模板
public async void SubmitTemplate()
{
string templateId = "1111";
string reserveDate = "2024-11-22";
// int num = 0;
//string chargeAreaId = "2";
string body = $@"
{{
""templateId"": ""{templateId}"",
""reserveDate"":""{reserveDate}""
}}";
string response = await web.SendRequest(web.URL + "/game/gameRoom/reserve", "POST", body, CreateHeaders());
Debug.Log("提交预定演练模板: " + response);
BindMaterial serverData = JsonConvert.DeserializeObject<BindMaterial>(response);
if (serverData.data == null)
{
Debug.Log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
Debug.Log(serverData.data);
}
}