userApi.ts 4.9 KB

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