gpt
This commit is contained in:
180
code/app/common/seo/SeoRenderer.php
Normal file
180
code/app/common/seo/SeoRenderer.php
Normal 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] ?? '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user