import { CaptchaRes, LoginReq, LoginRes } from "../types/login";
import { POST } from "./http";
import CryptoJS from "crypto-js";

export async function Login(data: LoginReq) {
  return await POST<LoginRes>(`/login`, {
    ...data,
    password: Sha256(data.password),
  });
}

// //获取验证码
// export const getCaptcha = "GetCaptcha";
// export async function GetCaptcha() {
//   return await POST<CaptchaRes>(`/captcha`, { username: "" });
// }

// 退出登录
export async function Logout() {
  return await POST<{ url: string }>(`/logout`, {});
}

function Sha256(password: string) {
  const hash = CryptoJS.SHA256(password);
  // 将散列值转换为字符串表示
  const hashInString = hash.toString(CryptoJS.enc.Hex);
  return hashInString;
}
