create-base
Version:
快速构建项目基础框架,支持vue/uni-app/后台管理模板等
28 lines (25 loc) • 653 B
text/typescript
import type { UserModel, UserRow } from '@/types/user'
import { TOKEN_KEY } from '@/utils/constant'
import { login } from '@/api/user'
interface UserState {
token: string
profile: UserRow
}
export const useUserStore = defineStore('user', {
state: (): UserState => ({
token: localStorage.getItem(TOKEN_KEY) || '',
profile: {} as UserRow
}),
actions: {
async login(model: UserModel) {
const { token, profile } = await login(model)
this.token = token
this.profile = profile
localStorage.setItem(TOKEN_KEY, token)
},
clear() {
localStorage.removeItem(TOKEN_KEY)
this.$reset()
}
}
})