import request from '@/utils/request'
import Qs from 'qs'
import md5 from 'js-md5';
// 登录方法
export function login(username, password, code, uuid) {
  return request({
    url: '/login',
    method: 'post',
    headers:{
      'Authorization':'Basic bGluZ3dlaTpscWtqMDA3',
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },
    transformRequest: [function(data) {      //在请求之前对data传参进行格式转换
      data = Qs.stringify(data)
      return data
    }],
    params:{},
    data:{
      username: username,
      password: md5(username + password),
      code: code,
      uuid: uuid,
      grant_type: "password"
    }
  })
}
//刷新token方法
export function refreshAccessToken(refreshToken){
  return request({
    url: '/oauth/token',
    method: 'get',
    headers:{
      isToken: false,
      'Authorization':'Basic bGluZ3dlaTpscWtqMDA3',
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    params:{
      refresh_token: refreshToken,
      grant_type: "refresh_token"
    }
  })
}

// 注册方法
export function register(data) {
  return request({
    url: '/register',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}

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

// 退出方法
export function logout() {
  return request({
    url: '/logout',
    method: 'post'
  })
}

// 获取验证码
export function getCodeImg() {
  return request({
    url: '/v1/captchaImage',
    method: 'get',
    timeout: 20000
  })
}
