diff --git a/src/stores/user.ts b/src/stores/user.ts index 387090a..5d8248b 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -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(null) + let userInfo = ref(null) const token = ref(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 } -}) \ No newline at end of file +}) diff --git a/src/views/LoginView.vue b/src/views/LoginView.vue index f51bef2..b326329 100644 --- a/src/views/LoginView.vue +++ b/src/views/LoginView.vue @@ -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('登录成功')