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/assets/ts/services/login.service.ts      |   25 ++++++
 PAMapp/pages/login/index.vue                    |   83 +++++++++++---------
 PAMapp/pages/questionnaire/_agentNo.vue         |    3 
 PAMapp/assets/ts/api/consultant.ts              |   54 -------------
 PAMapp/assets/ts/services/pamService.service.ts |   19 ----
 5 files changed, 74 insertions(+), 110 deletions(-)

diff --git a/PAMapp/assets/ts/api/consultant.ts b/PAMapp/assets/ts/api/consultant.ts
index d3462a4..5e4bea4 100644
--- a/PAMapp/assets/ts/api/consultant.ts
+++ b/PAMapp/assets/ts/api/consultant.ts
@@ -7,25 +7,6 @@
 import { http } from '../services/httpClient';
 import { FastQueryParams } from '../models/quickFilter.model';
 
-// 憿批恥��(TODO: OTP隤����� ���蝙�)
-export function login(user: any) {
-    return http.post('/authenticate', user)
-}
-
-// 憿批恥��-��TP
-export function sendOtp(loginInfo: LoginRequest) {
-    return http.post<OtpInfo>('/otp/sendOtp', loginInfo).then(res => res.data)
-}
-
-// 憿批恥��-撽�tp銝衣�
-export function loginVerify(loginVerify: LoginVerify) {
-    return http.post('/otp/verify', loginVerify)
-}
-
-// 憿批恥閮餃��
-export function register(registerInfo: RegisterInfo) {
-    return http.post('/otp/register', registerInfo)
-}
 
 // ��靽憿批��
 export function recommend() {
@@ -191,41 +172,6 @@
 }
 export interface RequestOfLoginSuccess{
   id_token: string;
-}
-
-export interface LoginRequest {
-    /** "SMS"=�����"EMAIL"=email */
-    loginType: string,
-    /** �loginType憛俟MS��府甈葆�����MAIL��葆��隞嗡縑蝞� */
-    account: string,
-}
-
-export interface OtpInfo {
-    /** ��撣嗅otp隤��� */
-    indexKey: string,
-    /** Otp�������� */
-    success: boolean,
-    failCode: string,
-    failReason: string,
-}
-
-export interface LoginVerify {
-    /** �撣嗅����mail */
-    account: string,
-    /** �otp��pi���ndex key */
-    indexKey: string,
-    /** �����縑蝞望�����Ⅳ */
-    otpCode: string
-}
-
-export interface RegisterInfo {
-    phone?: string,
-    email?: string,
-    indexKey: string,
-    otpCode: string,
-    name: string,
-    /** "SMS"嚗tp������"EMAIL":Otp�email */
-    contactType: string
 }
 
 export interface UserReviewsConsultantsParams{
diff --git a/PAMapp/assets/ts/services/login.service.ts b/PAMapp/assets/ts/services/login.service.ts
new file mode 100644
index 0000000..3a50e8a
--- /dev/null
+++ b/PAMapp/assets/ts/services/login.service.ts
@@ -0,0 +1,25 @@
+import { LoginRequest } from "../models/loginRequest.model";
+import { LoginSuccessToken } from "../models/loginSuccessToken.model";
+import { LoginVerify } from "../models/loginVerify.model";
+import { OtpInfo } from "../models/otpInfo.model";
+import { RegisterInfo } from "../models/registerInfo";
+import { http } from "./httpClient";
+
+class LoginService {
+    /** 憿批恥��-��TP **/
+  sendOtp(loginInfo: LoginRequest):Promise<OtpInfo> {
+    return http.post('/otp/sendOtp', loginInfo).then( res => res.data );
+  }
+
+  /** 憿批恥��-撽�TP **/
+  loginVerify(loginVerify: LoginVerify):Promise<LoginSuccessToken>{
+    return http.post('/otp/verify', loginVerify).then(res => res.data);
+  }
+
+  /** 憿批恥閮餃�� **/
+  register(registerInfo: RegisterInfo):Promise<LoginSuccessToken>{
+    return http.post('/otp/register', registerInfo).then(res => res.data);
+  }
+}
+
+export default new LoginService();
\ No newline at end of file
diff --git a/PAMapp/assets/ts/services/pamService.service.ts b/PAMapp/assets/ts/services/pamService.service.ts
index b9fb62f..451b6a5 100644
--- a/PAMapp/assets/ts/services/pamService.service.ts
+++ b/PAMapp/assets/ts/services/pamService.service.ts
@@ -5,11 +5,7 @@
 import { ConsultantLoginInfo } from '../models/ConsultantLoginInfo';
 import { UserSetting } from '../models/account.model';
 import { FastQueryParams } from '../models/quickFilter.model';
-import { LoginRequest } from "../models/loginRequest.model";
-import { OtpInfo } from "../models/otpInfo.model";
 import { Consultant } from '../models/consultant.model';
-import { RegisterInfo } from '../models/registerInfo';
-import { LoginVerify } from '../models/loginVerify.model';
 import { StrictQueryParams } from '../models/strictQueryParams';
 import { AppointmentParams } from '../models/appointmentParams';
 import { UserReviewsConsultantsParams } from '../models/UserReviewsConsultantsParams';
@@ -22,21 +18,6 @@
 
 class PamService {
   constructor() {}
-
-  /** 憿批恥��-��TP **/
-  sendOtp(loginInfo: LoginRequest):Promise<AxiosResponse<OtpInfo>> {
-    return http.post('/otp/sendOtp', loginInfo).then( res => res.data );
-  }
-
-  /** 憿批恥��-撽�TP **/
-  loginVerify(loginVerify: LoginVerify):Promise<AxiosResponse<any>>{
-    return http.post('/otp/verify', loginVerify);
-  }
-
-  /** 憿批恥閮餃�� **/
-  register(registerInfo: RegisterInfo):Promise<AxiosResponse<any>>{
-    return http.post('/otp/register', registerInfo);
-  }
 
   /** ��靽憿批�� **/
   recommend():Promise<AxiosResponse<Consultant[]>>{
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) {
diff --git a/PAMapp/pages/questionnaire/_agentNo.vue b/PAMapp/pages/questionnaire/_agentNo.vue
index ba67ffb..c9a622f 100644
--- a/PAMapp/pages/questionnaire/_agentNo.vue
+++ b/PAMapp/pages/questionnaire/_agentNo.vue
@@ -140,12 +140,13 @@
 
 <script lang="ts">
 import { Vue, Component, State, Action, Watch, namespace } from 'nuxt-property-decorator';
-import { addFavoriteConsultant, appointmentDemand, AppointmentParams, AppointmentRequests ,editAppointment,RegisterInfo } from '~/assets/ts/api/consultant';
+import { addFavoriteConsultant, appointmentDemand, AppointmentParams, AppointmentRequests ,editAppointment } from '~/assets/ts/api/consultant';
 import { getRequestQuestionFromStorage, getRequestsFromStorage, removeRequestQuestionFromStorage, setRequestsToStorage } from '~/assets/ts/storageRequests';
 import _ from 'lodash';
 import { Consultant } from '~/assets/ts/models/consultant.model';
 import { ContactType } from '~/assets/ts/models/enum/ContactType';
 import { Gender } from '~/assets/ts/models/enum/Gender';
+import { RegisterInfo } from '~/assets/ts/models/registerInfo';
 
   const roleStorage = namespace('localStorage');
   @Component

--
Gitblit v1.8.0