user.service.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Injectable } from '@angular/core';
  2. import { BehaviorSubject, Observable, of } from 'rxjs';
  3. import { HttpClient } from '@angular/common/http';
  4. import { map } from 'rxjs/operators';
  5. interface User {
  6. username: string;
  7. phone: string;
  8. avatar: string;
  9. password: string;
  10. }
  11. @Injectable({
  12. providedIn: 'root'
  13. })
  14. export class UserService {
  15. private isLoggedInSubject = new BehaviorSubject<boolean>(false);
  16. private currentUserSubject = new BehaviorSubject<User | null>(null);
  17. isLoggedIn$ = this.isLoggedInSubject.asObservable();
  18. currentUser$ = this.currentUserSubject.asObservable();
  19. // 模拟用户数据存储
  20. private users: { [key: string]: User } = {};
  21. constructor(private http: HttpClient) {
  22. this.checkLoginStatus();
  23. }
  24. checkLoginStatus() {
  25. const token = localStorage.getItem('userToken');
  26. const userData = localStorage.getItem('userData');
  27. if (token && userData) {
  28. this.isLoggedInSubject.next(true);
  29. this.currentUserSubject.next(JSON.parse(userData));
  30. }
  31. }
  32. login(credentials: {phone: string, password: string}): Observable<any> {
  33. // 模拟验证逻辑
  34. const user = this.users[credentials.phone];
  35. if (user && user.password === credentials.password) {
  36. localStorage.setItem('userToken', 'dummy-token');
  37. localStorage.setItem('userData', JSON.stringify(user));
  38. this.isLoggedInSubject.next(true);
  39. this.currentUserSubject.next(user);
  40. return of({ success: true, user });
  41. }
  42. return of({ success: false });
  43. }
  44. register(userData: any): Observable<any> {
  45. if (this.users[userData.phone]) {
  46. return of({ success: false, message: '该手机号已被注册' });
  47. }
  48. const newUser: User = {
  49. username: userData.username,
  50. phone: userData.phone,
  51. password: userData.password,
  52. avatar: userData.avatar || 'assets/default-avatar.png'
  53. };
  54. this.users[userData.phone] = newUser;
  55. return of({ success: true, user: newUser });
  56. }
  57. logout() {
  58. localStorage.removeItem('userToken');
  59. this.isLoggedInSubject.next(false);
  60. }
  61. updateAvatar(imageData: string): Observable<{success: boolean}> {
  62. const currentUser = this.currentUserSubject.value;
  63. if (currentUser) {
  64. try {
  65. // 更新用户数据
  66. currentUser.avatar = imageData;
  67. this.users[currentUser.phone].avatar = imageData;
  68. // 保存到本地存储
  69. localStorage.setItem('userData', JSON.stringify(currentUser));
  70. // 更新当前用户状态
  71. this.currentUserSubject.next({...currentUser});
  72. return of({ success: true });
  73. } catch (error) {
  74. console.error('Error updating avatar:', error);
  75. return of({ success: false });
  76. }
  77. }
  78. return of({ success: false });
  79. }
  80. getCurrentUser(): Observable<any> {
  81. return this.currentUser$;
  82. }
  83. changePassword(oldPassword: string, newPassword: string): Observable<any> {
  84. const currentUser = this.currentUserSubject.value;
  85. if (currentUser && this.users[currentUser.phone].password === oldPassword) {
  86. this.users[currentUser.phone].password = newPassword;
  87. return of({ success: true });
  88. }
  89. return of({ success: false, message: '原密码错误' });
  90. }
  91. switchAccount(credentials: {phone: string, password: string}): Observable<any> {
  92. return this.login(credentials);
  93. }
  94. deleteAccount(): Observable<any> {
  95. const currentUser = this.currentUserSubject.value;
  96. if (currentUser) {
  97. delete this.users[currentUser.phone];
  98. return of({ success: true });
  99. }
  100. return of({ success: false });
  101. }
  102. }