| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 | <?php// +----------------------------------------------------------------------// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]// +----------------------------------------------------------------------// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: 小夏 < 449134904@qq.com>// +----------------------------------------------------------------------namespace app\portal\service;use app\portal\model\PortalPostModel;use think\db\Query;class PostService{    /**     * 文章查询     * @param $filter     * @return \think\Paginator     * @throws \think\exception\DbException     */    public function adminArticleList($filter)    {        return $this->adminPostList($filter);    }    /**     * 页面文章列表     * @param $filter     * @return \think\Paginator     * @throws \think\exception\DbException     */    public function adminPageList($filter)    {        return $this->adminPostList($filter, true);    }    /**     * 文章查询     * @param      $filter     * @param bool $isPage     * @return \think\Paginator     * @throws \think\exception\DbException     */    public function adminPostList($filter, $isPage = false)    {        $join = [            ['__USER__ u', 'a.user_id = u.id']        ];        $field = 'a.*,u.user_login,u.user_nicename,u.user_email';        $category = empty($filter['category']) ? 0 : intval($filter['category']);        if (!empty($category)) {            array_push($join, [                '__PORTAL_CATEGORY_POST__ b', 'a.id = b.post_id'            ]);            $field = 'a.*,b.id AS post_category_id,b.list_order,b.category_id,u.user_login,u.user_nicename,u.user_email';        }        $portalPostModel = new PortalPostModel();        $articles        = $portalPostModel->alias('a')->field($field)            ->join($join)            ->where('a.create_time', '>=', 0)            ->where('a.delete_time', 0)            ->where(function (Query $query) use ($filter, $isPage) {                $category = empty($filter['category']) ? 0 : intval($filter['category']);                if (!empty($category)) {                    $query->where('b.category_id', $category);                }                $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);                $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);                if (!empty($startTime)) {                    $query->where('a.published_time', '>=', $startTime);                }                if (!empty($endTime)) {                    $query->where('a.published_time', '<=', $endTime);                }                $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];                if (!empty($keyword)) {                    $query->where('a.post_title', 'like', "%$keyword%");                }                if ($isPage) {                    $query->where('a.post_type', 2);                } else {                    $query->where('a.post_type', 1);                }            })            ->order('id', 'DESC')            ->paginate(10);        return $articles;    }    /**     * 已发布文章查询     * @param  int $postId     文章id     * @param int  $categoryId 分类id     * @return array|string|\think\Model|null     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function publishedArticle($postId, $categoryId = 0)    {        $portalPostModel = new PortalPostModel();        if (empty($categoryId)) {            $where = [                'post.post_type'   => 1,                'post.post_status' => 1,                'post.delete_time' => 0,                'post.id'          => $postId            ];            $article = $portalPostModel->alias('post')->field('post.*')                ->where($where)                ->where('post.published_time', ['< time', time()], ['> time', 0], 'and')                ->find();        } else {            $where = [                'post.post_type'       => 1,                'post.post_status'     => 1,                'post.delete_time'     => 0,                'relation.category_id' => $categoryId,                'relation.post_id'     => $postId            ];            $join    = [                ['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']            ];            $article = $portalPostModel->alias('post')->field('post.*')                ->join($join)                ->where($where)                ->where('post.published_time', ['< time', time()], ['> time', 0], 'and')                ->find();        }        return $article;    }    /**     * 上一篇文章     * @param int $postId     文章id     * @param int $categoryId 分类id     * @return array|string|\think\Model|null     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function publishedPrevArticle($postId, $categoryId = 0)    {        $portalPostModel = new PortalPostModel();        if (empty($categoryId)) {            $where = [                'post.post_type'   => 1,                'post.post_status' => 1,                'post.delete_time' => 0,                'post.id '         => ['<', $postId]            ];            $article = $portalPostModel                ->alias('post')                ->field('post.*')                ->where($where)                ->where('post.published_time', ['< time', time()], ['> time', 0], 'and')                ->order('id', 'DESC')                ->find();        } else {            $where = [                'post.post_type'       => 1,                'post.post_status'     => 1,                'post.delete_time'     => 0,                'relation.category_id' => $categoryId,                'relation.post_id'     => ['<', $postId]            ];            $join    = [                ['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']            ];            $article = $portalPostModel                ->alias('post')                ->field('post.*')                ->join($join)                ->where($where)                ->where('post.published_time', ['< time', time()], ['> time', 0], 'and')                ->order('id', 'DESC')                ->find();        }        return $article;    }    /**     * 下一篇文章     * @param int $postId     文章id     * @param int $categoryId 分类id     * @return array|string|\think\Model|null     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function publishedNextArticle($postId, $categoryId = 0)    {        $portalPostModel = new PortalPostModel();        if (empty($categoryId)) {            $where = [                'post.post_type'   => 1,                'post.post_status' => 1,                'post.delete_time' => 0,                'post.id'          => ['>', $postId]            ];            $article = $portalPostModel->alias('post')->field('post.*')                ->where($where)                ->where('post.published_time', ['< time', time()], ['> time', 0], 'and')                ->order('id', 'ASC')                ->find();        } else {            $where = [                'post.post_type'       => 1,                'post.post_status'     => 1,                'post.delete_time'     => 0,                'relation.category_id' => $categoryId,                'relation.post_id'     => ['>', $postId]            ];            $join    = [                ['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']            ];            $article = $portalPostModel->alias('post')->field('post.*')                ->join($join)                ->where($where)                ->where('post.published_time', ['< time', time()], ['> time', 0], 'and')                ->order('id', 'ASC')                ->find();        }        return $article;    }    /**     * 页面管理查询     * @param int $pageId 文章id     * @return array|string|\think\Model|null     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    public function publishedPage($pageId)    {        $where = [            'post_type'   => 2,            'post_status' => 1,            'delete_time' => 0,            'id'          => $pageId        ];        $portalPostModel = new PortalPostModel();        $page            = $portalPostModel            ->where($where)            ->where('published_time', ['< time', time()], ['> time', 0], 'and')            ->find();        return $page;    }}
 |