Files
5.24BuYu/Assets/MyGame/aa/UI/Home/Sub/UIHome_BY_ServicePanel.cs
T

117 lines
3.8 KiB
C#
Raw Normal View History

2026-04-14 18:30:34 +08:00
using Assets.MyGame.Scripts.Net;
2026-04-15 18:30:37 +08:00
using Assets.MyGame.Scripts.Net.APIRequest.Objs;
using Assets.MyGame.Scripts.Net.WebResponses;
using System;
2026-04-14 18:30:34 +08:00
using System.Collections;
2025-12-13 18:18:28 +08:00
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
2025-12-26 11:15:56 +08:00
public class UIHome_BY_ServicePanel : SubUIBase
2025-12-13 18:18:28 +08:00
{
2026-04-14 17:28:32 +08:00
public Toggle toggle_DefaultQuestion;
public Toggle toggle_YouChangeIChange;
public Toggle toggle_OnlineService;
2026-04-15 18:30:37 +08:00
[Header("客服")]
public TMP_InputField tMPInput_Text;
public GameObject dialogGreenPrefab;
public GameObject dialogOrangePrefab;
public Transform dialogPrefabParent;
public Button btn_SendText;
public string conversationId;
public void OnMessageReceived(CustomerMessageReceived customerMessageReceived)
{
if (customerMessageReceived.senderType==1)
{
if (dic_dialogItems.ContainsKey(customerMessageReceived.guid))
{
dic_dialogItems[customerMessageReceived.guid].waitMessage.SetActive(false);
//失败没有写
}
}
else
{
Instantiate(dialogOrangePrefab, dialogPrefabParent).GetComponent<DialogItem>().SetMessage(customerMessageReceived.content);
ScrollToBottom();
}
}
public void OnConversationStarted(CustomerConversationStartedResult customerConversationStartedResult)
{
conversationId = customerConversationStartedResult.conversation.id;
}
public void OnDestroy()
{
WebSocketSendList.Customer_LeaveConversation(conversationId);
WebSocketManager.Instance.Close(SocketType.customer);
}
2026-04-14 18:30:34 +08:00
public override void Awake()
2025-12-13 18:18:28 +08:00
{
2026-04-23 17:24:31 +08:00
base.Awake();
btn_Close.onClick.RemoveAllListeners();
2026-04-15 18:30:37 +08:00
btn_Close.onClick.AddListener(() =>
{
uIManager.Close(UIPanel.ServicePanel);
});
WebSocketManager.Instance.Connect(SocketType.customer, () =>
{
// 发起会话(服务端会回调 OnConversationStarted,自动 JoinConversation)
WebSocketSendList.Customer_StartConversation(
GameManager.Instance.user.id,
GameManager.Instance.user.BaseInfo.userName
);
// 连接后加入接单队列,服务端有新会话会回调 OnNewConversation(自动 JoinConversation)
WebSocketSendList.Customer_JoinCustomerQueue(conversationId);
});
2026-04-14 18:30:34 +08:00
toggle_DefaultQuestion.onValueChanged.AddListener((a) =>
{
if (a)
{
//APIRequestList.GetOnlineCustomers
}
});
2026-04-15 18:30:37 +08:00
btn_SendText.onClick.AddListener(() =>
{
var var = Instantiate(dialogGreenPrefab, dialogPrefabParent).GetComponent<DialogItem>();
var.SetMessage(tMPInput_Text.text);
ScrollToBottom();
string guid = Guid.NewGuid().ToString();
WebSocketSendList.Customer_SendMessage(conversationId, 1, GameManager.Instance.user.id, GameManager.Instance.user.BaseInfo.userName, null, 1, tMPInput_Text.text, guid);
dic_dialogItems.Add(guid, var);
});
}
Dictionary<string, DialogItem> dic_dialogItems = new Dictionary<string, DialogItem>();
public void ScrollToBottom()
{
StartCoroutine(ScrollBottomCoroutine());
2025-12-13 18:18:28 +08:00
}
2026-04-15 18:30:37 +08:00
IEnumerator ScrollBottomCoroutine()
{
// 等待所有布局刷新
yield return null;
yield return null; // 两帧最稳
yield return null; // 两帧最稳
dialogPrefabParent.parent.parent.GetComponent<ScrollRect>().verticalNormalizedPosition = 0;
// 再强制刷新一次,防止被覆盖
Canvas.ForceUpdateCanvases();
dialogPrefabParent.parent.parent.GetComponent<ScrollRect>().verticalNormalizedPosition = 0;
}
2025-12-13 18:18:28 +08:00
// Update is called once per frame
void Update()
{
2026-04-15 18:30:37 +08:00
2025-12-13 18:18:28 +08:00
}
}