473 lines
15 KiB
C#
473 lines
15 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System.Threading.Tasks;
|
||
using Newtonsoft.Json;
|
||
using UnityEngine.UI;
|
||
using Newtonsoft.Json.Linq;
|
||
/*public class selsecUserInfo : MonoBehaviour
|
||
{
|
||
public string token = null; // 保存最新的 token,初始为 null
|
||
public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置
|
||
|
||
void Start()
|
||
{
|
||
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
|
||
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
|
||
|
||
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated,但不执行其他方法
|
||
selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated;
|
||
}
|
||
|
||
// 当接收到 token 时触发此方法,仅保存 token
|
||
public void HandleTokenReceived(string receivedToken)
|
||
{
|
||
token = receivedToken; // 保存最新的 token
|
||
// 首次调用加载初始数据
|
||
LoadInitialData();
|
||
//LoadGameEscapeData();
|
||
//Debug.Log("接收到新的 token: " + token);
|
||
}
|
||
|
||
// 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId
|
||
public async void HandleGameEscapeIdUpdated(int newGameEscapeId)
|
||
{
|
||
escapeId = newGameEscapeId; // 保存最新的 escapeId
|
||
await selectUser();//=====================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改掉,吃服务器
|
||
//Debug.Log("接收到新的 GameEscapeId: " + escapeId);
|
||
}
|
||
|
||
|
||
// 加载初始数据,使用最新的 token
|
||
public async void LoadInitialData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载初始数据,token 未设置。");
|
||
return;
|
||
}
|
||
await selectUser();//=====================================================================放在此处只为解除一个黄色报错,看着闹心。但可以不要
|
||
}
|
||
|
||
// 加载游戏逃亡数据,使用最新的 escapeId 和 token
|
||
public async void selectQueryKill1()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
if (escapeId == -1)
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,escapeId 未设置。");
|
||
return;
|
||
}
|
||
|
||
await selectUser();//==========================================================================================================================================================================
|
||
|
||
}
|
||
|
||
//===============================================================================================================================================================================================================================
|
||
|
||
// 查询用户详情
|
||
public async Task<string> selectUser()
|
||
{
|
||
var headers = CreateHeaders();
|
||
Debug.Log("正在查询用户详细信息...");
|
||
string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/queryUserInfo", "POST", "{}", headers);
|
||
Debug.Log("查询用户详细信息响应: " + response);
|
||
return response;
|
||
}
|
||
//===============================================================================================================================================================================================================================
|
||
|
||
// 创建请求头,使用最新的 token
|
||
public Dictionary<string, string> CreateHeaders()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("尝试创建请求头时,token 未设置。");
|
||
return new Dictionary<string, string>();
|
||
}
|
||
|
||
return new Dictionary<string, string>
|
||
{
|
||
{ "Authorization", token }
|
||
};
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
// 注销监听事件,避免内存泄漏
|
||
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
|
||
selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
|
||
}
|
||
}*/
|
||
|
||
/*public class SelectUserInfo14 : MonoBehaviour
|
||
{
|
||
public string token = null; // 保存最新的 token,初始为 null
|
||
public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置
|
||
private string lastSelectUserInfoResponse = null; // 保存最新的用户详情查询响应
|
||
|
||
// 假设有一个手动查询用户详情的按钮
|
||
public Button selectUserInfoButton;
|
||
|
||
void Start()
|
||
{
|
||
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
|
||
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
|
||
|
||
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated
|
||
selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated;
|
||
|
||
// 假设按钮被点击时调用 OnSelectUserInfoButtonClicked
|
||
if (selectUserInfoButton != null)
|
||
{
|
||
selectUserInfoButton.onClick.AddListener(OnSelectUserInfoButtonClicked);
|
||
}
|
||
}
|
||
|
||
// 当接收到 token 时触发此方法,仅保存 token
|
||
public void HandleTokenReceived(string receivedToken)
|
||
{
|
||
token = receivedToken; // 保存最新的 token
|
||
Debug.Log("接收到新的 token: " + token);
|
||
|
||
// 首次调用加载初始数据
|
||
LoadInitialData();
|
||
}
|
||
|
||
// 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId
|
||
public async void HandleGameEscapeIdUpdated(int newGameEscapeId)
|
||
{
|
||
escapeId = newGameEscapeId; // 保存最新的 escapeId
|
||
Debug.Log("接收到新的 GameEscapeId: " + escapeId);
|
||
|
||
// 在接收到 escapeId 更新时自动调用查询用户详情
|
||
lastSelectUserInfoResponse = await SelectUser();
|
||
Debug.Log("HandleGameEscapeIdUpdated 处理的查询用户详情响应: " + lastSelectUserInfoResponse);
|
||
}
|
||
|
||
// 加载初始数据,使用最新的 token
|
||
public async void LoadInitialData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载初始数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
// 调用查询用户详情方法
|
||
lastSelectUserInfoResponse = await SelectUser();
|
||
Debug.Log("初始加载的查询用户详情响应: " + lastSelectUserInfoResponse);
|
||
}
|
||
|
||
// 按钮点击后触发用户详情查询操作
|
||
public async void OnSelectUserInfoButtonClicked()
|
||
{
|
||
// 检查 token 是否已正确设置
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法查询用户详情,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
// 调用查询用户详情方法
|
||
lastSelectUserInfoResponse = await SelectUser();
|
||
Debug.Log("用户按钮触发的查询用户详情响应: " + lastSelectUserInfoResponse);
|
||
|
||
// 根据响应做进一步的处理
|
||
HandleSelectUserResponse(lastSelectUserInfoResponse);
|
||
}
|
||
|
||
// 查询用户详情
|
||
public async Task<string> SelectUser()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法查询用户详情,token 未设置。");
|
||
return null;
|
||
}
|
||
|
||
var headers = CreateHeaders();
|
||
Debug.Log("正在查询用户详细信息...");
|
||
string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/queryUserInfo", "POST", "{}", headers);
|
||
Debug.Log("查询用户详细信息响应: " + response);
|
||
return response; // 返回响应
|
||
}
|
||
|
||
// 创建请求头,使用最新的 token
|
||
public Dictionary<string, string> CreateHeaders()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("尝试创建请求头时,token 未设置。");
|
||
return new Dictionary<string, string>();
|
||
}
|
||
|
||
return new Dictionary<string, string>
|
||
{
|
||
{ "Authorization", token }
|
||
};
|
||
}
|
||
|
||
// 处理 SelectUser 方法的响应
|
||
private void HandleSelectUserResponse(string response)
|
||
{
|
||
if (response.Contains("success"))
|
||
{
|
||
Debug.Log("查询用户详情成功!");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("查询用户详情失败,服务器响应: " + response);
|
||
}
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
// 注销监听事件,避免内存泄漏
|
||
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
|
||
selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
|
||
|
||
// 取消按钮的点击监听
|
||
if (selectUserInfoButton != null)
|
||
{
|
||
selectUserInfoButton.onClick.RemoveListener(OnSelectUserInfoButtonClicked);
|
||
}
|
||
}
|
||
}*/
|
||
|
||
|
||
//解析+单例
|
||
|
||
public class SelectUserInfo14 : MonoBehaviour
|
||
{
|
||
// 单例模式实现
|
||
public static SelectUserInfo14 Instance { get; private set; }
|
||
|
||
// 全局变量,用于存储服务器返回的具体字段
|
||
public string token = null; // 保存最新的 token,初始为 null
|
||
public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置
|
||
private string lastSelectUserInfoResponse = null; // 保存最新的用户详情查询响应
|
||
|
||
// 服务器返回的字段,作为全局变量
|
||
public int code; // 状态码,例如 200 表示成功
|
||
public string message; // 提示信息
|
||
public int userId; // 用户ID
|
||
public string userName; // 用户名
|
||
public string nickName; // 昵称
|
||
public string headImg; // 头像
|
||
public int gender; // 性别,1 表示男,2 表示女
|
||
public string birthday; // 生日
|
||
public int voluteCoin; // 蜗壳
|
||
public int beansCoin; // 蜗牛蛋
|
||
public int ichorCoin; // 灵液
|
||
public int cuteNo; // 靓号
|
||
|
||
// 假设有一个手动查询用户详情的按钮
|
||
public Button selectUserInfoButton;
|
||
|
||
void Awake()
|
||
{
|
||
// 实现单例模式
|
||
if (Instance == null)
|
||
{
|
||
Instance = this;
|
||
DontDestroyOnLoad(gameObject); // 保证此实例在场景切换时不会被销毁
|
||
}
|
||
else
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
|
||
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
|
||
|
||
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated
|
||
selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated;
|
||
|
||
// 假设按钮被点击时调用 OnSelectUserInfoButtonClicked
|
||
if (selectUserInfoButton != null)
|
||
{
|
||
selectUserInfoButton.onClick.AddListener(OnSelectUserInfoButtonClicked);
|
||
}
|
||
}
|
||
|
||
// 当接收到 token 时触发此方法,仅保存 token
|
||
public void HandleTokenReceived(string receivedToken)
|
||
{
|
||
token = receivedToken; // 保存最新的 token
|
||
Debug.Log("接收到新的 token: " + token);
|
||
|
||
// 首次调用加载初始数据
|
||
LoadInitialData();
|
||
}
|
||
|
||
// 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId
|
||
public async void HandleGameEscapeIdUpdated(int newGameEscapeId)
|
||
{
|
||
escapeId = newGameEscapeId; // 保存最新的 escapeId
|
||
Debug.Log("接收到新的 GameEscapeId: " + escapeId);
|
||
|
||
// 在接收到 escapeId 更新时自动调用查询用户详情
|
||
lastSelectUserInfoResponse = await SelectUser();
|
||
Debug.Log("HandleGameEscapeIdUpdated 处理的查询用户详情响应: " + lastSelectUserInfoResponse);
|
||
|
||
// 解析查询结果
|
||
ParseSelectUserInfoResponse(lastSelectUserInfoResponse);
|
||
}
|
||
|
||
// 加载初始数据,使用最新的 token
|
||
public async void LoadInitialData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载初始数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
// 调用查询用户详情方法
|
||
lastSelectUserInfoResponse = await SelectUser();
|
||
Debug.Log("初始加载的查询用户详情响应: " + lastSelectUserInfoResponse);
|
||
|
||
// 解析查询结果
|
||
ParseSelectUserInfoResponse(lastSelectUserInfoResponse);
|
||
}
|
||
|
||
// 按钮点击后触发用户详情查询操作
|
||
public async void OnSelectUserInfoButtonClicked()
|
||
{
|
||
// 检查 token 是否已正确设置
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法查询用户详情,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
// 调用查询用户详情方法
|
||
lastSelectUserInfoResponse = await SelectUser();
|
||
Debug.Log("用户按钮触发的查询用户详情响应: " + lastSelectUserInfoResponse);
|
||
|
||
// 解析查询结果
|
||
ParseSelectUserInfoResponse(lastSelectUserInfoResponse);
|
||
}
|
||
|
||
// 查询用户详情
|
||
public async Task<string> SelectUser()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法查询用户详情,token 未设置。");
|
||
return null;
|
||
}
|
||
|
||
var headers = CreateHeaders();
|
||
Debug.Log("正在查询用户详细信息...");
|
||
string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/queryUserInfo", "POST", "{}", headers);
|
||
Debug.Log("查询用户详细信息响应: " + response);
|
||
return response; // 返回响应
|
||
}
|
||
|
||
// 创建请求头,使用最新的 token
|
||
public Dictionary<string, string> CreateHeaders()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("尝试创建请求头时,token 未设置。");
|
||
return new Dictionary<string, string>();
|
||
}
|
||
|
||
return new Dictionary<string, string>
|
||
{
|
||
{ "Authorization", token }
|
||
};
|
||
}
|
||
|
||
// 处理 SelectUser 方法的响应,并解析成全局变量
|
||
private void ParseSelectUserInfoResponse(string response)
|
||
{
|
||
if (string.IsNullOrEmpty(response))
|
||
{
|
||
Debug.LogWarning("无法解析用户详情查询响应,响应为空。");
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
// 使用 Newtonsoft.Json 解析响应
|
||
SelectUserInfoResponse userInfoResponse = JsonConvert.DeserializeObject<SelectUserInfoResponse>(response);
|
||
|
||
// 将解析后的数据赋值给全局变量
|
||
code = userInfoResponse.code;
|
||
message = userInfoResponse.message;
|
||
|
||
if (userInfoResponse.data != null)
|
||
{
|
||
userId = userInfoResponse.data.userId;
|
||
userName = userInfoResponse.data.userName;
|
||
nickName = userInfoResponse.data.nickName;
|
||
headImg = userInfoResponse.data.headImg;
|
||
gender = userInfoResponse.data.gender;
|
||
birthday = userInfoResponse.data.birthday;
|
||
voluteCoin = userInfoResponse.data.voluteCoin;
|
||
beansCoin = userInfoResponse.data.beansCoin;
|
||
ichorCoin = userInfoResponse.data.ichorCoin;
|
||
cuteNo = userInfoResponse.data.cuteNo;
|
||
|
||
Debug.Log($"查询用户信息成功!用户 ID: {userId}, 用户名: {userName}, 昵称: {nickName}, 性别: {(gender == 1 ? "男" : "女")}, " +
|
||
$"蜗壳: {voluteCoin}, 蜗蛋: {beansCoin}, 灵液: {ichorCoin}, 靓号: {cuteNo}");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("数据对象为空,无法获取用户详细信息");
|
||
}
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
Debug.LogWarning("解析用户详情查询响应时出错: " + e.Message);
|
||
}
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
// 注销监听事件,避免内存泄漏
|
||
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
|
||
selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
|
||
|
||
// 取消按钮的点击监听
|
||
if (selectUserInfoButton != null)
|
||
{
|
||
selectUserInfoButton.onClick.RemoveListener(OnSelectUserInfoButtonClicked);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 用户详情查询响应数据类,用于存储解析后的响应数据
|
||
public class SelectUserInfoResponse
|
||
{
|
||
public int code { get; set; } // 返回状态码,例如 200 表示成功
|
||
public string message { get; set; } // 提示信息
|
||
public UserInfo data { get; set; } // 用户信息
|
||
}
|
||
|
||
// 用户信息数据类,用于存储用户详细信息
|
||
public class UserInfo
|
||
{
|
||
public int userId { get; set; } // 用户ID
|
||
public string userName { get; set; } // 用户名
|
||
public string nickName { get; set; } // 昵称
|
||
public string headImg { get; set; } // 头像
|
||
public int gender { get; set; } // 性别,1 表示男,2 表示女
|
||
public string birthday { get; set; } // 生日
|
||
public int voluteCoin { get; set; } // 蜗壳
|
||
public int beansCoin { get; set; } // 蜗牛蛋
|
||
public int ichorCoin { get; set; } // 灵液
|
||
public int cuteNo { get; set; } // 靓号
|
||
}
|