diff --git a/code/app/task/logic/VideoSiteMapLogic.php b/code/app/task/logic/VideoSiteMapLogic.php index a4e8d0a..4ef5cab 100644 --- a/code/app/task/logic/VideoSiteMapLogic.php +++ b/code/app/task/logic/VideoSiteMapLogic.php @@ -92,7 +92,7 @@ class VideoSiteMapLogic return $this->arrDomainModel; } - + /** * Generate Site Map & Video JSON * @@ -468,32 +468,61 @@ EOF; * } * ] */ + /** + * 线上专用:分页生成 Video JSON + * + * 每个文件格式: + * [ + * { + * "keyword": "...", + * "site": "...", + * "href": "https://www.xxx.com/...", + * "url": "#" + * } + * ] + */ public function generateVideoJsonByPage(int $intPage): void { + // ===== 1. 显式输出,防止静默失败 ===== + echo "[JSON] start page={$intPage}\n"; + $intStart = ($intPage - 1) * $this->intPageSize; foreach ($this->getDomain() as $DomainModel) { - $strDomainDir = $this->strSiteMapPath . $DomainModel->d_domain; + // ===== 2. 安全路径处理 ===== + $strDomainDir = rtrim($this->strSiteMapPath, '/') . '/' . $DomainModel->d_domain; + if (!is_dir($strDomainDir)) { - mkdir($strDomainDir, 0755, true); + if (!mkdir($strDomainDir, 0755, true) && !is_dir($strDomainDir)) { + file_put_contents( + '/tmp/video_json_error.log', + "[mkdir fail] {$strDomainDir}\n", + FILE_APPEND + ); + continue; + } } - $strJsonFile = sprintf( - '%s/video-list-%d.json', - $strDomainDir, - $intPage - ); + $strJsonFile = $strDomainDir . "/video-list-{$intPage}.json"; - $fp = fopen($strJsonFile, 'w'); + $fp = @fopen($strJsonFile, 'w'); + if ($fp === false) { + file_put_contents( + '/tmp/video_json_error.log', + "[fopen fail] {$strJsonFile}\n", + FILE_APPEND + ); + continue; + } - // JSON 数组开始 + // ===== 3. JSON 数组开始 ===== fwrite($fp, "[\n"); - $isFirst = true; foreach ($this->fetchVideosByBatch($intStart, $this->intPageSize) as $Video) { + // ===== 4. 生成 URL ===== $strUri = $DomainModel->getFomartUrlEx( "VIDEO_INFO_URL", $DomainModel->d_domain, @@ -505,34 +534,51 @@ EOF; ] ); - $strFullUrl = sprintf( - 'https://www.%s%s', - $DomainModel->d_domain, - $strUri - ); + $strFullUrl = 'https://www.' . $DomainModel->d_domain . $strUri; + // ===== 5. 构建数据(强制字符串) ===== $row = [ - 'keyword' => $Video->v_name, - 'site' => $DomainModel->d_domain, - 'href' => $strFullUrl, + 'keyword' => (string)$Video->v_name, + 'site' => (string)$DomainModel->d_domain, + 'href' => (string)$strFullUrl, 'url' => '#', ]; + // ===== 6. UTF-8 强制修复(关键) ===== + foreach ($row as $k => $v) { + if (is_string($v)) { + $row[$k] = mb_convert_encoding($v, 'UTF-8', 'UTF-8'); + } + } + + // ===== 7. JSON 编码(容错) ===== + $json = json_encode( + $row, + JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE + ); + + if ($json === false) { + file_put_contents( + '/tmp/video_json_error.log', + "[json_encode fail] " . json_last_error_msg() . "\n", + FILE_APPEND + ); + continue; + } + if (!$isFirst) { fwrite($fp, ",\n"); } - fwrite( - $fp, - json_encode($row, JSON_UNESCAPED_UNICODE) - ); - + fwrite($fp, $json); $isFirst = false; } - // JSON 数组结束 + // ===== 8. JSON 数组结束 ===== fwrite($fp, "\n]"); fclose($fp); + + echo "[JSON] done {$strJsonFile}\n"; } } }