Article.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\common\exception\BaseException;
  14. use app\common\model\Article as ArticleModel;
  15. /**
  16. * 商品评价模型
  17. * Class Article
  18. * @package app\api\model
  19. */
  20. class Article extends ArticleModel
  21. {
  22. /**
  23. * 隐藏字段
  24. * @var array
  25. */
  26. protected $hidden = [
  27. 'is_delete',
  28. 'store_id',
  29. //'create_time',
  30. 'update_time'
  31. ];
  32. /**
  33. * 获取器:文章详情HTML实体转换回普通字符
  34. * @param $value
  35. * @return string
  36. */
  37. public function getArticleContentAttr($value)
  38. {
  39. return htmlspecialchars_decode($value);
  40. }
  41. /**
  42. * 获取文章详情并累计阅读次数
  43. * @param int $articleId 文章ID
  44. * @return static|null
  45. * @throws BaseException
  46. */
  47. public static function getDetail(int $articleId)
  48. {
  49. // 获取文章详情
  50. $detail = parent::detail($articleId, ['image']);
  51. if (empty($detail) || $detail['is_delete']) {
  52. throwError('很抱歉,当前文章不存在');
  53. }
  54. // 累积文章实际阅读数
  55. static::setIncActualViews($articleId);
  56. return $detail;
  57. }
  58. /**
  59. * 累积文章实际阅读数
  60. * @param int $articleId 文章ID
  61. * @param int $num 递增的数量
  62. * @return mixed
  63. */
  64. private static function setIncActualViews(int $articleId, int $num = 1)
  65. {
  66. return (new static)->setInc($articleId, 'actual_views', $num);
  67. }
  68. /**
  69. * 获取文章列表
  70. * @param int $categoryId
  71. * @param int $limit
  72. * @return \think\Paginator
  73. * @throws \think\db\exception\DbException
  74. */
  75. public function getList(int $categoryId = 0, int $limit = 15)
  76. {
  77. // 检索查询条件
  78. $filter = [];
  79. $categoryId > 0 && $filter[] = ['category_id', '=', $categoryId];
  80. // 获取列表数据
  81. return $this->withoutField(['content'])
  82. ->with(['image', 'category'])
  83. ->where($filter)
  84. ->where('status', '=', 1)
  85. ->where('is_delete', '=', 0)
  86. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  87. ->paginate($limit);
  88. }
  89. }