18079408532 пре 1 година
родитељ
комит
56eae35ffb

+ 85 - 15
src/app/services/auth.service.ts

@@ -10,6 +10,10 @@ export class AuthService {
   currentUser$ = this.currentUserSubject.asObservable();
 
   constructor() {
+    if (!Parse.applicationId) {
+      Parse.initialize("dev", "dev");
+      Parse.CoreManager.set('SERVER_URL', 'http://dev.fmode.cn:1337/parse');
+    }
     this.checkCurrentUser();
   }
 
@@ -18,29 +22,95 @@ export class AuthService {
     this.currentUserSubject.next(currentUser);
   }
 
-  async login(username: string, password: string): Promise<Parse.User> {
-    const user = await Parse.User.logIn(username, password);
-    this.currentUserSubject.next(user);
-    return user;
+  async register(username: string, password: string, email: string): Promise<Parse.User> {
+    try {
+      if (!username?.trim()) {
+        throw new Error('用户名不能为空');
+      }
+      if (!password?.trim()) {
+        throw new Error('密码不能为空');
+      }
+      if (!email?.trim()) {
+        throw new Error('邮箱不能为空');
+      }
+
+      // 先检查用户名是否已存在
+      const query = new Parse.Query(Parse.User);
+      query.equalTo('username', username.trim());
+      const existingUser = await query.first();
+      
+      if (existingUser) {
+        throw new Error('用户名已存在');
+      }
+
+      // 创建新用户
+      const user = new Parse.User();
+      user.set('username', username.trim());
+      user.set('password', password.trim());
+      user.set('email', email.trim());
+
+      // 添加默认值,确保类型正确
+      user.set('avatar', 'https://app.fmode.cn/dev/jxnu/202226701019/头像示例.png');
+      user.set('realname', '');
+      user.set('gender', '');
+      user.set('age', 0);  // 设置为数字类型
+
+      console.log('Attempting to register user:', { 
+        username: username.trim(), 
+        email: email.trim() 
+      });
+
+      // 使用 signUp 而不是 save
+      const result = await user.signUp();
+
+      console.log('Registration successful:', result);
+      
+      this.currentUserSubject.next(result);
+      return result;
+    } catch (error: any) {
+      console.error('Registration error:', error);
+      if (error.code === 202) {
+        throw new Error('用户名已存在');
+      } else if (error.code === 203) {
+        throw new Error('邮箱已被使用');
+      } else {
+        throw new Error(error.message || '注册失败');
+      }
+    }
   }
 
-  async register(username: string, password: string, email: string): Promise<Parse.User> {
-    const user = new Parse.User();
-    user.set('username', username);
-    user.set('password', password);
-    user.set('email', email);
-    
-    await user.signUp();
-    this.currentUserSubject.next(user);
-    return user;
+  async login(username: string, password: string): Promise<Parse.User> {
+    try {
+      if (!username?.trim() || !password?.trim()) {
+        throw new Error('用户名和密码不能为空');
+      }
+
+      console.log('Attempting login:', { username: username.trim() });
+      
+      const user = await Parse.User.logIn(username.trim(), password.trim());
+      this.currentUserSubject.next(user);
+      return user;
+    } catch (error: any) {
+      console.error('Login error:', error);
+      throw new Error(error.message || '登录失败');
+    }
   }
 
   async logout() {
-    await Parse.User.logOut();
-    this.currentUserSubject.next(null);
+    try {
+      await Parse.User.logOut();
+      this.currentUserSubject.next(null);
+    } catch (error: any) {
+      console.error('Logout error:', error);
+      throw new Error(error.message || '登出失败');
+    }
   }
 
   isLoggedIn(): boolean {
     return Parse.User.current() !== null;
   }
+
+  getCurrentUser(): Parse.User | null {
+    return Parse.User.current() as Parse.User | null;
+  }
 }

+ 41 - 40
src/app/tab4/tab4.page.html

@@ -5,50 +5,51 @@
     </ion-title>
   </ion-toolbar>
 </ion-header>
+
 <ion-content [fullscreen]="true">
- 
   <!-- 用户登录状态 -->
   <ion-card>
     <!-- 未登录 -->
-     @if(!currentUser?.id){
-        <ion-content>
-          <ion-card class="login-card">
-              <ion-card-header>
-                  <ion-card-title>请登录</ion-card-title>
-                  <ion-card-subtitle>暂无信息</ion-card-subtitle>
-                  <ion-card-content>欢迎来到"生活智伴"!在这里,我们致力于成为您生活中的智慧伙伴。通过科学的生活规划和健康管理,让您的每一天都充满活力与平衡。我们提供个性化的生活建议,帮助您建立健康的生活方式,享受高质量的生活。让我们一起探索智慧生活的美好,开启健康快乐的人生旅程!登录后,您将获得更多贴心的生活建议和个性化服务。现在就加入我们,让生活更有智慧,让每一天都更加美好!</ion-card-content>
-              </ion-card-header>
-              <div class="image-container">
-                <img src="assets/images/lifepartner.jpg" alt="" class="responsive-image">
-            </div>
-          </ion-card>
-      </ion-content>
-      }
-        <!-- 已登录 -->
-     @if(currentUser?.id){
+    @if(!currentUser?.id){
+      <ion-card class="login-card">
+        <ion-card-header>
+          <ion-card-title>请登录</ion-card-title>
+          <ion-card-subtitle>暂无信息</ion-card-subtitle>
+          <ion-card-content>欢迎来到"生活智伴"!在这里,我们致力于成为您生活中的智慧伙伴。通过科学的生活规划和健康管理,让您的每一天都充满活力与平衡。我们提供个性化的生活建议,帮助您建立健康的生活方式,享受高质量的生活。让我们一起探索智慧生活的美好,开启健康快乐的人生旅程!登录后,您将获得更多贴心的生活建议和个性化服务。现在就加入我们,让生活更有智慧,让每一天都更加美好!</ion-card-content>
+        </ion-card-header>
+        <div class="image-container">
+          <img src="assets/images/lifepartner.jpg" alt="" class="responsive-image">
+        </div>
+      </ion-card>
+    }
+
+    <!-- 已登录 -->
+    @if(currentUser?.id){
       <ion-card-header class="card-header">
         <img [src]="currentUser?.get('avatar')" onerror="this.src='https://app.fmode.cn/dev/jxnu/202226701019/头像示例.png';" alt="图片加载失败" class="avatar" />
         <div class="user-info">
-            <ion-card-title>账号:{{currentUser?.get("username")}}</ion-card-title>
-            <ion-card-subtitle>
-                姓名: {{currentUser?.get("realname") || "-"}} 
-                性别: {{currentUser?.get("gender") || "-"}} 
-                年龄: {{currentUser?.get("age") || "-"}}
-            </ion-card-subtitle>
+          <ion-card-title>账号:{{currentUser?.get("username")}}</ion-card-title>
+          <ion-card-subtitle>
+            姓名: {{currentUser?.get("realname") || "-"}} 
+            性别: {{currentUser?.get("gender") || "-"}} 
+            年龄: {{currentUser?.get("age") || "-"}}
+          </ion-card-subtitle>
         </div>
-    </ion-card-header>
-      }
-      <ion-card-content>
+      </ion-card-header>
+    }
+
+    <ion-card-content>
       @if(!currentUser?.id){
         <ion-button expand="block" (click)="signup()" color="success">注册</ion-button>
         <ion-button expand="block" (click)="login()" color="success">登录</ion-button>
       }
-     @if(currentUser?.id){
-      <ion-button expand="block" (click)="editUser()" color="success">编辑资料</ion-button>
-      <ion-button expand="block" (click)="logout()" color="medium">登出</ion-button>
-    }
+      @if(currentUser?.id){
+        <ion-button expand="block" (click)="editUser()" color="success">编辑资料</ion-button>
+        <ion-button expand="block" (click)="logout()" color="medium">登出</ion-button>
+      }
     </ion-card-content>
   </ion-card>
+
   @if(currentUser?.id){
     <ion-card>
       <ion-card-header>
@@ -60,18 +61,18 @@
       </ion-card-content>
     </ion-card>
   }
+
   @if(currentUser?.id){
-  <ion-card class="memo-card">
-    <h2 class="memo-title">健康备忘录</h2>
-    <p class="memo-description">写下您问诊的医生名或者心动的科普知识,便于您下次查找(点击标签可删除)</p>
+    <ion-card class="memo-card">
+      <h2 class="memo-title">健康备忘录</h2>
+      <p class="memo-description">写下您问诊的医生名或者心动的科普知识,便于您下次查找(点击标签可删除)</p>
 
-    <h2 class="memo-title">收藏夹</h2>
-    <ul class="tag-list">
+      <h2 class="memo-title">收藏夹</h2>
+      <ul class="tag-list">
         @for(tag of editTags; track tag;){
-            <li class="tag-item">{{tag}}</li>
+          <li class="tag-item">{{tag}}</li>
         }
-    </ul>
-  </ion-card>
+      </ul>
+    </ion-card>
   }
-
-</ion-content>
+</ion-content>

+ 93 - 54
src/app/tab4/tab4.page.ts

@@ -1,13 +1,13 @@
-import { Component } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
+import { CommonModule } from '@angular/common';
+import { FormsModule } from '@angular/forms';
 import { IonHeader, IonToolbar, IonTitle, IonContent, IonCard, IonCardContent, 
          IonButton, IonCardHeader, IonCardTitle, IonCardSubtitle } from '@ionic/angular/standalone';
 import { ModalController, AlertController } from '@ionic/angular/standalone';
-
 import { Router } from '@angular/router';
 import { openUserLoginModal } from '../../lib/user/modal-user-login/modal-user-login.component';
 import { CloudUser } from '../../lib/ncloud';
 import { openUserEditModal } from '../../lib/user/modal-user-edit/modal-user-edit.component';
-import * as Parse from 'parse';
 import { AuthService } from '../services/auth.service';
 
 @Component({
@@ -16,79 +16,118 @@ import { AuthService } from '../services/auth.service';
   styleUrls: ['tab4.page.scss'],
   standalone: true,
   imports: [
-    IonHeader, IonToolbar, IonTitle, IonContent, 
-    IonCard, IonCardContent, IonButton, IonCardHeader, 
-    IonCardTitle, IonCardSubtitle,
-  ],
+    CommonModule,
+    FormsModule,
+    IonHeader,
+    IonToolbar,
+    IonTitle,
+    IonContent,
+    IonCard,
+    IonCardContent,
+    IonButton,
+    IonCardHeader,
+    IonCardTitle,
+    IonCardSubtitle
+  ]
 })
-export class Tab4Page {
-  goToCollection(){
-    console.log("goToCollection");
-  }
-
-  goToAvatar(){
-    console.log(['route'])
-    this.router.navigate(['/tabs/picture'])
-  }
+export class Tab4Page implements OnInit {
+  currentUser: CloudUser | undefined;
+  editTags: Array<String> = [];
 
-  currentUser:CloudUser|undefined
   constructor(
     private router: Router,
-    private modalCtrl:ModalController,
+    private modalCtrl: ModalController,
     private alertController: AlertController,
     private auth: AuthService
-  ) {
-    this.currentUser = new CloudUser();
+  ) {}
+
+  ngOnInit() {
+    // 监听认证状态变化
+    this.auth.currentUser$.subscribe(user => {
+      if (user) {
+        this.currentUser = user as unknown as CloudUser;
+      } else {
+        this.currentUser = undefined;
+      }
+    });
+  }
+
+  goToCollection() {
+    console.log("goToCollection");
+  }
+
+  goToAvatar() {
+    this.router.navigate(['/tabs/picture']);
   }
 
   async openLoginModal() {
-    const { data } = await openUserLoginModal(this.modalCtrl);
-    if (data?.role === 'confirm') {
-      const cloudUser = data.data as CloudUser;
-      if (cloudUser?.id) {
-        this.currentUser = cloudUser;
-        await this.auth.login(cloudUser.get('username'), cloudUser.get('password'));
+    try {
+      const { data } = await openUserLoginModal(this.modalCtrl);
+      if (data?.role === 'confirm') {
+        const { username, password } = data.data;
+        // 直接使用返回的用户名和密码登录
+        const user = await this.auth.login(username, password);
+        this.currentUser = user as unknown as CloudUser;
       }
+    } catch (error: any) {
+      console.error('Login error:', error);
+      const alert = await this.alertController.create({
+        header: '登录失败',
+        message: error.message || '请稍后重试',
+        buttons: ['确定']
+      });
+      await alert.present();
     }
   }
 
   async openSignupModal() {
-    const { data } = await openUserLoginModal(this.modalCtrl);
-    if (data?.role === 'confirm') {
-      const cloudUser = data.data as CloudUser;
-      if (cloudUser?.id) {
-        this.currentUser = cloudUser;
-        await this.auth.register(
-          cloudUser.get('username'), 
-          cloudUser.get('password'), 
-          cloudUser.get('email') || ''
-        );
+    try {
+      const { data } = await openUserLoginModal(this.modalCtrl);
+      if (data?.role === 'confirm') {
+        const { username, password, email } = data.data;
+        
+        if (!username || !password) {
+          throw new Error('用户名和密码不能为空');
+        }
+
+        // 直接使用返回的信息注册
+        const user = await this.auth.register(username, password, email || '');
+        this.currentUser = user as unknown as CloudUser;
       }
+    } catch (error: any) {
+      console.error('Registration error:', error);
+      const alert = await this.alertController.create({
+        header: '注册失败',
+        message: error.message || '请稍后重试',
+        buttons: ['确定']
+      });
+      await alert.present();
     }
   }
 
-  async handleUserAction() {
-    const { data } = await openUserLoginModal(this.modalCtrl);
-    if (!data?.data) {
-      return;
+  async logout() {
+    try {
+      await this.auth.logout();
+      this.currentUser?.logOut();
+      this.currentUser = undefined;
+    } catch (error: any) {
+      console.error('Logout error:', error);
+      const alert = await this.alertController.create({
+        header: '登出失败',
+        message: error.message || '请稍后重试',
+        buttons: ['确定']
+      });
+      await alert.present();
     }
-    this.currentUser = data.data as CloudUser;
-  }
-
-  logout(){
-    this.currentUser?.logOut();
-    this.auth.logout();
   }
 
-  editUser(){
-    openUserEditModal(this.modalCtrl)
+  editUser() {
+    openUserEditModal(this.modalCtrl);
   }
 
-  editTags:Array<String>=[]
-  async setTagsValue(ev:any){
+  async setTagsValue(ev: any) {
     let currentUser = new CloudUser();
-    let userPrompt = ``
-    if(!currentUser?.id){
+    if (!currentUser?.id) {
       console.log("用户未登录,请登录后重试");
       const { data } = await openUserLoginModal(this.modalCtrl);
       if (!data?.data) {
@@ -96,7 +135,7 @@ export class Tab4Page {
       }
       currentUser = data.data as CloudUser;
     }
-    this.editTags=ev;
+    this.editTags = ev;
   }
 
   login() {
@@ -106,4 +145,4 @@ export class Tab4Page {
   signup() {
     this.openSignupModal();
   }
-}
+}

+ 14 - 7
src/lib/user/modal-user-login/modal-user-login.component.ts

@@ -54,9 +54,13 @@ export class ModalUserLoginComponent {
         throw new Error('密码不能为空');
       }
 
-      const user = new CloudUser();
-      await user.logIn(this.username.trim(), this.password);
-      this.modalCtrl.dismiss({ data: user, role: 'confirm' });
+      await this.modalCtrl.dismiss({
+        data: {
+          username: this.username.trim(),
+          password: this.password
+        },
+        role: 'confirm'
+      });
     } catch (error: any) {
       await this.showAlert('登录失败', error.message || '请检查用户名和密码');
     }
@@ -74,11 +78,14 @@ export class ModalUserLoginComponent {
         throw new Error('邮箱不能为空');
       }
 
-      const user = new CloudUser();
-      await user.signUp(this.username.trim(), this.password, {
-        email: this.email.trim()
+      await this.modalCtrl.dismiss({
+        data: {
+          username: this.username.trim(),
+          password: this.password,
+          email: this.email.trim()
+        },
+        role: 'confirm'
       });
-      this.modalCtrl.dismiss({ data: user, role: 'confirm' });
     } catch (error: any) {
       await this.showAlert('注册失败', error.message || '注册失败,请稍后重试');
     }