登录注册完成

This commit is contained in:
Guwan 2024-12-07 20:27:48 +08:00
parent 0fab567259
commit 860d939add
2 changed files with 9 additions and 7 deletions

View File

@ -3,15 +3,17 @@ import { ref } from 'vue'
import type { UserInfo } from '@/api/user'
import { userApi } from '@/api/user'
export const useUserStore = defineStore('user', () => {
const userInfo = ref<UserInfo | null>(null)
let userInfo = ref<UserInfo | null>(null)
const token = ref<string | null>(null)
// 获取用户信息
const getUserInfo = async () => {
try {
const data = await userApi.getCurrentUser()
userInfo.value = data
userInfo = data
return data
} catch (error) {
console.error('获取用户信息失败:', error)
@ -20,9 +22,8 @@ export const useUserStore = defineStore('user', () => {
}
// 设置token
const setToken = (newToken: string) => {
token.value = newToken
localStorage.setItem('token', newToken)
const setToken = (token: string) => {
localStorage.setItem('token', token)
}
// 清除用户信息
@ -39,4 +40,4 @@ export const useUserStore = defineStore('user', () => {
setToken,
clearUserInfo
}
})
})

View File

@ -312,7 +312,8 @@ const handleLogin = async () => {
if (valid) {
loading.value = true
try {
const { token } = await userApi.login(formData)
const token = await userApi.login(formData)
console.log(token)
userStore.setToken(token)
await userStore.getUserInfo()
ElMessage.success('登录成功')