更新:产品中心数据绑定

virtual
zwesy 2020-12-03 15:33:56 +08:00
parent 7af1255408
commit c17d3b62e7
4 changed files with 182 additions and 44 deletions

View File

@ -5,6 +5,26 @@ use app\model\{Article as MArticle, Category};
class Article extends Base class Article extends Base
{ {
//列表页
public function index()
{
$categoryId = input('param.category_id/d', 0);
if($categoryId <= 0){
return $this->error('错误页面');
}
$category = Category::getById($categoryId);
if(empty($category)){
return $this->error('错误页面');
}
$description = $category['description'] ? $category['description'] : $this->system['seo_description'];
$this->setSeo($category['title'], $this->system['seo_keywords'], $description);
$this->data['category'] = $category;
$this->data['categoryId'] = $categoryId;
$this->templateAssign($category);
return $this->view($category['template_list'] ?? '');
}
//详情 //详情
public function detail($id=0) public function detail($id=0)
{ {
@ -17,51 +37,62 @@ class Article extends Base
} }
MArticle::updateById($id, ['views' => $article['views'] + 1]); MArticle::updateById($id, ['views' => $article['views'] + 1]);
$category = Category::getById($article['category_id']); $category = Category::getById($article['category_id']);
$prev = MArticle::getPrevArticleByIdAndCategoryId($id, $article['category_id']); $TopCId = Category::firstGradeById($category['id']);
$next = MArticle::getNextArticleByIdAndCategoryId($id, $article['category_id']); if($TopCId == $category['id']) {
$topCategory = $category;
} else {
$topCategory = Category::getById($TopCId);
}
$keywords = $article['seo_keywords'] ? $article['seo_keywords'] : $this->system['seo_keywords']; $keywords = $article['seo_keywords'] ? $article['seo_keywords'] : $this->system['seo_keywords'];
$description = $article['seo_description'] ? $article['seo_description'] : $this->system['seo_description']; $description = $article['seo_description'] ? $article['seo_description'] : $this->system['seo_description'];
$this->setSeo($article['title'], $keywords, $description); $this->setSeo($article['title'], $keywords, $description);
$this->data['article'] = $article; $this->data['article'] = $article;
$this->data['category'] = $category; $this->data['category'] = $category;
$this->data['categoryId'] = $category['id']; $this->data['categoryId'] = $category['id'];
$this->data['prev'] = $prev; $this->data['topCategory'] = $topCategory;
$this->data['next'] = $next; $this->data['prev'] = MArticle::getPrevArticleByIdAndCategoryId($id, $article['category_id'], true, $article['sort'], true);
$this->data['next'] = MArticle::getNextArticleByIdAndCategoryId($id, $article['category_id'], true, $article['sort'], true);
return $this->view($category['template_detail'] ?? ''); return $this->view($category['template_detail'] ?? '');
} }
//列表页
public function index() // 列表数据绑定
private function templateAssign($category)
{ {
$categoryId = input('param.category_id/d', 0); $template = strtolower($category['template_list'] ?? '');
if($categoryId <= 0){ $TopCId = Category::firstGradeById($category['id']);
return $this->error('错误页面'); if($TopCId == $category['id']) {
} $topCategory = $category;
$category = Category::getById($categoryId);
if(empty($category)){
return $this->error('错误页面');
}
$childCategory = Category::getChildrenByParentId($categoryId);
$description = $category['description'] ? $category['description'] : $this->system['seo_description'];
$this->setSeo($category['title'], $this->system['seo_keywords'], $description);
$childList = [];
$defaultList = [];
// 若存在下级子栏目则优先分组显示子栏目内容
if(!empty($childCategory)) {
foreach ($childCategory as $child) {
$childList[] = [
'category' => $child,
'items' => MArticle::getListPageByCategory($child['id'], $child['number'] ? $child['number'] : 20),
];
}
} else { } else {
$defaultList = MArticle::getListPageByCategory($categoryId, $category['number'] ? $category['number'] : 20); $topCategory = Category::getById($TopCId);
}
$this->data['topCategory'] = $topCategory;
$this->data['categoryChildren'] = Category::getChildrenByParentId($topCategory['id']);
switch($template) {
case 'news' :
$this->assignNews($topCategory, $category);
break;
default :
$this->data['items'] = MArticle::getListPageByCategory($category['id'], $category['number'] ? $category['number'] : 20);
} }
$this->data['items'] = $defaultList;
$this->data['childList'] = $childList;
$this->data['category'] = $category;
$this->data['categoryId'] = $categoryId;
$this->data['childCategory'] = $childCategory;
return $this->view($category['template_list'] ?? '');
} }
// 详情数据绑定
private function templateDetailAssign($article, $category)
{
}
// 新闻
private function assignNews($topCategory, $category)
{
}
} }

View File

@ -29,23 +29,67 @@ class Article extends Base
->select() ->select()
->toArray(); ->toArray();
} }
//根据文章ID和栏目ID获取下一篇文章
public static function getNextArticleByIdAndCategoryId($id, $categoryId) /**
* 根据文章ID和栏目ID获取下一篇文章
* @param int $id 当前文章ID
* @param int $categoryId 当前文章栏目ID
* @param bool $orderSort 是否以“sort”字段排序
* @param int $currentSort 当前文章的排序号
* @param bool $sortDesc 是否以sort字段倒叙排列
* @return array
*/
public static function getNextArticleByIdAndCategoryId($id, $categoryId, $orderSort = false, $currentSort = 0, $sortDesc = false)
{ {
return self::where('id','<',$id) $whereMap = [];
if($orderSort && $currentSort > 0) {
if($sortDesc) {
$whereMap[] = ['sort', '<', $currentSort];
$order = ['sort'=> 'desc'];
} else {
$whereMap[] = ['sort', '>', $currentSort];
$order = ['sort'=> 'asc'];
}
} else {
$whereMap[] = ['id', '>', $id];
$order = ['id'=> 'asc'];
}
return self::where($whereMap)
->where('category_id', $categoryId) ->where('category_id', $categoryId)
->where('status', 1) ->where('status', 1)
->order('sort desc') ->order($order)
->findOrEmpty() ->findOrEmpty()
->toArray(); ->toArray();
} }
//根据文章ID和栏目ID获取上一篇文章
public static function getPrevArticleByIdAndCategoryId($id, $categoryId) /**
* 根据文章ID和栏目ID获取上一篇文章
* @param int $id 当前文章ID
* @param int $categoryId 当前文章栏目ID
* @param bool $orderSort 是否以“sort”字段排序
* @param int $currentSort 当前文章的排序号
* @param bool $sortDesc 是否以sort字段倒叙排列
* @return array
*/
public static function getPrevArticleByIdAndCategoryId($id, $categoryId, $orderSort = false, $currentSort = 0, $sortDesc = false)
{ {
return self::where('id','>',$id) $whereMap = [];
if($orderSort && $currentSort > 0) {
if($sortDesc) {
$whereMap[] = ['sort', '>', $currentSort];
$order = ['sort'=> 'asc'];
} else {
$whereMap[] = ['sort', '<', $currentSort];
$order = ['sort'=> 'desc'];
}
} else {
$whereMap[] = ['id', '<', $id];
$order = ['id'=> 'desc'];
}
return self::where($whereMap)
->where('category_id',$categoryId) ->where('category_id',$categoryId)
->where('status', 1) ->where('status', 1)
->order('sort asc') ->order($order)
->findOrEmpty() ->findOrEmpty()
->toArray(); ->toArray();
} }

View File

@ -1,2 +1,38 @@
{layout name="layout" /} {layout name="layout" /}
新闻详情
<!-- banner -->
<div class="page-banner w-100" style="background-image: url({:getImgSrc($topCategory, '__IMG__/page_ban3.jpg')});">
<div class="info">
<div class="w-1200">
<strong>{$topCategory.title ?? ''}</strong>
<p>{:nl2br($topCategory.description ?? '')}</p>
</div>
</div>
</div>
<!-- -->
<div class="all-center-box">
<div class="news-info w-100">
<div class="w-1200">
<div class="top-box w-100">
<span>{$article.title}</span>
<p>{$article.create_time|date="Y/m/d"}</p>
</div>
<div class="cen-box w-100">
{:nl2br($article.content)}
</div>
<div class="lower-box w-100">
<p>
{if isset($prev) && count($prev) >0}
<a href="{:url('article/detail', ['id' => $prev.id])}">上一篇</a>
{/if}
{if (isset($prev) && count($prev) >0) && (isset($next) && count($next) >0)}&emsp;/&emsp;{/if}
{if isset($next) && count($next) >0}
<a href="{:url('article/detail', ['id' => $next.id])}">下一篇</a>
{/if}
</p>
<a href="javascript:history.back(-1);" class="btns">返回</a>
</div>
</div>
</div>
</div>

View File

@ -1,3 +1,30 @@
{layout name="layout" /} {layout name="layout" /}
新闻列表 <!-- banner -->
<div class="page-banner w-100" style="background-image: url({:getImgSrc($category, '__IMG__/page_ban3.jpg')});">
<div class="info">
<div class="w-1200">
<strong>{$category.title ?? ''}</strong>
<p>{:nl2br($category.description ?? '')}</p>
</div>
</div>
</div>
<!-- -->
<div class="all-center-box">
<div class="product-box w-100">
<div class="w-1200">
{if isset($items)}
<div class="center-block w-100">
<ul>
{foreach $items as $item}
<li><a href="{:url('article/detail', ['id' => $item.id])}"><span><img src="{$item.src ?? ''}" ></span><p>{$item.title}</p></a></li>
{/foreach}
</ul>
</div>
<div class="pager w-100">
{$items->render()|raw}
</div>
{/if}
</div>
</div>
</div>