feat: app功能基本实现
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import Taro from '@tarojs/taro'
|
||||
import type { TokenDTO, UserInfoDTO } from '@/api/types'
|
||||
|
||||
const tokenKey = 'simple-todo-app-token'
|
||||
const userKey = 'simple-todo-app-user'
|
||||
|
||||
export function getToken() {
|
||||
return Taro.getStorageSync<string>(tokenKey) || ''
|
||||
}
|
||||
|
||||
export function getCurrentUser() {
|
||||
return Taro.getStorageSync<UserInfoDTO>(userKey) || {}
|
||||
}
|
||||
|
||||
export function isLoggedIn() {
|
||||
return Boolean(getToken())
|
||||
}
|
||||
|
||||
export function setLoginSession(data: TokenDTO) {
|
||||
Taro.setStorageSync(tokenKey, data.token)
|
||||
Taro.setStorageSync(userKey, data.currentUser.userInfo)
|
||||
}
|
||||
|
||||
export function setCurrentUser(user: UserInfoDTO) {
|
||||
Taro.setStorageSync(userKey, user)
|
||||
}
|
||||
|
||||
export function clearLoginSession() {
|
||||
Taro.removeStorageSync(tokenKey)
|
||||
Taro.removeStorageSync(userKey)
|
||||
}
|
||||
|
||||
export function redirectToLogin() {
|
||||
clearLoginSession()
|
||||
Taro.reLaunch({ url: '/pages/login/index' })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user