import { localStg } from '@/utils/storage';
import { request } from '../request';
// 定义 API URL 常量
const BASE_URL = 'http://localhost:58926/api';
// 声明接口 URL 变量
const GET_Login_URL = `${BASE_URL}/System/LoginUser`;
const GET_UserInfo_URL = `${BASE_URL}/System/GetUserInfo`;

/**
 * Login
 *
 * @param userName User name
 * @param password Password
 */
export async function fetchLogin(userName: string, password: string) {
  return request<Api.Auth.LoginTokenData>({
    // url: '/auth/login',
    url: GET_Login_URL,
    method: 'post',
    data: {
      userName,
      password
    }
  });
}

/** Get user info */
export function fetchGetUserInfo() {
  return request<Api.Auth.SYS_USERData>({
    url: GET_UserInfo_URL,
    method: 'post'
  });
}

/**
 * Refresh token
 *
 * @param refreshToken Refresh token
 */
export function fetchRefreshToken(refreshToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/refreshToken',
    method: 'post',
    data: {
      refreshToken
    }
  });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/auth/error', params: { code, msg } });
}

/** Get token */
export function getToken() {
  return localStg.get('token') || '';
}
