import { defHttp } from '/@/utils/http/axios';
import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel';

import { ErrorMessageMode } from '/#/axios';
import { useGlobSetting } from '/@/hooks/setting';
const globSetting = useGlobSetting();

enum Api {
  Login = '/oauth2/psw/token',
  Logout = '/oauth2/psw/logout',
  RefreshToken = '/oauth2/psw/refresh',
  UpdatePsw = '/oauth2/psw/update-psw',
  GetUserInfo = '/getUserInfo',
  GetPermCode = '/getPermCode',
  getAccountInfo = '/getAccountInfo',
  saveAccountInfo = '/saveAccountInfo',
  getCaptcha = '/auth/captcha?key=',
}

/**
 * @description: 用户登录
 */
export function Login(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 用户退出
 * @returns
 */
export function Logout() {
  return defHttp.post({ url: Api.Logout });
}

/**
 * 刷新token
 * @returns
 */
export function RefreshToken(params: object) {
  return defHttp.get({ url: Api.RefreshToken, params: params });
}

/**
 * 更新密码
 * @returns
 */
export function UpdatePsw(params: object) {
  return defHttp.post({ url: Api.UpdatePsw, params: params });
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function getAccountInfo() {
  return defHttp.get({ url: Api.getAccountInfo });
}

export function saveAccountInfo(params?: object) {
  return defHttp.post({ url: Api.saveAccountInfo, params: params });
}

/**
 * 获取验证码
 */
export function getCaptcha(key) {
  return globSetting.apiUrl + Api.getCaptcha + key;
}
