import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";

export interface BasicResponseModel<T = any> {
  code: number;
  msg: string;
  data: T;
}

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type LoginNameResult = {
  code: number;
  msg: string;
  data: boolean;
};

/** 登录姓名验证 */
export const checkLoginNameApi = (data?: string) => {
  return http.request<LoginNameResult>(
    "post",
    baseUrlApi(`adm/account/checkLoginName/${data}`)
  );
};

export type VerificationCodeResult = {
  code: number;
  msg: string;
  captchaEnabled: boolean;
  uuid: string;
  img: string;
};

/** 获取验证码 */
export const getVerificationCode = (data?: object) => {
  return http.request<VerificationCodeResult>("get", baseUrlApi("code"), {
    data
  });
};

/** 获取短信验证码 */
export const getVerifySmsApi = (data?: object) => {
  return http.request<BasicResponseModel>(
    "post",
    baseUrlApi("adm/account/sendVerifySms"),
    { data }
  );
};

export type UserResult = {
  code: number;
  msg: string;
  data: {
    access_token: string;
    expires_in: number;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("auth/login"), { data });
};

/** 登录 */
export const verifySmsLogin = (data?: object) => {
  return http.request<UserResult>(
    "post",
    baseUrlApi("adm/account/verifySmsLogin"),
    { data }
  );
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", baseUrlApi("refresh-token"), {
    data
  });
};

/**
 * 获取用户信息
 */
export const getUserInfo = (data?: object) => {
  return http.request<BasicResponseModel>(
    "get",
    baseUrlApi("system/user/getInfo"),
    {
      data
    }
  );
};
