BuddhistService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\BuddhistCollectModel;
  13. use App\Models\BuddhistModel;
  14. /**
  15. * 佛经管理-服务类
  16. * @author wesmiler
  17. * @since 2020/11/11
  18. * Class BuddhistService
  19. * @package App\Services
  20. */
  21. class BuddhistService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * BuddhistService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new BuddhistModel();
  32. }
  33. /**
  34. * 获取列表
  35. * @return array
  36. * @since 2020/11/11
  37. * @author wesmiler
  38. */
  39. public function getList()
  40. {
  41. $params = request()->all();
  42. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  43. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  44. $dataList = $this->model::from('buddhists as a')
  45. ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
  46. ->where(function ($query) use ($params) {
  47. $query->where('a.mark', 1);
  48. $title = isset($params['title']) ? trim($params['title']) : '';
  49. if (!empty($title)) {
  50. $query->where('a.title', 'like', "%{$title}%");
  51. }
  52. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  53. if ($cateId > 0) {
  54. $query->where('a.cate_id', $cateId);
  55. }
  56. $status = isset($params['status']) ? $params['status'] : 0;
  57. if ($status > 0) {
  58. $query->where('a.status', $status);
  59. } else {
  60. $query->whereIn('a.status', [1, 2]);
  61. }
  62. })
  63. ->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'])
  64. ->orderBy('a.update_time', 'desc')
  65. ->paginate($pageSize);
  66. $dataList = $dataList ? $dataList->toArray() : [];
  67. if ($dataList) {
  68. foreach ($dataList['data'] as &$item) {
  69. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  70. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  71. $pageNum = BuddhistPagesService::make()->getCount($item['id']);
  72. $item['pageNum'] = intval($pageNum);
  73. }
  74. unset($item);
  75. }
  76. return [
  77. 'code' => 0,
  78. 'success'=> true,
  79. 'msg' => '操作成功',
  80. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  81. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  82. ];
  83. }
  84. /**
  85. * 获取列表
  86. * @return array
  87. * @since 2020/11/11
  88. * @author wesmiler
  89. */
  90. public function getDataList($params)
  91. {
  92. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  93. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  94. $dataList = $this->model::from('buddhists as a')
  95. ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
  96. ->where(function ($query) use ($params) {
  97. $query->where(['a.mark'=>1,'a.status'=> 1]);
  98. $title = isset($params['title']) ? trim($params['title']) : '';
  99. if (!empty($title)) {
  100. $query->where('a.title', 'like', "%{$title}%");
  101. }
  102. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  103. if ($cateId > 0) {
  104. $query->where('a.cate_id', $cateId);
  105. }
  106. $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
  107. if ($isRecommand > 0) {
  108. $query->where('a.is_recommand', $isRecommand);
  109. }
  110. })
  111. ->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'])
  112. ->orderBy('a.update_time', 'desc')
  113. ->paginate($pageSize);
  114. $dataList = $dataList ? $dataList->toArray() : [];
  115. if ($dataList) {
  116. foreach ($dataList['data'] as &$item) {
  117. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  118. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  119. $pageNum = BuddhistPagesService::make()->getCount($item['id']);
  120. $item['pageNum'] = intval($pageNum);
  121. }
  122. unset($item);
  123. }
  124. return [
  125. 'code' => 0,
  126. 'success'=> true,
  127. 'msg' => '操作成功',
  128. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  129. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  130. ];
  131. }
  132. /**
  133. * 添加或编辑
  134. * @return array
  135. * @since 2020/11/11
  136. * @author wesmiler
  137. */
  138. public function edit()
  139. {
  140. $data = request()->all();
  141. // 图片处理
  142. $image = trim($data['thumb']);
  143. $id = isset($data['id']) ? $data['id'] : 0;
  144. if (!$id && !$image) {
  145. return message('请上传封面图片', false);
  146. }
  147. if (strpos($image, "temp")) {
  148. $data['thumb'] = save_image($image, 'item');
  149. } else {
  150. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  151. }
  152. $data['update_time'] = time();
  153. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  154. return parent::edit($data); // TODO: Change the autogenerated stub
  155. }
  156. /**
  157. * 删除
  158. * @return array
  159. */
  160. public function delete()
  161. {
  162. $id = request()->get('id', 0);
  163. if($id && BuddhistPagesService::make()->getCount($id)){
  164. return message('请先删除章节数据', false);
  165. }
  166. return parent::delete(); // TODO: Change the autogenerated stub
  167. }
  168. /**
  169. * 相关推荐列表
  170. * @return array
  171. * @since 2020/11/11
  172. * @author wesmiler
  173. */
  174. public function getRelationList($params)
  175. {
  176. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  177. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  178. $id = isset($params['id']) ? intval($params['id']) : PERPAGE;
  179. $info = $this->model::from('buddhists as a')
  180. ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
  181. ->where(['a.id'=> $id,'a.mark'=> 1,'a.status'=> 1])
  182. ->select(['a.id','a.cate_id'])
  183. ->first();
  184. $dataList = $this->model::from('buddhists as a')
  185. ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
  186. ->where(function ($query) use ($params, $info) {
  187. $query->where(['a.mark'=>1,'a.status'=> 1]);
  188. $cateId = isset($info['cate_id']) ? intval($info['cate_id']) : 0;
  189. if ($cateId > 0) {
  190. $query->where('a.cate_id', $cateId);
  191. }
  192. })
  193. ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.is_recommand','a.publish_at','a.create_time'])
  194. ->orderBy('a.sort', 'desc')
  195. ->orderBy('a.update_time', 'desc')
  196. ->paginate($pageSize);
  197. $dataList = $dataList ? $dataList->toArray() : [];
  198. if(empty($dataList)){
  199. $dataList = $this->model::from('buddhists as a')
  200. ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
  201. ->where(['a.mark'=>1,'a.status'=> 1])
  202. ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.is_recommand','a.publish_at','a.create_time'])
  203. ->orderBy(\DB::raw('rand()'))
  204. ->orderBy('a.update_time', 'desc')
  205. ->paginate($pageSize);
  206. $dataList = $dataList ? $dataList->toArray() : [];
  207. }
  208. if ($dataList) {
  209. foreach ($dataList['data'] as &$item) {
  210. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  211. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  212. $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
  213. }
  214. unset($item);
  215. }
  216. return [
  217. 'code' => 0,
  218. 'success'=> true,
  219. 'msg' => '操作成功',
  220. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  221. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  222. ];
  223. }
  224. /**
  225. * 获取详情
  226. * @param $id
  227. */
  228. public function getDetail($id, $userId=0){
  229. $info = $this->model::from('buddhists as a')
  230. ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
  231. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  232. ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.is_recommand','a.publish_at','a.create_time'])
  233. ->first();
  234. $info = $info? $info->toArray() : [];
  235. if($info){
  236. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  237. $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
  238. $pageNum = BuddhistPagesService::make()->getCount($info['id']);
  239. $info['pageNum'] = intval($pageNum);
  240. // 是否收藏
  241. $info['is_collect'] = 0;
  242. if($userId){
  243. if(BuddhistCollectModel::where(['user_id'=> $userId,'source_id'=> $id,'mark'=> 1,'status'=> 1])->value('id')){
  244. $info['is_collect'] = 1;
  245. }
  246. }
  247. }
  248. return message(MESSAGE_OK, true, $info);
  249. }
  250. /**
  251. * 访问量
  252. * @return mixed
  253. */
  254. public function updateVisit($userId=0){
  255. $bid = request()->get('bid');
  256. $cacheKey = "caches:buddhist:visit:{$userId}_{$bid}";
  257. $check = RedisService::get($cacheKey);
  258. if($bid && !$check){
  259. RedisService::set($cacheKey, $bid, 3600);
  260. return $this->model::where(['id'=> $bid])->increment('view_num', 1);
  261. }
  262. }
  263. }