SocialService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 SocialService 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 PostModel();
  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. * 详情
  47. * @param int $num
  48. * @return array|mixed
  49. */
  50. public function getInfo($id)
  51. {
  52. $cacheKey = "caches:scoial:info_{$id}";
  53. $datas = RedisService::get($cacheKey);
  54. if($datas){
  55. return $datas;
  56. }
  57. $datas = $this->model->where(['id'=>$id,'status'=>1,'mark'=>1])
  58. ->orderBy('id','desc')
  59. ->first();
  60. $datas = $datas? $datas->toArray() : [];
  61. if($datas){
  62. RedisService::set($cacheKey, $datas, 600);
  63. }
  64. return $datas;
  65. }
  66. /**
  67. * @param $params
  68. * @param int $pageSize
  69. * @return array
  70. */
  71. public function getDataList($params, $pageSize = 15)
  72. {
  73. $query = $this->getQuery($params);
  74. $type = isset($params['type'])?intval($params['type']) : 0;
  75. if($type==1){
  76. $query->orderBy('posts.views','desc');
  77. }else{
  78. $query->orderBy('posts.id','desc');
  79. }
  80. $list = $query->select(['posts.*'])->withCount(['likes'])->paginate($pageSize > 0 ? $pageSize : 9999999);
  81. $list = $list? $list->toArray() :[];
  82. return [
  83. 'pageSize'=> $pageSize,
  84. 'total'=>isset($list['total'])? $list['total'] : 0,
  85. 'list'=> isset($list['data'])? $list['data'] : []
  86. ];
  87. }
  88. /**
  89. * 查询
  90. * @param $params
  91. * @return mixed
  92. */
  93. public function getQuery($params)
  94. {
  95. $where = ['posts.mark' => 1];
  96. $status = isset($params['status'])? $params['status'] : 0;
  97. $type = isset($params['type'])? $params['type'] : 0;
  98. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  99. if($status>0){
  100. $where['posts.status'] = $status;
  101. }
  102. if($type>0){
  103. $where['posts.type'] = $type;
  104. }
  105. return $this->model->with(['user','isLike'=>function($query) use($userId){
  106. $query->where('user_id', $userId);
  107. }])->from('posts')
  108. ->where($where)
  109. ->where(function ($query) use($params){
  110. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  111. if($keyword){
  112. $query->where('posts.title','like',"%{$keyword}%")
  113. ->orWhere('posts.tags','like',"%{$keyword}%");
  114. }
  115. $tag = isset($params['tag'])? $params['tag'] : '';
  116. if($tag){
  117. $query->where('posts.tags','like',"%{$tag},%");
  118. }
  119. });
  120. }
  121. /**
  122. * 发布笔记
  123. * @param $userId
  124. * @param $params
  125. * @return array|false
  126. */
  127. public function submit($userId, $params)
  128. {
  129. $title = isset($params['title']) ? trim($params['title']) : '';
  130. $content = isset($params['content']) ? trim($params['content']) : '';
  131. $type = isset($params['type']) ? intval($params['type']) : 1;
  132. $showType = isset($params['show_type']) ? intval($params['show_type']) : 1;
  133. if (empty($title)) {
  134. $this->error = '请添加标题';
  135. return false;
  136. }
  137. if (empty($content)) {
  138. $this->error = '请添加内容';
  139. return false;
  140. }
  141. $albums = isset($params['albums']) ? get_format_images($params['albums'], '') : '';
  142. if ($showType == 1 && empty($albums)) {
  143. $this->error = '请添加图片';
  144. return false;
  145. }
  146. $videoUrl = isset($params['video_url']) ? get_image_path($params['video_url']) : '';
  147. if ($showType == 2 && empty($videoUrl)) {
  148. $this->error = '请添加视频';
  149. return false;
  150. }
  151. $cacheKey = "caches:scoial:submit:{$userId}_".md5(json_encode($params,256));
  152. if (RedisService::get($cacheKey."_lock")) {
  153. $this->error = '请不要重复提交';
  154. return false;
  155. }
  156. $data = [
  157. 'user_id' => $userId,
  158. 'title' => $title,
  159. 'type' => $type,
  160. 'show_type' => $showType,
  161. 'cover' => $albums?$albums[0]:'',
  162. 'albums' => $albums,
  163. 'video_url' => $videoUrl,
  164. 'tags' => isset($params['tags']) && $params['tags']? implode(',', $params['tags']).',':'',
  165. 'content' => $content,
  166. 'create_time' => time(),
  167. 'update_time' => time(),
  168. 'status' => 1,
  169. 'mark' => 1,
  170. ];
  171. RedisService::set($cacheKey."_lock", $data, rand(20,30));
  172. if(!$id = $this->model->insertGetId($data)){
  173. RedisService::clear($cacheKey."_lock");
  174. $this->error = '发布失败';
  175. return false;
  176. }
  177. $this->error = '发布成功';
  178. RedisService::set($cacheKey, $data, 30);
  179. RedisService::clear($cacheKey."_lock");
  180. return ['id' => $id];
  181. }
  182. /**
  183. * 点赞
  184. * @param $userId
  185. * @param $params
  186. * @return array|false
  187. */
  188. public function like($userId, $params)
  189. {
  190. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  191. $status = isset($params['status']) && $params['status']? intval($params['status']) : 1;
  192. $info = $this->model->with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
  193. $postUserId = isset($info['user_id'])?$info['user_id']:0;
  194. $userInfo = isset($info['user'])?$info['user']:[];
  195. if (empty($info) || $sourceId<=0 || $postUserId<=0) {
  196. $this->error = '该内容已不存在';
  197. return false;
  198. }
  199. if (!in_array($status,[1,2])) {
  200. $this->error = '操作状态错误';
  201. return false;
  202. }
  203. $record = PostRecordModel::where(['source_id'=>$sourceId,'user_id'=>$userId,'type'=>2,'mark'=>1])->first();
  204. $recordId = isset($record['id'])?$record['id'] : 0;
  205. $recordStatus = isset($record['status'])?$record['status'] : 0;
  206. if($recordId && $recordStatus == $status){
  207. $this->error = '您已点赞过';
  208. return false;
  209. }
  210. $data = [
  211. 'source_id'=>$sourceId,
  212. 'user_id'=>$userId,
  213. 'type'=>2,
  214. 'create_time'=>time(),
  215. 'description'=> '点赞',
  216. 'status'=> $status,
  217. ];
  218. $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
  219. $msgData = [
  220. 'from_uid'=> $userId,
  221. 'type'=> 4,
  222. 'title'=>'互动消息',
  223. 'description'=> "用户{$nickname}点赞了您的笔记",
  224. 'msg_type'=>1,
  225. ];
  226. if($recordId){
  227. PostRecordModel::where(['id'=>$recordId])->update(['status'=>$status,'update_time'=>time()]);
  228. // 互动消息
  229. if($status == 1){
  230. MessageService::make()->pushMessage($postUserId, $msgData);
  231. }
  232. $this->error = $status==1? '点赞成功':'取消点赞成功';
  233. return ['id' => $sourceId];
  234. }else{
  235. if(!$sourceId = PostRecordModel::insertGetId($data)){
  236. $this->error = $status==1? '点赞失败':'取消点赞失败';
  237. return false;
  238. }
  239. // 互动消息
  240. if($status == 1){
  241. MessageService::make()->pushMessage($postUserId, $msgData);
  242. }
  243. $this->error = $status==1? '点赞成功':'取消点赞成功';
  244. return ['id' => $sourceId];
  245. }
  246. }
  247. /**
  248. * 评论
  249. * @param $userId
  250. * @param $params
  251. * @return array|false
  252. */
  253. public function comment($userId, $params)
  254. {
  255. $sourceId = isset($params['source_id']) ? intval($params['source_id']) : 0;
  256. $replyId = isset($params['reply_id']) ? intval($params['reply_id']) : 0;
  257. $replyUid = isset($params['reply_uid']) ? intval($params['reply_uid']) : 0;
  258. $description = isset($params['description']) && $params['description']? trim($params['description']) : '';
  259. $albums = isset($params['albums']) ? get_format_images($params['albums'], '') : '';
  260. if(empty($sourceId)){
  261. $this->error = '请选择评论的动态';
  262. return false;
  263. }
  264. if(empty($description)){
  265. $this->error = '评论内容不能为空';
  266. return false;
  267. }
  268. $cacheKey = "caches:members:comment_{$userId}_{$sourceId}";
  269. if(RedisService::get($cacheKey.'_lock')){
  270. $this->error = '请不要频繁提交';
  271. return false;
  272. }
  273. RedisService::set($cacheKey, $params, rand(20,30));
  274. $info = $this->model->with(['user'])->where(['id'=>$sourceId,'mark'=>1])->first();
  275. $postUserId = isset($info['user_id'])?$info['user_id']:0;
  276. $userInfo = isset($info['user'])?$info['user']:[];
  277. if (empty($info) || $sourceId<=0 || $postUserId<=0) {
  278. RedisService::clear($cacheKey.'_lock');
  279. $this->error = '该动态已不存在';
  280. return false;
  281. }
  282. $replyInfo = PostRecordModel::with(['user'])->where(['id'=>$replyId,'mark'=>1])->first();
  283. if ($replyId>0 && empty($replyInfo)) {
  284. RedisService::clear($cacheKey.'_lock');
  285. $this->error = '回复内容不存在';
  286. return false;
  287. }
  288. $data = [
  289. 'source_id'=>$sourceId,
  290. 'reply_id'=>$replyId,
  291. 'reply_uid'=>$replyUid,
  292. 'user_id'=> $userId,
  293. 'albums'=> $albums,
  294. 'type'=>1,
  295. 'create_time'=>time(),
  296. 'description'=> $description,
  297. 'status'=> 1,
  298. ];
  299. if($sourceId != PostRecordModel::insertGetId($data)){
  300. $this->error = $replyId>0?'回复失败':'评论失败';
  301. }
  302. $nickname = isset($userInfo['nickname'])?$userInfo['nickname']:'';
  303. $msgData = [
  304. 'from_uid'=> $userId,
  305. 'type'=> 4,
  306. 'title'=>'互动消息',
  307. 'description'=> $replyId? "回复{$nickname}评论了您的动态评论": "用户{$nickname}评论了您的动态",
  308. 'content'=> json_encode(['source_id'=>$sourceId,'page'=>'/pagesSub/pages/posts/detail?id='.$sourceId.'&replyId='.$replyId]),
  309. 'msg_type'=>4,
  310. ];
  311. // 互动消息
  312. MessageService::make()->pushMessage($postUserId, $msgData);
  313. $this->error = $replyId>0?'回复成功':'评论成功';
  314. return ['id' => $sourceId];
  315. }
  316. }