From 44fc686a0ad380cb75af7119f5352209df5b8ab2 Mon Sep 17 00:00:00 2001
From: Xiaoyu <3306310490@qq.com>
Date: Tue, 11 Feb 2025 15:52:41 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=A6=82=E4=BA=86pinia=E9=9B=86?=
=?UTF-8?q?=E4=B8=AD=E7=AE=A1=E7=90=86=E6=95=B0=E6=8D=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/layout/AdminLayout.vue | 35 +++++++++++++++-
src/stores/systemLog.ts | 63 +++++++++++++++++++++++++++++
src/stores/user.ts | 44 ++++++++++++++++++++
src/views/login/index.vue | 82 +++++++++++++++++++++++++++++++++-----
tsconfig.json | 8 +++-
vite.config.ts | 8 +++-
6 files changed, 227 insertions(+), 13 deletions(-)
create mode 100644 src/stores/systemLog.ts
create mode 100644 src/stores/user.ts
diff --git a/src/layout/AdminLayout.vue b/src/layout/AdminLayout.vue
index c5dd0e6..51c7bb9 100644
--- a/src/layout/AdminLayout.vue
+++ b/src/layout/AdminLayout.vue
@@ -1,6 +1,9 @@
@@ -112,7 +145,7 @@ const handleSelect = (key: string) => {
个人信息
修改密码
- 退出登录
+ 退出登录
diff --git a/src/stores/systemLog.ts b/src/stores/systemLog.ts
new file mode 100644
index 0000000..c368996
--- /dev/null
+++ b/src/stores/systemLog.ts
@@ -0,0 +1,63 @@
+import { defineStore } from 'pinia'
+
+interface LogEntry {
+ id: number;
+ type: string;
+ user: string;
+ action: string;
+ ip: string;
+ status: string;
+ detail: string;
+ createTime: string;
+}
+
+export const useSystemLogStore = defineStore('systemLog', {
+ state: () => ({
+ logs: [] as LogEntry[]
+ }),
+
+ actions: {
+ // 添加日志
+ addLog(log: Omit) {
+ const newLog: LogEntry = {
+ ...log,
+ id: Date.now(),
+ createTime: new Date().toLocaleString('zh-CN', {
+ year: 'numeric',
+ month: '2-digit',
+ day: '2-digit',
+ hour: '2-digit',
+ minute: '2-digit',
+ second: '2-digit',
+ hour12: false
+ }).replace(/\//g, '-')
+ };
+
+ this.logs.unshift(newLog);
+ this.saveLogs();
+ },
+
+ // 加载日志
+ loadLogs() {
+ const savedLogs = localStorage.getItem('systemLogs');
+ if (savedLogs) {
+ this.logs = JSON.parse(savedLogs);
+ }
+ },
+
+ // 保存日志到localStorage
+ saveLogs() {
+ localStorage.setItem('systemLogs', JSON.stringify(this.logs));
+ },
+
+ // 清空日志
+ clearLogs() {
+ this.logs = [];
+ this.saveLogs();
+ }
+ },
+
+ getters: {
+ getLogs: (state) => state.logs
+ }
+})
\ No newline at end of file
diff --git a/src/stores/user.ts b/src/stores/user.ts
new file mode 100644
index 0000000..fbd21fa
--- /dev/null
+++ b/src/stores/user.ts
@@ -0,0 +1,44 @@
+import { defineStore } from 'pinia'
+
+interface UserState {
+ token: string | null;
+ userInfo: {
+ username: string;
+ role: string;
+ } | null;
+}
+
+export const useUserStore = defineStore('user', {
+ state: (): UserState => ({
+ token: localStorage.getItem('token'),
+ userInfo: null
+ }),
+
+ actions: {
+ // 登录
+ async login(username: string, password: string) {
+ if (username === 'admin' && password === '123456') {
+ this.token = 'demo-token';
+ this.userInfo = {
+ username: 'admin',
+ role: '管理员'
+ };
+ localStorage.setItem('token', this.token);
+ return true;
+ }
+ return false;
+ },
+
+ // 退出登录
+ logout() {
+ this.token = null;
+ this.userInfo = null;
+ localStorage.removeItem('token');
+ }
+ },
+
+ getters: {
+ isLoggedIn: (state) => !!state.token,
+ username: (state) => state.userInfo?.username
+ }
+})
\ No newline at end of file
diff --git a/src/views/login/index.vue b/src/views/login/index.vue
index 8d76b6e..9813993 100644
--- a/src/views/login/index.vue
+++ b/src/views/login/index.vue
@@ -1,22 +1,75 @@