增加企业产品分类
This commit is contained in:
@@ -18,6 +18,8 @@ use app\model\Cms\Friendlink;
|
||||
use app\model\Cms\Services;
|
||||
use app\model\Cms\Products;
|
||||
use app\model\Tenant\Tenant;
|
||||
use app\model\Cms\Articles;
|
||||
use app\model\Cms\ArticlesCategory;
|
||||
|
||||
class Index extends BaseController
|
||||
{
|
||||
@@ -154,6 +156,12 @@ class Index extends BaseController
|
||||
die('tenantId未获取到,请检查域名解析');
|
||||
}
|
||||
|
||||
// 与 NewsCenterController::getNewsDetail 一致:已发布 + 当前租户(网页直出,非 JSON)
|
||||
$templateVars = $this->buildArticleDetailTemplateVars($id, $tid);
|
||||
if ($templateVars === null) {
|
||||
return response('文章不存在', 404, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
// 获取租户选择的模板
|
||||
$themeKey = 'default';
|
||||
if ($tid > 0) {
|
||||
@@ -176,13 +184,13 @@ class Index extends BaseController
|
||||
|
||||
// 优先使用 article_detail 模板
|
||||
if (is_file($templateFile)) {
|
||||
return $this->renderPhpTemplate($templateFile, $themeUrlPath);
|
||||
return $this->renderPhpTemplate($templateFile, $themeUrlPath, $templateVars);
|
||||
} elseif (is_file($templateHtmlFile)) {
|
||||
$content = file_get_contents($templateHtmlFile);
|
||||
$content = $this->fixTemplateAssets($content, $themeUrlPath);
|
||||
return response($content, 200, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
} elseif (is_file($blogDetailsFile)) {
|
||||
return $this->renderPhpTemplate($blogDetailsFile, $themeUrlPath);
|
||||
return $this->renderPhpTemplate($blogDetailsFile, $themeUrlPath, $templateVars);
|
||||
} elseif (is_file($blogDetailsHtmlFile)) {
|
||||
$content = file_get_contents($blogDetailsHtmlFile);
|
||||
$content = $this->fixTemplateAssets($content, $themeUrlPath);
|
||||
@@ -192,6 +200,81 @@ class Index extends BaseController
|
||||
return response('文章不存在', 404, ['Content-Type' => 'text/html; charset=utf-8']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装文章详情页模板变量(数据源与 getNewsDetail 一致,并限定租户 + 已发布)
|
||||
*
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
private function buildArticleDetailTemplateVars(int $id, int $tid): ?array
|
||||
{
|
||||
$article = Articles::published()
|
||||
->where('id', $id)
|
||||
->where('tid', $tid)
|
||||
->find();
|
||||
|
||||
if (!$article) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = $article->toArray();
|
||||
|
||||
$categoryName = '未分类';
|
||||
if (!empty($row['cate'])) {
|
||||
$cat = ArticlesCategory::where('id', $row['cate'])
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
if ($cat) {
|
||||
$categoryName = (string) $cat['name'];
|
||||
}
|
||||
}
|
||||
|
||||
$coverPath = $row['image'] ?? '';
|
||||
if ($coverPath === '' && !empty($row['cate'])) {
|
||||
$category = ArticlesCategory::where('id', $row['cate'])
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
if ($category && !empty($category['image'])) {
|
||||
$coverPath = (string) $category['image'];
|
||||
}
|
||||
}
|
||||
|
||||
$apiUrl = rtrim((string) (Env::get('app.api_url', '') ?: 'https://api.yunzer.cn'), '/');
|
||||
$articleCoverUrl = $this->resolveArticleMediaUrl($coverPath, $apiUrl);
|
||||
|
||||
$descPlain = strip_tags((string) ($row['desc'] ?? ''));
|
||||
$title = (string) ($row['title'] ?? '详情');
|
||||
$descShort = $descPlain !== ''
|
||||
? (function_exists('mb_substr') ? mb_substr($descPlain, 0, 160) : substr($descPlain, 0, 160))
|
||||
: $title;
|
||||
|
||||
return [
|
||||
'article' => $row,
|
||||
'articleCoverUrl' => $articleCoverUrl,
|
||||
'articleCategoryName' => $categoryName,
|
||||
'pageTitle' => $title . ' - 新闻详情',
|
||||
'pageDescription' => $descShort,
|
||||
'pageKeywords' => $title,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章封面等资源 URL(与列表页逻辑一致:相对路径拼 API 域名)
|
||||
*/
|
||||
private function resolveArticleMediaUrl(string $path, string $apiUrl): string
|
||||
{
|
||||
if ($path === '') {
|
||||
return '';
|
||||
}
|
||||
if (strpos($path, 'http://') === 0 || strpos($path, 'https://') === 0) {
|
||||
return $path;
|
||||
}
|
||||
if (strpos($path, '/') === 0) {
|
||||
return $apiUrl . $path;
|
||||
}
|
||||
|
||||
return $apiUrl . '/' . $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复模板中的资源路径
|
||||
* 将相对路径 (assets/, css/, js/, images/) 转换为绝对路径 (/themes/xxx/)
|
||||
@@ -223,7 +306,7 @@ class Index extends BaseController
|
||||
/**
|
||||
* 渲染PHP模板文件
|
||||
*/
|
||||
private function renderPhpTemplate(string $templateFile, string $themeUrlPath = '')
|
||||
private function renderPhpTemplate(string $templateFile, string $themeUrlPath = '', array $templateVars = [])
|
||||
{
|
||||
// 定义模板基础URL常量,供模板使用
|
||||
if (!defined('THEME_URL')) {
|
||||
@@ -234,6 +317,7 @@ class Index extends BaseController
|
||||
// 获取URL参数
|
||||
$getParams = Request::get();
|
||||
extract($getParams, EXTR_OVERWRITE);
|
||||
extract($templateVars, EXTR_OVERWRITE);
|
||||
include $templateFile;
|
||||
$content = ob_get_clean();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user