Article.php 2.1 KB

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