From 66352ed3c980e4fce70a177ef75acd349cb40521 Mon Sep 17 00:00:00 2001 From: heshunme Date: Mon, 1 Jul 2024 21:01:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=A5=BD=E4=BA=86=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=90=8E=E8=87=AA=E5=8A=A8=E8=B7=B3=E8=BD=AC=E5=88=B0Profile?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E5=8A=9F=E8=83=BD=E3=80=82=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BA=86=E7=99=BB=E5=BD=95=E6=97=B6=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E4=BC=A0=E6=9D=A5=E7=9A=84=E5=AD=97=E6=AE=B5=E6=B2=A1=E5=AF=B9?= =?UTF-8?q?=E4=B8=8A=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/authentication.js | 14 ++-------- src/views/authentication/Profile.vue | 38 +++++++++++++++++++++------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/store/authentication.js b/src/store/authentication.js index 82c2e83..ffbf71c 100644 --- a/src/store/authentication.js +++ b/src/store/authentication.js @@ -1,19 +1,16 @@ import axios from 'axios'; const state = { - user: null, isAuthenticated: false, token: null, }; const mutations = { - setUser(state, {user, token}) { - state.user = user; + setUser(state, {token}) { state.isAuthenticated = true; state.token = token; }, clearUser(state) { - state.user = null; state.isAuthenticated = false; state.token = null; }, @@ -23,20 +20,13 @@ const actions = { async login({commit}, credentials) { const response = await axios.post('/api/login', credentials); if (response.status === 200) { - commit('setUser', {user: response.data.username, token: response.data.token}); + commit('setUser', {token: response.data.token}); } return response; }, logout({commit}) { commit('clearUser'); }, - async fetchUserProfile({commit}) { - const response = await axios.get('/api/user-profile'); - if (response.status === 200) { - commit('setUser', {user: response.data}); - } - return response; - }, }; const getters = { diff --git a/src/views/authentication/Profile.vue b/src/views/authentication/Profile.vue index 812dbff..a59b8f7 100644 --- a/src/views/authentication/Profile.vue +++ b/src/views/authentication/Profile.vue @@ -2,8 +2,12 @@ import {onMounted, ref} from 'vue'; import 'element-plus/dist/index.css'; import {useStore} from 'vuex'; +import axios from "axios"; +import {ElMessage} from "element-plus"; +import { useRouter } from 'vue-router'; const store = useStore(); +const router = useRouter(); const nickname = ref(''); const phoneNumber = ref(''); @@ -13,15 +17,31 @@ const role = ref(''); const createdAt = ref(''); onMounted(async () => { - const response = await store.dispatch('authentication/fetchUserProfile'); - if (response.status === 200) { - const user = response.data; - nickname.value = user.nickname; - phoneNumber.value = user.phoneNumber; - email.value = user.email; - department.value = user.department; - role.value = user.role; - createdAt.value = user.createdAt; + try { + // 从Vuex中获取JWT token + const token = store.getters['authentication/token']; + if (!token) { + store.commit('authentication/logout'); + router.push('/login'); + } + + // 发送请求并附带token参数 + const response = await axios.get(`/api/userProfile?token=${token}`); + + if (response.status === 200) { + const user = response.data; + nickname.value = user.nickname; + phoneNumber.value = user.phoneNumber; + email.value = user.email; + department.value = user.department; + role.value = user.role; + createdAt.value = user.createdAt; + } else { + ElMessage.error("获取用户信息失败,请重试"); + } + } catch (error) { + console.error('Error fetching user profile:', error); + ElMessage.error("获取用户信息失败,请重试"); } });