| | |
| | | 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"; |
| | |
| | | |
| | | class LoginService { |
| | | /** 顧客登入-發送OTP **/ |
| | | 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); |
| | | // 弱掃Test1: 改為 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; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 顧客登入-驗證OTP |
| | | * @param loginVerify 包含驗證相關資訊的物件 |
| | | * @returns 回傳驗證成功後的Token |
| | | */ |
| | | async loginVerify(loginVerify: LoginVerify): Promise<LoginSuccessToken> { |
| | | try { |
| | | const response = await http.post('/otp/verify', 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; |
| | | } |
| | | } |
| | | |
| | | /** 顧客登入-驗證OTP **/ |
| | | async loginVerify(loginVerify: LoginVerify):Promise<LoginSuccessToken>{ |
| | | return http.post('/otp/verify', loginVerify).then(res => res.data); |
| | | } |
| | | /** 顧客註冊 **/ |
| | | 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; |
| | |
| | | |
| | | /** 顧問登入 **/ |
| | | 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}); |
| | | } |
| | | |
| | | async logout(): Promise<void> { |
| | | return http.post('/logout'); |
| | | } |
| | | } |
| | | |