BuddhistService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\BuddhistModel;
  13. /**
  14. * 佛经管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class BuddhistService
  18. * @package App\Services
  19. */
  20. class BuddhistService extends BaseService
  21. {
  22. /**
  23. * 构造函数
  24. * @author wesmiler
  25. * @since 2020/11/11
  26. * BuddhistService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new BuddhistModel();
  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('buddhists as a')
  44. ->leftJoin('buddhist_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. }
  55. $status = isset($params['status']) ? $params['status'] : 0;
  56. if ($status > 0) {
  57. $query->where('a.status', $status);
  58. } else {
  59. $query->whereIn('a.status', [1, 2]);
  60. }
  61. })
  62. ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title','a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort','a.publish_at'])
  63. ->orderBy('a.update_time', 'desc')
  64. ->paginate($pageSize);
  65. $dataList = $dataList ? $dataList->toArray() : [];
  66. if ($dataList) {
  67. foreach ($dataList['data'] as &$item) {
  68. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  69. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  70. $pageNum = BuddhistPagesService::make()->getCount($item['id']);
  71. $item['pageNum'] = intval($pageNum);
  72. }
  73. unset($item);
  74. }
  75. return [
  76. 'code' => 0,
  77. 'success'=> true,
  78. 'msg' => '操作成功',
  79. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  80. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  81. ];
  82. }
  83. /**
  84. * 添加或编辑
  85. * @return array
  86. * @since 2020/11/11
  87. * @author wesmiler
  88. */
  89. public function edit()
  90. {
  91. $data = request()->all();
  92. // 图片处理
  93. $image = trim($data['thumb']);
  94. $id = isset($data['id']) ? $data['id'] : 0;
  95. if (!$id && !$image) {
  96. return message('请上传封面图片', false);
  97. }
  98. if (strpos($image, "temp")) {
  99. $data['thumb'] = save_image($image, 'item');
  100. } else {
  101. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  102. }
  103. $data['update_time'] = time();
  104. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  105. return parent::edit($data); // TODO: Change the autogenerated stub
  106. }
  107. /**
  108. * 删除
  109. * @return array
  110. */
  111. public function delete()
  112. {
  113. $id = request()->get('id', 0);
  114. if($id && BuddhistPagesService::make()->getCount($id)){
  115. return message('请先删除章节数据', false);
  116. }
  117. return parent::delete(); // TODO: Change the autogenerated stub
  118. }
  119. }