From a6dc616621103c7017cc5dd63f7d018107048057 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Thu, 1 Jan 2026 06:59:29 +0800 Subject: [PATCH 1/3] Add index.php for experiment handling logic --- experiments/index.php | 198 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 experiments/index.php diff --git a/experiments/index.php b/experiments/index.php new file mode 100644 index 0000000..470c842 --- /dev/null +++ b/experiments/index.php @@ -0,0 +1,198 @@ + false]; + } + + // 提取模型内容 + if (!isset($result['Data']['ModelCode']) || !$result['Data']['ModelCode']) { + return ['success' => false]; + } + + // 保存文件 + $filename = $contentId . '.nlogo'; + $filepath = EXPERIMENTS_DIR . $filename; + + if (file_put_contents($filepath, $result['Data']['ModelCode']) !== false) { + return [ + 'success' => true, + 'filename' => $filename, + ]; + } + + return ['success' => false]; +} + +/** + * 处理模型文件请求 + */ +function handleModelFile() { + $filename = $_GET['file'] ?? ''; + + if (!$filename) { + echo '缺少文件参数'; + exit(); + } + + // 安全过滤文件名 + $filename = basename($filename); + $filepath = EXPERIMENTS_DIR . $filename; + + if (!file_exists($filepath)) { + echo '模型文件不存在'; + exit(); + } + + header('Content-Type: text/plain'); + header('Content-Disposition: inline'); + header('Cache-Control: no-cache, must-revalidate'); + header('Access-Control-Allow-Origin: *'); + readfile($filepath); + exit(); +} + +/** + * 获取模型URL + */ +function getModelUrl($filename) { + $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'; + $host = $_SERVER['HTTP_HOST'] ?? 'tuweb.page.gd'; + $path = dirname($_SERVER['PHP_SELF'] ?? '/experiments'); + + if (substr($path, -1) !== '/') { + $path .= '/'; + } + + return $protocol . '://' . $host . $path . 'experiments/' . urlencode($filename); +} + +/** + * 清理过期文件 + */ +function cleanupExpiredFiles() { + if (!is_dir(EXPERIMENTS_DIR)) return; + + $files = scandir(EXPERIMENTS_DIR); + $now = time(); + + foreach ($files as $file) { + if ($file == '.' || $file == '..') continue; + + $filePath = EXPERIMENTS_DIR . $file; + + if (file_exists($filePath) && is_file($filePath)) { + $fileTime = filemtime($filePath); + + if (($now - $fileTime) > MODEL_LIFETIME && strpos($file,'.nlogo') !== false) { + @unlink($filePath); + } + } + } +} + +/** + * 获取模型数据 + */ +function getSav($contentId, $token, $authCode) { + $baseUrl = 'http://nlm-api-cn.turtlesim.com/'; + $url = $baseUrl . 'Contents/GetWorkspace'; + + $requestData = ['ContentID' => $contentId]; + + $headers = [ + "Content-Type: application/json", + "Accept: application/json", + "x-API-Token: " . $token, + "x-API-AuthCode: " . $authCode, + ]; + + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => true, + CURLOPT_POST => true, + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => json_encode($requestData), + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_TIMEOUT => 5, + CURLOPT_SSL_VERIFYPEER => true, + ]); + + $response = curl_exec($ch); + + if ($response === false) { + return ['error' => 'CURL Error']; + } + + $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $body = substr($response, $headerSize); + + curl_close($ch); + + $responseData = json_decode($body, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + return ['error' => 'JSON Parse Error']; + } + + $responseData['http_status'] = $statusCode; + + return $responseData; +} +?> From 581ee8e09c0fdabaa33705ba5eac6ac3611d3a36 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Thu, 1 Jan 2026 07:00:12 +0800 Subject: [PATCH 2/3] Add model.html for NetLogo Web experiments --- experiments/model.html | 221 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 experiments/model.html diff --git a/experiments/model.html b/experiments/model.html new file mode 100644 index 0000000..dd2fe10 --- /dev/null +++ b/experiments/model.html @@ -0,0 +1,221 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 8ed57c0ee344cc7c63a22d3a6a7817360df62533 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Thu, 1 Jan 2026 07:02:14 +0800 Subject: [PATCH 3/3] Update runExperiment function to redirect to experiments --- med.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/med.php b/med.php index af07d23..8c2c404 100644 --- a/med.php +++ b/med.php @@ -449,7 +449,7 @@ function navigate(page) { // 实验操作函数 function runExperiment() { - alert('进入实验功能即将开放'); + location.href='experiments/?id== $contentId ?>'; } function favoriteExperiment() {