import { fetch, fetchSso, fetchSso1 } from '@/core/request';

// const headers = {
//   cookieType: 'admin',
// };
const headers = {
  "TOKEN-TYPE": 'YW',
};
// let basurl = 'http://172.18.0.247:9200'
// let basurl = ''
let basurl = ''
// 登录方法
export function login(username, password, captchaVerification, socialType, socialCode, socialState) {
  const data = {
    username,
    password,
    captchaVerification,
    // 社交相关
    socialType,
    socialCode,
    socialState,
  };
  return fetchSso({
    url: `${basurl}/ssoApi/htAuth/login`,
    method: 'post',
    data,
  });
}
export function mxlogin(username, password, captchaVerification, socialType, socialCode, socialState) {
  const data = {
    username,
    password,
    captchaVerification,
    // 社交相关
    socialType,
    socialCode,
    socialState,
  };
  return fetchSso1({
    url: `${basurl}/ssoApi/starAuth/login`,
    method: 'post',
    data,
  });
}
export function getVerify() {
  return fetchSso({
    url: `${basurl}/ssoApi/verify/get`,
    method: 'post',
  });
}
// 初始化
export function init() {
  return fetchSso({
    headers,
    url: 'mhzc/admin/user/init',
    method: 'post',
  });
}
// 退出方法
export function logout() {
  return fetchSso({
    url: 'ssoApi/htAuth/logout',
    method: 'post',
  });
}
export function mxlogout() {
  return fetchSso1({
    url: 'ssoApi/starAuth/logout',
    method: 'post',
  });
}
export function mhLogout() {
  return fetchSso({
    url: '/sso/auth/logout',
    method: 'post',
  });
}

// 获取重定向地址
export function getRedirectUri() {
  return fetchSso({
    url: `/sso/auth/getRedirectUri`,
    method: 'post',
  });
}
// 修改密码
export function resetPassword(params) {
  return fetchSso({
    headers,
    url: 'mhzc/admin/user/resetPassword',
    data: JSON.stringify(params),
    method: 'post',
  });
}

// ========== OAUTH 2.0 相关 ==========

export function authorize(responseType, clientId, redirectUri, state, autoApprove) {
  // 构建 scopes
  const scopes = {};
  /* for (const scope of checkedScopes) {
    scopes[scope] = true
  }
  for (const scope of uncheckedScopes) {
    scopes[scope] = false
  } */
  // 发起请求
  return fetch({
    url: '/oauth2/authorize',
    headers: {
      'Content-type': 'application/x-www-form-urlencoded',
    },
    params: {
      response_type: responseType,
      client_id: clientId,
      redirect_uri: redirectUri,
      state,
      auto_approve: autoApprove,
      scope: JSON.stringify(scopes),
    },
    method: 'post',
  });
}
