DynamicService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\CollectModel;
  13. use App\Models\DynamicCommentModel;
  14. use App\Models\DynamicModel;
  15. use App\Models\FollowModel;
  16. use App\Models\MemberModel;
  17. /**
  18. * 动态管理-服务类
  19. * @author wesmiler
  20. * @since 2020/11/11
  21. * Class DynamicService
  22. * @package App\Services
  23. */
  24. class DynamicService extends BaseService
  25. {
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author wesmiler
  30. * @since 2020/11/11
  31. * DynamicService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new DynamicModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return DynamicService|null
  40. */
  41. public static function make(){
  42. if(!self::$instance){
  43. self::$instance = new DynamicService();
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 获取列表
  49. * @return array
  50. * @since 2020/11/11
  51. * @author wesmiler
  52. */
  53. public function getList()
  54. {
  55. $params = request()->all();
  56. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  57. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  58. $dataList = $this->model::from('dynamic as a')
  59. ->where(function ($query) use ($params) {
  60. $query->where('a.mark', 1);
  61. $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
  62. if ($isRecommand > 0) {
  63. $query->where('a.is_recommand', $isRecommand);
  64. }
  65. $status = isset($params['status']) ? $params['status'] : 0;
  66. if ($status > 0) {
  67. $query->where('a.status', $status);
  68. } else {
  69. $query->whereIn('a.status', [1, 2]);
  70. }
  71. })
  72. ->select(['a.id', 'a.user_id', 'a.comment_close', 'a.source_id', 'a.is_recommand', 'a.content', 'a.albums', 'a.status', 'a.create_time', 'a.update_time'])
  73. ->orderBy('a.update_time', 'desc')
  74. ->paginate($pageSize);
  75. $dataList = $dataList ? $dataList->toArray() : [];
  76. if ($dataList) {
  77. foreach ($dataList['data'] as &$item) {
  78. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  79. }
  80. unset($item);
  81. }
  82. return [
  83. 'code' => 0,
  84. 'success'=> true,
  85. 'msg' => '操作成功',
  86. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  87. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  88. ];
  89. }
  90. /**
  91. * 获取列表
  92. * @return array
  93. * @since 2020/11/11
  94. * @author wesmiler
  95. */
  96. public function getDataList($params)
  97. {
  98. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  99. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  100. $dataList = $this->model::from('dynamic as a')
  101. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  102. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  103. ->where(['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1])
  104. ->where('m.user_id','>',0)
  105. ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a,albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
  106. ->orderBy('a.update_time', 'desc')
  107. ->paginate($pageSize);
  108. $dataList = $dataList ? $dataList->toArray() : [];
  109. if ($dataList) {
  110. foreach ($dataList['data'] as &$item) {
  111. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  112. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  113. $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : [];
  114. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  115. // 关注数量
  116. $item['collect'] = 0;
  117. if($item['id']){
  118. $count = CollectModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  119. $item['collect'] = $count? $count : 0;
  120. }
  121. // 评论数量
  122. $item['comment'] = 0;
  123. if($item['id']){
  124. $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  125. $item['comment'] = $count? $count : 0;
  126. }
  127. }
  128. unset($item);
  129. }
  130. return [
  131. 'code' => 0,
  132. 'success'=> true,
  133. 'msg' => '操作成功',
  134. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  135. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  136. ];
  137. }
  138. /**
  139. * 获取详情
  140. * @param $id
  141. */
  142. public function getDetail($id, $userId=0){
  143. $info = $this->model::from('dynamic as a')
  144. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  145. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  146. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id,'m.mark'=> 1,'m.status'=> 1])
  147. ->where('m.id','>',0)
  148. ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a,albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
  149. ->first();
  150. $info = $info? $info->toArray() : [];
  151. if($info){
  152. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  153. $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : '';
  154. $info['create_time'] = $info['create_time']? datetime( $info['create_time'],'Y-m-d H:i:s') : '';
  155. $info['is_follow'] = 0;
  156. $info[''] = 0;
  157. if($info['user_id']){
  158. $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
  159. $info['author'] = $author? $author : '报恩寺';
  160. }
  161. // 是否已收藏
  162. $info['is_collect'] = 0;
  163. if($userId){
  164. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){
  165. $info['is_collect'] = 1;
  166. }
  167. }
  168. }
  169. return $info;
  170. }
  171. /**
  172. * 添加或编辑
  173. * @return array
  174. * @since 2020/11/11
  175. * @author wesmiler
  176. */
  177. public function edit()
  178. {
  179. $data = request()->all();
  180. // 图片处理
  181. $type = isset($data['type'])? intval($data['type']) : 0;
  182. $image = $data['thumb']? trim($data['thumb']) : '';
  183. $id = isset($data['id']) ? $data['id'] : 0;
  184. if (!$id && !$image && $type == 1) {
  185. return message('请上传封面图片', false);
  186. }
  187. if (strpos($image, "temp")) {
  188. $data['thumb'] = save_image($image, 'item');
  189. } else {
  190. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  191. }
  192. $data['update_time'] = time();
  193. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  194. return parent::edit($data); // TODO: Change the autogenerated stub
  195. }
  196. }