import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

//loginByUm

export async function loginByUm(data: AuthApi.LoginParams) {
  const res = await requestClient.post<AuthApi.LoginResult>('/share/UmUser/login/username', data);
  return {
    accessToken: res.data,
  }
}
/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  const res = await requestClient.post<AuthApi.LoginResult>('/share/user/login/username', data);
  return {
    accessToken: res.data,
  }
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  // return requestClient.get<string[]>('/auth/codes');
  return [];
}

/**
 * 返回对象的值：
 * secretKey ： 密钥
 * qrCodeUrl ： 二维码图片url
 * @param params 
 * @returns 
 */
export async function setup2faApi(params: { username: string }) {
  return requestClient.get('/share/api/auth/2fa/setup?username=' + params.username, params);
}

/**
 * 验证2fa，返回对象的值：
 * data:为token值
 * @param params 
 * @returns 
 */
export async function verify2faApi(params) {
  const res = await requestClient.post('/share/api/auth/2fa/verify', params);
  return {
    accessToken: res.data,
  }
}
//2fa/update-secret/{uuid}
export async function update2faApi(params) {
  return requestClient.post('/share/api/auth/2fa/update-secret/' + params.uuid, params);
}