From db369f9ec74cd025e162b99f7ec1ac343733bbc8 Mon Sep 17 00:00:00 2001
From: Mila <Mila@pollex.com.tw>
Date: 星期四, 16 十二月 2021 11:07:48 +0800
Subject: [PATCH] refactor: login 1. 整理 methods 順序 2. 使用 loginService

---
 PAMapp/pages/login/index.vue |   83 +++++++++++++++++++++++------------------
 1 files changed, 47 insertions(+), 36 deletions(-)

diff --git a/PAMapp/pages/login/index.vue b/PAMapp/pages/login/index.vue
index f7c331f..04a446e 100644
--- a/PAMapp/pages/login/index.vue
+++ b/PAMapp/pages/login/index.vue
@@ -335,10 +335,14 @@
 <script lang="ts">
 import { namespace } from 'nuxt-property-decorator';
 import { Vue, Component, Ref } from 'vue-property-decorator';
-import { LoginRequest, LoginVerify, loginVerify, OtpInfo, register, RegisterInfo, sendOtp } from '~/assets/ts/api/consultant';
 import ErrorMessageBox from '~/assets/ts/errorService';
 import { OtpErrorCode } from '~/assets/ts/models/enum/otpErrorCode';
 import { Role } from '~/assets/ts/models/enum/Role';
+import { LoginRequest } from '~/assets/ts/models/loginRequest.model';
+import { LoginVerify } from '~/assets/ts/models/loginVerify.model';
+import { OtpInfo } from '~/assets/ts/models/otpInfo.model';
+import { RegisterInfo } from '~/assets/ts/models/registerInfo';
+import loginService from '~/assets/ts/services/login.service';
 
 const roleStorage = namespace('localStorage');
 
@@ -382,6 +386,8 @@
   applyAccount_onAction = false;
 
   previousPath = '';
+
+  /////////////////////////////////////////////////////
   mounted() {
     const phoneOtpTime = localStorage.getItem('phoneOtpTime');
     const emailOtpTime = localStorage.getItem('emailOtpTime');
@@ -395,6 +401,22 @@
     }
   }
 
+  beforeRouteEnter (to, from, next) {
+      next(vm => {
+        console.log(from.path, 'beforeRouteEnter');
+        vm.previousPath = from.path;
+      })
+  }
+
+  destroyed() {
+    this.removeOtpTime();
+    clearInterval(this.otpInterval);
+    clearInterval(this.emailResendInterval);
+    clearInterval(this.autoRedirectInterval);
+  }
+
+
+  //////////////////////////////////////////////////////////
   detectContractReadStatus(event: any): void {
     const scrollTop = Math.round(event.target.scrollTop);
     const height = event.target.scrollHeight - event.target.clientHeight;
@@ -445,7 +467,7 @@
       loginType: isMobile ? 'SMS' : 'EMAIL',
       account: isMobile ? this.phoneNumber : this.email,
     }
-    sendOtp(loginInfo).then(otpInfo => {
+    loginService.sendOtp(loginInfo).then(otpInfo => {
       if (otpInfo.success) {
         this.storageOtpTime(type, otpInfo);
         this.startOtpSetting(type);
@@ -484,8 +506,8 @@
     this.applyAccount_onAction = true;
     const registerInfo = this.setRegisterInfo();
 
-    register(registerInfo).then(res => {
-      this.storageIdToken(res.data.id_token);
+    loginService.register(registerInfo).then(res => {
+      this.storageIdToken(res.id_token);
       this.storageRole(Role.USER);
       this.storagePhoneOrEmail(registerInfo);
       this.autoRedirect();
@@ -501,36 +523,11 @@
     this.redirect();
   }
 
-  private autoRedirect() {
-    this.autoRedirectInterval = setInterval(() => {
-      this.autoRedirectCounter -= 1;
-
-      if (this.autoRedirectCounter === 0) {
-        clearInterval(this.autoRedirectInterval);
-        this.redirect();
-      }
-    }, 1000)
-  }
-
-  private redirect() {
-    const backToPrevious = ['questionnaire', 'myConsultantList'];
-    const find = backToPrevious.findIndex(item => this.previousPath.includes(item));
-    console.log(this.previousPath, find, 'redirect');
-    find > -1 ? this.$router.go(-1) : this.$router.push('/');
-  }
-
-  beforeRouteEnter (to, from, next) {
-      next(vm => {
-        console.log(from.path, 'beforeRouteEnter');
-        vm.previousPath = from.path;
-      })
-    }
-
   login() {
     const login: LoginVerify = this.setLoginInfo();
     this.removeOtpTime();
-    loginVerify(login).then(res => {
-      this.storageIdToken(res.data.id_token);
+    loginService.loginVerify(login).then(res => {
+      this.storageIdToken(res.id_token);
       this.storageRole(Role.USER);
       this.phoneSuccessConfirmVisable = true;
       this.autoRedirect();
@@ -539,6 +536,9 @@
       this.checkHttpErrorStatus(error);
     });
   }
+
+
+  //////////////////////////////////////////////////////////////////
   private checkHttpErrorStatus(error:any):void{
     switch (error.response.status) {
         case 401:
@@ -560,11 +560,22 @@
       }
   }
 
-  destroyed() {
-    this.removeOtpTime();
-    clearInterval(this.otpInterval);
-    clearInterval(this.emailResendInterval);
-    clearInterval(this.autoRedirectInterval);
+  private autoRedirect() {
+    this.autoRedirectInterval = setInterval(() => {
+      this.autoRedirectCounter -= 1;
+
+      if (this.autoRedirectCounter === 0) {
+        clearInterval(this.autoRedirectInterval);
+        this.redirect();
+      }
+    }, 1000)
+  }
+
+  private redirect() {
+    const backToPrevious = ['questionnaire', 'myConsultantList'];
+    const find = backToPrevious.findIndex(item => this.previousPath.includes(item));
+    console.log(this.previousPath, find, 'redirect');
+    find > -1 ? this.$router.go(-1) : this.$router.push('/');
   }
 
   private phoneDiffTime(parseOtpTime: any) {

--
Gitblit v1.8.0