limit($limit) ->select() ->toArray(); } //获取栏目下最新记录 public static function getLatestByCategory($categoryId, $limit = 5) { if(empty($categoryId)){ return []; } if($limit <= 0){ $limit = 5; } return self::where('category_id', $categoryId) ->order('create_time', 'desc') ->limit($limit) ->select() ->toArray(); } //根据文章ID和栏目ID获取下一篇文章 public static function getNextArticleByIdAndCategoryId($id, $categoryId) { return self::where('id','<',$id) ->where('category_id', $categoryId) ->where('status', 1) ->order('sort desc') ->findOrEmpty() ->toArray(); } //根据文章ID和栏目ID获取上一篇文章 public static function getPrevArticleByIdAndCategoryId($id, $categoryId) { return self::where('id','>',$id) ->where('category_id',$categoryId) ->where('status', 1) ->order('sort asc') ->findOrEmpty() ->toArray(); } //根据栏目ID获取文章列表 public static function getListByCategory($categoryId, $limit = 10) { return self::where('category_id', $categoryId) ->where('status', 1) ->order("sort desc") ->limit($limit) ->select() ->toArray(); } //根据栏目ID获取文章分页列表 public static function getListPageByCategory($categoryId, $per = 20, $keyword = '') { $where = [ ['category_id', '=', $categoryId], ['status', '=', 1], ]; $param['category_id'] = $categoryId; if($keyword!=''){ $where[] = ['title', 'like', '%'.$keyword.'%']; $param['keyword'] = $keyword; } $paginate = [ 'list_rows' => $per, 'query' => $param ]; return self::where($where) ->order("sort desc") ->paginate($paginate,false); } public static function onAfterInsert($article) { $article->sort = $article->id; $article->create_time = $article->update_time = time(); $auth = session('auth'); $article->created = $article->updated = $auth['userName']; $article->save(); } /** * 获取文章列表 * @param $categoryId 分类ID * @param int $per 每页数量 * @param string $keyword 关键词 * @param array $param 文章类型:置顶、热门、推荐 ['top','hot','recommend'] * @param int $status 文章状态,-1表示不限制 * @return \think\Paginator * @throws \think\db\exception\DbException */ public static function getList($categoryId, $per = 20, $keyword = '', $param = [], $status = -1) { $whereMap = []; $pageParam = []; if(is_numeric($categoryId) && $categoryId > 0) { $whereMap[] = ['category_id', '=', $categoryId]; $pageParam['category_id'] = $categoryId; } if(!empty($keyword)){ $whereMap[] = ['title', 'like', '%'.$keyword.'%']; $pageParam['keyword'] = $keyword; } if (is_array($param) && count($param) > 0) { $pageParam['param'] = $param; foreach ($param as $vo) { if(in_array($vo, ['top','hot','recommend'], true)) { $whereMap[] = ["{$vo}", '=', 1]; } } } $paginate = [ 'list_rows' => $per, 'query' => $pageParam ]; return self::when(count($whereMap) > 0, function($query) use ($whereMap) { $query->where($whereMap); }) ->order("sort desc") ->paginate($paginate,false); } //获取文章涉及到的图片 public static function getFilesInUse() { $items = self::select()->toArray(); $data = []; foreach($items as $item){ $src = trim($item['src']); if(!empty($src)){ $key = getKeyByPath($src); $data[$key] = $src; } $imgs = getImageUrlFromText($item['content']); if(!empty($imgs)){ $data = array_merge($data, $imgs); } $videos = getVideoUrlFromText($item['content']); if(!empty($videos)){ $data = array_merge($data, $videos); } } return $data; } }