Article.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 cores\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. 'virtual_views',
  28. 'actual_views',
  29. 'is_delete',
  30. 'store_id',
  31. 'create_time',
  32. 'update_time'
  33. ];
  34. /**
  35. * 获取器:文章详情HTML实体转换回普通字符
  36. * @param $value
  37. * @return string
  38. */
  39. public function getArticleContentAttr($value): string
  40. {
  41. return htmlspecialchars_decode($value);
  42. }
  43. /**
  44. * 获取文章详情
  45. * @param int $articleId 文章ID
  46. * @return Article|array|null
  47. * @throws \cores\exception\BaseException
  48. */
  49. public static function getDetail(int $articleId)
  50. {
  51. // 获取文章详情
  52. $detail = parent::detail($articleId, ['image']);
  53. if (empty($detail) || $detail['is_delete']) {
  54. throwError('很抱歉,当前文章不存在');
  55. }
  56. // 累积文章实际阅读数
  57. static::setIncActualViews($articleId);
  58. return $detail;
  59. }
  60. /**
  61. * 累积文章实际阅读数
  62. * @param int $articleId 文章ID
  63. * @return void
  64. */
  65. private static function setIncActualViews(int $articleId): void
  66. {
  67. (new static)->setInc($articleId, 'actual_views', 1);
  68. }
  69. /**
  70. * 获取文章列表
  71. * @param int $categoryId
  72. * @param int $limit
  73. * @return \think\Paginator
  74. * @throws \think\db\exception\DbException
  75. */
  76. public function getList(int $categoryId = 0, int $limit = 15): \think\Paginator
  77. {
  78. // 检索查询条件
  79. $filter = [];
  80. $categoryId > 0 && $filter[] = ['category_id', '=', $categoryId];
  81. // 获取列表数据
  82. return $this->withoutField(['content'])
  83. ->with(['image', 'category'])
  84. ->where($filter)
  85. ->where('status', '=', 1)
  86. ->where('is_delete', '=', 0)
  87. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  88. ->paginate($limit);
  89. }
  90. }