faqApi.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { apiUrl } 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. // 使用相对路径构建 URL,支持查询参数
  27. let url = apiUrl("/faqs");
  28. if (query) {
  29. url += `?query=${encodeURIComponent(query)}`;
  30. }
  31. const res = await fetch(url, {
  32. cache: "no-store",
  33. });
  34. if (!res.ok) {
  35. throw new Error("获取 FAQ 列表失败");
  36. }
  37. const data = await res.json();
  38. return data.faqs || [];
  39. }
  40. // 获取 FAQ 详情
  41. export async function fetchFAQ(id: number): Promise<FAQSummary> {
  42. const res = await fetch(apiUrl(`/faqs/${id}`), {
  43. cache: "no-store",
  44. });
  45. if (!res.ok) {
  46. if (res.status === 404) {
  47. throw new Error("FAQ 不存在");
  48. }
  49. throw new Error("获取 FAQ 详情失败");
  50. }
  51. return res.json();
  52. }
  53. // 创建 FAQ
  54. export async function createFAQ(data: CreateFAQRequest): Promise<FAQSummary> {
  55. const res = await fetch(apiUrl("/faqs"), {
  56. method: "POST",
  57. headers: { "Content-Type": "application/json" },
  58. body: JSON.stringify(data),
  59. });
  60. if (!res.ok) {
  61. const error = await res.json().catch(() => ({}));
  62. throw new Error(error.error || "创建 FAQ 失败");
  63. }
  64. return res.json();
  65. }
  66. // 更新 FAQ
  67. export async function updateFAQ(
  68. id: number,
  69. data: UpdateFAQRequest
  70. ): Promise<FAQSummary> {
  71. const res = await fetch(apiUrl(`/faqs/${id}`), {
  72. method: "PUT",
  73. headers: { "Content-Type": "application/json" },
  74. body: JSON.stringify(data),
  75. });
  76. if (!res.ok) {
  77. const error = await res.json().catch(() => ({}));
  78. if (res.status === 404) {
  79. throw new Error("FAQ 不存在");
  80. }
  81. throw new Error(error.error || "更新 FAQ 失败");
  82. }
  83. return res.json();
  84. }
  85. // 删除 FAQ
  86. export async function deleteFAQ(id: number): Promise<void> {
  87. const res = await fetch(apiUrl(`/faqs/${id}`), {
  88. method: "DELETE",
  89. });
  90. if (!res.ok) {
  91. const error = await res.json().catch(() => ({}));
  92. if (res.status === 404) {
  93. throw new Error("FAQ 不存在");
  94. }
  95. throw new Error(error.error || "删除 FAQ 失败");
  96. }
  97. }