userService.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // src/lib/services/userService.ts
  2. import api from '@/lib/api';
  3. export interface UserListParams {
  4. page?: number;
  5. page_size?: number;
  6. size?: number;
  7. keyword?: string;
  8. [key: string]: any;
  9. }
  10. export const userService = {
  11. /**
  12. * 分页查询用户列表
  13. */
  14. async listAll(params?: UserListParams) {
  15. const res = await api.get('/api/user/list_all', { params });
  16. return res.data;
  17. },
  18. /**
  19. * 获取用户详情
  20. */
  21. async getDetail(userId: number) {
  22. try {
  23. const res = await api.get('/api/user/get_by_id', {
  24. params: { user_id: userId },
  25. });
  26. return res.data;
  27. } catch {
  28. const res = await api.get('/api/user/detail', {
  29. params: { user_id: userId },
  30. });
  31. return res.data;
  32. }
  33. },
  34. /**
  35. * 更新用户信息 / 权限
  36. */
  37. async update(userId: number | string, payload: Record<string, any>) {
  38. const res = await api.post('/api/user/update', payload, {
  39. params: { user_id: userId, uid: userId },
  40. });
  41. return res.data;
  42. },
  43. /**
  44. * 用户修改个人资料 (前台)
  45. */
  46. async setProfiles(payload: { phone?: string; nickname?: string; avatar_url?: string; [key: string]: any }) {
  47. const res = await api.post('/api/user/set_profiles', payload);
  48. return res.data;
  49. },
  50. /**
  51. * 上传文件 / 头像资源
  52. */
  53. async uploadFile(file: File) {
  54. const formData = new FormData();
  55. formData.append('file', file);
  56. const res = await api.post('/api/resource/upload_file', formData, {
  57. headers: { 'Content-Type': 'multipart/form-data' },
  58. });
  59. return res.data;
  60. },
  61. };