login.page.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Component, OnInit } from '@angular/core';
  2. import { AlertController, NavController } from '@ionic/angular';
  3. import * as Parse from "parse"
  4. // 引用Router服务
  5. @Component({
  6. selector: 'app-login',
  7. templateUrl: './login.page.html',
  8. styleUrls: ['./login.page.scss'],
  9. })
  10. export class LoginPage implements OnInit {
  11. username:string = ""
  12. password:string = ""
  13. constructor(
  14. // 新增:Router服务,用于路由跳转
  15. private navCtrl:NavController,
  16. private alertController:AlertController
  17. ) { }
  18. ngOnInit() {
  19. }
  20. async login(){
  21. let user
  22. try {
  23. user = await Parse.User.logIn(this.username,this.password)
  24. } catch (error:any) {
  25. let message:string = ""
  26. // 新增提示词详情,根据Parse.User.login方法返回的不同英文提示词,增加对应的中文内容转换
  27. if(error?.message.indexOf("is required")>-1){
  28. message = "必须输入账号或邮箱"
  29. }
  30. if(error?.message.indexOf("Invalid username")>-1){
  31. message = "账号或密码错误,请检查"
  32. }
  33. this.presentAlert({
  34. header:"登录失败",
  35. subHeader:"状态码:"+error.code,
  36. message:message || error.message
  37. })
  38. }
  39. console.log(user)
  40. if(user?.id){
  41. this.navCtrl.back()
  42. }
  43. }
  44. async register(){
  45. let user = new Parse.User()
  46. user.set("username",this.username)
  47. user.set("password",this.password)
  48. try {
  49. let result = await user.signUp();
  50. console.log(result)
  51. if(result?.id){
  52. this.navCtrl.back()
  53. }
  54. // Hooray! Let them use the app now.
  55. } catch (error:any) {
  56. // 新增提示词详情,根据Parse.User.signUp方法返回的不同英文提示词,增加对应的中文内容转换
  57. let message:string = ""
  58. if(error?.message.indexOf("already exists")>-1){
  59. message = "该账号已存在请修改后重试"
  60. }
  61. if(error?.message.indexOf("empty")>-1){
  62. message = "账号不能为空请输入后重试"
  63. }
  64. this.presentAlert({
  65. header:"注册失败",
  66. subHeader:"状态码:"+error.code,
  67. message:message || error.message
  68. })
  69. }
  70. }
  71. async presentAlert(options:{header:string,subHeader:string,message:string}) {
  72. const alert = await this.alertController.create({
  73. header: options?.header,
  74. subHeader: options?.subHeader,
  75. message: options?.message,
  76. buttons: ['好的'],
  77. });
  78. await alert.present();
  79. }
  80. /**
  81. * 返回上级页面函数
  82. * @desc
  83. */
  84. back(){
  85. this.navCtrl.back()
  86. }
  87. }