import { Component, OnInit } from '@angular/core'; import { LoginService } from './login.service'; import { LoadingController, NavController } from '@ionic/angular'; import { PublicService } from '../PublicService'; import { Constants } from '../Constant'; import { JwtHelperService } from '@auth0/angular-jwt'; @Component({ selector: 'app-login', templateUrl: './login.page.html', styleUrls: ['./login.page.scss'], }) export class LoginPage implements OnInit { private user; private userName; private passWord; private gesturePassWord; private userInfo; private openStyle; constructor(private readonly navController: NavController, private readonly publicService: PublicService, private readonly loginService: LoginService, private readonly loadingCtrl: LoadingController, private readonly jwt: JwtHelperService) { } ngOnInit() { } ionViewWillEnter() { this.user = this.publicService.getCurrentUser(); this.userName = this.user ? this.user.userName : ''; this.passWord = this.user ? this.user.passWord : ''; this.gesturePassWord = this.user ? this.user.userConfig.gesturePsw : ''; this.openStyle = this.user ? this.user.userConfig.defUnlocking : ''; console.log(this.gesturePassWord); } async toLogin() { const load = await this.publicService.showLoading(this.loadingCtrl, '登录中...'); this.loginService.login(this.userName, this.passWord).then(res => { if (res.code === Constants.SUCCESS){ const jwtData = this.jwt.decodeToken(res.data); const userId = jwtData.iss; this.getUserInfo(userId, res.data, load); }else{ this.publicService.presentToast(res.msg); load.dismiss(); } }, err => { this.publicService.checkNetworkToast(err); load.dismiss(); }); } // 手势登录 receiveVal(pwd){ if (this.gesturePassWord === pwd) { this.toLogin(); } else { this.publicService.presentToast('手势密码错误'); } } getUserInfo(userId, token, load ){ this.publicService.getUserInfo(userId, token).then(res => { if (res.code === Constants.SUCCESS) { this.userInfo = res.data; this.getUserConfig(userId, token, load); }else{ load.dismiss(); this.publicService.presentToast(res.msg); } }, err => { load.dismiss(); this.publicService.checkNetworkToast(err); }); } getUserConfig(userId, token, load){ this.publicService.getUserConfig(userId, token).then(res => { if (res.code === Constants.SUCCESS) { const user = {userName: this.userName, passWord: this.passWord, userInfo: this.userInfo, userConfig: res.data}; this.publicService.setToken(token); this.publicService.setCurrentUser(user); this.navController.navigateRoot('/tabs/home'); }else{ this.publicService.presentToast(res.msg); } load.dismiss(); }, err => { load.dismiss(); this.publicService.checkNetworkToast(err); }); } }