文件
构造方法
constructor(router: Router, alertController: AlertController, cdr: ChangeDetectorRef)
|
|
|
参数列表 :
| 名称 |
类型 |
可选的 |
| router |
Router
|
否
|
| alertController |
AlertController
|
否
|
| cdr |
ChangeDetectorRef
|
否
|
|
方法
|
cancelEditing
|
cancelEditing()
|
|
|
|
|
|
Async
ngOnInit
|
ngOnInit()
|
|
|
|
|
|
onFileSelected
|
onFileSelected(event: any)
|
|
|
|
|
|
Async
refreshPage
|
refreshPage()
|
|
|
|
|
|
refreshView
|
refreshView()
|
|
|
|
|
|
saveChanges
|
saveChanges()
|
|
|
|
|
|
startEditing
|
startEditing()
|
|
|
|
|
|
currentUser
|
类型 : Parse.User | undefined
|
|
|
|
editedUser
|
类型 : object
|
缺省值 : { ...this.user }
|
|
|
|
isLoggedIn
|
类型 : boolean
|
缺省值 : false
|
|
|
|
user
|
类型 : object
|
缺省值 : {
nickname: 'John Doe',
avatarUrl: 'assets/美津未.jpg',
campus: 'Main Campus',
building: 'A',
gender: 'male'
}
|
|
|
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AlertController } from '@ionic/angular';
import { ChangeDetectorRef } from '@angular/core';
// import { AuthService } from './AuthService';
import Parse from "parse"
@Component({
selector: 'app-tab3',
templateUrl: 'tab3.page.html',
styleUrls: ['tab3.page.scss']
})
export class Tab3Page {
isLoggedIn: boolean = false;
// isLoggedIn: boolean = false;
user = {
nickname: 'John Doe',
avatarUrl: 'assets/美津未.jpg',
campus: 'Main Campus',
building: 'A',
gender: 'male'
};
// isLoggedin: boolean = false;
editedUser = { ...this.user }; // Clone of user for editing
//editing = false;
constructor(
private router: Router,
private alertController: AlertController,private cdr: ChangeDetectorRef
) {
const navigation = this.router.getCurrentNavigation();
const state = navigation?.extras.state;
if (state && state['isLoggedIn']) {
this.isLoggedIn = true;
};
}
refreshView() {
// 执行变化检测并刷新视图
this.cdr.detectChanges();
};
currentUser:Parse.User|undefined
async ngOnInit() {
this.currentUser = await Parse.User.current();
setInterval(()=>{
this.refreshPage()
},1000)
if (this.currentUser) {
// 如果有当前用户信息,则更新user对象
this.user.nickname = this.currentUser.get("nickname");
this.user.gender=this.currentUser.get("gender");
// 根据你的数据库字段来更新其他用户信息
this.user.avatarUrl = this.currentUser.get("avatarUrl");
this.user.campus = this.currentUser.get("campus");
this.user.building = this.currentUser.get("building");
// this.user.gender = this.username.get("gender");
console.log(this.user.nickname);
//detectChanges();
}
}
// async ngOnInit() {
// this.username = await Parse.User.current()
// setInterval(async ()=>{
// this.username = await Parse.User.current()
// },1000)
async refreshPage() {
this.currentUser = await Parse.User.current();
if (this.currentUser) {
this.user.nickname = this.currentUser.get("nickname");
this.user.gender = this.currentUser.get("gender");
this.user.campus = this.currentUser.get("campus");
this.user.building = this.currentUser.get("building");
// detectChanges( );
// 可根据需要更新其他用户信息
}
}
onFileSelected(event: any) {
const file: File = event.target.files[0];
const reader = new FileReader();
reader.onload = () => {
this.editedUser.avatarUrl = reader.result as string;
};
reader.readAsDataURL(file);
this.refreshPage();
}
startEditing() {
//this.editing = true;
}
saveChanges() {
// Save changes to user object and exit editing mode
this.user = { ...this.editedUser };
//this.editing = false;
// Optionally, you can add logic here to save changes to backend
}
cancelEditing() {
// Exit editing mode without saving changes
this.editedUser = { ...this.user }; // Reset editedUser to original user
//this.editing = false;
}
// login() {
// // Handle login logic here, for example navigate to login page
// this.router.navigate(['/user/login']);
// }
logout() {
// Handle logout logic here
Parse.User.logOut();
this.refreshPage();
// detectChanges();
console.log("退出登入!");
}
}
<ion-header [translucent]="true">
</ion-header>
<ion-content>
<!-- 根据登录状态显示不同界面 -->
<div *ngIf="!currentUser?.id"> <!-- 如果没有登录显示未登录界面 -->
<ion-card>
<ion-card-header>
<ion-card-title>未登录</ion-card-title>
<ion-card-subtitle>请您登录后继续使用</ion-card-subtitle>
</ion-card-header>
<ion-button fill="clear" routerLink="/login">登录</ion-button>
</ion-card>
</div>
<div *ngIf="currentUser?.id"> <!-- 如果登录成功显示个人信息界面 -->
<div class="profile-header">
<div class="avatar-section">
<input type="file" (change)="onFileSelected($event)" accept="image/*" id="fileInput" hidden>
<label for="fileInput">
<img [src]="user.avatarUrl" alt="Avatar" class="avatar-img">
<ion-icon name="create-outline" class="edit-icon" routerLink="/edit-info"></ion-icon>
</label>
</div>
<div class="profile-details">
<div class="username">{{user.nickname}}</div>
<div class="campus-building">
<div class="campus">{{ user.campus }}</div>
<div class="building">{{ user.building }}</div>
</div>
<div class="gender">
<ion-icon [name]="user.gender === 'male' ? 'man' : 'woman'"></ion-icon>
<div>{{ user.gender === 'male' ? '男' : '女' }}</div>
</div>
</div>
</div>
</div>
<!-- 其他功能条目 -->
<ion-list lines="full" class="ion-no-margin" >
<ion-item routerLink="/mine/completed">
<ion-icon slot="start" name="checkmark-circle"></ion-icon>
<ion-label>已完成</ion-label>
<ion-icon slot="end" name="chevron-forward-outline"></ion-icon>
</ion-item>
<ion-item routerLink="/mine/in-progress">
<ion-icon slot="start" name="hourglass"></ion-icon>
<ion-label>进行中</ion-label>
<ion-icon slot="end" name="chevron-forward-outline"></ion-icon>
</ion-item>
<ion-item routerLink="/mine/not-completed">
<ion-icon slot="start" name="close-circle"></ion-icon>
<ion-label>未完成</ion-label>
<ion-icon slot="end" name="chevron-forward-outline"></ion-icon>
</ion-item>
<ion-item routerLink="/mine/pending">
<ion-icon slot="start" name="alert-circle"></ion-icon>
<ion-label>待接单</ion-label>
<ion-icon slot="end" name="chevron-forward-outline"></ion-icon>
</ion-item>
<ion-item (click)="logout()" class="logout-button">
<ion-icon slot="start" routerLink="/login" name="log-out"></ion-icon>
<ion-label>退出登录</ion-label>
</ion-item>
</ion-list>
</ion-content>
// src/app/mine/mine.page.scss
.profile-header {
display: flex;
align-items: center;
padding: 16px;
border-bottom: 1px solid #ccc;
}
body{
color:#efefef
}
.avatar-section {
margin-right: 16px;
position: relative;
}
.avatar-img {
width: 80px;
height: 80px;
border-radius: 50%;
}
.edit-icon {
position: absolute;
bottom: 0;
right: 0;
background-color: #fff;
border-radius: 50%;
padding: 4px;
cursor: pointer;
}
.profile-details {
flex: 1;
}
.username {
font-weight: bold;
color: #333;
font-size: 1.2em; /* Adjust font size as needed */
margin-bottom: 8px; /* Optional margin adjustment */
}
.campus-building {
display: flex;
align-items: center;
font-size: 0.9em; /* Adjust font size as needed */
}
.campus {
margin-right: 8px;
}
.gender {
display: flex;
align-items: center;
margin-top: 8px; /* Optional margin adjustment */
}
.gender ion-icon {
font-size: 1.2em;
margin-right: 8px;
}
.profile-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px;
}
.avatar-section {
position: relative;
cursor: pointer;
}
.avatar-img {
width: 100px;
height: 100px;
border-radius: 50%;
object-fit: cover;
}
.edit-icon {
position: absolute;
bottom: 0;
right: 0;
background-color: #fff;
border-radius: 50%;
padding: 5px;
cursor: pointer;
}
.profile-details {
flex: 1;
padding-left: 20px;
}
.profile-label {
font-weight: bold;
margin-top: 10px;
}
.avatar {
width: 100px;
height: 100px;
margin: 0 auto;
}
.profile-header h2 {
margin-top: 10px;
font-size: 1.5rem;
}
.profile-header p {
margin-top: 5px;
font-size: 1rem;
}
ion-button {
margin-top: 20px;
}
ion-list {
ion-item {
ion-label {
font-size: 1rem;
}
}
ion-item.logout-button {
--background: #fff;
--color: #f44336;
font-weight: bold;
}
}
.profile ion-input{
color:#323232;
font-size: 16px ;
font-weight: 200;
}
.avatar-right {
display: flex;
justify-content: flex-end;
}
.avatar-right img {
margin-right: 0; /* Adjust as needed */
}