0210348 4 месяцев назад
Родитель
Сommit
a61b066dc2

+ 0 - 1
app/app.module.ts

@@ -10,7 +10,6 @@ import { FormsModule } from '@angular/forms';
 import { CalendarModule, DateAdapter } from 'angular-calendar';
 import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';
 
-
 @NgModule({
   declarations: [AppComponent],
   imports: [

+ 6 - 6
app/tab1/tab1.page.html

@@ -8,26 +8,26 @@
     </ion-toolbar>
   </ion-header>
     
-  <!-- 方框 -->
-  <ion-card (click)="openModal()">
+   <!-- 方框 -->
+   <ion-card (click)="openEditModal()">
     <ion-card-content>
       <ion-grid>
         <ion-row>
           <ion-col size="3">
             <div class="info-box">初始</div>
-            <div class="info-value">--KG</div>
+            <div class="info-value">{{ initial }}</div>
           </ion-col>
           <ion-col size="3">
             <div class="info-box">当前</div>
-            <div class="info-value">--KG</div>
+            <div class="info-value">{{ current }}</div>
           </ion-col>
           <ion-col size="3">
             <div class="info-box">目标</div>
-            <div class="info-value">--KG</div>
+            <div class="info-value">{{ target }}</div>
           </ion-col>
           <ion-col size="3">
             <div class="info-box">赏金</div>
-            <div class="info-value">--元</div>
+            <div class="info-value">{{ reward }}</div>
           </ion-col>
         </ion-row>
       </ion-grid>

+ 147 - 13
app/tab1/tab1.page.ts

@@ -1,32 +1,166 @@
-import { Component } from '@angular/core';
-import { AlertController } from '@ionic/angular';
+import { Component, OnInit,OnDestroy } from '@angular/core';
+import { AlertController, NavController } from '@ionic/angular';
 import { Router } from '@angular/router';
+import * as Parse from 'parse';
+import { interval, Subscription } from 'rxjs';
 
 @Component({
   selector: 'app-tab1',
   templateUrl: 'tab1.page.html',
   styleUrls: ['tab1.page.scss']
 })
-export class Tab1Page {
+export class Tab1Page implements OnInit,OnDestroy {
+  initial: string = '0KG';
+  current: string = '0KG';
+  target: string = '0KG';
+  reward: string = '0元';
+  username: string = ''; // 当前用户的用户名
+  blueColor: boolean = false;
+  private loadUserDataSubscription: Subscription | undefined;
+  
+  constructor(
+    private alertController: AlertController,
+    private router: Router,
+    private navCtrl: NavController,
+    
+  ) {}
 
-  constructor(private alertController: AlertController,private router: Router) {}
+  ngOnInit() {
+    this.loadUserData();
+    //每隔一秒检测登录状况
+    this.loadUserDataSubscription = interval(1000).subscribe(() => {
+      this.loadUserData();
+      console.log(this.username);
+    });
+  }
+
+  ngOnDestroy() {
+    if (this.loadUserDataSubscription) {
+      this.loadUserDataSubscription.unsubscribe();
+    }
+  }
+
+  async loadUserData() {
+    const currentUser = Parse.User.current();
+    if (currentUser) {
+      this.username = currentUser.getUsername()!;
+      this.loadData();
+    } else {
+      this.username = '未登录';
+      this.loadData();
+    }
+  }
+
+  async loadData() {
+    try {
+      const query = new Parse.Query('weight_status');
+      query.equalTo('username', this.username); // 查找当前用户的数据
+      const weightStatus = await query.first();
+      if (weightStatus) {
+        this.initial = weightStatus.get('initial') + 'KG' || '--KG';
+        this.current = weightStatus.get('current') + 'KG' || '--KG';
+        this.target = weightStatus.get('target') + 'KG' || '--KG';
+        this.reward = weightStatus.get('reward') + '元' || '--元';
+      }
+    } catch (error) {
+      console.error('Error loading data', error);
+    }
+  }
+
+  async openEditModal() {
+    if (this.username == '未登录') {
+      // 如果用户未登录,则显示登录提示
+      this.presentLoginAlert();
+      return;
+    }
 
-  async openModal() {
     const alert = await this.alertController.create({
-      header: '提示',
-      message: '弹窗了',
-      buttons: ['确定']
+      header: '编辑体重信息',
+      inputs: [
+        {
+          name: 'initial',
+          type: 'text',
+          placeholder: '初始',
+          value: this.initial.replace('KG', '')
+        },
+        {
+          name: 'current',
+          type: 'text',
+          placeholder: '当前',
+          value: this.current.replace('KG', '')
+        },
+        {
+          name: 'target',
+          type: 'text',
+          placeholder: '目标',
+          value: this.target.replace('KG', '')
+        },
+        {
+          name: 'reward',
+          type: 'text',
+          placeholder: '赏金',
+          value: this.reward.replace('元', '')
+        }
+      ],
+      buttons: [
+        {
+          text: '取消',
+          role: 'cancel'
+        },
+        {
+          text: '保存',
+          handler: async (data) => {
+            try {
+              const query = new Parse.Query('weight_status');
+              query.equalTo('username', this.username); // 查找当前用户的数据
+              let weightStatus = await query.first();
+              if (!weightStatus) {
+                weightStatus = new Parse.Object('weight_status');
+              }
+              weightStatus.set('username', this.username);
+              weightStatus.set('initial', data.initial);
+              weightStatus.set('current', data.current);
+              weightStatus.set('target', data.target);
+              weightStatus.set('reward', data.reward);
+              await weightStatus.save();
+              this.loadData(); // 更新本地数据
+            } catch (error) {
+              console.error('Error updating data', error);
+            }
+          }
+        }
+      ]
     });
+
     await alert.present();
   }
-  blueColor:boolean=false;
-  toggleButtonColor(habit:string){
-    this.blueColor=!this.blueColor;
+
+  async presentLoginAlert() {
+    const alert = await this.alertController.create({
+      header: '未登录',
+      message: '请先登录以继续操作',
+      buttons: [
+        {
+          text: '取消',
+          role: 'cancel'
+        },
+        {
+          text: '去登录',
+          handler: () => {
+            this.navCtrl.navigateForward('/user/login');
+          }
+        }
+      ]
+    });
+
+    await alert.present();
+  }
+
+  toggleButtonColor(habit: string) {
+    this.blueColor = !this.blueColor;
   }
 
-  //3d圆点击事件
   navigateToTreePage() {
     this.router.navigate(['/tabs/tree']);
   }
 }
-

+ 2 - 2
modules/user/login/login-routing.module.ts

@@ -1,13 +1,13 @@
 import { NgModule } from '@angular/core';
 import { Routes, RouterModule } from '@angular/router';
-
 import { LoginPage } from './login.page';
 
 const routes: Routes = [
   {
     path: '',
     component: LoginPage
-  }
+  },
+
 ];
 
 @NgModule({

+ 34 - 5
modules/user/login/login.page.ts

@@ -1,6 +1,6 @@
 import { Component, OnInit } from '@angular/core';
 import { AlertController, NavController } from '@ionic/angular';
-import * as Parse from "parse"
+import * as Parse from 'parse';
 
 @Component({
   selector: 'app-login',
@@ -9,8 +9,8 @@ import * as Parse from "parse"
 })
 export class LoginPage implements OnInit {
 
-  username: string = ""
-  password: string = ""
+  username: string = "";
+  password: string = "";
   
   constructor(
     private navCtrl: NavController,
@@ -23,6 +23,8 @@ export class LoginPage implements OnInit {
     let user;
     try {
       user = await Parse.User.logIn(this.username, this.password);
+      // 登录成功后立即加载用户数据
+      this.loadUserData();
     } catch (error: any) {
       let message: string = "";
       if (error?.message.indexOf("is required") > -1) {
@@ -39,7 +41,7 @@ export class LoginPage implements OnInit {
     }
     console.log(user);
     if (user?.id) {
-      this.navCtrl.navigateForward('/tabs/tab3'); // 修改这里
+      this.navCtrl.navigateForward('/tabs/tab3'); // 导航到主页或其他页面
     }
   }
 
@@ -51,7 +53,9 @@ export class LoginPage implements OnInit {
       let result = await user.signUp();
       console.log(result);
       if (result?.id) {
-        this.navCtrl.navigateForward('/tabs/tab3'); // 修改这里
+        // 注册成功后立即加载用户数据
+        this.loadUserData();
+        this.navCtrl.navigateForward('/tabs/tab3'); // 导航到主页或其他页面
       }
     } catch (error: any) {
       let message: string = "";
@@ -83,4 +87,29 @@ export class LoginPage implements OnInit {
   back() {
     this.navCtrl.back();
   }
+
+  async loadUserData() {
+    const currentUser = Parse.User.current();
+    if (currentUser) {
+      // 如果当前用户已登录,加载用户数据
+      console.log('当前用户已登录:', currentUser.getUsername());
+      // 这里可以调用你需要的加载用户数据的方法,比如触发检测数据的方法
+      // 示例:
+      // this.loadData();
+    }
+  }
+
+  async logout() {
+    try {
+      await Parse.User.logOut();
+      console.log('用户已登出');
+      // 清除本地存储的用户数据,如需要可以重置页面数据或状态
+      // 示例:
+      // this.username = '';
+      // this.password = '';
+      // this.loadData(); // 清除用户数据
+    } catch (error) {
+      console.error('登出失败:', error);
+    }
+  }
 }

+ 1 - 2
modules/user/user-routing.module.ts

@@ -4,9 +4,8 @@ import { RouterModule, Routes } from '@angular/router';
 const routes: Routes = [
     {path: 'login', loadChildren: () => import('./login/login.module').then(mod => mod.LoginPageModule)},
     {path: 'mine', loadChildren: () => import('./mine/mine.module').then(mod => mod.MinePageModule)},
-    {path: 'edit/info', loadChildren: () => import('./edit-info/edit-info.module').then(mod => mod.EditInfoPageModule)},
+    {path: 'edit/info', loadChildren: () => import('./edit-info/edit-info.module').then(mod => mod.EditInfoPageModule)}
 ];
-
 @NgModule({
   imports: [RouterModule.forChild(routes)],
   exports: [RouterModule]

+ 0 - 1
modules/user/user.module.ts

@@ -3,7 +3,6 @@ import { CommonModule } from '@angular/common';
 
 import { UserRoutingModule } from './user-routing.module';
 
-
 @NgModule({
   declarations: [],
   imports: [

+ 3 - 0
src/app/tab1/tab1.page.ts

@@ -6,6 +6,9 @@ import { AlertController } from '@ionic/angular';
   styleUrls: ['tab1.page.scss']
 })
 export class Tab1Page {
+  loadData() {
+    throw new Error('Method not implemented.');
+  }
 
   constructor(private alertController: AlertController) {}