modal-user-login.component.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { Input, OnInit } from '@angular/core';
  2. import { Component } from '@angular/core';
  3. import { IonicModule } from '@ionic/angular';
  4. import { ModalController } from '@ionic/angular/standalone';
  5. import { CloudUser } from 'src/lib/ncloud';
  6. @Component({
  7. selector: 'app-modal-user-login',
  8. templateUrl: './modal-user-login.component.html',
  9. styleUrls: ['./modal-user-login.component.scss'],
  10. standalone: true,
  11. imports: [IonicModule
  12. ],
  13. })
  14. export class ModalUserLoginComponent implements OnInit {
  15. @Input()
  16. type:"login"|"signup" = "login"
  17. typeChange(ev:any){
  18. this.type = ev?.detail?.value || ev?.value || 'login'
  19. }
  20. username:string = ""
  21. usernameChange(ev:any){
  22. console.log(ev)
  23. this.username = ev?.detail?.value
  24. }
  25. password:string = ""
  26. passwordChange(ev:any){
  27. this.password = ev?.detail?.value
  28. }
  29. password2:string = ""
  30. password2Change(ev:any){
  31. this.password2 = ev?.detail?.value
  32. }
  33. constructor(private modalCtrl:ModalController) { }
  34. ngOnInit() {}
  35. async login(){
  36. if(!this.username || !this.password){
  37. console.log("请输入完整")
  38. return
  39. }
  40. let user:any = new CloudUser();
  41. user = await user.login(this.username,this.password);
  42. if(user?.id){
  43. this.modalCtrl.dismiss(user,"confirm")
  44. }else{
  45. console.log("登录失败")
  46. }
  47. }
  48. async signup(){
  49. if(!this.username || !this.password || !this.password2){
  50. console.log("请输入完整")
  51. return
  52. }
  53. if(this.password!=this.password2){
  54. console.log("两次密码不符,请修改")
  55. return
  56. }
  57. let user:any = new CloudUser();
  58. user = await user.signUp(this.username,this.password);
  59. if(user){
  60. this.type = "login"
  61. console.log("注册成功请登录")
  62. }
  63. }
  64. }
  65. export async function openUserLoginModal(modalCtrl:ModalController,type:"login"|"signup"="login"):Promise<CloudUser|null>{
  66. const modal = await modalCtrl.create({
  67. component: ModalUserLoginComponent,
  68. componentProps:{
  69. type:type
  70. },
  71. breakpoints:[0.5,0.7],
  72. initialBreakpoint:0.5
  73. });
  74. modal.present();
  75. const { data, role } = await modal.onWillDismiss();
  76. if (role === 'confirm') {
  77. return data;
  78. }
  79. return null
  80. }