conversationApi.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { API_BASE_URL } from "@/lib/config";
  2. import {
  3. ConversationDetail,
  4. ConversationSummary,
  5. } from "../types";
  6. export async function fetchConversations(userId?: number): Promise<ConversationSummary[]> {
  7. const url = userId
  8. ? `${API_BASE_URL}/conversations?user_id=${userId}`
  9. : `${API_BASE_URL}/conversations`;
  10. const res = await fetch(url, {
  11. cache: "no-store",
  12. });
  13. if (!res.ok) {
  14. throw new Error("获取对话列表失败");
  15. }
  16. const data = await res.json();
  17. if (!Array.isArray(data)) {
  18. return [];
  19. }
  20. return data.map((item) => ({
  21. ...item,
  22. unread_count: item.unread_count ?? 0,
  23. has_participated: item.has_participated ?? false,
  24. }));
  25. }
  26. export async function searchConversations(
  27. query: string,
  28. userId?: number
  29. ): Promise<ConversationSummary[]> {
  30. const url = userId
  31. ? `${API_BASE_URL}/conversations/search?q=${encodeURIComponent(query)}&user_id=${userId}`
  32. : `${API_BASE_URL}/conversations/search?q=${encodeURIComponent(query)}`;
  33. const res = await fetch(url, {
  34. cache: "no-store",
  35. });
  36. if (!res.ok) {
  37. throw new Error("搜索对话失败");
  38. }
  39. const data = await res.json();
  40. if (!Array.isArray(data)) {
  41. return [];
  42. }
  43. return data.map((item) => ({
  44. ...item,
  45. unread_count: item.unread_count ?? 0,
  46. has_participated: item.has_participated ?? false,
  47. }));
  48. }
  49. export async function fetchConversationDetail(
  50. conversationId: number
  51. ): Promise<ConversationDetail | null> {
  52. const res = await fetch(`${API_BASE_URL}/conversations/${conversationId}`, {
  53. cache: "no-store",
  54. });
  55. if (!res.ok) {
  56. return null;
  57. }
  58. const data = await res.json();
  59. return {
  60. ...data,
  61. unread_count: data.unread_count ?? 0,
  62. };
  63. }
  64. export interface UpdateConversationContactPayload {
  65. email?: string;
  66. phone?: string;
  67. notes?: string;
  68. }
  69. export interface UpdateConversationContactResult {
  70. email: string;
  71. phone: string;
  72. notes: string;
  73. }
  74. export async function updateConversationContact(
  75. conversationId: number,
  76. payload: UpdateConversationContactPayload
  77. ): Promise<UpdateConversationContactResult> {
  78. const res = await fetch(
  79. `${API_BASE_URL}/conversations/${conversationId}/contact`,
  80. {
  81. method: "PUT",
  82. headers: { "Content-Type": "application/json" },
  83. body: JSON.stringify(payload),
  84. }
  85. );
  86. if (!res.ok) {
  87. throw new Error("更新访客联系信息失败");
  88. }
  89. const data = await res.json();
  90. return {
  91. email: data.email ?? "",
  92. phone: data.phone ?? "",
  93. notes: data.notes ?? "",
  94. };
  95. }