import website from '~src/config/index'
import { calcDate, delStoreData, getStoreData, setStoreData } from 'js-fast-way'

export const getToken = () => {
    getTokenTime(website.tokenKey)
    return getStoreData(website.tokenKey)
}

export const setToken = (token) => {
    setStoreData(website.tokenKey, token)
}

export const getRefreshToken = () => {
    getTokenTime(website.refreshTokenKey)
    return getStoreData(website.refreshTokenKey)
}

export const setRefreshToken = (token) => {
    setStoreData(website.refreshTokenKey, token)
}

export const removeToken = () => {
    delStoreData(website.tokenKey)
}

export const removeRefreshToken = () => {
    delStoreData(website.refreshTokenKey)
}

//计算token是否过期
const getTokenTime = (tokenKey) => {
    const token = getStoreData(tokenKey, true)
    if (token) {
        const date = calcDate(token.datetime, new Date().getTime())
        if (date.seconds > 2000) {
            removeToken()
            removeRefreshToken()
        }
    }
}
