PostRecordService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.reply_id'=>0,'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. $sourceId = isset($params['source_id'])? $params['source_id'] : 0;
  74. if($status>0){
  75. $where['posts_records.status'] = $status;
  76. }
  77. if($userId>0){
  78. $where['posts_records.user_id'] = $userId;
  79. }
  80. if($sourceId>0){
  81. $where['posts_records.source_id'] = $sourceId;
  82. }
  83. if($type>0){
  84. $where['posts_records.type'] = $type;
  85. }
  86. return $this->model->with(['user','replyUser','replys'])->from('posts_records')
  87. ->where($where)
  88. ->where(function ($query) use($params){
  89. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  90. if($keyword){
  91. $query->where('posts_records.title','like',"%{$keyword}%")
  92. ->orWhere('posts_records.tags','like',"%{$keyword}%");
  93. }
  94. $tag = isset($params['tag'])? $params['tag'] : '';
  95. if($tag){
  96. $query->where('posts_records.tags','like',"%{$tag},%");
  97. }
  98. });
  99. }
  100. /**
  101. * 评论
  102. * @param $userId
  103. * @param $params
  104. * @return array|false
  105. */
  106. public function submit($userId, $params)
  107. {
  108. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  109. $replyId = isset($params['reply_id']) ? intval($params['reply_id']) : 0;
  110. $replyUid = isset($params['reply_uid']) ? intval($params['reply_uid']) : 0;
  111. $description = isset($params['content']) && $params['content']? trim($params['content']) : '';
  112. $albums = isset($params['albums']) ? get_format_images($params['albums'], 'url') : '';
  113. if(empty($sourceId)){
  114. $this->error = '请选择评论的动态';
  115. return false;
  116. }
  117. if(empty($description)){
  118. $this->error = '评论内容不能为空';
  119. return false;
  120. }
  121. if($userId<=0){
  122. $this->error = '请先登录';
  123. $this->errorCode = 403;
  124. return false;
  125. }
  126. $cacheKey = "caches:members:comment_{$userId}_{$sourceId}";
  127. if(RedisService::get($cacheKey.'_lock')){
  128. $this->error = '请不要频繁提交';
  129. return false;
  130. }
  131. RedisService::set($cacheKey, $params, rand(20,30));
  132. $info = PostModel::with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
  133. $postUserId = isset($info['user_id'])?$info['user_id']:0;
  134. $userInfo = isset($info['user'])?$info['user']:[];
  135. if (empty($info) || $sourceId<=0 || $postUserId<=0) {
  136. RedisService::clear($cacheKey.'_lock');
  137. $this->error = '该动态已不存在';
  138. return false;
  139. }
  140. $replyInfo = $this->model->with(['user'])->where(['id'=>$replyId,'mark'=>1])->first();
  141. if ($replyId>0 && empty($replyInfo)) {
  142. RedisService::clear($cacheKey.'_lock');
  143. $this->error = '回复内容不存在';
  144. return false;
  145. }
  146. $data = [
  147. 'source_id'=>$sourceId,
  148. 'reply_id'=>$replyId,
  149. 'reply_uid'=>$replyUid,
  150. 'user_id'=> $userId,
  151. 'albums'=> $albums,
  152. 'type'=>1,
  153. 'create_time'=>time(),
  154. 'description'=> $description,
  155. 'status'=> 1,
  156. ];
  157. if(!$cid = $this->model->insertGetId($data)){
  158. $this->error = $replyId>0?'回复失败':'评论失败';
  159. }
  160. $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
  161. $msgData = [
  162. 'from_uid'=> $userId,
  163. 'type'=> 4,
  164. 'title'=>'互动消息',
  165. 'description'=> $replyId? "回复{$nickname}评论了您的动态评论": "用户{$nickname}评论了您的动态",
  166. 'content'=> json_encode(['source_id'=>$sourceId,'cid'=>$cid,'page'=>'/pagesSub/pages/posts/detail?id='.$sourceId.'&replyId='.$replyId]),
  167. 'msg_type'=>4,
  168. ];
  169. // 互动消息
  170. MessageService::make()->pushMessage($postUserId, $msgData);
  171. $this->error = $replyId>0?'回复成功':'评论成功';
  172. $info = $this->model->where(['id'=> $cid])->with(['user','replyUser'])->first();
  173. return $info;
  174. }
  175. /**
  176. * 点赞
  177. * @param $userId
  178. * @param $params
  179. * @return array|false
  180. */
  181. public function like($userId, $params)
  182. {
  183. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  184. $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
  185. $info = PostModel::with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
  186. $postUserId = isset($info['user_id'])?$info['user_id']:0;
  187. $userInfo = isset($info['user'])?$info['user']:[];
  188. if (empty($info) || $sourceId<=0 || $postUserId<=0) {
  189. $this->error = '该内容已不存在';
  190. return false;
  191. }
  192. if (!in_array($status,[1,2])) {
  193. $this->error = '操作状态错误';
  194. return false;
  195. }
  196. $record = $this->model->where(['source_id'=>$sourceId,'user_id'=>$userId,'type'=>2,'mark'=>1])->first();
  197. $recordId = isset($record['id'])?$record['id'] : 0;
  198. $data = [
  199. 'source_id'=>$sourceId,
  200. 'user_id'=>$userId,
  201. 'type'=>2,
  202. 'create_time'=>time(),
  203. 'description'=> '点赞',
  204. 'status'=> $status,
  205. ];
  206. $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
  207. $msgData = [
  208. 'from_uid'=> $userId,
  209. 'type'=> 4,
  210. 'title'=>'互动消息',
  211. 'description'=> "用户{$nickname}点赞了您的笔记",
  212. 'msg_type'=>1,
  213. ];
  214. if($recordId){
  215. $this->model->where(['id'=>$recordId])->update(['status'=>$status,'update_time'=>time()]);
  216. // 互动消息
  217. if($status == 1){
  218. MessageService::make()->pushMessage($postUserId, $msgData);
  219. }
  220. $count = $this->model->where(['source_id'=>$sourceId,'type'=> 2,'status'=>1,'mark'=>1])->count('id');
  221. $this->error = $status==1? '点赞成功':'取消点赞成功';
  222. RedisService::keyDel("caches:social:info_{$sourceId}*");
  223. $this->errorData = $status==2? ['count'=>$count,'data'=>[]] : ['count'=>$count,'data'=>['id' => $sourceId,'user_id'=>$userId,'status'=>$status]];
  224. return true;
  225. }else{
  226. if(!$sourceId = $this->model->insertGetId($data)){
  227. $this->error = $status==1? '点赞失败':'取消点赞失败';
  228. return false;
  229. }
  230. // 互动消息
  231. if($status == 1){
  232. MessageService::make()->pushMessage($postUserId, $msgData);
  233. }
  234. $this->error = $status==1? '点赞成功':'取消点赞成功';
  235. RedisService::keyDel("caches:social:info_{$sourceId}*");
  236. $count = $this->model->where(['source_id'=>$sourceId,'type'=> 2,'status'=>1,'mark'=>1])->count('id');
  237. $this->errorData = $status==2? ['count'=>$count,'data'=>[]] : ['count'=>$count,'data'=>['id' => $sourceId,'user_id'=>$userId,'status'=>$status]];
  238. return true;
  239. }
  240. }
  241. /**
  242. * 删除
  243. * @return array
  244. */
  245. public function delete()
  246. {
  247. $this->model->where('mark',0)->where('create_time','<=', 600)->delete();
  248. return parent::delete(); // TODO: Change the autogenerated stub
  249. }
  250. }