import { http } from "@/utils/http";
import { BaseResponse } from "@/api/base";

export type LoginResult = {
  /** 用户Id */
  id: string;
  /** 用户名 */
  username: string;
  /** AccessToken */
  access_token: string;
  /** RefreshToken */
  refresh_token: string;
  /** 有效期 */
  expires_in: number;
  /** 过期时间 */
  expires: Date;
  /** token类型 */
  token_type: string;
  /** 当前登陆用户的角色 */
  roles?: Array<string>;
};

/** 登录 */
export const getToken = (data: object) => {
  return http.request<BaseResponse<LoginResult>>(
    "post",
    "/admin/v1/token",
    { data },
    {
      auth: {
        username: "admin",
        password: "kycgfS4sBjx2rPVD"
      }
    }
  );
};

/** 刷新Token */
export const refreshToken = (refreshToken: string) => {
  const data = { refresh_token: refreshToken };
  return http.request<BaseResponse<LoginResult>>(
    "post",
    "/admin/v1/token/refresh",
    { data },
    {
      auth: {
        username: "admin",
        password: "kycgfS4sBjx2rPVD"
      }
    }
  );
};
