ArticleDao.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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)->when(isset($where['ids']) && count($where['ids']), function ($query) use ($where) {
  43. $query->whereNotIn('id', $where['ids']);
  44. });
  45. }
  46. /**
  47. * 获取文章列表
  48. * @param array $where
  49. * @param int $page
  50. * @param int $limit
  51. * @return mixed
  52. */
  53. public function getList(array $where, int $page, int $limit)
  54. {
  55. return $this->search($where)->with(['content', 'storeInfo', 'cateName'])->page($page, $limit)->order('sort desc,id desc')->select()->toArray();
  56. }
  57. /**
  58. * 获取一条数据
  59. * @param $id
  60. * @return mixed
  61. */
  62. public function read($id)
  63. {
  64. $data = $this->search()->with(['content', 'storeInfo', 'cateName'])->find($id);
  65. if (!$data) throw new ValidateException('文章不存在');
  66. $data['store_info'] = $data['storeInfo'];
  67. return $data;
  68. }
  69. /**
  70. * @param array $where
  71. * @param int $page
  72. * @param int $limit
  73. * @param string $field
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function cidByArticleList(array $where, int $page, int $limit, string $field = '*')
  80. {
  81. return $this->search(['status' => 1, 'hide' => 0])
  82. ->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  83. $query->where('cid', $where['cid']);
  84. })->when(isset($where['is_hot']) && $where['is_hot'], function ($query) use ($where) {
  85. $query->where('is_hot', $where['is_hot']);
  86. })->when(isset($where['is_banner']) && $where['is_banner'], function ($query) use ($where) {
  87. $query->where('is_banner', $where['is_banner']);
  88. })->when($page != 0, function ($query) use ($page, $limit) {
  89. $query->page($page, $limit);
  90. })->order('add_time desc')->field($field)->select()->toArray();
  91. }
  92. /**新闻分类下的文章
  93. * @param $new_id
  94. * @return mixed
  95. */
  96. public function articleLists($new_id)
  97. {
  98. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->select();
  99. }
  100. /**图文详情
  101. * @param $new_id
  102. * @return mixed
  103. */
  104. public function articleContentList($new_id)
  105. {
  106. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->with('content')->select();
  107. }
  108. }