| | |
| | | import { http } from "./httpClient"; |
| | | import { AxiosResponse } from 'axios'; |
| | | import _ from "lodash"; |
| | | import AES from 'crypto-js/aes'; |
| | | |
| | | import { ConsultantLoginInfo } from "../models/ConsultantLoginInfo"; |
| | | import { LoginRequest } from "../models/loginRequest.model"; |
| | |
| | | import { LoginVerify } from "../models/loginVerify.model"; |
| | | import { OtpInfo } from "../models/otpInfo.model"; |
| | | import { RegisterInfo } from "../models/registerInfo"; |
| | | import CryptoJS from "crypto-js"; |
| | | |
| | | 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); |
| | | if (response !== null) { |
| | | 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 |
| | |
| | | } |
| | | |
| | | /** 顧客註冊 **/ |
| | | 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>{ |
| | |
| | | |
| | | /** 顧問登入 **/ |
| | | logInToConsultant(consultantDto:ConsultantLoginInfo, verificationCode: string):Promise<AxiosResponse<LoginSuccessToken>>{ |
| | | return http.post(`/eService/authenticate/${verificationCode}`,consultantDto); |
| | | const plaintext = consultantDto.password; |
| | | const key = "PAM KEY"; |
| | | const iv = "0123456789abcdef"; |
| | | const keyBytes = CryptoJS.enc.Utf8.parse(key); |
| | | const ivBytes = CryptoJS.enc.Utf8.parse(iv); |
| | | const encrypted = CryptoJS.AES.encrypt(plaintext, keyBytes, { iv: ivBytes, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); |
| | | |
| | | return http.post(`/eService/authenticate/${verificationCode}`, { ...consultantDto, password: encrypted.toString() }); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | export default new LoginService(); |