From 078cdb2b41d1dec47e2d981c2d2e618d12beddb4 Mon Sep 17 00:00:00 2001
From: Mila <Mila@pollex.com.tw>
Date: 星期四, 26 十二月 2024 09:43:23 +0800
Subject: [PATCH] feat(顧問登入): 串接 otp 發送/驗證 api

---
 PAMapp/shared/services/login.service.ts |  104 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 94 insertions(+), 10 deletions(-)

diff --git a/PAMapp/shared/services/login.service.ts b/PAMapp/shared/services/login.service.ts
index ac9e2fd..ecf40e5 100644
--- a/PAMapp/shared/services/login.service.ts
+++ b/PAMapp/shared/services/login.service.ts
@@ -1,6 +1,7 @@
-import { http } from "./httpClient";
 import { AxiosResponse } from 'axios';
-import _ from "lodash";
+import forge from "node-forge";
+import { http } from "./httpClient";
+// import CryptoJS from "asmcrypto-js";
 
 import { ConsultantLoginInfo } from "../models/ConsultantLoginInfo";
 import { LoginRequest } from "../models/loginRequest.model";
@@ -11,9 +12,22 @@
 
 class LoginService {
     /** 憿批恥��-��TP **/
-  async sendOtp(loginInfo: LoginRequest, verifyCode: string):Promise<OtpInfo> {
-    return http.post(`/otp/sendOtp/${verifyCode}`, loginInfo).then( res => res.data );
-  }
+    async sendOtp(loginInfo: LoginRequest, verifyCode: string): Promise<OtpInfo> {
+      try {
+        const response = await http.post(`/otp/sendOtp/${verifyCode}`, loginInfo);
+        // 撘望�est1: �� if (response)
+        if (response) {
+          return response.data;
+        } else {
+          throw new Error('http.post returned null-like value.');
+        }
+      } catch (error) {
+        console.error('An error occurred while sending OTP:', error);
+        // �隞亙甇方���隤斗����身��
+        throw error;
+      }
+    }
+
 
   /**
    * 憿批恥��-撽�TP
@@ -36,17 +50,29 @@
   }
 
   /** 憿批恥閮餃�� **/
-  async register(registerInfo: RegisterInfo):Promise<LoginSuccessToken>{
-    return http.post('/otp/register', registerInfo).then(res => res.data);
+  async register(registerInfo: RegisterInfo): Promise<LoginSuccessToken> {
+    try {
+      const response = await http.post('/otp/register', registerInfo);
+      if (response !== null) {
+        return response.data;
+      } else {
+        throw new Error('http.post returned null-like value.');
+      }
+    } catch (error) {
+      console.error('An error occurred while registering:', error);
+      // �隞亙甇方���隤斗����身��
+      throw error;
+    }
   }
+
 
   /** �����Ⅳ���� **/
   async getImgOfVerification():Promise<string>{
     return http.get('/login/validate/get_img_code',{ responseType : 'arraybuffer' })
       .then( response => {
         const toBase64 = window.btoa(
-                          _.reduce( new Uint8Array(response.data),(data,byte) =>
-                            data + String.fromCharCode(byte),'')
+                          Array.from(new Uint8Array(response.data)).reduce((data, byte) =>
+                            data + String.fromCharCode(byte), '')
                         );
         const imgSrc = `data:image/jpeg;base64,${toBase64}`;
         return imgSrc;
@@ -60,8 +86,66 @@
 
   /** 憿批�� **/
   logInToConsultant(consultantDto:ConsultantLoginInfo, verificationCode: string):Promise<AxiosResponse<LoginSuccessToken>>{
-    return http.post(`/eService/authenticate/${verificationCode}`,consultantDto);
+
+  const iv = "0123456789abcdef";
+  const key = "PAMKEY1234567890";
+  const cipher = forge.cipher.createCipher('AES-GCM', key);
+  cipher.start({
+    iv:iv
+  });
+  cipher.update(forge.util.createBuffer(forge.util.encodeUtf8(consultantDto.password)));
+  cipher.finish();
+  const encry = cipher.output;
+  var tag = cipher.mode.tag;
+  const encryptedPassword = window.btoa(encry.data+tag.data);
+
+    return http.post(`/eService/authenticate/${verificationCode}`, { ...consultantDto, password: encryptedPassword});
   }
+
+  /**
+   * 憿批�� - ��� otp
+   * @param agentNo
+   * @returns OtpInfo
+   */
+  async sentOtpWithConsultant(agentNo: string): Promise<OtpInfo> {
+    try {
+      const response = await http.post(`/otp/consultant/sendOtp/${agentNo}`);
+      // 撘望�est1: �� if (response)
+      if (response) {
+        return response.data;
+      } else {
+        throw new Error('http.post returned null-like value.');
+      }
+    } catch (error) {
+      console.error('An error occurred while sending OTP:', error);
+      // �隞亙甇方���隤斗����身��
+      throw error;
+    }
+  }
+
+    /**
+   * 憿批��-撽�TP
+   * @param loginVerify ��撽������隞�
+   * @returns ��撽�����oken
+   */
+    async loginVerifyWithConsultant(loginVerify: LoginVerify): Promise<LoginSuccessToken> {
+      try {
+        const response = await http.post('/otp/consultant/verifyOtp', loginVerify);
+        if (response !== null) {
+          return response.data;
+        } else {
+          throw new Error('http.post returned null-like value.');
+        }
+      } catch (error) {
+        // �隞亙甇方���隤斗����身��
+        console.error('An error occurred while verifying OTP:', error);
+        throw error;
+      }
+    }
+
+    async logout(): Promise<void> {
+      return http.post('/logout');
+    }
 }
 
 export default new LoginService();

--
Gitblit v1.8.0