25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

35 satır
899 B

  1. const TOKEN_KEY = 'token'
  2. const USER_KEY = 'userInfo'
  3. const LOGIN_TIME_KEY = 'login_time'
  4. // 后端 30 天过期,前端保险起见提前 30 分钟判定失效
  5. const EXPIRE = 30 * 24 * 60 * 60 - 30 * 60
  6. export function getToken() {
  7. const loginTime = uni.getStorageSync(LOGIN_TIME_KEY)
  8. const curr = parseInt(Date.now() / 1000)
  9. if (loginTime && curr - loginTime > EXPIRE) {
  10. clearAuth()
  11. return ''
  12. }
  13. return uni.getStorageSync(TOKEN_KEY) || ''
  14. }
  15. export function setToken(token) {
  16. uni.setStorageSync(TOKEN_KEY, token)
  17. uni.setStorageSync(LOGIN_TIME_KEY, parseInt(Date.now() / 1000))
  18. }
  19. export function getUser() {
  20. return uni.getStorageSync(USER_KEY) || {}
  21. }
  22. export function setUser(user) {
  23. uni.setStorageSync(USER_KEY, user || {})
  24. }
  25. export function clearAuth() {
  26. uni.removeStorageSync(TOKEN_KEY)
  27. uni.removeStorageSync(USER_KEY)
  28. uni.removeStorageSync(LOGIN_TIME_KEY)
  29. }