137 lines
4.3 KiB
C#
137 lines
4.3 KiB
C#
using Assets.MyGame.Scripts.Net;
|
||
using Assets.MyGame.Scripts.Net.APIRequest.Objs;
|
||
using Assets.MyGame.Scripts.Net.WebResponses;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class UIHome_BY_ServicePanel : SubUIBase
|
||
{
|
||
public Toggle toggle_DefaultQuestion;
|
||
public Toggle toggle_YouChangeIChange;
|
||
public Toggle toggle_OnlineService;
|
||
|
||
[Header("客服")]
|
||
public TMP_InputField tMPInput_Text;
|
||
public DialogItem dialogGreenPrefab; // 用户消息(showAvatar=true)
|
||
public DialogItem dialogOrangePrefab; // 客服消息(showAvatar=false)
|
||
public Transform dialogPrefabParent;
|
||
public Button btn_SendText;
|
||
public string conversationId;
|
||
|
||
/// <summary>guid → 用户发送的气泡(用于状态更新)</summary>
|
||
Dictionary<string, DialogItem> dic_dialogItems = new Dictionary<string, DialogItem>();
|
||
|
||
// ==================== WebSocket 回调 ====================
|
||
|
||
/// <summary>用户消息发送确认 / 客服新消息</summary>
|
||
public void OnMessageReceived(CustomerMessageReceived msg)
|
||
{
|
||
if (msg.senderType == 1)
|
||
{
|
||
// 用户自己的消息发送成功 → 隐藏等待图标
|
||
if (!string.IsNullOrEmpty(msg.guid) && dic_dialogItems.TryGetValue(msg.guid, out var item))
|
||
{
|
||
if (item.waitMessage != null)
|
||
item.waitMessage.SetActive(false);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 客服发来的新消息 → 创建橙色气泡,不显示头像
|
||
var orange = Instantiate(dialogOrangePrefab, dialogPrefabParent);
|
||
orange.showAvatar = false;
|
||
orange.SetMessage(msg.content);
|
||
ScrollToBottom();
|
||
}
|
||
}
|
||
|
||
/// <summary>发起会话回调</summary>
|
||
public void OnConversationStarted(CustomerConversationStartedResult result)
|
||
{
|
||
conversationId = result.conversation.id;
|
||
}
|
||
|
||
// ==================== 生命周期 ====================
|
||
|
||
public override void Awake()
|
||
{
|
||
base.Awake();
|
||
btn_Close.onClick.RemoveAllListeners();
|
||
btn_Close.onClick.AddListener(() => uIManager.Close(UIPanel.ServicePanel));
|
||
|
||
WebSocketManager.Instance.Connect(SocketType.customer, () =>
|
||
{
|
||
WebSocketSendList.Customer_StartConversation(
|
||
GameManager.Instance.user.id,
|
||
GameManager.Instance.user.BaseInfo.userName
|
||
);
|
||
WebSocketSendList.Customer_JoinCustomerQueue(conversationId);
|
||
});
|
||
|
||
toggle_DefaultQuestion.onValueChanged.AddListener((a) =>
|
||
{
|
||
if (a)
|
||
{
|
||
//APIRequestList.GetOnlineCustomers
|
||
}
|
||
});
|
||
|
||
btn_SendText.onClick.AddListener(SendMessage);
|
||
}
|
||
|
||
private void SendMessage()
|
||
{
|
||
if (string.IsNullOrEmpty(tMPInput_Text.text.Trim()))
|
||
{
|
||
UIManager.Instance.ShowBoardError("不能发送空白");
|
||
return;
|
||
}
|
||
|
||
var content = tMPInput_Text.text.Trim();
|
||
tMPInput_Text.text = "";
|
||
|
||
// 创建绿色气泡(用户消息,显示头像)
|
||
var green = Instantiate(dialogGreenPrefab, dialogPrefabParent);
|
||
green.showAvatar = true;
|
||
green.SetMessage(content);
|
||
ScrollToBottom();
|
||
|
||
string guid = Guid.NewGuid().ToString();
|
||
WebSocketSendList.Customer_SendMessage(
|
||
conversationId, 1,
|
||
GameManager.Instance.user.id,
|
||
GameManager.Instance.user.BaseInfo.userName,
|
||
null, 1, content, guid
|
||
);
|
||
dic_dialogItems[guid] = green;
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
WebSocketSendList.Customer_LeaveConversation(conversationId);
|
||
WebSocketManager.Instance.Close(SocketType.customer);
|
||
}
|
||
|
||
// ==================== 滚动 ====================
|
||
|
||
public void ScrollToBottom()
|
||
{
|
||
StartCoroutine(ScrollBottomCoroutine());
|
||
}
|
||
|
||
IEnumerator ScrollBottomCoroutine()
|
||
{
|
||
yield return null;
|
||
yield return null;
|
||
yield return null;
|
||
var scrollRect = dialogPrefabParent.parent.parent.GetComponent<ScrollRect>();
|
||
scrollRect.verticalNormalizedPosition = 0;
|
||
Canvas.ForceUpdateCanvases();
|
||
scrollRect.verticalNormalizedPosition = 0;
|
||
}
|
||
}
|