ArticleDao.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. public function search(array $where = [])
  31. {
  32. return parent::search($where)->when(isset($where['ids']) && count($where['ids']), function ($query) use ($where) {
  33. $query->whereNotIn('id', $where['ids']);
  34. });
  35. }
  36. /**
  37. * 获取文章列表
  38. * @param array $where
  39. * @param int $page
  40. * @param int $limit
  41. * @return mixed
  42. */
  43. public function getList(array $where, int $page, int $limit)
  44. {
  45. return $this->search($where)->with(['content', 'storeInfo', 'cateName'])->page($page, $limit)->order('sort desc,id desc')->select()->toArray();
  46. }
  47. /**
  48. * 获取一条数据
  49. * @param $id
  50. * @return mixed
  51. */
  52. public function read($id)
  53. {
  54. $data = $this->search()->with(['content', 'storeInfo', 'cateName'])->find($id);
  55. if (!$data) throw new ValidateException('文章不存在');
  56. $data['store_info'] = $data['storeInfo'];
  57. return $data;
  58. }
  59. /**
  60. * @param array $where
  61. * @param int $page
  62. * @param int $limit
  63. * @param string $field
  64. * @return array
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function cidByArticleList(array $where, int $page, int $limit, string $field = '*')
  70. {
  71. return $this->search(['status' => 1, 'hide' => 0])
  72. ->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  73. $query->where('cid', $where['cid']);
  74. })->when(isset($where['is_hot']) && $where['is_hot'], function ($query) use ($where) {
  75. $query->where('is_hot', $where['is_hot']);
  76. })->when(isset($where['is_banner']) && $where['is_banner'], function ($query) use ($where) {
  77. $query->where('is_banner', $where['is_banner']);
  78. })->when($page != 0, function ($query) use ($page, $limit) {
  79. $query->page($page, $limit);
  80. })->order('add_time desc')->field($field)->select()->toArray();
  81. }
  82. /**新闻分类下的文章
  83. * @param $new_id
  84. * @return mixed
  85. */
  86. public function articleLists($new_id)
  87. {
  88. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->select();
  89. }
  90. /**图文详情
  91. * @param $new_id
  92. * @return mixed
  93. */
  94. public function articleContentList($new_id)
  95. {
  96. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->with('content')->select();
  97. }
  98. }