This commit is contained in:
make
2025-12-18 01:29:20 +08:00
parent 19314f6077
commit ae57b68f99

View File

@@ -483,46 +483,34 @@ EOF;
*/ */
public function generateVideoJsonByPage(int $intPage): void public function generateVideoJsonByPage(int $intPage): void
{ {
// ===== 1. 显式输出,防止静默失败 =====
echo "[JSON] start page={$intPage}\n"; echo "[JSON] start page={$intPage}\n";
$intStart = ($intPage - 1) * $this->intPageSize; $intStart = ($intPage - 1) * $this->intPageSize;
foreach ($this->getDomain() as $DomainModel) { foreach ($this->getDomain() as $DomainModel) {
// ===== 2. 安全路径处理 =====
$strDomainDir = rtrim($this->strSiteMapPath, '/') . '/' . $DomainModel->d_domain; $strDomainDir = rtrim($this->strSiteMapPath, '/') . '/' . $DomainModel->d_domain;
if (!is_dir($strDomainDir)) { if (!is_dir($strDomainDir)) {
if (!mkdir($strDomainDir, 0755, true) && !is_dir($strDomainDir)) { mkdir($strDomainDir, 0755, true);
file_put_contents(
'/tmp/video_json_error.log',
"[mkdir fail] {$strDomainDir}\n",
FILE_APPEND
);
continue;
}
} }
$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) { if ($fp === false) {
file_put_contents( file_put_contents('/tmp/video_json_error.log', "[open fail] {$tmpFile}\n", FILE_APPEND);
'/tmp/video_json_error.log',
"[fopen fail] {$strJsonFile}\n",
FILE_APPEND
);
continue; continue;
} }
// ===== 3. JSON 数组开始 ===== // ===== 一定先写 '[' =====
fwrite($fp, "[\n"); fwrite($fp, "[\n");
$isFirst = true; $isFirst = true;
foreach ($this->fetchVideosByBatch($intStart, $this->intPageSize) as $Video) { foreach ($this->fetchVideosByBatch($intStart, $this->intPageSize) as $Video) {
// ===== 4. 生成 URL =====
$strUri = $DomainModel->getFomartUrlEx( $strUri = $DomainModel->getFomartUrlEx(
"VIDEO_INFO_URL", "VIDEO_INFO_URL",
$DomainModel->d_domain, $DomainModel->d_domain,
@@ -534,33 +522,33 @@ EOF;
] ]
); );
$strFullUrl = 'https://www.' . $DomainModel->d_domain . $strUri; $fullUrl = 'https://www.' . $DomainModel->d_domain . $strUri;
// ===== 5. 构建数据(强制字符串) =====
$row = [ $row = [
'keyword' => (string)$Video->v_name, 'keyword' => (string)$Video->v_name,
'site' => (string)$DomainModel->d_domain, 'site' => (string)$DomainModel->d_domain,
'href' => (string)$strFullUrl, 'href' => (string)$fullUrl,
'url' => '#', 'url' => '#',
]; ];
// ===== 6. UTF-8 强制修复(关键) ===== foreach ($row as &$v) {
foreach ($row as $k => $v) {
if (is_string($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( $json = json_encode(
$row, $row,
JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE JSON_UNESCAPED_UNICODE
| JSON_UNESCAPED_SLASHES // ⭐ 关键
| JSON_INVALID_UTF8_SUBSTITUTE
); );
if ($json === false) { if ($json === false) {
file_put_contents( file_put_contents(
'/tmp/video_json_error.log', '/tmp/video_json_error.log',
"[json_encode fail] " . json_last_error_msg() . "\n", "[json fail] " . json_last_error_msg() . "\n",
FILE_APPEND FILE_APPEND
); );
continue; continue;
@@ -574,11 +562,14 @@ EOF;
$isFirst = false; $isFirst = false;
} }
// ===== 8. JSON 数组结束 ===== // ===== 一定写 ']' =====
fwrite($fp, "\n]"); fwrite($fp, "\n]");
fclose($fp); fclose($fp);
echo "[JSON] done {$strJsonFile}\n"; // ===== 原子替换,防止半文件 =====
rename($tmpFile, $finalFile);
echo "[JSON] done {$finalFile}\n";
} }
} }
} }