236 lines
7.7 KiB
C#
236 lines
7.7 KiB
C#
using Cysharp.Threading.Tasks;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using WXGame.Network;
|
||
|
||
namespace DefaultNamespace.Component
|
||
{
|
||
/// <summary>
|
||
/// 好友-聊天
|
||
/// </summary>
|
||
public class FriendChat : MonoBehaviour
|
||
{
|
||
//数据
|
||
/// <summary>
|
||
/// 好友数据
|
||
/// </summary>
|
||
public FriendData friendData;
|
||
/// <summary>
|
||
/// 自身数据
|
||
/// </summary>
|
||
public FriendData friendDataMy;
|
||
/// <summary>
|
||
/// 历史消息
|
||
/// </summary>
|
||
public HistoryRoot historyRoot;
|
||
|
||
//预制体
|
||
public GameObject friendBubblePrefab;
|
||
public GameObject myBubblePrefab;
|
||
public GameObject timePrefab;
|
||
|
||
public Transform contentMy;
|
||
//UI
|
||
/// <summary>
|
||
/// 关闭按钮
|
||
/// </summary>
|
||
public Button closeBt;
|
||
/// <summary>
|
||
/// 消息发送按钮
|
||
/// </summary>
|
||
public Button btnSend;
|
||
/// <summary>
|
||
/// 输入框
|
||
/// </summary>
|
||
public TMP_InputField inputField;
|
||
|
||
public List<GameObject> gameObjectList = new List<GameObject>();
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
closeBt.onClick.AddListener(delegate {
|
||
gameObjectList.ForEach(x=> {
|
||
x.gameObject.SetActive(false);
|
||
});
|
||
UIManager.Instance.HidePopup("好友聊天");
|
||
});
|
||
friendDataMy = new FriendData();
|
||
friendDataMy.friend_id = MqttManager.Instance.userInfo.user_id;
|
||
friendDataMy.user_username = MqttManager.Instance.userInfo.user_username;
|
||
friendDataMy.user_image = MqttManager.Instance.userInfo.user_image;
|
||
friendDataMy.user_honor_level = MqttManager.Instance.userInfo.user_honor_level;
|
||
//Invoke(nameof(Init), 0.5f);
|
||
btnSend.onClick.AddListener(delegate {
|
||
if (inputField.text != "")
|
||
{
|
||
SendMessageMy(inputField.text);
|
||
AddBubble(inputField.text, true);
|
||
}
|
||
inputField.text = "";
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化
|
||
/// </summary>
|
||
public void Init()
|
||
{
|
||
if (GetComponent<BackpackPage>() && GetComponent<BackpackPage>().strParam != "")
|
||
{
|
||
friendData= JsonUtility.FromJson<FriendData>(GetComponent<BackpackPage>().strParam);
|
||
if (friendData != null)
|
||
GetHistory();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取历史消息
|
||
/// </summary>
|
||
async void GetHistory()
|
||
{
|
||
try
|
||
{
|
||
Debug.Log("开始获取历史消息...");
|
||
|
||
// 使用UserDataNetworkManager获取用户信息
|
||
HistoryRoot response = await UserDataNetworkManager.Instance.GetHistroy(friendData.friend_id.ToString());
|
||
|
||
if (response != null && response.data != null)
|
||
{
|
||
historyRoot = response;
|
||
foreach (var item in historyRoot.data)
|
||
{
|
||
AddBubble(item.chat_content, item.chat_id != friendData.friend_id);
|
||
}
|
||
|
||
Debug.Log("获取历史消息获取成功!");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("获取历史消息为空");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"获取历史消息发生错误: {ex.Message}");
|
||
// 可以在这里添加错误处理逻辑,比如显示错误提示给用户
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送消息
|
||
/// </summary>
|
||
/// <param name="value"></param>
|
||
async void SendMessageMy(string value)
|
||
{
|
||
try
|
||
{
|
||
Debug.Log("发送消息...");
|
||
|
||
// 使用UserDataNetworkManager获取用户信息
|
||
|
||
SendMessageRoot response = await UserDataNetworkManager.Instance.SendMessage(value, friendData.friend_id.ToString());
|
||
|
||
// 根据code判断发送是否成功,code为0表示成功
|
||
if (response != null && response.code == 0)
|
||
{
|
||
Debug.Log($"发送消息成功!响应信息: {response.data}");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"发送消息失败!code: {response?.code}, msg: {response?.msg}");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"发送消息发生错误: {ex.Message}");
|
||
// 可以在这里添加错误处理逻辑,比如显示错误提示给用户
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加气泡
|
||
/// </summary>
|
||
/// <param name="content"></param>
|
||
/// <param name="isMy"></param>
|
||
public void AddBubble(string content, bool isMy)
|
||
{
|
||
GameObject newBubble = isMy ? Instantiate(myBubblePrefab, contentMy) : Instantiate(friendBubblePrefab, contentMy);
|
||
gameObjectList.Add(newBubble);
|
||
FriendChatBubble friendChatBubble = newBubble.GetComponent<FriendChatBubble>();
|
||
FriendData _friendData = new FriendData();
|
||
if (isMy)
|
||
{
|
||
_friendData = friendDataMy;
|
||
}
|
||
else
|
||
{
|
||
_friendData = friendData;
|
||
}
|
||
friendChatBubble.username.text = _friendData.user_username;
|
||
friendChatBubble.user_content.text = content;
|
||
DownloadAvatarAsync(_friendData.user_image, friendChatBubble.avatarImage);
|
||
}
|
||
|
||
private async UniTask DownloadAvatarAsync(string imageUrl, RawImage avatarImage)
|
||
{
|
||
try
|
||
{
|
||
// 检查URL是否有效
|
||
if (string.IsNullOrEmpty(imageUrl))
|
||
{
|
||
Debug.Log("头像URL为空,使用默认头像");
|
||
return;
|
||
}
|
||
|
||
Debug.Log($"开始下载头像: {imageUrl}");
|
||
|
||
// 使用WebRequestManager下载图片
|
||
Texture2D texture = await WebRequestManager.Instance.DownloadImageAsync(imageUrl);
|
||
|
||
if (texture != null)
|
||
{
|
||
// 设置头像
|
||
SetAvatarTexture(texture, avatarImage);
|
||
Debug.Log($"头像设置完成,尺寸: {texture.width}x{texture.height}");
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("头像下载失败,使用默认头像");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.Log($"下载头像时发生异常: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置头像纹理
|
||
/// </summary>
|
||
/// <param name="texture">要设置的纹理</param>
|
||
private void SetAvatarTexture(Texture2D texture, RawImage avatarImage)
|
||
{
|
||
try
|
||
{
|
||
if (avatarImage != null && texture != null)
|
||
{
|
||
avatarImage.texture = texture;
|
||
Debug.Log("头像纹理设置成功");
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("头像图片组件未设置或纹理为空");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.Log($"设置头像纹理时发生异常: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
} |