ArticleDao.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\article;
  12. use app\dao\BaseDao;
  13. use app\model\article\Article;
  14. use think\exception\ValidateException;
  15. /**
  16. * 文章dao
  17. * Class ArticleDao
  18. * @package app\dao\article
  19. */
  20. class ArticleDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return Article::class;
  29. }
  30. /**
  31. * 文章搜索
  32. * @param array $where
  33. * @param bool $search
  34. * @return \crmeb\basic\BaseModel
  35. * @throws \ReflectionException
  36. * @author 吴汐
  37. * @email 442384644@qq.com
  38. * @date 2023/03/20
  39. */
  40. public function search(array $where = [], bool $search = false)
  41. {
  42. return parent::search($where, $search)
  43. ->when(isset($where['ids']) && count($where['ids']), function ($query) use ($where) {
  44. $query->whereNotIn('id', $where['ids']);
  45. })->when(isset($where['in_ids']) && count($where['in_ids']), function ($query) use ($where) {
  46. $query->whereIn('id', $where['in_ids']);
  47. });
  48. }
  49. /**
  50. * 获取文章列表
  51. * @param array $where
  52. * @param int $page
  53. * @param int $limit
  54. * @param string $order
  55. * @return mixed
  56. * @throws \ReflectionException
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function getList(array $where, int $page, int $limit, string $order = 'sort desc,id desc')
  62. {
  63. return $this->search($where)->with(['content', 'storeInfo', 'cateName'])->page($page, $limit)->order($order)->select()->toArray();
  64. }
  65. /**
  66. * 获取一条数据
  67. * @param $id
  68. * @return mixed
  69. * @throws \ReflectionException
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function read($id)
  75. {
  76. $data = $this->search()->with(['content', 'storeInfo', 'cateName'])->find($id);
  77. if (!$data) throw new ValidateException('文章不存在');
  78. $data['store_info'] = $data['storeInfo'];
  79. return $data;
  80. }
  81. /**
  82. * 新闻分类下的文章
  83. * @param $new_id
  84. * @return mixed
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function articleLists($new_id)
  90. {
  91. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->select();
  92. }
  93. /**
  94. * 图文详情
  95. * @param $new_id
  96. * @return mixed
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function articleContentList($new_id)
  102. {
  103. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->with('content')->select();
  104. }
  105. }