From c8893e4473d453ef2982152d26a5d5da3cb26dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Wed, 9 Apr 2025 14:07:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E7=AE=A1=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/superadmin/config/route.php | 5 ++ .../superadmin/controller/Auth.php | 63 +++++++++++++++++++ .../superadmin/data/administrator.sql | 2 + .../superadmin/model/Administrator.php | 48 ++++++++++++++ Server/route/route.php | 4 +- SuperAdmin/app/dashboard/page.tsx | 43 ++++++++++++- SuperAdmin/app/login/page.tsx | 42 +++++++++++-- SuperAdmin/hooks/useAuthCheck.ts | 19 ++++++ SuperAdmin/lib/utils.ts | 8 +++ 9 files changed, 228 insertions(+), 6 deletions(-) create mode 100644 Server/application/superadmin/config/route.php create mode 100644 Server/application/superadmin/controller/Auth.php create mode 100644 Server/application/superadmin/data/administrator.sql create mode 100644 Server/application/superadmin/model/Administrator.php create mode 100644 SuperAdmin/hooks/useAuthCheck.ts diff --git a/Server/application/superadmin/config/route.php b/Server/application/superadmin/config/route.php new file mode 100644 index 00000000..5413a593 --- /dev/null +++ b/Server/application/superadmin/config/route.php @@ -0,0 +1,5 @@ +request->isPost()) { + return json(['code' => 405, 'msg' => '请求方法不允许']); + } + + $account = $this->request->post('account'); + $password = $this->request->post('password'); + + if (empty($account) || empty($password)) { + return json(['code' => 400, 'msg' => '账号和密码不能为空']); + } + + $admin = Administrator::login($account, $password); + + if (!$admin) { + return json(['code' => 401, 'msg' => '账号或密码错误']); + } + + // 更新登录信息 + $admin->lastLoginTime = time(); + $admin->lastLoginIp = $this->request->ip(); + $admin->save(); + + // 设置登录Cookie,有效期24小时 + cookie('admin_id', $admin->id, 86400); + cookie('admin_token', $this->createToken($admin), 86400); + + return json([ + 'code' => 200, + 'msg' => '登录成功', + 'data' => [ + 'id' => $admin->id, + 'name' => $admin->name, + 'account' => $admin->account, + 'token' => cookie('admin_token') + ] + ]); + } + + /** + * 创建登录令牌 + * @param Administrator $admin + * @return string + */ + private function createToken($admin) + { + $data = $admin->id . '|' . $admin->account . '|' . time(); + return md5($data . 'cunkebao_admin_secret'); + } +} \ No newline at end of file diff --git a/Server/application/superadmin/data/administrator.sql b/Server/application/superadmin/data/administrator.sql new file mode 100644 index 00000000..02073d56 --- /dev/null +++ b/Server/application/superadmin/data/administrator.sql @@ -0,0 +1,2 @@ +INSERT INTO `tk_administrators` (`name`, `account`, `password`, `status`, `createTime`, `updateTime`) +VALUES ('超级管理员', 'admin', MD5('123456'), 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()); \ No newline at end of file diff --git a/Server/application/superadmin/model/Administrator.php b/Server/application/superadmin/model/Administrator.php new file mode 100644 index 00000000..be2ce650 --- /dev/null +++ b/Server/application/superadmin/model/Administrator.php @@ -0,0 +1,48 @@ +find(); + } +} \ No newline at end of file diff --git a/Server/route/route.php b/Server/route/route.php index 7dfa30d5..cd50328c 100644 --- a/Server/route/route.php +++ b/Server/route/route.php @@ -24,12 +24,14 @@ include __DIR__ . '/../application/api/config/route.php'; // 加载Common模块路由配置 include __DIR__ . '/../application/common/config/route.php'; -// 加载Devices模块路由配置 +// 加载Cunkebao模块路由配置 include __DIR__ . '/../application/cunkebao/config/route.php'; // 加载Store模块路由配置 include __DIR__ . '/../application/store/config/route.php'; +// 加载Superadmin模块路由配置 +include __DIR__ . '/../application/superadmin/config/route.php'; // 加载CozeAI模块路由配置 include __DIR__ . '/../application/cozeai/config/route.php'; diff --git a/SuperAdmin/app/dashboard/page.tsx b/SuperAdmin/app/dashboard/page.tsx index dfe14b45..2daa9ee4 100644 --- a/SuperAdmin/app/dashboard/page.tsx +++ b/SuperAdmin/app/dashboard/page.tsx @@ -1,11 +1,52 @@ +"use client" + +import { useEffect, useState } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Users, FolderKanban, UserCog } from "lucide-react" +import useAuthCheck from "@/hooks/useAuthCheck" export default function DashboardPage() { + const [greeting, setGreeting] = useState("") + const [userName, setUserName] = useState("") + + // 验证用户是否已登录 + useAuthCheck() + + useEffect(() => { + // 获取用户信息 + const adminInfo = localStorage.getItem("admin_info") + if (adminInfo) { + try { + const { name } = JSON.parse(adminInfo) + setUserName(name || "管理员") + } catch (err) { + console.error("解析用户信息失败:", err) + } + } + + // 获取当前时间 + const hour = new Date().getHours() + let timeGreeting = "" + + if (hour >= 5 && hour < 12) { + timeGreeting = "上午好" + } else if (hour >= 12 && hour < 14) { + timeGreeting = "中午好" + } else if (hour >= 14 && hour < 18) { + timeGreeting = "下午好" + } else { + timeGreeting = "晚上好" + } + + setGreeting(timeGreeting) + }, []) + return (

欢迎使用超级管理员后台

-

通过此平台,您可以管理项目、客户和管理员权限。

+

+ {greeting},{userName}!通过此平台,您可以管理项目、客户和管理员权限。 +

diff --git a/SuperAdmin/app/login/page.tsx b/SuperAdmin/app/login/page.tsx index da72cff5..0397d814 100644 --- a/SuperAdmin/app/login/page.tsx +++ b/SuperAdmin/app/login/page.tsx @@ -8,22 +8,55 @@ import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Label } from "@/components/ui/label" +import { md5 } from "@/lib/utils" export default function LoginPage() { const [username, setUsername] = useState("") const [password, setPassword] = useState("") const [isLoading, setIsLoading] = useState(false) + const [error, setError] = useState("") const router = useRouter() const handleLogin = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) + setError("") - // Simulate login API call - setTimeout(() => { + try { + // 对密码进行MD5加密 + const encryptedPassword = md5(password) + + // 调用登录接口 + const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/auth/login`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + account: username, + password: encryptedPassword + }), + credentials: "include" + }) + + const result = await response.json() + + if (result.code === 200) { + // 保存用户信息到本地存储 + localStorage.setItem("admin_info", JSON.stringify(result.data)) + localStorage.setItem("admin_token", result.data.token) + + // 跳转到仪表盘 + router.push("/dashboard") + } else { + setError(result.msg || "登录失败") + } + } catch (err) { + console.error("登录失败:", err) + setError("网络错误,请稍后再试") + } finally { setIsLoading(false) - router.push("/dashboard") - }, 1500) + } } return ( @@ -32,6 +65,7 @@ export default function LoginPage() { 超级管理员后台 请输入您的账号和密码登录系统 + {error &&

{error}

}
diff --git a/SuperAdmin/hooks/useAuthCheck.ts b/SuperAdmin/hooks/useAuthCheck.ts new file mode 100644 index 00000000..085dcb85 --- /dev/null +++ b/SuperAdmin/hooks/useAuthCheck.ts @@ -0,0 +1,19 @@ +import { useEffect } from 'react'; +import { useRouter } from 'next/navigation'; + +/** + * 检查用户是否已登录的钩子 + * @param redirectTo 如果未登录,重定向到的路径 + */ +export default function useAuthCheck(redirectTo: string = '/login') { + const router = useRouter(); + + useEffect(() => { + // 检查本地存储中是否有token + const token = localStorage.getItem('admin_token'); + + if (!token) { + router.push(redirectTo); + } + }, [redirectTo, router]); +} \ No newline at end of file diff --git a/SuperAdmin/lib/utils.ts b/SuperAdmin/lib/utils.ts index bd0c391d..ad400f14 100644 --- a/SuperAdmin/lib/utils.ts +++ b/SuperAdmin/lib/utils.ts @@ -1,6 +1,14 @@ import { clsx, type ClassValue } from "clsx" import { twMerge } from "tailwind-merge" +import crypto from "crypto" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +/** + * MD5加密函数 + */ +export function md5(text: string): string { + return crypto.createHash("md5").update(text).digest("hex") +}