import request from '@/utils/request';
import { encryptWithAes, parseAesKey } from '@/utils/crypto';

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data) {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || 'password',
    fromSystem: data.fromSystem || 'yjzp'
  };
  params.password = encryptWithAes(params.password, parseAesKey(params.uuid));

  return request({
    url: '/api/auth/login',
    headers: {
      isToken: false,
      isEncrypt: true,
      repeatSubmit: false
    },
    method: 'post',
    data: params
  });
}

/**
 * 注销
 */
export function logout() {
  return request({
    url: '/api/auth/logout',
    method: 'post'
  });
}

/**
 * 获取验证码
 */
export function getCodeImg() {
  return request({
    url: '/api/auth/code',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  });
}

// 获取用户详细信息
export function getInfo() {
  return request({
    url: '/api/system/user/getInfo',
    method: 'get'
  });
}
