|
|
@@ -1,13 +1,13 @@
|
|
|
-import { Component } from '@angular/core';
|
|
|
+import { Component, OnInit } from '@angular/core';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import { FormsModule } from '@angular/forms';
|
|
|
import { IonHeader, IonToolbar, IonTitle, IonContent, IonCard, IonCardContent,
|
|
|
IonButton, IonCardHeader, IonCardTitle, IonCardSubtitle } from '@ionic/angular/standalone';
|
|
|
import { ModalController, AlertController } from '@ionic/angular/standalone';
|
|
|
-
|
|
|
import { Router } from '@angular/router';
|
|
|
import { openUserLoginModal } from '../../lib/user/modal-user-login/modal-user-login.component';
|
|
|
import { CloudUser } from '../../lib/ncloud';
|
|
|
import { openUserEditModal } from '../../lib/user/modal-user-edit/modal-user-edit.component';
|
|
|
-import * as Parse from 'parse';
|
|
|
import { AuthService } from '../services/auth.service';
|
|
|
|
|
|
@Component({
|
|
|
@@ -16,79 +16,118 @@ import { AuthService } from '../services/auth.service';
|
|
|
styleUrls: ['tab4.page.scss'],
|
|
|
standalone: true,
|
|
|
imports: [
|
|
|
- IonHeader, IonToolbar, IonTitle, IonContent,
|
|
|
- IonCard, IonCardContent, IonButton, IonCardHeader,
|
|
|
- IonCardTitle, IonCardSubtitle,
|
|
|
- ],
|
|
|
+ CommonModule,
|
|
|
+ FormsModule,
|
|
|
+ IonHeader,
|
|
|
+ IonToolbar,
|
|
|
+ IonTitle,
|
|
|
+ IonContent,
|
|
|
+ IonCard,
|
|
|
+ IonCardContent,
|
|
|
+ IonButton,
|
|
|
+ IonCardHeader,
|
|
|
+ IonCardTitle,
|
|
|
+ IonCardSubtitle
|
|
|
+ ]
|
|
|
})
|
|
|
-export class Tab4Page {
|
|
|
- goToCollection(){
|
|
|
- console.log("goToCollection");
|
|
|
- }
|
|
|
-
|
|
|
- goToAvatar(){
|
|
|
- console.log(['route'])
|
|
|
- this.router.navigate(['/tabs/picture'])
|
|
|
- }
|
|
|
+export class Tab4Page implements OnInit {
|
|
|
+ currentUser: CloudUser | undefined;
|
|
|
+ editTags: Array<String> = [];
|
|
|
|
|
|
- currentUser:CloudUser|undefined
|
|
|
constructor(
|
|
|
private router: Router,
|
|
|
- private modalCtrl:ModalController,
|
|
|
+ private modalCtrl: ModalController,
|
|
|
private alertController: AlertController,
|
|
|
private auth: AuthService
|
|
|
- ) {
|
|
|
- this.currentUser = new CloudUser();
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ // 监听认证状态变化
|
|
|
+ this.auth.currentUser$.subscribe(user => {
|
|
|
+ if (user) {
|
|
|
+ this.currentUser = user as unknown as CloudUser;
|
|
|
+ } else {
|
|
|
+ this.currentUser = undefined;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ goToCollection() {
|
|
|
+ console.log("goToCollection");
|
|
|
+ }
|
|
|
+
|
|
|
+ goToAvatar() {
|
|
|
+ this.router.navigate(['/tabs/picture']);
|
|
|
}
|
|
|
|
|
|
async openLoginModal() {
|
|
|
- const { data } = await openUserLoginModal(this.modalCtrl);
|
|
|
- if (data?.role === 'confirm') {
|
|
|
- const cloudUser = data.data as CloudUser;
|
|
|
- if (cloudUser?.id) {
|
|
|
- this.currentUser = cloudUser;
|
|
|
- await this.auth.login(cloudUser.get('username'), cloudUser.get('password'));
|
|
|
+ try {
|
|
|
+ const { data } = await openUserLoginModal(this.modalCtrl);
|
|
|
+ if (data?.role === 'confirm') {
|
|
|
+ const { username, password } = data.data;
|
|
|
+ // 直接使用返回的用户名和密码登录
|
|
|
+ const user = await this.auth.login(username, password);
|
|
|
+ this.currentUser = user as unknown as CloudUser;
|
|
|
}
|
|
|
+ } catch (error: any) {
|
|
|
+ console.error('Login error:', error);
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '登录失败',
|
|
|
+ message: error.message || '请稍后重试',
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async openSignupModal() {
|
|
|
- const { data } = await openUserLoginModal(this.modalCtrl);
|
|
|
- if (data?.role === 'confirm') {
|
|
|
- const cloudUser = data.data as CloudUser;
|
|
|
- if (cloudUser?.id) {
|
|
|
- this.currentUser = cloudUser;
|
|
|
- await this.auth.register(
|
|
|
- cloudUser.get('username'),
|
|
|
- cloudUser.get('password'),
|
|
|
- cloudUser.get('email') || ''
|
|
|
- );
|
|
|
+ try {
|
|
|
+ const { data } = await openUserLoginModal(this.modalCtrl);
|
|
|
+ if (data?.role === 'confirm') {
|
|
|
+ const { username, password, email } = data.data;
|
|
|
+
|
|
|
+ if (!username || !password) {
|
|
|
+ throw new Error('用户名和密码不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 直接使用返回的信息注册
|
|
|
+ const user = await this.auth.register(username, password, email || '');
|
|
|
+ this.currentUser = user as unknown as CloudUser;
|
|
|
}
|
|
|
+ } catch (error: any) {
|
|
|
+ console.error('Registration error:', error);
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '注册失败',
|
|
|
+ message: error.message || '请稍后重试',
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async handleUserAction() {
|
|
|
- const { data } = await openUserLoginModal(this.modalCtrl);
|
|
|
- if (!data?.data) {
|
|
|
- return;
|
|
|
+ async logout() {
|
|
|
+ try {
|
|
|
+ await this.auth.logout();
|
|
|
+ this.currentUser?.logOut();
|
|
|
+ this.currentUser = undefined;
|
|
|
+ } catch (error: any) {
|
|
|
+ console.error('Logout error:', error);
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '登出失败',
|
|
|
+ message: error.message || '请稍后重试',
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
}
|
|
|
- this.currentUser = data.data as CloudUser;
|
|
|
- }
|
|
|
-
|
|
|
- logout(){
|
|
|
- this.currentUser?.logOut();
|
|
|
- this.auth.logout();
|
|
|
}
|
|
|
|
|
|
- editUser(){
|
|
|
- openUserEditModal(this.modalCtrl)
|
|
|
+ editUser() {
|
|
|
+ openUserEditModal(this.modalCtrl);
|
|
|
}
|
|
|
|
|
|
- editTags:Array<String>=[]
|
|
|
- async setTagsValue(ev:any){
|
|
|
+ async setTagsValue(ev: any) {
|
|
|
let currentUser = new CloudUser();
|
|
|
- let userPrompt = ``
|
|
|
- if(!currentUser?.id){
|
|
|
+ if (!currentUser?.id) {
|
|
|
console.log("用户未登录,请登录后重试");
|
|
|
const { data } = await openUserLoginModal(this.modalCtrl);
|
|
|
if (!data?.data) {
|
|
|
@@ -96,7 +135,7 @@ export class Tab4Page {
|
|
|
}
|
|
|
currentUser = data.data as CloudUser;
|
|
|
}
|
|
|
- this.editTags=ev;
|
|
|
+ this.editTags = ev;
|
|
|
}
|
|
|
|
|
|
login() {
|
|
|
@@ -106,4 +145,4 @@ export class Tab4Page {
|
|
|
signup() {
|
|
|
this.openSignupModal();
|
|
|
}
|
|
|
-}
|
|
|
+}
|