// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\api\model; use app\common\exception\BaseException; use app\common\model\Article as ArticleModel; /** * 商品评价模型 * Class Article * @package app\api\model */ class Article extends ArticleModel { /** * 隐藏字段 * @var array */ protected $hidden = [ 'is_delete', 'store_id', 'create_time', 'update_time' ]; /** * 获取器:文章详情HTML实体转换回普通字符 * @param $value * @return string */ public function getArticleContentAttr($value) { return htmlspecialchars_decode($value); } /** * 获取文章详情并累计阅读次数 * @param int $articleId * @return ArticleModel|null * @throws BaseException * @throws \think\Exception */ public static function detail(int $articleId) { if (!$model = parent::detail($articleId)) { throwError('文章不存在'); } // 累积实际阅读数 $model->setInc($articleId, 'actual_views', 1); return $model; } /** * 获取文章列表 * @param int $categoryId * @param int $limit * @return \think\Paginator * @throws \think\db\exception\DbException */ public function getList(int $categoryId = 0, int $limit = 15) { // 检索查询条件 $filter = []; $categoryId > 0 && $filter[] = ['category_id', '=', $categoryId]; // 获取列表数据 return $this->withoutField(['content']) ->with(['image', 'category']) ->where($filter) ->where('status', '=', 1) ->where('is_delete', '=', 0) ->order(['sort' => 'asc', 'create_time' => 'desc']) ->paginate($limit); } }