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

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

  export interface PhoneCodeParams {
    phoneNumber?: string;
    code?:string;
  }

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

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

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('/auth/login', data);
}

export async function phoneLoginApi(data: AuthApi.PhoneCodeParams) {
  return requestClient.post<AuthApi.LoginResult>('/auth/phoneLogin', 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');
}

/**
 *  获取手机验证码
 * @param phoneNumber 
 * @returns 
 */
export async function getPhoneCodeApi(phoneNumber: string) {
  return requestClient.post<string>('/auth/phoneCode', {
    phoneNumber,
  });
}

