import Cookies from 'js-cookie'
import AES from "@/utils/aes"

export function getCookies(name) {
  if (Cookies.get(name)) {
    return AES.decrypt(Cookies.get(name))
  }
}

export function getToken() {
  // let userId = JSON.parse(localStorage.getItem('myInfo')).userId
  // let userId = localStorage.getItem('myInfo').userId
  let myInfo = getCookies('myInfo')
  if (myInfo) {
    return JSON.parse(myInfo).userId
  } else {
    return null
  }
}

export function setCookies(name, cookies) {
  return Cookies.set(name, AES.encrypt(cookies))
}

export function removeCookies(name) {
  return Cookies.remove(name)
}

export function removeAllCookie() {
  var date = new Date();
  date.setTime(date.getTime() - 10000);
  var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  if (keys) {
    for (var i = keys.length; i--;)
      Cookies.remove(keys[i])
  }
}

const getters = (name) => {
  return Cookies.get(name)
}

export default function (Vue) {
  //添加全局API
  Vue.prototype.$auth = {
    getters
  }
}