保誠-保戶業務員媒合平台
Tomas
2021-12-09 ac235850a9287dae6977c964213176fa7c86b140
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Vue, Component , namespace } from 'nuxt-property-decorator';
import { AxiosError } from 'axios';
import { getImgOfVerification, logInToConsultant, getVerificationStatus } from '~/assets/ts/api/consultant';
import { Role } from '~/assets/ts/models/enum/role.enum';
import ErrorMessageBox from '~/assets/ts/errorService';
 
const roleStorage = namespace('localStorage');
@Component({
  layout: 'home'
})
export default class ConsultantLogin extends Vue {
  @roleStorage.Mutation storageIdToken!: (token: string) => void;
  @roleStorage.Mutation storageRole!: (role: string) => void;
  @roleStorage.Mutation storageConsultantId!:(id:string) => void;
  isRememberUserName = false;
  isShowPassword = false;
  imgSrc = '';
  verificationCode='';
  consultantDto = {
    username: '',
    password: '',
  }
 
  get isAlreadyDone():boolean{
    return !!(this.verificationCode && this.consultantDto.username && this.consultantDto.password);
  }
 
  mounted() {
    this.getInitUserName();
    this.regenerateImgOfVerification();
  };
 
  private getInitUserName(): void {
    const username = localStorage.getItem('consultantUserName')
    if (username) {
      this.consultantDto.username = username;
      this.isRememberUserName = true;
    }
  }
 
  public regenerateImgOfVerification(): void {
    getImgOfVerification().then( imgOfBase64 =>
      this.imgSrc = imgOfBase64
    );
  };
 
  public isRememberChange():void{
    this.isRememberUserName = !this.isRememberUserName;
    this.storeUserName();
  }
 
  public sendInfo():void{
    this.isAlreadyDone ? this.verify() : ErrorMessageBox('請確認帳號、密碼以及驗證碼是否填寫完畢');
  }
 
  private verify():void{
    getVerificationStatus(this.verificationCode).then( verifySuccess => {
      if(verifySuccess.data){
        this.loginWithConsultant()
      }else{
        this.clearValue();
        this.regenerateImgOfVerification();
        ErrorMessageBox('驗證碼輸入錯誤');
      }
    });
  }
 
  private loginWithConsultant(): void {
    logInToConsultant(this.consultantDto).then(res => {
      this.storageIdToken(res.data.id_token);
      this.storageRole(Role.ADMIN);
      this.storageConsultantId(this.consultantDto.username)
      this.storeUserName();
      this.$router.push('/myAppointmentList/appointmentList');
    }).catch((error:AxiosError)=>{
      this.checkHttpErrorStatus(error);
    });
  }
  private checkHttpErrorStatus(error:any):void{
    this.clearValue();
    this.regenerateImgOfVerification();
    switch (error.response.status) {
      case 401:
        const errorMsg = error.response.data.detail;
        ErrorMessageBox(errorMsg);
        break;
 
      default:
        ErrorMessageBox('',error);
        break;
    }
  }
 
  private storeUserName(): void {
    localStorage.setItem('consultantUserName', this.isRememberUserName ? this.consultantDto.username : '');
  };
 
  private clearValue():void{
    if (!this.isRememberUserName) this.consultantDto.username='';
    this.consultantDto.password = '';
    this.verificationCode = '';
  }
};