Browse Source

0210329合并分支

未来全栈 5 months ago
parent
commit
42643e1b6e

+ 28 - 0
app/app.module.ts

@@ -0,0 +1,28 @@
+import { NgModule } from '@angular/core';
+import { BrowserModule } from '@angular/platform-browser';
+import { RouteReuseStrategy } from '@angular/router';
+
+import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
+
+import { AppRoutingModule } from './app-routing.module';
+import { AppComponent } from './app.component';
+import { FormsModule } from '@angular/forms';
+import { CalendarModule, DateAdapter } from 'angular-calendar';
+import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';
+
+@NgModule({
+  declarations: [AppComponent],
+  imports: [
+    BrowserModule,
+    IonicModule.forRoot(),
+    AppRoutingModule,
+    FormsModule,
+    CalendarModule.forRoot({
+      provide: DateAdapter,
+      useFactory: adapterFactory,
+    }),
+  ],
+  providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }],
+  bootstrap: [AppComponent],
+})
+export class AppModule {}

+ 107 - 0
app/tab1/tab1.page.html

@@ -0,0 +1,107 @@
+<ion-header [translucent]="true">
+</ion-header>
+
+<ion-content [fullscreen]="true">
+  <ion-header collapse="condense">
+    <ion-toolbar>
+      <ion-searchbar placeholder="搜索"></ion-searchbar>
+    </ion-toolbar>
+  </ion-header>
+    
+   <!-- 方框 -->
+   <ion-card (click)="openEditModal()">
+    <ion-card-content>
+      <ion-grid>
+        <ion-row>
+          <ion-col size="3">
+            <div class="info-box">初始</div>
+            <div class="info-value">{{ initial }}</div>
+          </ion-col>
+          <ion-col size="3">
+            <div class="info-box">当前</div>
+            <div class="info-value">{{ current }}</div>
+          </ion-col>
+          <ion-col size="3">
+            <div class="info-box">目标</div>
+            <div class="info-value">{{ target }}</div>
+          </ion-col>
+          <ion-col size="3">
+            <div class="info-box">赏金</div>
+            <div class="info-value">{{ reward }}</div>
+          </ion-col>
+        </ion-row>
+      </ion-grid>
+    </ion-card-content>
+  </ion-card>
+  
+<!-- 3D效果的圆 -->
+<div class="wave-container" (click)="navigateToTreePage()">
+  <div class="wave-circle">
+    <div class="wave">
+      <div class="wave-inner">
+        <span class="start-self-discipline">开始自律</span>
+      </div>
+    </div>
+  </div>
+</div>
+
+
+
+
+  <!-- 每日小习惯 -->
+  <div class="daily-habit">
+    <div class="daily-habit-line"></div>
+    <div class="daily-habit-text">每日小习惯</div>
+  </div>
+
+  
+   <!-- 大方框 -->
+  <ion-card>
+    <ion-card-content>
+      <ion-grid>
+        <ion-row>
+          <ion-col size="3">
+            <button class="habit-button" (click)="toggleButtonColor('运动')">
+              <ion-icon name="dumbbell"></ion-icon>
+              <div class="habit-label">运动</div>
+            </button>
+          </ion-col>
+          <ion-col size="3">
+            <ion-button shape="round" class="habit-button" (click)="toggleButtonColor('少吃')">
+              <ion-icon name="nutrition"></ion-icon>
+              <div>少吃</div>
+            </ion-button>
+          </ion-col>
+          <ion-col size="3">
+            <ion-button shape="round" class="habit-button" (click)="toggleButtonColor('喝水')">
+              <ion-icon name="water"></ion-icon>
+              <div>喝水</div>
+            </ion-button>
+          </ion-col>
+          <ion-col size="3">
+            <ion-button shape="round" class="habit-button" (click)="toggleButtonColor('吃蔬菜')">
+              <ion-icon name="leaf"></ion-icon>
+              <div>吃蔬菜</div>
+            </ion-button>
+          </ion-col>
+        </ion-row>
+        <ion-row>
+          <ion-col size="3">
+            <ion-button shape="round" class="habit-button" (click)="toggleButtonColor('水果')">
+              <ion-icon name="apple"></ion-icon>
+              <div>水果</div>
+            </ion-button>
+          </ion-col>
+          <ion-col size="3">
+            <ion-button shape="round" class="habit-button" (click)="toggleButtonColor('经常站立')">
+              <ion-icon name="walk"></ion-icon>
+              <div>经常站立</div>
+            </ion-button>
+          </ion-col>
+        </ion-row>
+      </ion-grid>
+    </ion-card-content>
+  </ion-card>
+</ion-content>
+  
+

+ 166 - 0
app/tab1/tab1.page.ts

@@ -0,0 +1,166 @@
+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 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);
+    });
+  }
+
+  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;
+    }
+
+    const alert = await this.alertController.create({
+      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();
+  }
+
+  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;
+  }
+
+  navigateToTreePage() {
+    this.router.navigate(['/tabs/tree']);
+  }
+}

+ 17 - 0
modules/user/login/login-routing.module.ts

@@ -0,0 +1,17 @@
+import { NgModule } from '@angular/core';
+import { Routes, RouterModule } from '@angular/router';
+import { LoginPage } from './login.page';
+
+const routes: Routes = [
+  {
+    path: '',
+    component: LoginPage
+  },
+
+];
+
+@NgModule({
+  imports: [RouterModule.forChild(routes)],
+  exports: [RouterModule],
+})
+export class LoginPageRoutingModule {}

+ 115 - 0
modules/user/login/login.page.ts

@@ -0,0 +1,115 @@
+import { Component, OnInit } from '@angular/core';
+import { AlertController, NavController } from '@ionic/angular';
+import * as Parse from 'parse';
+
+@Component({
+  selector: 'app-login',
+  templateUrl: './login.page.html',
+  styleUrls: ['./login.page.scss'],
+})
+export class LoginPage implements OnInit {
+
+  username: string = "";
+  password: string = "";
+  
+  constructor(
+    private navCtrl: NavController,
+    private alertController: AlertController
+  ) { }
+
+  ngOnInit() { }
+
+  async login() {
+    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) {
+        message = "必须输入账号或邮箱";
+      }
+      if (error?.message.indexOf("Invalid username") > -1) {
+        message = "账号或密码错误,请检查";
+      }
+      this.presentAlert({
+        header: "登录失败",
+        subHeader: "状态码:" + error.code,
+        message: message || error.message
+      });
+    }
+    console.log(user);
+    if (user?.id) {
+      this.navCtrl.navigateForward('/tabs/tab3'); // 导航到主页或其他页面
+    }
+  }
+
+  async register() {
+    let user = new Parse.User();
+    user.set("username", this.username);
+    user.set("password", this.password);
+    try {
+      let result = await user.signUp();
+      console.log(result);
+      if (result?.id) {
+        // 注册成功后立即加载用户数据
+        this.loadUserData();
+        this.navCtrl.navigateForward('/tabs/tab3'); // 导航到主页或其他页面
+      }
+    } catch (error: any) {
+      let message: string = "";
+      if (error?.message.indexOf("already exists") > -1) {
+        message = "该账号已存在请修改后重试";
+      }
+      if (error?.message.indexOf("empty") > -1) {
+        message = "账号不能为空请输入后重试";
+      }
+      this.presentAlert({
+        header: "注册失败",
+        subHeader: "状态码:" + error.code,
+        message: message || error.message
+      });
+    }
+  }
+
+  async presentAlert(options: { header: string, subHeader: string, message: string }) {
+    const alert = await this.alertController.create({
+      header: options?.header,
+      subHeader: options?.subHeader,
+      message: options?.message,
+      buttons: ['好的'],
+    });
+
+    await alert.present();
+  }
+
+  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);
+    }
+  }
+}

+ 13 - 0
modules/user/user-routing.module.ts

@@ -0,0 +1,13 @@
+import { NgModule } from '@angular/core';
+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)}
+];
+@NgModule({
+  imports: [RouterModule.forChild(routes)],
+  exports: [RouterModule]
+})
+export class UserRoutingModule { }

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

@@ -0,0 +1,13 @@
+import { NgModule } from '@angular/core';
+import { CommonModule } from '@angular/common';
+
+import { UserRoutingModule } from './user-routing.module';
+
+@NgModule({
+  declarations: [],
+  imports: [
+    CommonModule,
+    UserRoutingModule
+  ]
+})
+export class UserModule { }

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

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