false, 'message' => '仅支持 POST 请求' ]); exit(); } // 获取原始输入 $input = file_get_contents('php://input'); if (empty($input)) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => '请求数据为空' ]); exit(); } // 解析 JSON 数据 $data = json_decode($input, true); if (json_last_error() !== JSON_ERROR_NONE) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => 'JSON 解析失败: ' . json_last_error_msg() ]); exit(); } // 验证必要字段 $requiredFields = ['user_id', 'user_type', 'username']; $missingFields = []; foreach ($requiredFields as $field) { if (!isset($data[$field]) || empty($data[$field])) { $missingFields[] = $field; } } if (!empty($missingFields)) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => '缺少必要字段: ' . implode(', ', $missingFields) ]); exit(); } // 验证 user_type 是否合法 $validUserTypes = ['student', 'parent', 'admin']; if (!in_array($data['user_type'], $validUserTypes)) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => '无效的用户类型' ]); exit(); } // 验证 JWT Token $authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? ''; if (empty($authHeader) || !preg_match('/^Bearer\s+(.+)$/i', $authHeader, $matches)) { http_response_code(401); echo json_encode([ 'success' => false, 'message' => '缺少认证令牌' ]); exit(); } $token = $matches[1]; $apiUrl = API_BASE_URL . '/api/auth/me'; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $apiUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $token, 'Content-Type: application/json' ], CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0 ]); $apiResponse = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200 || empty($apiResponse)) { http_response_code(401); echo json_encode([ 'success' => false, 'message' => '认证令牌无效或已过期' ]); exit(); } $tokenData = json_decode($apiResponse, true); if (!$tokenData || !isset($tokenData['success']) || !$tokenData['success']) { http_response_code(401); echo json_encode([ 'success' => false, 'message' => '认证验证失败' ]); exit(); } // 验证 token 中的 user_id 与请求数据中的 user_id 一致 $tokenUserId = $tokenData['data']['user_id'] ?? null; if ($tokenUserId === null || intval($tokenUserId) !== intval($data['user_id'])) { http_response_code(403); echo json_encode([ 'success' => false, 'message' => '身份验证不匹配' ]); exit(); } // 设置 Session 变量 $_SESSION['user_id'] = $data['user_id']; $_SESSION['user_type'] = $data['user_type']; $_SESSION['username'] = $data['username']; $_SESSION['real_name'] = $data['real_name'] ?? ''; $_SESSION['role'] = $data['role'] ?? ''; $_SESSION['login_time'] = time(); $_SESSION['jwt_token'] = $token; // 如果是学生,额外设置 student_id if ($data['user_type'] === 'student') { if (empty($data['student_id'])) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => '学生类型必须提供 student_id' ]); exit(); } $_SESSION['student_id'] = $data['student_id']; } // 保存 Session session_write_close(); // 返回成功响应 http_response_code(200); echo json_encode([ 'success' => true, 'message' => 'Session 保存成功' ]); exit();