浏览代码

0210331-同步更改

0210331 4 月之前
父节点
当前提交
d82e800125

+ 0 - 1
src/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
src/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>

+ 43 - 0
src/app/tab1/tab1.page.scss

@@ -75,6 +75,49 @@ ion-searchbar {
 } */
 
 
+// 每日小习惯
+.daily-habit {
+  display: flex;
+  align-items: center;
+  margin: 20px 0;
+}
+
+.daily-habit-line {
+  width: 5px;
+  height: 20px;
+  background-color: green;
+  margin-right: 10px;
+}
+
+.daily-habit-text {
+  font-size: 1.2em;
+  color: #666;
+}
+
+.habit-button {
+  width: 60px;
+  height: 60px;
+  display: flex;
+  justify-content: center;
+  align-items: center;
+  cursor: pointer;
+  transition: transform 0.3s ease-in-out;
+  color: #ffffff;
+  font-size: 1.2rem;
+  font-weight: bold;
+  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.5);
+}
+
+/* @keyframes wave {
+  0% {
+    transform: translateX(-50%) translateY(-50%) rotate(0deg);
+  }
+  100% {
+    transform: translateX(-50%) translateY(-50%) rotate(360deg);
+  }
+} */
+
+
 
 //每日习惯css
 .button-container {

+ 148 - 18
src/app/tab1/tab1.page.ts

@@ -1,35 +1,164 @@
-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,
+    
+  ) {}
+
+  ngOnInit() {
+    this.loadUserData();
+    //每隔一秒检测登录状况
+    this.loadUserDataSubscription = interval(1000).subscribe(() => {
+      this.loadUserData();
+      console.log(this.username);
+    });
+  }
 
-  constructor(private alertController: AlertController,private router: Router) {}
+  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;
-  }
 
-  //3d圆点击事件
-  navigateToTreePage() {
-    this.router.navigate(['/tabs/tree']);
-  };
+  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;
+  }
 
 
   //每日习惯按钮颜色变化
@@ -80,6 +209,7 @@ export class Tab1Page {
   }
 
 
-
+  navigateToTreePage() {
+    this.router.navigate(['/tabs/tree']);
+  }
 }
-

+ 1 - 0
src/app/tab2/tab2-routing.module.ts

@@ -2,6 +2,7 @@ import { NgModule } from '@angular/core';
 import { RouterModule, Routes } from '@angular/router';
 import { Tab2Page } from './tab2.page';
 
+
 const routes: Routes = [
   {
     path: '',

+ 36 - 33
src/app/tab2/tab2.page.html

@@ -1,44 +1,47 @@
 <ion-header>
   <ion-toolbar>
-    <ion-title>备忘录</ion-title>
+    <ion-title>每日记录</ion-title>
   </ion-toolbar>
 </ion-header>
 
 <ion-content>
-  <!-- 搜索框 -->
-  <ion-item>
-    <ion-label position="floating">搜索备忘录</ion-label>
-    <ion-input [(ngModel)]="searchQuery"></ion-input>
-  </ion-item>
+  <ion-segment [(ngModel)]="segment">
+    <ion-segment-button value="memo">
+      <ion-label>备忘录</ion-label>
+    </ion-segment-button>
+    <ion-segment-button value="chat">
+      <ion-label>AI对话</ion-label>
+    </ion-segment-button>
+  </ion-segment>
 
-  <!-- 备忘录输入 -->
-  <ion-item>
-    <ion-label position="floating">备忘录内容</ion-label>
-    <ion-input [(ngModel)]="memoContent"></ion-input>
-  </ion-item>
-  <ion-button expand="full" (click)="addMemo()">添加备忘录</ion-button>
+  <div *ngIf="segment === 'memo'">
+    <ion-list>
+      <ion-item *ngFor="let memo of memos">
+        {{ memo }}
+        <ion-button slot="end" color="danger" (click)="confirmDeleteMemo(memo)">
+          删除
+        </ion-button>
+      </ion-item>
+    </ion-list>
 
-  <!-- 备忘录列表 -->
-  <ion-list>
-    <ion-item *ngFor="let memo of filteredMemos()">
-      {{ memo }}
+    <ion-item>
+      <ion-input placeholder="添加新备忘录" [(ngModel)]="newMemo"></ion-input>
+      <ion-button (click)="addMemo()">添加</ion-button>
     </ion-item>
-  </ion-list>
+  </div>
 
-  <!-- 日历 -->
-  <!-- 日历 -->
-  <ion-card>
-    <ion-card-header>
-      <ion-card-title>日历</ion-card-title>
-    </ion-card-header>
-    <ion-card-content>
-      <div class="calendar-container">
-        <mwl-calendar-month-view
-          [viewDate]="viewDate"
-          [events]="events"
-          (dayClicked)="dayClicked($event.day)">
-        </mwl-calendar-month-view>
-      </div>
-    </ion-card-content>
-  </ion-card>
+  <div *ngIf="segment === 'chat'">
+    <ion-list>
+      <ion-item *ngFor="let message of messages">
+        <ion-label [ngClass]="{'ai': message.sender === 'ai', 'user': message.sender === 'user'}">
+          {{ message.text }}
+        </ion-label>
+      </ion-item>
+    </ion-list>
+
+    <ion-item>
+      <ion-input placeholder="输入消息" [(ngModel)]="newMessage"></ion-input>
+      <ion-button (click)="sendMessage()">发送</ion-button>
+    </ion-item>
+  </div>
 </ion-content>

+ 9 - 25
src/app/tab2/tab2.page.scss

@@ -1,25 +1,9 @@
-ion-card {
-      margin: 20px 0;
-    }
-    
-ion-item {
-      margin: 10px 0;
-    }
-    
-ion-button {
-      margin: 20px 0;
-    }
-.calendar-container {
-      display: flex;
-      justify-content: center;
-      align-items: center;
-      width: 100%;
-      overflow-x: auto;
-      padding: 20px 0;
-    }
-    
-::ng-deep mwl-calendar-month-view {
-      width: 100%;
-      max-width: 100%;
-    }
-    
+ion-label.ai {
+  color: blue;
+  text-align: left;
+}
+
+ion-label.user {
+  color: green;
+  text-align: right;
+}

+ 85 - 19
src/app/tab2/tab2.page.ts

@@ -1,32 +1,98 @@
-import { Component } from '@angular/core';
-import { CalendarEvent } from 'angular-calendar';
+import { Component,OnInit } from '@angular/core';
+import { AlertController } from '@ionic/angular';
 @Component({
   selector: 'app-tab2',
-  templateUrl: 'tab2.page.html',
-  styleUrls: ['tab2.page.scss']
+  templateUrl: './tab2.page.html',
+  styleUrls: ['./tab2.page.scss'],
 })
-
-export class Tab2Page {
-  memoContent: string = '';
-  searchQuery: string = '';
+export class Tab2Page implements OnInit {
+  segment: string = 'memo';
   memos: string[] = [];
-  viewDate: Date = new Date();
-  events: CalendarEvent[] = [];
+  newMemo: string = '';
+  messages: { sender: string, text: string }[] = [];
+  newMessage: string = '';
 
-  constructor() {}
+  constructor(private alertController: AlertController) {}
 
-  addMemo() {
-    if (this.memoContent.trim() !== '') {
-      this.memos.push(this.memoContent);
-      this.memoContent = '';
+  ngOnInit() {
+    this.loadMemos();
+  }
+  //加载备忘录
+  loadMemos() {
+    const memos = localStorage.getItem('memos');
+    if (memos) {
+      this.memos = JSON.parse(memos);
     }
   }
+  //保存备忘录
+  saveMemos() {
+    localStorage.setItem('memos', JSON.stringify(this.memos));
+  }
 
-  filteredMemos() {
-    return this.memos.filter((memo) => memo.includes(this.searchQuery));
+  //获取时间戳
+  getCurrentTimestamp(): string {
+    const now = new Date();
+    const year = now.getFullYear();
+    const month = (now.getMonth() + 1).toString().padStart(2, '0');
+    const day = now.getDate().toString().padStart(2, '0');
+    const hours = now.getHours().toString().padStart(2, '0');
+    const minutes = now.getMinutes().toString().padStart(2, '0');
+    return `${year}年${month}月${day}日${hours}:${minutes}`;
   }
 
-  dayClicked(day: any) {
-    console.log('Day clicked:', day);
+  //异步函数,添加备忘录
+  async addMemo() {
+    if (this.newMemo.trim().length > 0) {
+      this.memos.push(this.newMemo);
+      this.newMemo = '';
+      this.saveMemos();
+      const alert = await this.alertController.create({
+        header: '提示',
+        message: '添加成功',
+        buttons: ['确定']
+      });
+      await alert.present();
+    }
+  }
+  //异步函数,确认删除
+  async confirmDeleteMemo(memo: string) {
+    const alert = await this.alertController.create({
+      header: '确认删除',
+      message: `你确定要删除 "${memo}" 吗?`,
+      buttons: [
+        {
+          text: '取消',
+          role: 'cancel'
+        },
+        {
+          text: '删除',
+          handler: () => {
+            this.memos = this.memos.filter(m => m !== memo);
+            this.saveMemos();
+          }
+        }
+      ]
+    });
+    await alert.present();
   }
+
+  //删除备忘录
+  deleteMemo(memo: string) {
+    this.memos = this.memos.filter(m => m !== memo);
+    this.saveMemos();
+  }
+
+  //
+  sendMessage() {
+    if (this.newMessage.trim().length > 0) {
+      this.messages.push({ sender: 'user', text: this.newMessage });
+      this.newMessage = '';
+      
+      // 模拟AI回复
+      setTimeout(() => {
+        this.messages.push({ sender: 'ai', text: 'AI的回复' });
+      }, 1000);
+    }
+  }
+
 }

+ 1 - 0
src/app/tabs/tabs.page.html

@@ -19,5 +19,6 @@
       <ion-icon aria-hidden="true" name="calendar"></ion-icon>
       <ion-label>日历</ion-label>
     </ion-tab-button> -->
+   
   </ion-tab-bar>
 </ion-tabs>

+ 34 - 5
src/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);
+    }
+  }
 }