diff --git a/Server/application/superadmin/config/route.php b/Server/application/superadmin/config/route.php index 4a086ac1..7748ea26 100644 --- a/Server/application/superadmin/config/route.php +++ b/Server/application/superadmin/config/route.php @@ -1,19 +1,28 @@ middleware(['app\\superadmin\\middleware\\AdminAuth']); \ No newline at end of file diff --git a/Server/application/superadmin/controller/Auth.php b/Server/application/superadmin/controller/Auth.php index 47a9024d..bffa5b01 100644 --- a/Server/application/superadmin/controller/Auth.php +++ b/Server/application/superadmin/controller/Auth.php @@ -57,7 +57,7 @@ class Auth extends Controller */ private function createToken($admin) { - $data = $admin->id . '|' . $admin->account . '|' . time(); + $data = $admin->id . '|' . $admin->account; return md5($data . 'cunkebao_admin_secret'); } } \ No newline at end of file diff --git a/Server/application/superadmin/middleware/AdminAuth.php b/Server/application/superadmin/middleware/AdminAuth.php new file mode 100644 index 00000000..f4bb9897 --- /dev/null +++ b/Server/application/superadmin/middleware/AdminAuth.php @@ -0,0 +1,74 @@ + 401, + 'msg' => '请先登录', + 'data' => null + ]); + } + + // 获取管理员信息 + $admin = \app\superadmin\model\Administrator::where([ + ['id', '=', $adminId], + ['status', '=', 1], + ['deleteTime', '=', 0] + ])->find(); + + // 如果管理员不存在,返回401未授权 + if (!$admin) { + return json([ + 'code' => 401, + 'msg' => '管理员账号不存在或已被禁用', + 'data' => null + ]); + } + + // 验证Token是否有效 + $expectedToken = $this->createToken($admin); + + if ($adminToken !== $expectedToken) { + return json([ + 'code' => 401, + 'msg' => '登录已过期,请重新登录', + 'data' => null + ]); + } + + // 将管理员信息绑定到请求对象,方便后续控制器使用 + $request->adminInfo = $admin; + + // 继续执行后续操作 + return $next($request); + } + + /** + * 创建登录令牌 + * @param \app\superadmin\model\Administrator $admin + * @return string + */ + private function createToken($admin) + { + $data = $admin->id . '|' . $admin->account; + return md5($data . 'cunkebao_admin_secret'); + } +} \ No newline at end of file diff --git a/Server/public/index.php b/Server/public/index.php index ea854690..ed629339 100644 --- a/Server/public/index.php +++ b/Server/public/index.php @@ -12,16 +12,16 @@ // [ 应用入口文件 ] namespace think; -//处理跨域预检请求 -if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){ - //允许的源域名 - header("Access-Control-Allow-Origin: *"); - //允许的请求头信息 - header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization"); - //允许的请求类型 - header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH'); - exit; -} +////处理跨域预检请求 +//if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){ +// //允许的源域名 +// header("Access-Control-Allow-Origin: *"); +// //允许的请求头信息 +// header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization"); +// //允许的请求类型 +// header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH'); +// exit; +//} define('ROOT_PATH', dirname(__DIR__)); define('DS', DIRECTORY_SEPARATOR); diff --git a/Server/route/route.php b/Server/route/route.php index cd50328c..94f08633 100644 --- a/Server/route/route.php +++ b/Server/route/route.php @@ -12,11 +12,11 @@ use think\facade\Route; // 允许跨域 - header('Access-Control-Allow-Origin: *'); - header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, PATCH'); - header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With, X-Token, X-Api-Token'); - header('Access-Control-Max-Age: 1728000'); - header('Access-Control-Allow-Credentials: true'); +// header('Access-Control-Allow-Origin: *'); +// header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, PATCH'); +// header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With, X-Token, X-Api-Token'); +// header('Access-Control-Max-Age: 1728000'); +// header('Access-Control-Allow-Credentials: true'); // 加载Store模块路由配置 include __DIR__ . '/../application/api/config/route.php'; diff --git a/SuperAdmin/app/dashboard/layout.tsx b/SuperAdmin/app/dashboard/layout.tsx index 557729fe..aa73fed2 100644 --- a/SuperAdmin/app/dashboard/layout.tsx +++ b/SuperAdmin/app/dashboard/layout.tsx @@ -1,11 +1,13 @@ "use client" import type React from "react" -import { useState } from "react" +import { useState, useEffect } from "react" +import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" -import { Menu, X, LogOut } from "lucide-react" +import { Menu, X } from "lucide-react" import { Sidebar } from "@/components/layout/sidebar" import { Header } from "@/components/layout/header" +import { getAdminInfo } from "@/lib/utils" export default function DashboardLayout({ children, @@ -13,6 +15,20 @@ export default function DashboardLayout({ children: React.ReactNode }) { const [sidebarOpen, setSidebarOpen] = useState(true) + const router = useRouter() + + // 认证检查 + useEffect(() => { + const checkAuth = () => { + const adminInfo = getAdminInfo() + if (!adminInfo) { + // 未登录时跳转到登录页 + router.push('/login') + } + } + + checkAuth() + }, [router]) return (
{error}
}