PostRecordService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\PostModel;
  13. use App\Models\PostRecordModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 动态评论/点赞-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Api
  21. */
  22. class PostRecordService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new PostRecordModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $query = $this->getQuery($params);
  53. $query->orderBy('posts_records.id','desc');
  54. $list = $query->select(['posts_records.*'])->paginate($pageSize > 0 ? $pageSize : 9999999);
  55. $list = $list? $list->toArray() :[];
  56. return [
  57. 'pageSize'=> $pageSize,
  58. 'total'=>isset($list['total'])? $list['total'] : 0,
  59. 'list'=> isset($list['data'])? $list['data'] : []
  60. ];
  61. }
  62. /**
  63. * 查询
  64. * @param $params
  65. * @return mixed
  66. */
  67. public function getQuery($params)
  68. {
  69. $where = ['posts_records.mark' => 1];
  70. $status = isset($params['status'])? $params['status'] : 0;
  71. $type = isset($params['type'])? $params['type'] : 0;
  72. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  73. if($status>0){
  74. $where['posts_records.status'] = $status;
  75. }
  76. if($type>0){
  77. $where['posts_records.type'] = $type;
  78. }
  79. return $this->model->with(['user'])->from('posts_records')
  80. ->where($where)
  81. ->where(function ($query) use($params){
  82. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  83. if($keyword){
  84. $query->where('posts_records.title','like',"%{$keyword}%")
  85. ->orWhere('posts_records.tags','like',"%{$keyword}%");
  86. }
  87. $tag = isset($params['tag'])? $params['tag'] : '';
  88. if($tag){
  89. $query->where('posts_records.tags','like',"%{$tag},%");
  90. }
  91. });
  92. }
  93. /**
  94. * 评论
  95. * @param $userId
  96. * @param $params
  97. * @return array|false
  98. */
  99. public function submit($userId, $params)
  100. {
  101. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  102. $replyId = isset($params['reply_id']) ? intval($params['reply_id']) : 0;
  103. $replyUid = isset($params['reply_uid']) ? intval($params['reply_uid']) : 0;
  104. $description = isset($params['description']) && $params['description']? trim($params['description']) : '';
  105. $albums = isset($params['albums']) ? get_format_images($params['albums'], '') : '';
  106. if(empty($sourceId)){
  107. $this->error = '请选择评论的动态';
  108. return false;
  109. }
  110. if(empty($description)){
  111. $this->error = '评论内容不能为空';
  112. return false;
  113. }
  114. $cacheKey = "caches:members:comment_{$userId}_{$sourceId}";
  115. if(RedisService::get($cacheKey.'_lock')){
  116. $this->error = '请不要频繁提交';
  117. return false;
  118. }
  119. RedisService::set($cacheKey, $params, rand(20,30));
  120. $info = PostModel::with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
  121. $postUserId = isset($info['user_id'])?$info['user_id']:0;
  122. $userInfo = isset($info['user'])?$info['user']:[];
  123. if (empty($info) || $sourceId<=0 || $postUserId<=0) {
  124. RedisService::clear($cacheKey.'_lock');
  125. $this->error = '该动态已不存在';
  126. return false;
  127. }
  128. $replyInfo = $this->model->with(['user'])->where(['id'=>$replyId,'mark'=>1])->first();
  129. if ($replyId>0 && empty($replyInfo)) {
  130. RedisService::clear($cacheKey.'_lock');
  131. $this->error = '回复内容不存在';
  132. return false;
  133. }
  134. $data = [
  135. 'source_id'=>$sourceId,
  136. 'reply_id'=>$replyId,
  137. 'reply_uid'=>$replyUid,
  138. 'user_id'=> $userId,
  139. 'albums'=> $albums,
  140. 'type'=>1,
  141. 'create_time'=>time(),
  142. 'description'=> $description,
  143. 'status'=> 1,
  144. ];
  145. if($sourceId != $this->model->insertGetId($data)){
  146. $this->error = $replyId>0?'回复失败':'评论失败';
  147. }
  148. $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
  149. $msgData = [
  150. 'from_uid'=> $userId,
  151. 'type'=> 4,
  152. 'title'=>'互动消息',
  153. 'description'=> $replyId? "回复{$nickname}评论了您的动态评论": "用户{$nickname}评论了您的动态",
  154. 'content'=> json_encode(['source_id'=>$sourceId,'page'=>'/pagesSub/pages/posts/detail?id='.$sourceId.'&replyId='.$replyId]),
  155. 'msg_type'=>4,
  156. ];
  157. // 互动消息
  158. MessageService::make()->pushMessage($postUserId, $msgData);
  159. $this->error = $replyId>0?'回复成功':'评论成功';
  160. return ['id' => $sourceId];
  161. }
  162. /**
  163. * 点赞
  164. * @param $userId
  165. * @param $params
  166. * @return array|false
  167. */
  168. public function like($userId, $params)
  169. {
  170. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  171. $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
  172. $info = PostModel::with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
  173. $postUserId = isset($info['user_id'])?$info['user_id']:0;
  174. $userInfo = isset($info['user'])?$info['user']:[];
  175. if (empty($info) || $sourceId<=0 || $postUserId<=0) {
  176. $this->error = '该内容已不存在';
  177. return false;
  178. }
  179. if (!in_array($status,[1,2])) {
  180. $this->error = '操作状态错误';
  181. return false;
  182. }
  183. $record = $this->model->where(['source_id'=>$sourceId,'user_id'=>$userId,'type'=>2,'mark'=>1])->first();
  184. $recordId = isset($record['id'])?$record['id'] : 0;
  185. $recordStatus = isset($record['status'])?$record['status'] : 0;
  186. if($recordId && $recordStatus == $status){
  187. $this->error = '您已点赞过';
  188. return false;
  189. }
  190. $data = [
  191. 'source_id'=>$sourceId,
  192. 'user_id'=>$userId,
  193. 'type'=>2,
  194. 'create_time'=>time(),
  195. 'description'=> '点赞',
  196. 'status'=> $status,
  197. ];
  198. $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
  199. $msgData = [
  200. 'from_uid'=> $userId,
  201. 'type'=> 4,
  202. 'title'=>'互动消息',
  203. 'description'=> "用户{$nickname}点赞了您的笔记",
  204. 'msg_type'=>1,
  205. ];
  206. if($recordId){
  207. $this->model->where(['id'=>$recordId])->update(['status'=>$status,'update_time'=>time()]);
  208. // 互动消息
  209. if($status == 1){
  210. MessageService::make()->pushMessage($postUserId, $msgData);
  211. }
  212. $count = $this->model->where(['source_id'=>$sourceId,'type'=> 2,'status'=>1,'mark'=>1])->count('id');
  213. $this->error = $status==1? '点赞成功':'取消点赞成功';
  214. $this->errorData = $status==2? ['count'=>$count,'data'=>[]] : ['count'=>$count,'data'=>['id' => $sourceId,'user_id'=>$userId,'status'=>$status]];
  215. return true;
  216. }else{
  217. if(!$sourceId = $this->model->insertGetId($data)){
  218. $this->error = $status==1? '点赞失败':'取消点赞失败';
  219. return false;
  220. }
  221. // 互动消息
  222. if($status == 1){
  223. MessageService::make()->pushMessage($postUserId, $msgData);
  224. }
  225. $this->error = $status==1? '点赞成功':'取消点赞成功';
  226. $count = $this->model->where(['source_id'=>$sourceId,'type'=> 2,'status'=>1,'mark'=>1])->count('id');
  227. $this->errorData = $status==2? ['count'=>$count,'data'=>[]] : ['count'=>$count,'data'=>['id' => $sourceId,'user_id'=>$userId,'status'=>$status]];
  228. return true;
  229. }
  230. }
  231. }