import { request } from 'umi';

export async function Login(data: {
  username: string;
  pwd: string;
  DepId: string;
}) {
  return request('/api/v2/user/login', {
    method: 'POST',
    data,
  });
}

export async function Logout() {
  return request('/api/v1/user/logout', {
    method: 'POST',
  });
}
export async function queryDepList(userName: string) {
  return request(`/api/v2/user/dep`, {
    params: { userName },
  });
}

export async function queryCurrent(): Promise<Api.IUser> {
  return request('/api/v2/user/current-user');
}
export async function queryCurrentV2() {
  return request('/api/v2/user/current-user', { method: 'GET' });
}

// 获取角色在此项目下的权限
export async function queryAccess(
  projectId: number | string,
): Promise<Api.IAccess> {
  const { data } = await request(`/api/v2/user/project/menu/${projectId}`);
  let permission: { [key: string]: boolean } = {};
  data?.forEach((item: any) => {
    if (!item.Menus || typeof item.Menus != 'object') return;
    permission = {
      ...permission,
      ...item.Menus,
    };
  });
  return permission;
}
