150 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			150 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
<?php
 | 
						|
namespace app\controller;
 | 
						|
use app\model\Archives;
 | 
						|
use app\model\ArchivesCategory as ArchivesCategoryModel;
 | 
						|
use app\model\ArchivesModel;
 | 
						|
use app\model\Block;
 | 
						|
use app\service\DxtcPage;
 | 
						|
use Exception;
 | 
						|
use think\Paginator;
 | 
						|
use think\response\Redirect;
 | 
						|
use think\response\View;
 | 
						|
/**
 | 
						|
 *  auth  王兴龙 2022-02-18
 | 
						|
 * */
 | 
						|
class Page extends Base
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * @return Redirect
 | 
						|
     * @throws Exception
 | 
						|
     */
 | 
						|
    public function index()
 | 
						|
    {
 | 
						|
        $categoryId = input("categoryId/d",0);
 | 
						|
 | 
						|
 | 
						|
        if($categoryId==ArchivesCategoryModel::index_id){
 | 
						|
            return   $this->redirect("/");
 | 
						|
        }
 | 
						|
        $category = ArchivesCategoryModel::findById($categoryId);
 | 
						|
        if(empty($category)){
 | 
						|
            return  $this->error("内容不存在");
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        //如果有链接
 | 
						|
        if(!empty($category['link'])){
 | 
						|
            return $this->redirect($category['link']);
 | 
						|
        }
 | 
						|
 | 
						|
        $this->data["category"] = $category;
 | 
						|
        $this->setActiveCategory($category['id']);
 | 
						|
        $this->data['topCategoryId'] = ArchivesCategoryModel::firstGradeById($category['id']) ;
 | 
						|
 | 
						|
        $categoryModel = ArchivesModel::allModel();
 | 
						|
        //所有类型都要把碎片加上
 | 
						|
        $this->data["blocks"]   = Block:: getByCategoryId($category['id']);
 | 
						|
 | 
						|
        switch ($category['model_id']){
 | 
						|
            //文章模型
 | 
						|
            case $categoryModel[ArchivesModel::MODEL_ARCHIVES]:
 | 
						|
                return  $this->archives($category,$category['cover_template']);
 | 
						|
                break;
 | 
						|
            default:
 | 
						|
                return  $this->redirect("/");
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    //文章列表
 | 
						|
 | 
						|
    /**
 | 
						|
     *
 | 
						|
     * @param $categoryId 栏目id
 | 
						|
     * @param $categoryTemplate 模板
 | 
						|
     */
 | 
						|
    protected function archives($category,$categoryTemplate)
 | 
						|
    {
 | 
						|
        //动态设置当前分页驱动
 | 
						|
        app('think\App')->bind(Paginator::class, DxtcPage::class);
 | 
						|
        $this->data["archives"]  = Archives::getListPageByCategory($category['id'],$category['page_size']);
 | 
						|
        return $this->view(empty($categoryTemplate)?"archives_default":$categoryTemplate);
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     *
 | 
						|
     * @param $categoryId 栏目id
 | 
						|
     * @param $categoryTemplate 模板
 | 
						|
     */
 | 
						|
    protected function page($categoryTemplate)
 | 
						|
    {
 | 
						|
        return $this->view(empty($categoryTemplate)?"page_default":$categoryTemplate);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 文章详情
 | 
						|
     *
 | 
						|
     * @param $articleId
 | 
						|
     */
 | 
						|
    public function archivesInfo()
 | 
						|
    {
 | 
						|
        $articleId = input("articleId/d",0);
 | 
						|
        $archive = Archives::findOne([["id","=",$articleId]]);
 | 
						|
        if(empty($archive)){
 | 
						|
            return  $this->error("内容不存在");
 | 
						|
        }
 | 
						|
        $archive->inc("views")->update();
 | 
						|
        $this->data["archive"] = $archive;
 | 
						|
 | 
						|
 | 
						|
        $category = ArchivesCategoryModel::findById($archive['category_id']);
 | 
						|
        if(empty($category)){
 | 
						|
            return  $this->error("内容不存在");
 | 
						|
        }
 | 
						|
        $this->data["category"] = $category;
 | 
						|
        $this->setActiveCategory($category['id']);
 | 
						|
        $this->data['topCategoryId'] = ArchivesCategoryModel::firstGradeById($category['id']) ;
 | 
						|
 | 
						|
 | 
						|
        //所有类型都要把碎片加上
 | 
						|
        $this->data["blocks"]   = Block:: getByCategoryId($category['id']);
 | 
						|
 | 
						|
 | 
						|
        $seo_title      = empty($archive['seo_title'])
 | 
						|
            ?
 | 
						|
            $archive['title']
 | 
						|
            :
 | 
						|
            $archive['seo_title'];
 | 
						|
        $seo_keywords   = empty($archive['seo_keywords'])
 | 
						|
            ?
 | 
						|
            ""
 | 
						|
            :
 | 
						|
            $archive['seo_keywords'];
 | 
						|
        $seo_description = empty($archive['seo_description'])
 | 
						|
            ?
 | 
						|
            ""
 | 
						|
            :
 | 
						|
            $archive['seo_description'];
 | 
						|
 | 
						|
 | 
						|
        $this->setSeo( $seo_title,$seo_keywords,$seo_description);
 | 
						|
 | 
						|
 | 
						|
         // 上一篇
 | 
						|
        $this->data["prev"] = Archives::findOne([["category_id","=",$category['id']], ['sort', '<', $archive['sort']]]
 | 
						|
            , [], function ($q) {
 | 
						|
            return $q->with(["archivesCategory"])->order(['sort'=> 'desc']);
 | 
						|
        });
 | 
						|
 | 
						|
        // 下一篇
 | 
						|
        $this->data["next"] = Archives::findOne([["category_id","=",$category['id']], ['sort', '>', $archive['sort']]]
 | 
						|
            , [], function ($q) {
 | 
						|
            return $q->with(["archivesCategory"])->order(['sort'=> 'asc']);
 | 
						|
        });
 | 
						|
 | 
						|
        return $this->view(!empty($category['detail_template'])?$category['detail_template']:"archives_default");
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
} |