import request from '@/utils/request'

function urlencoded(data) {
  const newData = Object.keys(data).map((key) => {
    return `${key}=${encodeURIComponent(data[key])}`
  }).join('&')
  return newData
}
export function login(data) {
  return request({
    url: '/aiccs-api/user/login',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: urlencoded(data)
  })
}

export function getInfo() {
  return request({
    url: '/aiccs-api/user/info',
    method: 'get'
  })
}

export function logout() {
  return request({
    url: '/aiccs-api/user/logout',
    method: 'get'
  })
}

/**
 * 检查是否已经进行SSO登录
 */
export function checkSSOLogin() {
  return request({
    url: '/aiccs-api/user/checkSSOLogin',
    method: 'post'
  })
}

export function getOpsToken() {
  return request({
    url: '/aiccs-api/user/getOpsToken',
    method: 'get'
  })
}

/**
 * 信创门户单点登录 ticket
 */
export function casLogin(params) {
  return request({
    url: '/aiccs-api/user/casLogin',
    method: 'get',
    params
  })
}

/**
 * 登录年报后台管理系统
 */
export function logManage(params) {
  return request({
    url: process.env.VUE_APP_AICEPS_URL + '/loginManageController/loginManage.do',
    method: 'get',
    params
  })
}

export function getCode(phone) {
  const params = new FormData()
  params.append('phone', phone)
  return request({
    url: '/aiccs-api/user/getCode',
    method: 'post',
    data: params
  })
}

export function loginByCode(phone, code) {
  const params = new FormData()
  params.append('phone', phone)
  params.append('code', code)
  return request({
    url: '/aiccs-api/user/loginByCode',
    method: 'post',
    data: params
  })
}

/**
 * 统一门户单点登录 ticket
 */
export function casLogin_Unite(params) {
  return request({
    url: '/aiccs-api/user/casLogin_Unite',
    method: 'get',
    params
  })
}

/**
 * 统一门户单点登录 ticket
 */
export function isHaveUser(phone) {
  const params = new FormData()
  params.append('phone', phone)
  return request({
    url: '/aiccs-api/user/isHaveUser',
    method: 'post',
    data: params
  })
}
