From d288d5e33dc71197ee07eccf9b54f2f0baf2b43e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BD=B3=E7=A8=8B?= <484502012@qq.com> Date: Mon, 23 Dec 2024 05:59:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E9=A1=B5=E9=9D=A2=E6=95=B4?= =?UTF-8?q?=E6=94=B91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frontend/src/stores/authStore.js | 32 +++ Frontend/src/stores/user.js | 39 ++-- Frontend/src/views/ArticleView.vue | 304 +++++++++++++++++++++++++---- Frontend/src/views/accounts.vue | 8 - 4 files changed, 311 insertions(+), 72 deletions(-) create mode 100644 Frontend/src/stores/authStore.js diff --git a/Frontend/src/stores/authStore.js b/Frontend/src/stores/authStore.js new file mode 100644 index 0000000..04dc7b8 --- /dev/null +++ b/Frontend/src/stores/authStore.js @@ -0,0 +1,32 @@ +// src/stores/authStore.js +import { defineStore } from 'pinia'; + +export const useAuthStore = defineStore('auth', { + state: () => ({ + isAuthenticated: false, // 用户是否已登录 + user: null, // 用户信息 + }), + actions: { + async login(phone, password) { + try { + const response = await loginUser(phone, password); + this.isAuthenticated = true; + this.user = response.data; // 假设 API 返回的数据包含用户信息 + ElMessage({ + message: '登录成功', + type: 'success' + }); + return true; + } catch (error) { + this.isAuthenticated = false; + this.user = null; + ElMessage.error('登录失败,请检查您的账号和密码'); + throw error; + } + }, + logout() { + this.isAuthenticated = false; + this.user = null; + } + } +}); diff --git a/Frontend/src/stores/user.js b/Frontend/src/stores/user.js index 6d27a62..a170077 100644 --- a/Frontend/src/stores/user.js +++ b/Frontend/src/stores/user.js @@ -1,32 +1,25 @@ -// stores/user.js +// user.js (store) import { defineStore } from 'pinia'; -import axios from 'axios'; export const useUserStore = defineStore('user', { state: () => ({ - user: null, + isAuthenticated: false, + currentUser: null, }), actions: { - async login(account, password) { - try { - const response = await axios.get('http://localhost:3002/users', { - params: { - account, - password, - }, - }); + loginUser(username, password) { + // 假设这里的逻辑是调用后端API进行登录验证 + // 登录成功后更新状态 + this.isAuthenticated = true; + this.currentUser = { username, password }; // 简化示例,实际应包含更多用户信息 - if (response.data.length > 0) { - this.user = response.data[0]; - // 确保 isAdmin 字段正确设置 - this.user.isAdmin = response.data[0].isAdmin || false; - } - } catch (error) { - console.error('登录失败:', error); - } + // 这里可以添加更多的业务逻辑,比如保存到本地存储等 }, - getCurrentUser() { - return this.user; - }, - }, + logoutUser() { + this.isAuthenticated = false; + this.currentUser = null; + + // 清除本地存储等操作 + } + } }); diff --git a/Frontend/src/views/ArticleView.vue b/Frontend/src/views/ArticleView.vue index 6ac4359..8815603 100644 --- a/Frontend/src/views/ArticleView.vue +++ b/Frontend/src/views/ArticleView.vue @@ -1,66 +1,248 @@ diff --git a/Frontend/src/views/accounts.vue b/Frontend/src/views/accounts.vue index 9eedcb2..6334f81 100644 --- a/Frontend/src/views/accounts.vue +++ b/Frontend/src/views/accounts.vue @@ -22,7 +22,6 @@