/**
 * Created by HaijunZhang on 2018/11/16.
 */
import { encrypt, decrypt } from 'utils/crypto'
import { tokenKey, trxTokenKey, ukeyPasswordKey } from '@/config'

export function getToken() {
  return sessionStorage.getItem(tokenKey)
}

export function setToken(token: string) {
  return sessionStorage.setItem(tokenKey, token)
}

export function removeToken() {
  return sessionStorage.removeItem(tokenKey)
}
// 为什么这里使用 localStorage, 防止刷新后丢失
export function getTrxToken() {
  const value = localStorage.getItem(trxTokenKey)
  if (value === 'undefined') return ''
  return value
}

export function setTrxToken(token: string) {
  return localStorage.setItem(trxTokenKey, token)
}

export function removeTrxToken() {
  removeUkeyPassword()
  return localStorage.removeItem(trxTokenKey)
}

// 用于保活与监测 ukey 是否拔出
export function setUkeyPassword(password: string) {
  return localStorage.setItem(ukeyPasswordKey, encrypt(password))
}
export function getUkeyPassword() {
  return decrypt(localStorage.getItem(ukeyPasswordKey))
}
export function removeUkeyPassword() {
  return localStorage.removeItem(ukeyPasswordKey)
}
