增加企业产品分类
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
|
||||
|
||||
Reference in New Issue
Block a user