_xiaofang/xiaofang/Assets/Script/DirllInterface/getRoleList.cs
2024-12-04 15:26:46 +08:00

79 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class getRoleList : MonoBehaviour
{
// 创建请求头,使用最新的 token
public Dictionary<string, string> CreateHeaders()
{
if (string.IsNullOrEmpty(MyGlobal.global.loginResponse.data.access_token))
{
Debug.LogWarning("尝试创建请求头时token 未设置。");
return new Dictionary<string, string>();
}
return new Dictionary<string, string>
{
{ "Authorization","Bearer "+MyGlobal.global.loginResponse.data.access_token },
{"clientId", "e5cd7e4891bf95d1d19206ce24a7b32e" }
};
}
//角色列表
public async void queryRoleList(string secneId)
{
getRoleListBody getRoleListBody = new getRoleListBody();
getRoleListBody.sceneId = secneId;//==============================================================================
string response = await web.SendRequest(web.URL + "/game/role/list", "GET", JsonConvert.SerializeObject(getRoleListBody), 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 class getRoleListBody
{
public string sceneId;//=====必须
}
//====返回值解析数据类==========================================================================================
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" 字段对应
}