This commit is contained in:
nanguaboss
2026-08-03 00:07:01 +08:00
commit 71c6e8d9c1
112 changed files with 7815 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
<?php
/**
* Session 鉴权中间件
* 所有接口(登录/找回密码除外)均需通过 requireLogin() / checkPermission() 校验。
* 写类接口还必须通过 checkAjax()(要求 X-Requested-With: XMLHttpRequest)。
*/
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/response.php';
/** 启动 Session */
function startSession()
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
}
/** 校验写类接口的 Ajax 头 */
function checkAjax()
{
if (($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') !== 'XMLHttpRequest') {
Response::error('非法请求', 400);
}
}
/** 校验是否已登录,未登录返回 401 */
function requireLogin()
{
startSession();
if (empty($_SESSION['user_id'])) {
Response::error('未登录或会话已过期', 401);
}
}
/**
* 校验当前用户是否拥有某模块权限(同时校验登录态)
* @param string $module 菜单标识:dashboard/company/person/media/need/marketing/channel/document/preliminary/system/log
*/
function checkPermission($module)
{
requireLogin();
$perms = $_SESSION['permissions'] ?? [];
if (!in_array($module, $perms, true)) {
Response::error('无权操作', 403);
}
}
/** 仅超级管理员可操作(如操作日志查询) */
function requireSuperAdmin()
{
requireLogin();
if (($_SESSION['role_name'] ?? '') !== '超级管理员') {
Response::error('仅超级管理员可操作', 403);
}
}
/** 获取客户端 IP */
function clientIp()
{
return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
}
/** 分页参数解析:page / limit */
function pageParams($defaultLimit = 20)
{
$page = max(1, (int)($_REQUEST['page'] ?? 1));
$limit = min(200, max(1, (int)($_REQUEST['limit'] ?? $defaultLimit)));
return [$page, $limit];
}