SocialService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, $userId=0)
  51. {
  52. $cacheKey = "caches:social:info_{$id}_{$userId}";
  53. $datas = RedisService::get($cacheKey);
  54. if($datas){
  55. return $datas;
  56. }
  57. $datas = $this->model->with(['user','isLike'=>function($query) use($userId){
  58. $query->where('user_id', $userId);
  59. }])
  60. ->where(['id'=>$id,'status'=>1,'mark'=>1])
  61. ->orderBy('id','desc')
  62. ->first();
  63. $datas = $datas? $datas->toArray() : [];
  64. if($datas){
  65. RedisService::set($cacheKey, $datas, rand(10,20));
  66. }
  67. return $datas;
  68. }
  69. /**
  70. * @param $params
  71. * @param int $pageSize
  72. * @return array
  73. */
  74. public function getDataList($params, $pageSize = 15)
  75. {
  76. $query = $this->getQuery($params);
  77. $sortType = isset($params['sort_type'])?intval($params['sort_type']) : 0;
  78. if($sortType==1){
  79. $query->orderBy('posts.views','desc');
  80. }else{
  81. $query->orderBy('posts.id','desc');
  82. }
  83. $list = $query->select(['posts.*'])->withCount(['likes'])->paginate($pageSize > 0 ? $pageSize : 9999999);
  84. $list = $list? $list->toArray() :[];
  85. return [
  86. 'pageSize'=> $pageSize,
  87. 'total'=>isset($list['total'])? $list['total'] : 0,
  88. 'list'=> isset($list['data'])? $list['data'] : []
  89. ];
  90. }
  91. /**
  92. * 查询
  93. * @param $params
  94. * @return mixed
  95. */
  96. public function getQuery($params)
  97. {
  98. $where = ['posts.mark' => 1];
  99. $status = isset($params['status'])? $params['status'] : 0;
  100. $type = isset($params['type'])? $params['type'] : 0;
  101. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  102. if($status>0){
  103. $where['posts.status'] = $status;
  104. }
  105. if($type>0){
  106. $where['posts.type'] = $type;
  107. }
  108. return $this->model->with(['user','isLike'=>function($query) use($userId){
  109. $query->where('user_id', $userId);
  110. }])->from('posts')
  111. ->where($where)
  112. ->where(function ($query) use($params){
  113. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  114. if($keyword){
  115. $query->where('posts.title','like',"%{$keyword}%")
  116. ->orWhere('posts.tags','like',"%{$keyword}%");
  117. }
  118. $tag = isset($params['tag'])? $params['tag'] : '';
  119. if($tag){
  120. $query->where('posts.tags','like',"%{$tag},%");
  121. }
  122. });
  123. }
  124. /**
  125. * 发布笔记
  126. * @param $userId
  127. * @param $params
  128. * @return array|false
  129. */
  130. public function submit($userId, $params)
  131. {
  132. $title = isset($params['title']) ? trim($params['title']) : '';
  133. $content = isset($params['content']) ? trim($params['content']) : '';
  134. $type = isset($params['type']) ? intval($params['type']) : 1;
  135. $showType = isset($params['show_type']) ? intval($params['show_type']) : 1;
  136. if (empty($title)) {
  137. $this->error = '请添加标题';
  138. return false;
  139. }
  140. if (empty($content)) {
  141. $this->error = '请添加内容';
  142. return false;
  143. }
  144. $albums = isset($params['albums']) ? $params['albums'] : [];
  145. $cover = isset($albums[0])?get_image_path($albums[0]):'';
  146. $albums = $albums? get_format_images($albums, '') : '';
  147. if ($showType == 1 && empty($albums)) {
  148. $this->error = '请添加图片';
  149. return false;
  150. }
  151. $videoUrl = isset($params['video_url']) ? get_image_path($params['video_url']) : '';
  152. if ($showType == 2 && empty($videoUrl)) {
  153. $this->error = '请添加视频';
  154. return false;
  155. }
  156. $cacheKey = "caches:scoial:submit:{$userId}_".md5(json_encode($params,256));
  157. if (RedisService::get($cacheKey."_lock")) {
  158. $this->error = '请不要重复提交';
  159. return false;
  160. }
  161. $tags = isset($params['tags'])?$params['tags'] : [];
  162. $tags = array_filter($tags);
  163. $data = [
  164. 'user_id' => $userId,
  165. 'title' => $title,
  166. 'type' => $type,
  167. 'show_type' => $showType,
  168. 'cover' => $cover,
  169. 'albums' => $albums,
  170. 'video_url' => $videoUrl,
  171. 'tags' => $tags? implode(',', $tags).',':'',
  172. 'content' => $content,
  173. 'create_time' => time(),
  174. 'update_time' => time(),
  175. 'status' => 1,
  176. 'mark' => 1,
  177. ];
  178. RedisService::set($cacheKey."_lock", $data, rand(20,30));
  179. if(!$id = $this->model->insertGetId($data)){
  180. RedisService::clear($cacheKey."_lock");
  181. $this->error = '发布失败';
  182. return false;
  183. }
  184. $this->error = '发布成功';
  185. RedisService::set($cacheKey, $data, 30);
  186. RedisService::clear($cacheKey."_lock");
  187. return ['id' => $id];
  188. }
  189. public function delete()
  190. {
  191. $ids = $this->model->where(['mark'=>0])->where('create_time','<', time() - 600)->pluck('id');
  192. $ids = $ids? $ids->toArray() : [];
  193. $this->model->where(['mark'=>0])->where('create_time','<', time() - 600)->delete();
  194. if($ids){
  195. PostRecordModel::whereIn('source_id', $ids)->where('create_time','<', time() - 600)->delete();
  196. }
  197. return parent::delete(); // TODO: Change the autogenerated stub
  198. }
  199. }