page-mine.component.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Component, OnInit } from '@angular/core';
  2. import { IonAvatar,
  3. IonText,
  4. IonButton,
  5. IonIcon,
  6. IonCard,
  7. IonList,
  8. IonItem,
  9. IonLabel,
  10. IonBadge,
  11. IonContent,
  12. IonInput,
  13. AlertController,
  14. ModalController } from '@ionic/angular/standalone';
  15. import { ModalUserEditComponent } from '../modal-user-edit/modal-user-edit.component';
  16. import { FormsModule } from '@angular/forms';
  17. import { CloudUser } from 'src/lib/ncloud';
  18. import { ModalUserRegisterComponent } from '../modal-user-register/modal-user-register.component';
  19. @Component({
  20. selector: 'app-page-mine',
  21. templateUrl: './page-mine.component.html',
  22. styleUrls: ['./page-mine.component.scss'],
  23. standalone: true,
  24. imports: [
  25. IonContent,
  26. IonAvatar,
  27. IonText,
  28. IonButton,
  29. IonIcon,
  30. IonCard,
  31. IonList,
  32. IonItem,
  33. IonLabel,
  34. IonBadge,
  35. IonInput,
  36. FormsModule
  37. ]
  38. })
  39. export class PageMineComponent implements OnInit {
  40. currentUser: CloudUser | undefined;
  41. username: string = '';
  42. password: string = '';
  43. constructor(
  44. private modalCtrl: ModalController,
  45. private alertCtrl: AlertController
  46. ) {
  47. this.currentUser = new CloudUser();
  48. }
  49. async edit() {
  50. const modal = await this.modalCtrl.create({
  51. component: ModalUserEditComponent,
  52. });
  53. modal.present();
  54. const { data, role } = await modal.onWillDismiss();
  55. }
  56. async login() {
  57. if (!this.username || !this.password) {
  58. const alert = await this.alertCtrl.create({
  59. header: '错误',
  60. message: '请输入用户名和密码',
  61. buttons: ['确定']
  62. });
  63. await alert.present();
  64. return;
  65. }
  66. try {
  67. let user: any = new CloudUser();
  68. user = await user?.login(this.username, this.password);
  69. if (user?.id) {
  70. this.currentUser = user;
  71. } else {
  72. const alert = await this.alertCtrl.create({
  73. header: '登录失败',
  74. message: '用户名或密码错误',
  75. buttons: ['确定']
  76. });
  77. await alert.present();
  78. }
  79. } catch (error) {
  80. const alert = await this.alertCtrl.create({
  81. header: '登录错误',
  82. message: '登录过程中发生错误,请重试',
  83. buttons: ['确定']
  84. });
  85. await alert.present();
  86. }
  87. }
  88. async openRegisterModal() {
  89. const modal = await this.modalCtrl.create({
  90. component: ModalUserRegisterComponent
  91. });
  92. await modal.present();
  93. const { data, role } = await modal.onWillDismiss();
  94. if (role === 'success') {
  95. this.currentUser = data.user;
  96. }
  97. }
  98. logout() {
  99. this.currentUser?.logout();
  100. this.currentUser = undefined;
  101. this.username = '';
  102. this.password = '';
  103. }
  104. ngOnInit() {}
  105. }