emailNotificationApi.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { apiUrl, getAgentHeaders } from "@/lib/config";
  2. export interface EmailNotificationConfig {
  3. id?: number;
  4. enabled: boolean;
  5. smtp_host: string;
  6. smtp_port: number;
  7. smtp_user: string;
  8. smtp_password_masked?: string;
  9. from_email: string;
  10. from_name: string;
  11. offline_delay_seconds: number;
  12. effective_enabled: boolean;
  13. effective_delay_seconds: number;
  14. persisted_in_database: boolean;
  15. env_enabled: boolean;
  16. env_delay_seconds: number;
  17. updated_at?: string;
  18. }
  19. export interface UpdateEmailNotificationConfigRequest {
  20. enabled?: boolean;
  21. smtp_host?: string;
  22. smtp_port?: number;
  23. smtp_user?: string;
  24. smtp_password?: string;
  25. from_email?: string;
  26. from_name?: string;
  27. offline_delay_seconds?: number;
  28. }
  29. export async function fetchEmailNotificationConfig(
  30. userId: number
  31. ): Promise<EmailNotificationConfig> {
  32. const res = await fetch(
  33. `${apiUrl("/agent/email-notification-config")}?user_id=${userId}`,
  34. { cache: "no-store", headers: getAgentHeaders() }
  35. );
  36. if (!res.ok) {
  37. throw new Error("获取离线邮件配置失败");
  38. }
  39. return res.json();
  40. }
  41. export async function updateEmailNotificationConfig(
  42. userId: number,
  43. data: UpdateEmailNotificationConfigRequest
  44. ): Promise<EmailNotificationConfig> {
  45. const res = await fetch(apiUrl("/agent/email-notification-config"), {
  46. method: "PUT",
  47. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  48. body: JSON.stringify({ user_id: userId, ...data }),
  49. });
  50. if (!res.ok) {
  51. const err = await res.json();
  52. throw new Error(err.error || "更新离线邮件配置失败");
  53. }
  54. return res.json();
  55. }
  56. export async function resetEmailNotificationConfig(
  57. userId: number
  58. ): Promise<EmailNotificationConfig> {
  59. const res = await fetch(
  60. `${apiUrl("/agent/email-notification-config")}?user_id=${userId}`,
  61. { method: "DELETE", headers: getAgentHeaders() }
  62. );
  63. if (!res.ok) {
  64. const err = await res.json();
  65. throw new Error(err.error || "恢复离线邮件配置失败");
  66. }
  67. return res.json();
  68. }
  69. export async function sendEmailNotificationTest(
  70. userId: number,
  71. to: string
  72. ): Promise<void> {
  73. const res = await fetch(apiUrl("/agent/email-notification-config/test"), {
  74. method: "POST",
  75. headers: { "Content-Type": "application/json", ...getAgentHeaders() },
  76. body: JSON.stringify({ user_id: userId, to }),
  77. });
  78. if (!res.ok) {
  79. const err = await res.json();
  80. throw new Error(err.error || "发送测试邮件失败");
  81. }
  82. }