From 860d939add9a2af0e8bf1097b8333a180072209c Mon Sep 17 00:00:00 2001 From: Guwan Date: Sat, 7 Dec 2024 20:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=B3=A8=E5=86=8C=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/user.ts | 13 +++++++------ src/views/LoginView.vue | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) 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('登录成功')