userApi.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { apiUrl } from "@/lib/config";
  2. // 用户摘要信息(列表)
  3. export interface UserSummary {
  4. id: number;
  5. username: string;
  6. role: "admin" | "agent";
  7. nickname: string;
  8. email: string;
  9. avatar_url: string;
  10. receive_ai_conversations: boolean;
  11. created_at: string;
  12. updated_at: string;
  13. }
  14. // 创建用户请求
  15. export interface CreateUserRequest {
  16. username: string;
  17. password: string;
  18. role: "admin" | "agent";
  19. nickname?: string;
  20. email?: string;
  21. }
  22. // 更新用户请求
  23. export interface UpdateUserRequest {
  24. role?: "admin" | "agent";
  25. nickname?: string;
  26. email?: string;
  27. receive_ai_conversations?: boolean;
  28. }
  29. // 更新密码请求
  30. export interface UpdatePasswordRequest {
  31. old_password?: string; // 可选,管理员修改其他用户密码时不需要
  32. new_password: string;
  33. }
  34. // 获取所有用户列表
  35. export async function fetchUsers(
  36. currentUserId: number
  37. ): Promise<UserSummary[]> {
  38. const res = await fetch(
  39. `${apiUrl("/admin/users")}?current_user_id=${currentUserId}`,
  40. {
  41. cache: "no-store",
  42. }
  43. );
  44. if (!res.ok) {
  45. if (res.status === 403) {
  46. throw new Error("权限不足,只有管理员才能查看用户列表");
  47. }
  48. if (res.status === 401) {
  49. throw new Error("未提供当前用户ID");
  50. }
  51. throw new Error("获取用户列表失败");
  52. }
  53. const data = await res.json();
  54. if (!Array.isArray(data)) {
  55. return [];
  56. }
  57. return data;
  58. }
  59. // 获取用户详情
  60. export async function fetchUser(
  61. id: number,
  62. currentUserId: number
  63. ): Promise<UserSummary> {
  64. const res = await fetch(
  65. `${apiUrl(`/admin/users/${id}`)}?current_user_id=${currentUserId}`,
  66. {
  67. cache: "no-store",
  68. }
  69. );
  70. if (!res.ok) {
  71. if (res.status === 403) {
  72. throw new Error("权限不足,只有管理员才能查看用户详情");
  73. }
  74. if (res.status === 404) {
  75. throw new Error("用户不存在");
  76. }
  77. throw new Error("获取用户详情失败");
  78. }
  79. const data = await res.json();
  80. return data;
  81. }
  82. // 创建新用户
  83. export async function createUser(
  84. data: CreateUserRequest,
  85. currentUserId: number
  86. ): Promise<UserSummary> {
  87. const res = await fetch(
  88. `${apiUrl("/admin/users")}?current_user_id=${currentUserId}`,
  89. {
  90. method: "POST",
  91. headers: { "Content-Type": "application/json" },
  92. body: JSON.stringify(data),
  93. }
  94. );
  95. if (!res.ok) {
  96. const error = await res.json().catch(() => ({}));
  97. if (res.status === 403) {
  98. throw new Error("权限不足,只有管理员才能创建用户");
  99. }
  100. throw new Error(error.error || "创建用户失败");
  101. }
  102. const result = await res.json();
  103. return result.user;
  104. }
  105. // 更新用户信息
  106. export async function updateUser(
  107. id: number,
  108. data: UpdateUserRequest,
  109. currentUserId: number
  110. ): Promise<UserSummary> {
  111. const res = await fetch(
  112. `${apiUrl(`/admin/users/${id}`)}?current_user_id=${currentUserId}`,
  113. {
  114. method: "PUT",
  115. headers: { "Content-Type": "application/json" },
  116. body: JSON.stringify(data),
  117. }
  118. );
  119. if (!res.ok) {
  120. const error = await res.json().catch(() => ({}));
  121. if (res.status === 403) {
  122. throw new Error("权限不足,只有管理员才能更新用户信息");
  123. }
  124. if (res.status === 404) {
  125. throw new Error("用户不存在");
  126. }
  127. throw new Error(error.error || "更新用户失败");
  128. }
  129. const result = await res.json();
  130. return result.user;
  131. }
  132. // 删除用户
  133. export async function deleteUser(
  134. id: number,
  135. currentUserId: number
  136. ): Promise<void> {
  137. const res = await fetch(
  138. `${apiUrl(`/admin/users/${id}`)}?current_user_id=${currentUserId}`,
  139. {
  140. method: "DELETE",
  141. }
  142. );
  143. if (!res.ok) {
  144. const error = await res.json().catch(() => ({}));
  145. if (res.status === 403) {
  146. throw new Error("权限不足,只有管理员才能删除用户");
  147. }
  148. if (res.status === 404) {
  149. throw new Error("用户不存在");
  150. }
  151. throw new Error(error.error || "删除用户失败");
  152. }
  153. }
  154. // 更新用户密码
  155. export async function updateUserPassword(
  156. id: number,
  157. data: UpdatePasswordRequest,
  158. currentUserId: number
  159. ): Promise<void> {
  160. const res = await fetch(
  161. `${apiUrl(`/admin/users/${id}/password`)}?current_user_id=${currentUserId}`,
  162. {
  163. method: "PUT",
  164. headers: { "Content-Type": "application/json" },
  165. body: JSON.stringify(data),
  166. }
  167. );
  168. if (!res.ok) {
  169. const error = await res.json().catch(() => ({}));
  170. if (res.status === 403) {
  171. throw new Error("权限不足,只有管理员才能修改用户密码");
  172. }
  173. if (res.status === 404) {
  174. throw new Error("用户不存在");
  175. }
  176. throw new Error(error.error || "更新密码失败");
  177. }
  178. }