// import Cookies from 'js-cookie'
import {
	login
} from '@/api/login.js'

const TokenKey = 'Admin-Token'

//获取token
export async function getToken() {
	let tokenObj = uni.getStorageSync(TokenKey)
	if (new Date().getTime() - tokenObj.date < 1000 * 60 * 28) {
		return tokenObj.token
	} else {
		let res = await login()
		setToken(res.data.token)
		return res.data.token
	}
}

//设置token
export function setToken(token) {
	let tokenObj = {
		token,
		date: new Date().getTime()
	}
	return uni.setStorageSync(TokenKey, tokenObj)
}

//刷新token时间
export function refreshToken(token) {
	let tokenObj = uni.getStorageSync(TokenKey)
	if (!tokenObj) return
	tokenObj.date = new Date().getTime()
	return uni.setStorageSync(TokenKey, tokenObj)
}

//移除token
export function removeToken() {
	return uni.removeStorageSync(TokenKey)
}
