|
|
@@ -1,11 +1,18 @@
|
|
|
import { Component } from '@angular/core';
|
|
|
-import { IonHeader, IonToolbar, IonTitle, IonContent, IonCard, IonCardContent,
|
|
|
- IonButton, IonCardHeader, IonCardTitle, IonCardSubtitle, IonInput,
|
|
|
- IonItem, IonSegment, IonSegmentButton, IonLabel, IonButtons } from '@ionic/angular/standalone';
|
|
|
-import { ModalController, AlertController } from '@ionic/angular/standalone';
|
|
|
-import { CloudUser } from '../../ncloud';
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
+import { IonHeader, IonToolbar, IonTitle, IonContent, IonButtons, IonButton,
|
|
|
+ IonList, IonItem, IonLabel, IonInput } from '@ionic/angular/standalone';
|
|
|
+import { ModalController, AlertController } from '@ionic/angular/standalone';
|
|
|
+import { CloudUser } from '../../../lib/ncloud';
|
|
|
+
|
|
|
+export async function openUserLoginModal(modalCtrl: ModalController) {
|
|
|
+ const modal = await modalCtrl.create({
|
|
|
+ component: ModalUserLoginComponent
|
|
|
+ });
|
|
|
+ await modal.present();
|
|
|
+ return modal.onDidDismiss();
|
|
|
+}
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-modal-user-login',
|
|
|
@@ -15,52 +22,40 @@ import { FormsModule } from '@angular/forms';
|
|
|
imports: [
|
|
|
CommonModule,
|
|
|
FormsModule,
|
|
|
- IonHeader, IonToolbar, IonTitle, IonContent, IonCard,
|
|
|
- IonCardContent, IonButton, IonCardHeader, IonCardTitle,
|
|
|
- IonCardSubtitle, IonInput, IonItem, IonSegment,
|
|
|
- IonSegmentButton, IonLabel,
|
|
|
- IonButtons
|
|
|
+ IonHeader,
|
|
|
+ IonToolbar,
|
|
|
+ IonTitle,
|
|
|
+ IonContent,
|
|
|
+ IonButtons,
|
|
|
+ IonButton,
|
|
|
+ IonList,
|
|
|
+ IonItem,
|
|
|
+ IonLabel,
|
|
|
+ IonInput
|
|
|
]
|
|
|
})
|
|
|
export class ModalUserLoginComponent {
|
|
|
- type: string = 'login';
|
|
|
- username: string = '';
|
|
|
- password: string = '';
|
|
|
- password2: string = '';
|
|
|
- email: string = '';
|
|
|
+ username = '';
|
|
|
+ password = '';
|
|
|
+ isSignup = false;
|
|
|
+ email = '';
|
|
|
|
|
|
constructor(
|
|
|
private modalCtrl: ModalController,
|
|
|
- private alertController: AlertController
|
|
|
+ private alertCtrl: AlertController
|
|
|
) {}
|
|
|
|
|
|
- async showAlert(header: string, message: string) {
|
|
|
- const alert = await this.alertController.create({
|
|
|
- header,
|
|
|
- message,
|
|
|
- buttons: ['确定']
|
|
|
- });
|
|
|
- await alert.present();
|
|
|
- }
|
|
|
-
|
|
|
- typeChange(ev: any) {
|
|
|
- this.type = ev.detail.value;
|
|
|
- // 切换时清空表单
|
|
|
- this.username = '';
|
|
|
- this.password = '';
|
|
|
- this.password2 = '';
|
|
|
- this.email = '';
|
|
|
- }
|
|
|
-
|
|
|
async login() {
|
|
|
- if (!this.username || !this.password) {
|
|
|
- await this.showAlert('错误', '请输入用户名和密码');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
try {
|
|
|
+ if (!this.username.trim()) {
|
|
|
+ throw new Error('用户名不能为空');
|
|
|
+ }
|
|
|
+ if (!this.password) {
|
|
|
+ throw new Error('密码不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
const user = new CloudUser();
|
|
|
- await user.logIn(this.username, this.password);
|
|
|
+ await user.logIn(this.username.trim(), this.password);
|
|
|
this.modalCtrl.dismiss({ data: user, role: 'confirm' });
|
|
|
} catch (error: any) {
|
|
|
await this.showAlert('登录失败', error.message || '请检查用户名和密码');
|
|
|
@@ -68,40 +63,41 @@ export class ModalUserLoginComponent {
|
|
|
}
|
|
|
|
|
|
async signup() {
|
|
|
- if (!this.username || !this.password || !this.password2) {
|
|
|
- await this.showAlert('错误', '请填写所有必填字段');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (this.password !== this.password2) {
|
|
|
- await this.showAlert('错误', '两次输入的密码不一致');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (this.password.length < 6) {
|
|
|
- await this.showAlert('错误', '密码长度至少6位');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
try {
|
|
|
+ if (!this.username.trim()) {
|
|
|
+ throw new Error('用户名不能为空');
|
|
|
+ }
|
|
|
+ if (!this.password) {
|
|
|
+ throw new Error('密码不能为空');
|
|
|
+ }
|
|
|
+ if (!this.email.trim()) {
|
|
|
+ throw new Error('邮箱不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
const user = new CloudUser();
|
|
|
- const additionalData = this.email ? { email: this.email } : {};
|
|
|
- await user.signUp(this.username, this.password, additionalData);
|
|
|
+ await user.signUp(this.username.trim(), this.password, {
|
|
|
+ email: this.email.trim()
|
|
|
+ });
|
|
|
this.modalCtrl.dismiss({ data: user, role: 'confirm' });
|
|
|
} catch (error: any) {
|
|
|
await this.showAlert('注册失败', error.message || '注册失败,请稍后重试');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- dismiss() {
|
|
|
+ cancel() {
|
|
|
this.modalCtrl.dismiss(null, 'cancel');
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-export async function openUserLoginModal(modalController: ModalController) {
|
|
|
- const modal = await modalController.create({
|
|
|
- component: ModalUserLoginComponent
|
|
|
- });
|
|
|
- await modal.present();
|
|
|
- return modal.onDidDismiss();
|
|
|
+ toggleMode() {
|
|
|
+ this.isSignup = !this.isSignup;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async showAlert(header: string, message: string) {
|
|
|
+ const alert = await this.alertCtrl.create({
|
|
|
+ header,
|
|
|
+ message,
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
}
|