Article.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\api\model;
  3. use app\common\exception\BaseException;
  4. use app\common\model\Article as ArticleModel;
  5. /**
  6. * 商品评价模型
  7. * Class Article
  8. * @package app\api\model
  9. */
  10. class Article extends ArticleModel
  11. {
  12. /**
  13. * 追加字段
  14. * @var array
  15. */
  16. protected $append = [
  17. 'show_views',
  18. 'view_time'
  19. ];
  20. /**
  21. * 隐藏字段
  22. * @var array
  23. */
  24. protected $hidden = [
  25. 'is_delete',
  26. 'wxapp_id',
  27. 'create_time',
  28. 'update_time'
  29. ];
  30. /**
  31. * 文章详情:HTML实体转换回普通字符
  32. * @param $value
  33. * @return string
  34. */
  35. public function getArticleContentAttr($value)
  36. {
  37. return htmlspecialchars_decode($value);
  38. }
  39. public function getViewTimeAttr($value, $data)
  40. {
  41. return date('Y-m-d', $data['create_time']);
  42. }
  43. /**
  44. * 文章详情
  45. * @param $article_id
  46. * @return ArticleModel|null
  47. * @throws BaseException
  48. * @throws \think\Exception
  49. * @throws \think\exception\DbException
  50. */
  51. public static function detail($article_id)
  52. {
  53. if (!$model = parent::detail($article_id)) {
  54. throw new BaseException(['msg' => '文章不存在']);
  55. }
  56. // 累积阅读数
  57. $model->setInc('actual_views', 1);
  58. return $model;
  59. }
  60. /**
  61. * 获取文章列表
  62. * @param int $category_id
  63. * @param int $limit
  64. * @return \think\Paginator
  65. * @throws \think\exception\DbException
  66. */
  67. public function getList($category_id = 0, $limit = 15)
  68. {
  69. $category_id > 0 && $this->where('category_id', '=', $category_id);
  70. return $this->field(['article_content'], true)
  71. ->with(['image', 'category'])
  72. ->where('article_status', '=', 1)
  73. ->where('is_delete', '=', 0)
  74. ->order(['article_sort' => 'asc', 'create_time' => 'desc'])
  75. ->paginate($limit, false, [
  76. 'query' => \request()->request()
  77. ]);
  78. }
  79. }