WX-Game1/Assets/Scripts/UserDataNetworkManager.cs

305 lines
9.9 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 UnityEngine;
using WXGame.Network;
using Cysharp.Threading.Tasks;
using System;
using System.Collections.Generic;
using MotionFramework;
namespace DefaultNamespace
{
/// <summary>
/// 用户数据网络管理类
/// </summary>
public class UserDataNetworkManager : ModuleSingleton<UserDataNetworkManager>, IModule
{
private UserInfoResponse _myInfo;
public UserInfoResponse MyInfo => _myInfo;
private MqttConfigRoot _mqttConfig;
public MqttConfigRoot MqttConfig=> _mqttConfig;
/// <summary>
/// 获取我的好友(异步方式)
/// </summary>
/// <returns>UniTask<FriendResponse></returns>
public async UniTask<FriendResponse> GetMyFriend()
{
Debug.Log("开始异步获取好友列表...");
try
{
// 构建POST请求参数urlencoded格式
string postData = $"page={1}";
Debug.Log(postData);
// 使用异步方式发送POST请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetMyFriend(),
signParams: new Dictionary<string, string>()
{
{ "page", "1" },
}
);
Debug.Log($"好友列表请求成功!状态码: {result.ResponseCode}");
Debug.Log($"响应内容: {result.ResponseText}");
FriendResponse friendResponse = JsonUtility.FromJson<FriendResponse>(result.ResponseText);
return friendResponse;
}
catch (Exception ex)
{
Debug.LogError($"获取好友列表失败: {ex.Message}");
throw;
}
}
/// <summary>
/// 获取我的用户信息(异步方式)
/// </summary>
/// <returns>UniTask<UserInfoResponse></returns>
public async UniTask<UserInfoResponse> GetMyInfo()
{
Debug.Log("开始异步获取用户信息...");
try
{
// 使用异步方式发送POST请求获取用户信息
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetMyInfo()
);
Debug.Log($"用户信息请求成功!状态码: {result.ResponseCode}");
Debug.Log($"响应内容: {result.ResponseText}");
// 解析JSON响应数据
UserInfoResponse userInfoResponse = JsonUtility.FromJson<UserInfoResponse>(result.ResponseText);
_myInfo = userInfoResponse;
// 验证响应数据
if (userInfoResponse == null)
{
throw new Exception("用户信息响应数据为空");
}
if (userInfoResponse.code != 0)
{
throw new Exception($"获取用户信息失败: {userInfoResponse.msg}");
}
if (userInfoResponse.data == null)
{
throw new Exception("用户信息数据为空");
}
Debug.Log($"用户信息解析成功 - 用户ID: {userInfoResponse.data.user_id}, 昵称: {userInfoResponse.data.user_username}");
return userInfoResponse;
}
catch (Exception ex)
{
Debug.LogError($"获取用户信息失败: {ex.Message}");
throw;
}
}
/// <summary>
/// 获取mqtt连接配置
/// </summary>
/// <returns></returns>
public async UniTask<MqttConfigRoot> GetMqttConfig()
{
Debug.Log("开始异步获取用户信息...");
try
{
// 使用异步方式发送POST请求获取用户信息
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetMqttConfig()
);
Debug.Log($"mqtt连接配置请求成功状态码: {result.ResponseCode}");
Debug.Log($"响应内容: {result.ResponseText}");
// 解析JSON响应数据
MqttConfigRoot mqttConfigRoot = JsonUtility.FromJson<MqttConfigRoot>(result.ResponseText);
_mqttConfig = mqttConfigRoot;
// 验证响应数据
if (mqttConfigRoot == null)
{
throw new Exception("mqtt连接配置数据为空");
}
if (mqttConfigRoot.code != 0)
{
throw new Exception($"mqtt连接配置失败: {mqttConfigRoot.msg}");
}
if (mqttConfigRoot.data == null)
{
throw new Exception("用户信息数据为空");
}
Debug.Log($"用户信息解析成功 - 用户ID: {mqttConfigRoot.data.host}, 昵称: {mqttConfigRoot.data.port}");
return mqttConfigRoot;
}
catch (Exception ex)
{
Debug.LogError($"获取用户信息失败: {ex.Message}");
throw;
}
}
/// <summary>
/// 获取历史数据
/// </summary>
/// <returns></returns>
public async UniTask<HistoryRoot> GetHistroy()
{
Debug.Log("开始异步获取历史消息...");
try
{
// 构建POST请求参数urlencoded格式
string postData = $"page={1}";
Debug.Log(postData);
// 使用异步方式发送POST请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetHistoty(),
signParams: new Dictionary<string, string>()
{
{ "page", "1" },
}
);
Debug.Log($"响应内容: {result.ResponseText}");
HistoryRoot friendResponse = JsonUtility.FromJson<HistoryRoot>(result.ResponseText);
return friendResponse;
}
catch (Exception ex)
{
Debug.LogError($"获取历史消息: {ex.Message}");
throw;
}
}
/// <summary>
/// 获取历史数据
/// </summary>
/// <param name="other_uid"></param>
/// <returns></returns>
public async UniTask<HistoryRoot> GetHistroy(string other_uid)
{
Debug.Log("开始异步获取历史消息...");
try
{
// 使用异步方式发送POST请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetHistoty(),
signParams: new Dictionary<string, string>()
{
{ "page", "1" },
{ "other_uid", other_uid },
}
);
Debug.Log($"响应内容: {result.ResponseText}");
HistoryRoot friendResponse = JsonUtility.FromJson<HistoryRoot>(result.ResponseText);
return friendResponse;
}
catch (Exception ex)
{
Debug.LogError($"获取历史消息: {ex.Message}");
throw;
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async UniTask<SendMessageRoot> SendMessage(string message)
{
Debug.Log("发送消息...");
try
{
// 使用异步方式发送POST请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetSendMessage(),
signParams: new Dictionary<string, string>()
{
{ "message", message },
}
);
Debug.Log($"响应内容: {result.ResponseText}");
SendMessageRoot sendMessageRoot = JsonUtility.FromJson<SendMessageRoot>(result.ResponseText);
return sendMessageRoot;
}
catch (Exception ex)
{
Debug.LogError($"获取历史消息: {ex.Message}");
throw;
}
}
/// <summary>
/// 发送消息(私人聊天)
/// </summary>
/// <param name="message"></param>
/// <param name="other_uid"></param>
/// <returns></returns>
public async UniTask<SendMessageRoot> SendMessage(string message, string other_uid)
{
Debug.Log("发送消息...");
try
{
// 使用异步方式发送POST请求
var result = await WebRequestManager.Instance.PostRequestAsync(
url: Apis.GetSendMessage(),
signParams: new Dictionary<string, string>()
{
{ "message", message },
{ "other_uid", other_uid },
}
);
Debug.Log($"响应内容: {result.ResponseText}");
SendMessageRoot sendMessageRoot = JsonUtility.FromJson<SendMessageRoot>(result.ResponseText);
return sendMessageRoot;
}
catch (Exception ex)
{
Debug.LogError($"获取历史消息: {ex.Message}");
throw;
}
}
public void OnCreate(object createParam)
{
}
public void OnUpdate()
{
}
public void OnDestroy()
{
}
public void OnGUI()
{
}
}
}