ArticleDao.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. /**
  15. * 文章dao
  16. * Class ArticleDao
  17. * @package app\dao\article
  18. */
  19. class ArticleDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return Article::class;
  28. }
  29. public function search(array $where = [])
  30. {
  31. return parent::search($where)->when(isset($where['ids']) && count($where['ids']), function ($query) use ($where) {
  32. $query->whereNotIn('id', $where['ids']);
  33. });
  34. }
  35. /**
  36. * 获取文章列表
  37. * @param array $where
  38. * @param int $page
  39. * @param int $limit
  40. * @return mixed
  41. */
  42. public function getList(array $where, int $page, int $limit)
  43. {
  44. return $this->search($where)->with(['content', 'storeInfo', 'cateName'])->page($page, $limit)->order('sort desc,id desc')->select()->toArray();
  45. }
  46. /**
  47. * 获取一条数据
  48. * @param $id
  49. * @return mixed
  50. */
  51. public function read($id)
  52. {
  53. $data = $this->search()->with(['content', 'storeInfo', 'cateName'])->find($id);
  54. if($data){
  55. $data['store_info'] = $data['storeInfo'];
  56. }
  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. }