faqApi.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { API_BASE_URL } from "@/lib/config";
  2. // FAQ 摘要信息
  3. export interface FAQSummary {
  4. id: number;
  5. question: string; // 问题
  6. answer: string; // 答案
  7. keywords: string; // 关键词(用于搜索)
  8. created_at: string; // 创建时间
  9. updated_at: string; // 更新时间
  10. }
  11. // 创建 FAQ 请求
  12. export interface CreateFAQRequest {
  13. question: string; // 问题(必需)
  14. answer: string; // 答案(必需)
  15. keywords?: string; // 关键词(可选)
  16. }
  17. // 更新 FAQ 请求
  18. export interface UpdateFAQRequest {
  19. question?: string; // 问题(可选)
  20. answer?: string; // 答案(可选)
  21. keywords?: string; // 关键词(可选)
  22. }
  23. // 获取 FAQ 列表(支持关键词搜索)
  24. // query 格式:关键词之间用 % 分隔,例如 "openai%api%调用"
  25. export async function fetchFAQs(query?: string): Promise<FAQSummary[]> {
  26. const url = new URL(`${API_BASE_URL}/faqs`);
  27. if (query) {
  28. url.searchParams.set("query", query);
  29. }
  30. const res = await fetch(url.toString(), {
  31. cache: "no-store",
  32. });
  33. if (!res.ok) {
  34. throw new Error("获取 FAQ 列表失败");
  35. }
  36. const data = await res.json();
  37. return data.faqs || [];
  38. }
  39. // 获取 FAQ 详情
  40. export async function fetchFAQ(id: number): Promise<FAQSummary> {
  41. const res = await fetch(`${API_BASE_URL}/faqs/${id}`, {
  42. cache: "no-store",
  43. });
  44. if (!res.ok) {
  45. if (res.status === 404) {
  46. throw new Error("FAQ 不存在");
  47. }
  48. throw new Error("获取 FAQ 详情失败");
  49. }
  50. return res.json();
  51. }
  52. // 创建 FAQ
  53. export async function createFAQ(data: CreateFAQRequest): Promise<FAQSummary> {
  54. const res = await fetch(`${API_BASE_URL}/faqs`, {
  55. method: "POST",
  56. headers: { "Content-Type": "application/json" },
  57. body: JSON.stringify(data),
  58. });
  59. if (!res.ok) {
  60. const error = await res.json().catch(() => ({}));
  61. throw new Error(error.error || "创建 FAQ 失败");
  62. }
  63. return res.json();
  64. }
  65. // 更新 FAQ
  66. export async function updateFAQ(
  67. id: number,
  68. data: UpdateFAQRequest
  69. ): Promise<FAQSummary> {
  70. const res = await fetch(`${API_BASE_URL}/faqs/${id}`, {
  71. method: "PUT",
  72. headers: { "Content-Type": "application/json" },
  73. body: JSON.stringify(data),
  74. });
  75. if (!res.ok) {
  76. const error = await res.json().catch(() => ({}));
  77. if (res.status === 404) {
  78. throw new Error("FAQ 不存在");
  79. }
  80. throw new Error(error.error || "更新 FAQ 失败");
  81. }
  82. return res.json();
  83. }
  84. // 删除 FAQ
  85. export async function deleteFAQ(id: number): Promise<void> {
  86. const res = await fetch(`${API_BASE_URL}/faqs/${id}`, {
  87. method: "DELETE",
  88. });
  89. if (!res.ok) {
  90. const error = await res.json().catch(() => ({}));
  91. if (res.status === 404) {
  92. throw new Error("FAQ 不存在");
  93. }
  94. throw new Error(error.error || "删除 FAQ 失败");
  95. }
  96. }