43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { STORAGE_KEYS } from './config'
|
|
|
|
export interface TokenData {
|
|
accessToken: string
|
|
refreshToken: string
|
|
expireTime: number
|
|
}
|
|
|
|
export function getAccessToken(): string {
|
|
return wx.getStorageSync(STORAGE_KEYS.ACCESS_TOKEN) || ''
|
|
}
|
|
|
|
export function getRefreshToken(): string {
|
|
return wx.getStorageSync(STORAGE_KEYS.REFRESH_TOKEN) || ''
|
|
}
|
|
|
|
export function getExpireTime(): number {
|
|
return wx.getStorageSync(STORAGE_KEYS.EXPIRE_TIME) || 0
|
|
}
|
|
|
|
export function setTokens(data: TokenData): void {
|
|
wx.setStorageSync(STORAGE_KEYS.ACCESS_TOKEN, data.accessToken)
|
|
wx.setStorageSync(STORAGE_KEYS.REFRESH_TOKEN, data.refreshToken)
|
|
wx.setStorageSync(STORAGE_KEYS.EXPIRE_TIME, data.expireTime)
|
|
}
|
|
|
|
export function clearTokens(): void {
|
|
wx.removeStorageSync(STORAGE_KEYS.ACCESS_TOKEN)
|
|
wx.removeStorageSync(STORAGE_KEYS.REFRESH_TOKEN)
|
|
wx.removeStorageSync(STORAGE_KEYS.EXPIRE_TIME)
|
|
}
|
|
|
|
export function isLoggedIn(): boolean {
|
|
return !!getAccessToken()
|
|
}
|
|
|
|
// token 过期判定统一由后端 401 驱动,前端不再猜测 expire_time 的单位
|
|
// (秒 / 毫秒 / 剩余秒数无法可靠区分,误判会导致不必要的刷新与误踢登录)
|
|
|
|
export function clearUserInfo(): void {
|
|
wx.removeStorageSync(STORAGE_KEYS.USER_INFO)
|
|
}
|