auth.js 918 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {getStorage, setStorage, delStorage} from "@/utils/storage";
  2. import {calcDate} from 'js-fast-way'
  3. //计算token是否过期
  4. export const getTokenTime = (tokenKey) => {
  5. const token = getStorage(tokenKey, true)
  6. if (token) {
  7. const date = calcDate(token.datetime, new Date().getTime())
  8. if (date.seconds > 2000) {
  9. removeToken()
  10. removeRefreshToken()
  11. }
  12. }
  13. }
  14. export const getToken = () => {
  15. getTokenTime('token')
  16. return getStorage('token')
  17. }
  18. export const setToken = (token) => {
  19. setStorage('token', token)
  20. }
  21. export const getRefreshToken = () => {
  22. getTokenTime('refreshToken')
  23. return getStorage('refreshToken')
  24. }
  25. export const setRefreshToken = (token) => {
  26. setStorage('refreshToken', token)
  27. }
  28. export const removeToken = () => {
  29. delStorage('token')
  30. }
  31. export const removeRefreshToken = () => {
  32. delStorage('refreshToken')
  33. }