From a420b5d908c3c83b6eb0585d0401e18744e49f11 Mon Sep 17 00:00:00 2001 From: heshunme Date: Sun, 30 Jun 2024 17:36:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E5=AE=8C=E5=96=84=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/authentication.js | 61 +++++++++++++++++------------- src/views/authentication/Login.vue | 20 +++++----- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/store/authentication.js b/src/store/authentication.js index 77ff04e..cc60c35 100644 --- a/src/store/authentication.js +++ b/src/store/authentication.js @@ -1,30 +1,37 @@ -const state = { - user: null -} +import { createStore } from 'vuex'; +import axios from 'axios'; -const mutations = { - setUser(state, user) { - state.user = user - } -} - -const actions = { - login({ commit }, user) { - commit('setUser', user) +const store = createStore({ + state: { + user: null, + isAuthenticated: false, }, - logout({ commit }) { - commit('setUser', null) - } -} + mutations: { + setUser(state, user) { + state.user = user; + state.isAuthenticated = true; + }, + clearUser(state) { + state.user = null; + state.isAuthenticated = false; + }, + }, + actions: { + async login({ commit }, credentials) { + const response = await axios.post('/api/login', credentials); + if (response.status === 200) { + commit('setUser', response.data.username); + } + return response; + }, + logout({ commit }) { + commit('clearUser'); + }, + }, + getters: { + isAuthenticated: (state) => state.isAuthenticated, + user: (state) => state.user, + }, +}); -const getters = { - isAuthenticated: state => !!state.user -} - -export default { - namespaced: true, - state, - mutations, - actions, - getters -} +export default store; diff --git a/src/views/authentication/Login.vue b/src/views/authentication/Login.vue index bd4a9da..d7a3430 100644 --- a/src/views/authentication/Login.vue +++ b/src/views/authentication/Login.vue @@ -2,7 +2,11 @@ import { ref } from 'vue'; import { ElInput, ElButton, ElCheckbox, ElForm, ElFormItem, ElMessage } from 'element-plus'; import 'element-plus/dist/index.css'; -import axios from 'axios'; +import { useStore } from 'vuex'; +import { useRouter } from 'vue-router'; + +const store = useStore(); +const router = useRouter(); const username = ref(''); const password = ref(''); @@ -12,26 +16,20 @@ const handleLogin = async () => { console.log('Logging in with', { username: username.value, password: password.value, rememberMe: rememberMe.value }); try { - const response = await axios.post('/api/login', { + const response = await store.dispatch('login', { username: username.value, password: password.value, }); if (response.status === 200) { - const data = response.data; - ElMessage.success(data.message); - // 保存token到本地存储或Vuex状态管理 - localStorage.setItem('token', data.token); - // 处理登录成功逻辑,例如跳转到主页 - // router.push('/home'); + ElMessage.success('登录成功'); + router.push('/profile'); } else { - // 处理非200的情况 ElMessage.error('登录失败,请稍后再试'); } } catch (error) { if (error.response && error.response.status === 401) { - const data = error.response.data; - ElMessage.error(data.message); + ElMessage.error('用户名或密码错误'); } else { ElMessage.error('登录失败,请稍后再试'); }