增加企业产品分类
This commit is contained in:
@@ -103,11 +103,18 @@ class ArticleController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章分类列表
|
||||
* 获取文章分类列表(mete_articles_category:当前租户 tid、启用状态)
|
||||
* 支持 baseUrl=租户前台域名,与 getCenterNews 一致,便于 api 域名跨域调用时解析 tid
|
||||
*
|
||||
* @return Json
|
||||
*/
|
||||
public function getArticleCategories(): Json
|
||||
{
|
||||
$baseUrl = $this->request->get('baseUrl', '');
|
||||
if (!empty($baseUrl)) {
|
||||
$this->tenantId = BaseController::getTenantIdByDomain($baseUrl);
|
||||
}
|
||||
|
||||
$tid = $this->getTenantId();
|
||||
|
||||
if (empty($tid)) {
|
||||
@@ -120,6 +127,9 @@ class ArticleController extends BaseController
|
||||
|
||||
$articleCategories = ArticlesCategory::where('delete_time', null)
|
||||
->where('tid', $tid)
|
||||
->where('status', 1)
|
||||
->order('sort', 'asc')
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
|
||||
@@ -14,19 +14,22 @@ use think\db\exception\DbException;
|
||||
|
||||
use app\model\Cms\Articles;
|
||||
use app\model\Cms\ArticlesCategory;
|
||||
|
||||
use tidy;
|
||||
|
||||
class NewsCenterController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 根据域名获取新闻数据
|
||||
*
|
||||
* 返回字段:list 列表;total 当前租户下已发布新闻总数;count 当前筛选(全部或 cate)下的条数,翻页按 count 与 page_size。
|
||||
*
|
||||
* @return Json
|
||||
*/
|
||||
public function getCenterNews(): Json
|
||||
{
|
||||
$baseUrl = $this->request->get('baseUrl', '');
|
||||
|
||||
if (!empty($baseUrl)) {
|
||||
$baseUrl = (string) $this->request->param('baseUrl', '');
|
||||
|
||||
if ($baseUrl !== '') {
|
||||
$this->tenantId = BaseController::getTenantIdByDomain($baseUrl);
|
||||
}
|
||||
|
||||
@@ -40,15 +43,67 @@ class NewsCenterController extends BaseController
|
||||
]);
|
||||
}
|
||||
|
||||
// 查询该租户下的文章
|
||||
$articles = Articles::published()
|
||||
// param 合并 GET,避免个别环境下仅 get 取不到查询串
|
||||
$cateId = (int) $this->request->param('cate', 0);
|
||||
$listPage = (int) $this->request->param('page', 0);
|
||||
$pageSizeReq = (int) $this->request->param('page_size', 0);
|
||||
|
||||
// 点击左侧分类:文章 cate 可能挂在子分类上,需包含「该分类 + 其下所有子分类」id
|
||||
$categoryIdsForFilter = [];
|
||||
if ($cateId > 0) {
|
||||
$categoryIdsForFilter = $this->resolveCategoryBranchIds($cateId, $tid);
|
||||
if ($categoryIdsForFilter === []) {
|
||||
$categoryIdsForFilter = [$cateId];
|
||||
}
|
||||
}
|
||||
|
||||
$baseQuery = static function () use ($tid, $cateId, $categoryIdsForFilter) {
|
||||
$q = Articles::published()
|
||||
->where('tid', $tid)
|
||||
->where('delete_time', null);
|
||||
if ($cateId > 0 && $categoryIdsForFilter !== []) {
|
||||
$q->whereIn('cate', $categoryIdsForFilter);
|
||||
}
|
||||
|
||||
return $q;
|
||||
};
|
||||
|
||||
// 当前租户下「全部」已发布新闻数(不受 cate 筛选影响)
|
||||
$totalTenantNews = (int) Articles::published()
|
||||
->where('tid', $tid)
|
||||
->where('delete_time', null)
|
||||
->order('publish_date', 'desc')
|
||||
->limit(8)
|
||||
->select();
|
||||
->count();
|
||||
|
||||
// 处理图片:如果文章image为空,则取分类的image
|
||||
// 当前列表条件下的总数:tid +(可选)cate,用于翻页总页数 = ceil(count / page_size)
|
||||
$countFiltered = (int) $baseQuery()->count();
|
||||
|
||||
$useListPagination = $listPage > 0 && $pageSizeReq > 0;
|
||||
|
||||
if ($useListPagination) {
|
||||
// 新闻中心列表页:服务端分页;不要用查询参数名 page,避免与路由占位符冲突
|
||||
$pageSizeReq = max(1, min($pageSizeReq, 50));
|
||||
$listPage = max(1, $listPage);
|
||||
$articles = $baseQuery()
|
||||
->order('publish_date', 'desc')
|
||||
->page($listPage, $pageSizeReq)
|
||||
->select();
|
||||
} else {
|
||||
// 首页等:按 limit 取前 N 条(默认 8)
|
||||
$limit = (int) $this->request->param('limit', 8);
|
||||
if ($limit < 1) {
|
||||
$limit = 8;
|
||||
}
|
||||
if ($limit > 200) {
|
||||
$limit = 200;
|
||||
}
|
||||
|
||||
$articles = $baseQuery()
|
||||
->order('publish_date', 'desc')
|
||||
->limit($limit)
|
||||
->select();
|
||||
}
|
||||
|
||||
// 处理图片:如果文章 image 为空,则取分类的 image
|
||||
foreach ($articles as &$article) {
|
||||
if (empty($article['image']) && !empty($article['cate'])) {
|
||||
$category = ArticlesCategory::where('id', $article['cate'])
|
||||
@@ -59,14 +114,96 @@ class NewsCenterController extends BaseController
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($article);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $articles,
|
||||
// 当前租户下新闻总条数(全部状态为已发布)
|
||||
'total' => $totalTenantNews,
|
||||
// 当前筛选(全部或某分类)下的条数,列表翻页按 count 与 page_size 计算
|
||||
'count' => $countFiltered,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析分类筛选用的 id 列表:自身 + mete_articles_category 中 cid=该 id 的所有子孙(同 tid、未删除)
|
||||
*/
|
||||
private function resolveCategoryBranchIds(int $rootId, int $tid): array
|
||||
{
|
||||
if ($rootId <= 0 || $tid <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$root = ArticlesCategory::where('id', $rootId)
|
||||
->where('tid', $tid)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
if (!$root) {
|
||||
return [$rootId];
|
||||
}
|
||||
|
||||
$ids = [$rootId];
|
||||
$queue = [$rootId];
|
||||
while ($queue !== []) {
|
||||
$pid = array_shift($queue);
|
||||
$children = ArticlesCategory::where('cid', $pid)
|
||||
->where('tid', $tid)
|
||||
->where('delete_time', null)
|
||||
->column('id');
|
||||
foreach ($children as $id) {
|
||||
$id = (int) $id;
|
||||
if ($id > 0 && !in_array($id, $ids, true)) {
|
||||
$ids[] = $id;
|
||||
$queue[] = $id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取新闻详情
|
||||
* @param int $id 文章ID
|
||||
* @return Json
|
||||
*/
|
||||
public function getNewsDetail(int $id): Json
|
||||
{
|
||||
try {
|
||||
$baseUrl = $this->request->get('baseUrl', '');
|
||||
if (!empty($baseUrl)) {
|
||||
$this->tenantId = BaseController::getTenantIdByDomain($baseUrl);
|
||||
}
|
||||
|
||||
$tid = $this->getTenantId();
|
||||
$query = Articles::published()->where('id', $id);
|
||||
if ($tid > 0) {
|
||||
$query->where('tid', $tid);
|
||||
}
|
||||
$article = $query->find();
|
||||
if (!$article) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '文章不存在',
|
||||
'list' => [],
|
||||
]);
|
||||
}
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'list' => $article,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取新闻详情失败:' . $e->getMessage(),
|
||||
'list' => [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上一篇下一篇
|
||||
* @param int $id 文章ID
|
||||
|
||||
@@ -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