_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Login/11/UpdateUserInfo13.cs

327 lines
11 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 System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine.UI;
/*public class UpdateUserInfo13 : 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 UpdateUserInformation();//=====================================================================放在这里仅因为懒得写触发条件可以放在任何地方比如input.GetKeyDown.....必须改掉,吃服务器
//Debug.Log("接收到新的 GameEscapeId: " + escapeId);
}
// 加载初始数据,使用最新的 token
public async void LoadInitialData()
{
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法加载初始数据token 未设置。");
return;
}
await UpdateUserInformation();//=====================================================================放在此处只为解除一个黄色报错,看着闹心。但可以不要
}
// 加载游戏逃亡数据,使用最新的 escapeId 和 token
public async void selectQueryKill1()
{
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法加载游戏逃亡数据token 未设置。");
return;
}
if (escapeId == -1)
{
Debug.LogWarning("无法加载游戏逃亡数据escapeId 未设置。");
return;
}
// 查询击杀数据
await UpdateUserInformation();//==========================================================================================================================================================================
}
//===============================================================================================================================================================================================================================
// 更新用户信息
public async Task UpdateUserInformation()
{
var headers = CreateHeaders();
string body = @"
{
""nickName"": ""wulongxiao"",
""headImg"": ""https://fantasymonster-app.oss-cn-hangzhou.aliyuncs.com/upload/imgs/127e4e42d7c0405aab53359c1b278a9c.png"",
""gender"": 1,
""birthday"": ""2023-12-12 12:12:12""
}";
Debug.Log("正在更新用户信息...");
string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/update", "POST", body, headers);
Debug.Log("更新用户信息响应: " + 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 UpdateUserInfo13 : MonoBehaviour
{
// 单例模式实现
public static UpdateUserInfo13 Instance { get; private set; }
// 全局变量,用于存储服务器返回的具体字段
public string token = null; // 保存最新的 token初始为 null
public int escapeId = -1; // 保存最新的 escapeId初始为 -1 表示未设置
private string lastUpdateUserInfoResponse = null; // 保存最新的用户信息更新响应
// 服务器返回的字段,作为全局变量
public int code; // 状态码,例如 200 表示成功
public string message; // 提示信息
public bool data; // 用户信息更新结果true 表示成功
// 假设有一个手动更新用户信息的按钮
public Button updateUserInfoButton;
void Awake()
{
// 实现单例模式
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject); // 保证此实例在场景切换时不会被销毁
}
else
{
Destroy(gameObject);
}
}
void Start()
{
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated
selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated;
// 假设按钮被点击时调用 OnUpdateUserInfoButtonClicked
if (updateUserInfoButton != null)
{
updateUserInfoButton.onClick.AddListener(OnUpdateUserInfoButtonClicked);
}
}
// 当接收到 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 更新时自动调用更新用户信息
lastUpdateUserInfoResponse = await UpdateUserInformation();
Debug.Log("HandleGameEscapeIdUpdated 处理的用户信息更新响应: " + lastUpdateUserInfoResponse);
// 解析查询结果
ParseUpdateUserInfoResponse(lastUpdateUserInfoResponse);
}
// 加载初始数据,使用最新的 token
public async void LoadInitialData()
{
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法加载初始数据token 未设置。");
return;
}
// 调用更新用户信息方法
lastUpdateUserInfoResponse = await UpdateUserInformation();
Debug.Log("初始加载的用户信息更新响应: " + lastUpdateUserInfoResponse);
// 解析查询结果
ParseUpdateUserInfoResponse(lastUpdateUserInfoResponse);
}
// 按钮点击后触发用户信息更新操作
public async void OnUpdateUserInfoButtonClicked()
{
// 检查 token 是否已正确设置
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法更新用户信息token 未设置。");
return;
}
// 调用更新用户信息方法
lastUpdateUserInfoResponse = await UpdateUserInformation();
Debug.Log("用户按钮触发的用户信息更新响应: " + lastUpdateUserInfoResponse);
// 解析查询结果
ParseUpdateUserInfoResponse(lastUpdateUserInfoResponse);
// 根据响应做进一步的处理
HandleUpdateUserInfoResponse(lastUpdateUserInfoResponse);
}
// 更新用户信息
public async Task<string> UpdateUserInformation()
{
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法更新用户信息token 未设置。");
return null;
}
var headers = CreateHeaders();
string body = @"
{
""nickName"": ""wulongxiao"",
""headImg"": ""https://fantasymonster-app.oss-cn-hangzhou.aliyuncs.com/upload/imgs/127e4e42d7c0405aab53359c1b278a9c.png"",
""gender"": 1,
""birthday"": ""2023-12-12 12:12:12""
}";
Debug.Log("正在更新用户信息...");
string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/update", "POST", body, 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 }
};
}
// 处理 UpdateUserInformation 方法的响应
private void HandleUpdateUserInfoResponse(string response)
{
if (code == 200 && data)
{
Debug.Log("更新用户信息成功!");
}
else
{
Debug.LogWarning("更新用户信息失败,服务器响应: " + response);
}
}
// 解析用户信息更新响应,并保存到全局变量
private void ParseUpdateUserInfoResponse(string response)
{
if (string.IsNullOrEmpty(response))
{
Debug.LogWarning("无法解析用户信息更新响应,响应为空。");
return;
}
try
{
// 使用 Newtonsoft.Json 解析响应
UpdateUserInfoResponse updateResponse = JsonConvert.DeserializeObject<UpdateUserInfoResponse>(response);
// 将解析后的数据赋值给全局变量
code = updateResponse.code;
message = updateResponse.message;
data = updateResponse.data;
Debug.Log("解析后的用户信息更新结果: " +
"\n状态码: " + code +
"\n提示信息: " + message +
"\n更新成功: " + data);
}
catch (System.Exception e)
{
Debug.LogWarning("解析用户信息更新响应时出错: " + e.Message);
}
}
void OnDestroy()
{
// 注销监听事件,避免内存泄漏
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
// 取消按钮的点击监听
if (updateUserInfoButton != null)
{
updateUserInfoButton.onClick.RemoveListener(OnUpdateUserInfoButtonClicked);
}
}
}
// 用户信息更新响应数据类,用于存储解析后的响应数据
public class UpdateUserInfoResponse
{
public int code { get; set; } // 返回状态码,例如 200 表示成功
public string message { get; set; } // 提示信息
public bool data { get; set; } // 用户信息更新结果true 表示成功
}