ArticleService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 南京Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ArticleModel;
  13. /**
  14. * 文章管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class ArticleService
  18. * @package App\Services
  19. */
  20. class ArticleService extends BaseService
  21. {
  22. /**
  23. * 构造函数
  24. * @author wesmiler
  25. * @since 2020/11/11
  26. * ArticleService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new ArticleModel();
  31. }
  32. /**
  33. * 获取列表
  34. * @return array
  35. * @since 2020/11/11
  36. * @author wesmiler
  37. */
  38. public function getList()
  39. {
  40. $params = request()->all();
  41. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  42. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  43. $dataList = $this->model::from('article as a')
  44. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  45. ->where(function ($query) use ($params) {
  46. $query->where('a.mark', 1);
  47. $title = isset($params['title']) ? trim($params['title']) : '';
  48. if (!empty($title)) {
  49. $query->where('a.title', 'like', "%{$title}%");
  50. }
  51. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  52. if ($cateId > 0) {
  53. $query->where('a.cate_id', $cateId);
  54. } else if ($cateId == -1) {
  55. $query->where('a.is_recommand', 1);
  56. }
  57. $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
  58. if ($isTop > 0) {
  59. $query->where('a.is_top', $isTop);
  60. }
  61. $status = isset($params['status']) ? $params['status'] : 0;
  62. if ($status > 0) {
  63. $query->where('a.status', $status);
  64. } else {
  65. $query->whereIn('a.status', [1, 2]);
  66. }
  67. })
  68. ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title', 'a.is_form', 'a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort', 'a.content','a.publish_at'])
  69. ->orderBy('a.update_time', 'desc')
  70. ->paginate($pageSize);
  71. $dataList = $dataList ? $dataList->toArray() : [];
  72. if ($dataList) {
  73. foreach ($dataList['data'] as &$item) {
  74. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  75. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  76. }
  77. unset($item);
  78. }
  79. return [
  80. 'code' => 0,
  81. 'success'=> true,
  82. 'msg' => '操作成功',
  83. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  84. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  85. ];
  86. }
  87. /**
  88. * 添加或编辑
  89. * @return array
  90. * @since 2020/11/11
  91. * @author wesmiler
  92. */
  93. public function edit()
  94. {
  95. $data = request()->all();
  96. // 图片处理
  97. $image = trim($data['thumb']);
  98. $id = isset($data['id']) ? $data['id'] : 0;
  99. if (!$id && !$image) {
  100. return message('请上传文章图片', false);
  101. }
  102. if (strpos($image, "temp")) {
  103. $data['thumb'] = save_image($image, 'item');
  104. } else {
  105. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  106. }
  107. $data['update_time'] = time();
  108. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  109. return parent::edit($data); // TODO: Change the autogenerated stub
  110. }
  111. }