Browse Source

feat: optimize & new edit-info page

ryanemax 8 months ago
parent
commit
b139b199c4

+ 56 - 0
README.md

@@ -0,0 +1,56 @@
+
+### 模块介绍
+- 模块项目:study-ng-user
+- 模块名称:user 
+- 模块类名:UserModule
+- 模块内容
+    - 页面 3个
+        - mine 我的页面
+        - login 注册页面
+        - edit-info 资料编辑页面
+### 安装依赖环境
+``` bash
+npm i -S parse
+npm i -S @types/parse
+```
+
+### 复制user模块
+- 将src/modules/user整个目录
+- 复制到自己项目src/modules/user目录处
+
+
+
+### 通过app根路由加载:用户模块
+- 文件:src\app\app-routing.module.ts
+- 新增路由内容
+``` ts
+const routes: Routes = [
+    // xxx
+    {
+        path: 'user',
+        loadChildren: () => import('../modules/user/user.module').then(m => m.UserModule)
+    }
+    // xxx
+]
+      
+```
+
+### 通过tabs路由加载:我的页面
+- 文件:src\app\tabs\tabs-routing.module.ts
+- 新增路由内容
+``` ts
+const routes: Routes = [
+  {
+    path: 'tabs',
+    component: TabsPage,
+    children: [
+        // xxxxxx
+        {
+            path: 'tab3',
+            loadChildren: () => import('../../modules/user/mine/mine.module').then(mod => mod.MinePageModule)
+        }
+        // xxxxxx
+    ]
+}
+      
+```

+ 36 - 2
src/modules/user/edit-info/edit-info.page.html

@@ -1,13 +1,47 @@
 <ion-header [translucent]="true">
   <ion-toolbar>
-    <ion-title>edit-info</ion-title>
+    <ion-title>资料编辑</ion-title>
   </ion-toolbar>
 </ion-header>
 
 <ion-content [fullscreen]="true">
   <ion-header collapse="condense">
     <ion-toolbar>
-      <ion-title size="large">edit-info</ion-title>
+      <ion-title size="large">资料编辑</ion-title>
     </ion-toolbar>
   </ion-header>
+
+  <ion-card>
+    <ion-card-header>
+      <ion-card-title>{{currentUser?.get('username')}} - {{currentUser?.id}}</ion-card-title>
+    </ion-card-header>
+    <ion-card-content>
+      <ion-list>
+        <ion-item>
+          <ion-input label="姓名" type="text" [(ngModel)]="userInfo.name"></ion-input>
+        </ion-item>
+        <ion-item>
+          <ion-input label="手机" type="tel" [(ngModel)]="userInfo.mobile"></ion-input>
+        </ion-item>
+        <ion-item>
+          <ion-select label="性别" [(ngModel)]="userInfo.gender">
+            <ion-select-option value="男">男</ion-select-option>
+            <ion-select-option value="女">女</ion-select-option>
+          </ion-select>
+        </ion-item>
+        <ion-item>
+          <ion-label>生日</ion-label>
+          <ion-datetime-button datetime="birthday"></ion-datetime-button>
+          <ion-modal [keepContentsMounted]="true">
+            <ng-template>
+              <ion-datetime id="birthday" displayFormat="MM/DD/YYYY" [(ngModel)]="userInfo.birthday"></ion-datetime>
+            </ng-template>
+          </ion-modal>
+        </ion-item>
+      </ion-list>
+    </ion-card-content>
+  </ion-card>
+
+  <ion-button expand="block" (click)="save()">保存</ion-button>
+  <ion-button expand="block" (click)="cancel()">取消</ion-button>
 </ion-content>

+ 42 - 1
src/modules/user/edit-info/edit-info.page.ts

@@ -1,4 +1,6 @@
 import { Component, OnInit } from '@angular/core';
+import { NavController } from '@ionic/angular';
+import * as Parse from 'parse';
 
 @Component({
   selector: 'app-edit-info',
@@ -7,9 +9,48 @@ import { Component, OnInit } from '@angular/core';
 })
 export class EditInfoPage implements OnInit {
 
-  constructor() { }
+  userInfo: any = {
+    name: '',
+    mobile: '',
+    gender: '',
+    birthday: ''
+  };
+  currentUser:Parse.User|undefined
+  constructor(private navController: NavController) {}
 
   ngOnInit() {
+    this.currentUser = Parse.User.current();
+    if (this.currentUser) {
+      // 修改uesrInfo赋值逻辑,仅加载被编辑的字段属性值
+      let json = this.currentUser.toJSON();
+      for (const key in json) {
+        if (this.userInfo.hasOwnProperty(key)) {
+          this.userInfo[key] = json[key]
+        }
+      }
+    }
+    console.log(this.userInfo)
+  }
+
+  save() {
+    this.currentUser = Parse.User.current();
+    if (this.currentUser) {
+      console.log(this.userInfo)
+      for (const key in this.userInfo) {
+        if (this.userInfo.hasOwnProperty(key)) {
+          this.currentUser.set(key, this.userInfo[key]);
+        }
+      }
+      this.currentUser.save().then(() => {
+        this.navController.back();
+      }).catch((error) => {
+        console.error('Error saving user data: ', error);
+      });
+    }
+  }
+
+  cancel() {
+    this.navController.back();
   }
 
 }

+ 3 - 1
src/modules/user/login/login.page.html

@@ -32,5 +32,7 @@
     <ion-button (click)="login()" fill="clear">登录</ion-button>
     <ion-button (click)="register()" fill="clear">注册</ion-button>
   </ion-card>
-  <ion-button expand="block" routerLink="/tabs/tab3">返回</ion-button>
+
+  <!-- 新增路由返回逻辑,执行back函数 -->
+  <ion-button expand="block" (click)="back()">返回</ion-button>
 </ion-content>

+ 19 - 6
src/modules/user/login/login.page.ts

@@ -1,7 +1,7 @@
 import { Component, OnInit } from '@angular/core';
-import { Router } from '@angular/router';
-import { AlertController } from '@ionic/angular';
+import { AlertController, NavController } from '@ionic/angular';
 import * as Parse from "parse"
+// 引用Router服务
 @Component({
   selector: 'app-login',
   templateUrl: './login.page.html',
@@ -11,7 +11,11 @@ export class LoginPage implements OnInit {
 
   username:string = ""
   password:string = ""
-  constructor(private router:Router,private alertController:AlertController) { }
+  constructor(
+    // 新增:Router服务,用于路由跳转
+    private navCtrl:NavController,
+    private alertController:AlertController
+  ) { }
 
   ngOnInit() {
   }
@@ -22,6 +26,7 @@ export class LoginPage implements OnInit {
       user = await Parse.User.logIn(this.username,this.password)
     } catch (error:any) {
       let message:string = ""
+      // 新增提示词详情,根据Parse.User.login方法返回的不同英文提示词,增加对应的中文内容转换
       if(error?.message.indexOf("is required")>-1){
         message = "必须输入账号或邮箱"
       }
@@ -36,7 +41,7 @@ export class LoginPage implements OnInit {
     }
     console.log(user)
     if(user?.id){
-      this.router.navigateByUrl("/tabs/tab3")
+      this.navCtrl.back()
     }
   }
   async register(){
@@ -47,11 +52,11 @@ export class LoginPage implements OnInit {
         let result = await user.signUp();
         console.log(result)
         if(result?.id){
-          this.router.navigateByUrl("/tabs/tab3")
+          this.navCtrl.back()
         }
         // Hooray! Let them use the app now.
     } catch (error:any) {
-        // Show the error message somewhere and let the user try again.
+        // 新增提示词详情,根据Parse.User.signUp方法返回的不同英文提示词,增加对应的中文内容转换
         let message:string = ""
         if(error?.message.indexOf("already exists")>-1){
           message = "该账号已存在请修改后重试"
@@ -77,4 +82,12 @@ export class LoginPage implements OnInit {
 
     await alert.present();
   }
+
+  /**
+   * 返回上级页面函数
+   * @desc
+   */
+  back(){
+    this.navCtrl.back()
+  }
 }

+ 4 - 4
src/modules/user/mine/mine.page.html

@@ -16,12 +16,12 @@
     <ion-card-header>
       <ion-card-title>{{user?.get("username") || '未登录'}}</ion-card-title>
       <ion-card-subtitle *ngIf="!user?.id">请您登陆后继续使用</ion-card-subtitle>
+      <ion-card-subtitle *ngIf="user?.id">{{user?.get("name")}}-{{user?.get("gender")}}</ion-card-subtitle>
     </ion-card-header>
-  
-    <!-- <ion-card-content>
-      Here's a small text description for the card content. Nothing more, nothing less.
-    </ion-card-content> -->
+ 
+    <!-- 新增:根据用户状态,显示登录/登出按钮,执行跳转或登出函数 -->
     <ion-button *ngIf="!user?.id" fill="clear" routerLink="/user/login">登录</ion-button>
+    <ion-button *ngIf="user?.id" fill="clear" routerLink="/user/edit/info">编辑资料</ion-button>
     <ion-button *ngIf="user?.id" fill="clear" (click)="logout()">登出</ion-button>
   </ion-card>
 

+ 3 - 2
src/modules/user/mine/mine.page.ts

@@ -1,4 +1,5 @@
 import { Component, OnInit } from '@angular/core';
+// 由于Parse本身是js库,在ts中加载需要通过 * as Parse转换一下
 import * as Parse from "parse"
 @Component({
   selector: 'app-mine',
@@ -7,12 +8,12 @@ import * as Parse from "parse"
 })
 export class MinePage implements OnInit {
 
-  // user:Parse.User|undefined = Parse.User.current()
+  // 由于Parse.User.current()是随着localStorage变化的属性
+  // 为了避免首次复制后用户状态变化,页面不同步,通过get方法实现实时获取
   get user():Parse.User|undefined{
     return Parse.User.current()
   }
   constructor() {
-
   }
 
   ngOnInit() {