This commit is contained in:
make
2026-01-09 01:28:23 +08:00
parent eb8c097777
commit 90fc16fbc3
241 changed files with 26518 additions and 3570 deletions

View File

@@ -0,0 +1,180 @@
<?php
namespace app\common\seo;
use think\facade\Request;
use app\common\helper\SiteStyle;
use think\helper\Str;
class SeoRenderer
{
protected array $cfg; // 冻结 cfg 中的 seo
protected array $pool; // 运行期 seo_phrase 池
public function __construct(array $tpStyle)
{
$this->cfg = $tpStyle['template_cfg']['seo'] ?? [];
// 根据 pool_id 取运行期池(不冻结)
// $poolId = $this->cfg['pool_id'] ?? '';
// $this->pool = SiteStyle::SEO_POOL[$poolId]['seo_phrase'] ?? [];
}
/**
* 只负责:选一条模板
*/
// public function getTemplate(string $field, string $strPage): string
// {
// // 1⃣ 取当前字段 + 页面类型的文案池
// $phrases = loadSeoFile(
// $this->cfg['pool_id'],
// $strPage,
// $field
// );
// if (!$phrases) {
// return '';
// }
// // 2⃣ 冻结的 slot
// $slot = $this->cfg['phrase_pick'][$strPage][$field] ?? 0;
// // slot 是二维
// if (isset($phrases[$slot]) && is_array($phrases[$slot])) {
// $list = $phrases[$slot];
// } else {
// $list = $phrases;
// }
// // 3⃣ 稳定消费 index不受扩容影响
// $len = $this->cfg['slot_len'][$strPage][$field] ?? count($list);
// $len = max(1, min($len, count($list)));
// // $idx = abs(crc32(
// // Request::pathinfo() ?: Request::host()
// // )) % $len;
// $idx = $this->cfg['phrase_idx'][$strPage][$field] ?? 0;
// return $list[$idx] ?? '';
// return $list[$idx] ?? '';
// }
// public function getTemplate(string $field, string $strPage): string
// {
// if (empty($this->cfg['pool_id'])) {
// return '';
// }
// // 1⃣ 加载运行期文案池(不冻结)
// $phrases = loadSeoFile(
// $this->cfg['pool_id'],
// $strPage,
// $field
// );
// if (!$phrases || !is_array($phrases)) {
// return '';
// }
// // 2⃣ 冻结 slot
// $slot = $this->cfg['phrase_pick'][$strPage][$field] ?? 0;
// if (isset($phrases[$slot]) && is_array($phrases[$slot])) {
// $list = $phrases[$slot];
// } else {
// // 兜底:当成单 slot
// $list = $phrases;
// }
// if (!$list) {
// return '';
// }
// // 3⃣ 冻结 idx核心
// $idx = $this->cfg['phrase_idx'][$strPage][$field] ?? 0;
// return $list[$idx] ?? '';
// }
public function getTemplate(string $field, string $strPage): string
{
if (empty($this->cfg['pool_id'])) {
return '';
}
// 1⃣ 加载运行期文案池
$phrases = loadSeoFile(
$this->cfg['pool_id'],
$strPage,
$field
);
if (!$phrases || !is_array($phrases)) {
return '';
}
// 2⃣ 冻结 slot
$slot = $this->cfg['phrase_pick'][$strPage][$field] ?? 0;
if (isset($phrases[$slot]) && is_array($phrases[$slot])) {
$list = $phrases[$slot];
} else {
$list = $phrases;
}
if (!$list) {
return '';
}
// ===============================
// 3⃣ idx 选择(终态)
// ===============================
$idx = 0;
switch ($strPage) {
case 'detail':
case 'play':
// 视频实体页seed + videoId
$videoId = (int) Request::param('intVId', 0);
if ($videoId > 0) {
$len = count($list);
$idx = abs(crc32(
$this->cfg['pool_id']
. '|detail|'
. $field
. '|' . $videoId
)) % $len;
}
break;
case 'detail_forge':
// 伪造详情seed + videoId + forgeId
$videoId = (int) Request::param('intVId', 0);
$forgeId = (int) Request::param('intVForgeId', 0);
if ($videoId > 0 && $forgeId > 0) {
$len = count($list);
$idx = abs(crc32(
$this->cfg['pool_id']
. '|detail_forge|'
. $field
. '|' . $videoId
. '|' . $forgeId
)) % $len;
}
break;
default:
// 非实体页:域名级冻结 idx
$idx = $this->cfg['phrase_idx'][$strPage][$field] ?? 0;
break;
}
return $list[$idx] ?? '';
}
}