Files
5.24BuYu/Assets/MyGame/Scripts/Net/APIRequestList_Customer.cs
T
2026-07-09 16:59:26 +08:00

170 lines
6.5 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.API;
using Assets.MyGame.Scripts.Net.APIRequest.Objs;
using Assets.MyGame.Scripts.Net.APIRequest.Params;
using Assets.MyGame.Scripts.Net.APIResponses.Customer_SubClass;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace Assets.MyGame.Scripts.Net
{
public static partial class APIRequestList
{
#region Customer
/// <summary>
/// 发起会话(POST)
/// 参数:userId、userName
/// 返回:ConversationInfo(会话信息)
/// </summary>
public static void StartConversation(StartConversationObj req, Action<ConversationInfo> callback)
{
APIManager.Response<ConversationInfo>(APIType.Customer_Start, req, callback);
}
/// <summary>
/// 发送消息(POST)
/// 参数:conversationId、senderType、senderId、senderName、senderAvatar、contentType、content
/// 返回:CustomerMessageInfo(消息信息)
/// </summary>
public static void SendMessage(SendMessageObj req, Action<CustomerMessageInfo> callback)
{
APIManager.Response<CustomerMessageInfo>(APIType.Customer_Message, req, callback);
}
/// <summary>
/// 获取消息列表(GET)
/// 参数:conversationId
/// 返回:List<CustomerMessageInfo>(消息列表)
/// </summary>
public static void GetMessages(string conversationId, Action<List<CustomerMessageInfo>> callback)
{
APIManager.Response<List<CustomerMessageInfo>>(APIType.Customer_Messages, new GetMessagesParam
{
conversationId = conversationId
}, callback);
}
/// <summary>
/// 获取会话列表(GET)
/// 参数:customerId(可选)
/// 返回:List<ConversationInfo>(会话列表)
/// </summary>
public static void GetConversations(Action<List<ConversationInfo>> callback)
{
APIManager.Response<List<ConversationInfo>>(APIType.Customer_Conversations, callback);
}
/// <summary>
/// 获取会话列表(GET,带客服ID筛选)
/// 参数:customerId
/// 返回:List<ConversationInfo>(会话列表)
/// </summary>
public static void GetConversations(string customerId, Action<List<ConversationInfo>> callback)
{
APIManager.Response<List<ConversationInfo>>(APIType.Customer_Conversations, new GetConversationsParam
{
customerId = customerId
}, callback);
}
/// <summary>
/// 结束会话(POST)
/// 参数:conversationId
/// 返回:string(结果提示)
/// </summary>
public static void EndConversation(EndConversationObj req, Action<JObject> callback)
{
APIManager.Response<JObject>(APIType.Customer_End, req, callback);
}
/// <summary>
/// 获取在线客服列表(GET)
/// 返回:List<CustomerOnlineInfo>(在线客服列表)
/// </summary>
public static void GetOnlineCustomers(Action<List<CustomerOnlineInfo>> callback)
{
APIManager.Response<List<CustomerOnlineInfo>>(APIType.Customer_Online, callback);
}
/// <summary>
/// 更新客服状态(POST)
/// 参数:userId、status(0:离线 1:在线)
/// 返回:string(结果提示)
/// </summary>
public static void UpdateCustomerStatus(UpdateCustomerStatusObj req, Action<JObject> callback)
{
APIManager.Response<JObject>(APIType.Customer_Status, req, callback);
}
/// <summary>
/// 获取聊天记录(GET)
/// 参数:conversationId
/// 返回:List<CustomerMessageInfo>(聊天记录列表)
/// </summary>
public static void GetChatLog(string conversationId, Action<List<CustomerMessageInfo>> callback)
{
APIManager.Response<List<CustomerMessageInfo>>(APIType.Customer_ChatLog, new GetMessagesParam
{
conversationId = conversationId
}, callback);
}
/// <summary>
/// 获取我的会话列表(含消息历史)
/// GET /customerapi/Customer/my-conversations
/// 注意:客服 API 使用 code=1 为成功(非标准 0),需独立处理
/// 返回:List<MyConversationInfo>
/// </summary>
public static void GetMyConversations(Action<List<MyConversationInfo>> callback)
{
UIManager.Instance.StartCoroutine(FetchMyConversations(callback));
}
private static System.Collections.IEnumerator FetchMyConversations(Action<List<MyConversationInfo>> callback)
{
string url = APIManager.baseURL + "/customerapi/Customer/my-conversations";
using (var req = UnityEngine.Networking.UnityWebRequest.Get(url))
{
req.SetRequestHeader("Content-Type", "application/json");
if (!string.IsNullOrEmpty(APIManager.token))
req.SetRequestHeader("token", APIManager.token);
yield return req.SendWebRequest();
if (req.result == UnityEngine.Networking.UnityWebRequest.Result.Success)
{
var json = req.downloadHandler.text;
// 客服 API 返回:{ code: 1, data: [...] },直接取 data
var jObj = Newtonsoft.Json.Linq.JObject.Parse(json);
if (jObj["data"] != null)
{
var list = jObj["data"].ToObject<List<MyConversationInfo>>();
callback?.Invoke(list);
}
else
{
callback?.Invoke(new List<MyConversationInfo>());
}
}
else
{
callback?.Invoke(new List<MyConversationInfo>());
}
}
}
/// <summary>
/// 上传客服图片(POST,multipart/form-data)
/// 参数:file(文件二进制)
/// 返回:string(图片URL)
/// </summary>
public static void UploadCustomerImage(string filePath, Action<JObject> callback)
{
APIManager.Response<JObject>(APIType.ImageUpload_Upload, callback);
}
#endregion
}
}