Files
5.24BuYu/Assets/MyGame/aa/UI/Home/Sub/UIHome_BY_ServicePanel.cs
T
2026-04-23 17:24:31 +08:00

117 lines
3.8 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 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 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);
}
public override void Awake()
{
base.Awake();
btn_Close.onClick.RemoveAllListeners();
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);
});
toggle_DefaultQuestion.onValueChanged.AddListener((a) =>
{
if (a)
{
//APIRequestList.GetOnlineCustomers
}
});
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());
}
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;
}
// Update is called once per frame
void Update()
{
}
}