From ae57b68f992f030bea7bb5913d37f3a49be6380c Mon Sep 17 00:00:00 2001 From: make Date: Thu, 18 Dec 2025 01:29:20 +0800 Subject: [PATCH] debug --- code/app/task/logic/VideoSiteMapLogic.php | 51 ++++++++++------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/code/app/task/logic/VideoSiteMapLogic.php b/code/app/task/logic/VideoSiteMapLogic.php index 4ef5cab..de658fc 100644 --- a/code/app/task/logic/VideoSiteMapLogic.php +++ b/code/app/task/logic/VideoSiteMapLogic.php @@ -483,46 +483,34 @@ EOF; */ public function generateVideoJsonByPage(int $intPage): void { - // ===== 1. 显式输出,防止静默失败 ===== echo "[JSON] start page={$intPage}\n"; $intStart = ($intPage - 1) * $this->intPageSize; foreach ($this->getDomain() as $DomainModel) { - // ===== 2. 安全路径处理 ===== $strDomainDir = rtrim($this->strSiteMapPath, '/') . '/' . $DomainModel->d_domain; if (!is_dir($strDomainDir)) { - if (!mkdir($strDomainDir, 0755, true) && !is_dir($strDomainDir)) { - file_put_contents( - '/tmp/video_json_error.log', - "[mkdir fail] {$strDomainDir}\n", - FILE_APPEND - ); - continue; - } + mkdir($strDomainDir, 0755, true); } - $strJsonFile = $strDomainDir . "/video-list-{$intPage}.json"; + $finalFile = $strDomainDir . "/video-list-{$intPage}.json"; + $tmpFile = $finalFile . '.tmp'; - $fp = @fopen($strJsonFile, 'w'); + $fp = fopen($tmpFile, 'w'); if ($fp === false) { - file_put_contents( - '/tmp/video_json_error.log', - "[fopen fail] {$strJsonFile}\n", - FILE_APPEND - ); + file_put_contents('/tmp/video_json_error.log', "[open fail] {$tmpFile}\n", FILE_APPEND); continue; } - // ===== 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, @@ -534,33 +522,33 @@ EOF; ] ); - $strFullUrl = 'https://www.' . $DomainModel->d_domain . $strUri; + $fullUrl = 'https://www.' . $DomainModel->d_domain . $strUri; - // ===== 5. 构建数据(强制字符串) ===== $row = [ 'keyword' => (string)$Video->v_name, 'site' => (string)$DomainModel->d_domain, - 'href' => (string)$strFullUrl, + 'href' => (string)$fullUrl, 'url' => '#', ]; - // ===== 6. UTF-8 强制修复(关键) ===== - foreach ($row as $k => $v) { + foreach ($row as &$v) { if (is_string($v)) { - $row[$k] = mb_convert_encoding($v, 'UTF-8', 'UTF-8'); + $v = mb_convert_encoding($v, 'UTF-8', 'UTF-8'); } } + unset($v); - // ===== 7. JSON 编码(容错) ===== $json = json_encode( $row, - JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE + JSON_UNESCAPED_UNICODE + | JSON_UNESCAPED_SLASHES // ⭐ 关键 + | JSON_INVALID_UTF8_SUBSTITUTE ); if ($json === false) { file_put_contents( '/tmp/video_json_error.log', - "[json_encode fail] " . json_last_error_msg() . "\n", + "[json fail] " . json_last_error_msg() . "\n", FILE_APPEND ); continue; @@ -574,11 +562,14 @@ EOF; $isFirst = false; } - // ===== 8. JSON 数组结束 ===== + // ===== 一定写 ']' ===== fwrite($fp, "\n]"); fclose($fp); - echo "[JSON] done {$strJsonFile}\n"; + // ===== 原子替换,防止半文件 ===== + rename($tmpFile, $finalFile); + + echo "[JSON] done {$finalFile}\n"; } } }