ArticleController.php 5.1 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;
//use cmf\controller\HomeBaseController;
use app\portal\model\MemberModel;
use app\portal\model\UserModel;
use app\portal\service\PostService;
use think\Db;
use think\Request;
use think\Loader;
/**
 * @title 文章接口
 * @description 接口说明
 * @group 接口分组
 */

class ArticleController extends CommonController
{
    /**
     * @title  文章列表
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Article/index
     * @method POST
     * @param name:articleType type:int require:1 default: other: desc:类型
     * @param name:page type:int require:1 default: other: desc:页码
     * @return data:''@
     * @data  post_id:文章id
     * @data  category_id:类型id
     * @data  name:类型名
     * @data  post_title:文章标题
     *
     */
    public function index(Request $request)
    {
        $size = 2;
        $page = $request->param('page');
        $where_cate['status'] = 1;
        $catrgory = Db::name('PortalCategory')->where($where_cate)->field('id,name')->select()->toArray();
        $final['cate'] = $catrgory;
        $where_connect['j.status'] = 1;
        $where_connect['p.post_status'] = 1;
        $where_connect['j.category_id'] = $_POST['articleType'];
        $article_list = Db::name('PortalCategoryPost')->alias('j')
            ->where($where_connect)
            ->field("j.post_id,j.category_id , c.name , p.post_title,p.create_time")
            ->join("PortalCategory c","c.id = j.category_id")
            ->join("PortalPost p","p.id = j.post_id")
            ->page("$page,$size")
            ->select()->toArray();

        foreach ($article_list as $artk=>$artv){
            $article_list[$artk]['create_time'] = date('Y-m-d H:i:s',$artv['create_time']);
        }
        if(empty($article_list)){
            $this->apiResponse('1','暂无数据');
        }else{
            $final['art'] = $article_list;
        }

        $count = Db::name('PortalCategoryPost')->alias('j')
            ->where($where_connect)
            ->field("j.post_id,j.category_id , c.name , p.post_title,p.create_time")
            ->join("PortalCategory c","c.id = j.category_id")
            ->join("PortalPost p","p.id = j.post_id")
            ->count();
        $all_page = ceil($count/$size);
        $final['all_page'] = $all_page;
        $this->apiResponse('1','成功',$final);
    }

    /**
     * @title  文章详情
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Article/detail
     * @method POST
     *
     * @param name:post_id type:int require:1 default: other: desc:文章id
     *
     * @return content:''@!
     * @content  thumb:文章图片
     * @content   title:文章标题
     * @content  content:文章内容
     * @content  update_time:创建时间
     * @content  category_id:类型id
     * @content  name:类型名
     * @return hot:''@
     * @hot  title:文章标题
     * @hot  article_id:文章id
     *
     */
    public function detail(Request $request)
    {
        $where_art['p.id'] = $request->param('post_id');
        $where_art['p.post_status'] = 1;
        $article_list = Db::name('PortalCategoryPost')
            ->alias('j')
            ->where($where_art)
            ->field("p.post_title as title,p.post_content as content,p.more,p.update_time , 
                           c.name,c.id as category_id")
            ->join("PortalCategory c","c.id = j.category_id")
            ->join("PortalPost p","p.id = j.post_id")
            ->find();
        if(empty($article_list)){
            $this->apiResponse('1','暂无数据');
        }
//        处理图片
        $thumb = json_decode($article_list['more'],true);
        $article_list['thumb'] = $thumb['thumbnail'];
        unset($article_list['more']);
//        处理内容
        $article_list['content'] = htmlspecialchars_decode($article_list['content']);
        $article_list['create_time'] = date('Y-m-d',$article_list['update_time']);
        $final['content'] = $article_list;
//        热门文章
        $where_hot['p.recommended'] = 1;
        $where_hot['p.post_status'] = 1;
        $hotArticle = Db::name('PortalCategoryPost')
            ->alias('j')
            ->where($where_hot)
            ->field("p.post_title as title,p.id as article_id")
//            ->join("PortalCategory c","c.id = j.category_id")
            ->join("PortalPost p","p.id = j.post_id")
            ->select();
        $final['hot'] = $hotArticle;
        $this->apiResponse('1','成功',$final);
    }



}