登录注册完成

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

View File

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