ArticleService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\article;
  12. use app\model\article\Article;
  13. use core\base\BaseAdminService;
  14. /**
  15. * 文章服务层
  16. * Class ArticleService
  17. * @package app\service\admin\article
  18. */
  19. class ArticleService extends BaseAdminService
  20. {
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->model = new Article();
  25. }
  26. /**
  27. * 获取文章列表
  28. * @param array $where
  29. * @return array
  30. */
  31. public function getPage(array $where = [])
  32. {
  33. $where[] = [ 'site_id', '=', $this->site_id ];
  34. $field = 'id, category_id, site_id, title, intro, summary, image, author, content, visit, visit_virtual, is_show, sort, create_time, update_time';
  35. $order = 'create_time desc';
  36. $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ 'title', 'category_id', 'is_show'], $where)->with('articleCategory')->field($field)->order($order)->append(['article_url','image_thumb_small']);
  37. return $this->pageQuery($search_model);
  38. }
  39. /**
  40. * 获取文章列表
  41. * @return array
  42. */
  43. public function getAll(int $limit)
  44. {
  45. $field = 'id, title, create_time';
  46. $order = 'sort desc, create_time desc';
  47. $data = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->field($field)->order($order)->limit($limit)->select()->toArray();
  48. return $data;
  49. }
  50. /**
  51. * 获取文章信息
  52. * @param int $id
  53. * @return array
  54. */
  55. public function getInfo(int $id)
  56. {
  57. $field = 'id, category_id, site_id, title, intro, summary, image, author, content, visit, visit_virtual, is_show, sort, create_time, update_time';
  58. return $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->with('articleCategory')->field($field)->append(['image_thumb_small'])->findOrEmpty()->toArray();
  59. }
  60. /**
  61. * 添加文章
  62. * @param array $data
  63. * @return mixed
  64. */
  65. public function add(array $data)
  66. {
  67. $data[ 'site_id' ] = $this->site_id;
  68. $data[ 'create_time' ] = time();
  69. $res = $this->model->create($data);
  70. return $res->id;
  71. }
  72. /**
  73. * 文章编辑
  74. * @param int $id
  75. * @param array $data
  76. * @return true
  77. */
  78. public function edit(int $id, array $data)
  79. {
  80. $data[ 'update_time' ] = time();
  81. $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);
  82. return true;
  83. }
  84. /**
  85. * 删除文章
  86. * @param int $id
  87. * @return bool
  88. */
  89. public function del(int $id)
  90. {
  91. return $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->delete();
  92. }
  93. }