CircleMessageRepository.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Circle;
  9. use App\Models\InterestCircle;
  10. use App\Models\InterestCircleArticle;
  11. use App\Models\InterestCircleMessage;
  12. use App\Models\InterestCircleMessageComment;
  13. use App\Models\InterestCircleUser;
  14. use App\Models\Post;
  15. use Illuminate\Database\QueryException;
  16. use Dingo\Api\Http\Response;
  17. use Illuminate\Support\Carbon;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. use Illuminate\Support\Facades\Redis;
  21. class CircleMessageRepository
  22. {
  23. public function __construct(InterestCircle $interestCircle,
  24. InterestCircleMessage $interestCircleMessage,
  25. InterestCircleMessageComment $interestCircleMessageComment
  26. )
  27. {
  28. $this->interestCircle = $interestCircle;
  29. $this->interestCircleMessage = $interestCircleMessage;
  30. $this->interestCircleMessageComment = $interestCircleMessageComment;
  31. }
  32. /**
  33. * 提问列表
  34. */
  35. public function lists($request)
  36. {
  37. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  38. $where[] = ['circle_id', $request['id']];
  39. return $this->interestCircleMessage
  40. ->where($where)
  41. ->with('imgs')
  42. ->orderBy('is_recommend', 'desc')
  43. ->orderBy('id', 'desc')
  44. ->paginate($perPage);
  45. }
  46. /**
  47. * 发布提问
  48. */
  49. public function create($request)
  50. {
  51. //验证小号
  52. $userInfo = $this->getUserInfo();
  53. if (empty($userInfo)) {
  54. return jsonError('获取用户信息失败');
  55. }
  56. if (!$userInfo['sns_status']) {
  57. return jsonError('您已被禁言');
  58. }
  59. $isValid = 0;
  60. if ($userInfo['strength']) {
  61. $isValid = 1;
  62. }
  63. $oneHourTime = Carbon::now()->addHours(-1)->toDateTimeString();
  64. $oneHourPostCount = $this->post->where('uid', $userInfo['uid'])->where('created_at', '>', $oneHourTime)->count();
  65. if ($oneHourPostCount > 5) {
  66. return jsonError('创作欲望太强啦,休息一下,看看其他用户的内容吧!');
  67. }
  68. $detectionText = strip_tags($request['title']) . ',' . strip_tags($request['content']);
  69. $detectionTextResult = $this->detectionService->checkText($detectionText);
  70. if ($detectionTextResult['code'] < 0) {
  71. return jsonError('内容违规,请修正哦');
  72. }
  73. $topicIds = json_decode($request['topic_ids'], true);
  74. $topicCount = count($topicIds);
  75. if ($topicCount == 0 || $topicCount > 2) {
  76. return jsonError('所选话题必须1-2个');
  77. }
  78. //验证话题
  79. $hasTopicCount = $this->topic->whereIn('id', $topicIds)->count();
  80. if ($topicCount != $hasTopicCount) {
  81. Log::error('所选话题非法' . $request['topic_ids']);
  82. return jsonError('所选话题非法');
  83. }
  84. $imgs = [];
  85. if ($request['type'] == 'image') {
  86. $imgs = json_decode($request['imgs'], true);
  87. $imgCount = count($imgs);
  88. if ($imgCount == 0 || $imgCount > 9) {
  89. return jsonError('所传图集必须1-9个');
  90. }
  91. }
  92. $allImg = array_merge($imgs, [$request['img']]);
  93. foreach ($allImg as &$img) {
  94. $img = $img . '&x-oss-process=image/resize,p_50/quality,Q_50';
  95. }
  96. $detectionImageResult = $this->detectionService->checkImg($allImg);
  97. if ($detectionImageResult['code'] < 0) {
  98. Log::debug('图片违规,请修正哦' . json_encode($detectionImageResult));
  99. return jsonError('图片违规,请修正哦');
  100. }
  101. $videoUrl = "";
  102. $videoId = "";
  103. if (isset($request['video']) && $request['video']) {
  104. $videoId = $request['video'];
  105. for ($i = 0; $i < 3; $i++) {
  106. $videoUrl = $this->aliYunVodService->getPlayUrlByVideoId($request['video']);
  107. Log::debug('video-url:' . $videoUrl);
  108. if ($videoUrl) {
  109. break;
  110. }
  111. }
  112. if (empty($videoUrl)) {
  113. return jsonError('发布失败,请重试');
  114. }
  115. }
  116. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  117. $score = $fresh / 43200;
  118. $data = [
  119. 'uid' => $userInfo['uid'],
  120. 'username' => $userInfo['username'],
  121. 'mobile' => $userInfo['mobile'],
  122. 'avatar' => $userInfo['avatar'] ?? '',
  123. 'type' => $request['type'],
  124. 'img' => $request['img'],
  125. 'video' => $videoUrl,
  126. 'video_id' => $videoId,
  127. 'topic_ids' => implode(',', $topicIds),
  128. 'title' => isset($request['title']) ? $request['title'] : '',
  129. 'content' => $request['content'],
  130. 'location' => isset($request['location']) ? $request['location'] : '',
  131. 'is_suggest' => 0,
  132. 'is_hide' => 0,
  133. 'weight' => $score
  134. ];
  135. $date = date('Y-m-d H:i:s');
  136. DB::beginTransaction();
  137. try {
  138. $post = $this->post->create($data);
  139. $postData = $this->postData->create([
  140. 'post_id' => $post->id,
  141. 'pv' => 0,
  142. 'pv_real' => 0,
  143. 'dislike_count' => 0,
  144. 'praise_count' => 0,
  145. 'praise_real_count' => 0,
  146. 'share_count' => 0,
  147. 'share_real_count' => 0,
  148. 'comment_count' => 0,
  149. 'collect_count' => 0,
  150. 'collect_real_count' => 0,
  151. 'available_bean' => $this->availableBean(),
  152. 'will_collect_bean' => rand(100, 200),
  153. 'collect_bean' => 0
  154. ]);
  155. if ($imgs) {
  156. $imgData = [];
  157. foreach ($imgs as $img) {
  158. $imgData[] = [
  159. 'post_id' => $post->id,
  160. 'img' => $img,
  161. 'created_at' => $date,
  162. 'updated_at' => $date
  163. ];
  164. }
  165. $this->postImgs->insert($imgData);
  166. }
  167. DB::commit();
  168. Redis::zadd('post_trigger_type', $isValid, $post->id);
  169. foreach ($topicIds as $id) {
  170. Redis::zincrby('topic.user_uid' . $userInfo['uid'], 1, $id);
  171. Redis::zincrby('topic.just_use_count', 1, $id);
  172. }
  173. Redis::HSET('post_info_' . $post->id,
  174. 'id', $post->id,
  175. 'uid', $post->uid,
  176. 'type', $post->type,
  177. 'img', $post->img,
  178. 'imgs', json_encode($imgs),
  179. 'video', $post->video,
  180. 'topic_ids', $post->topic_ids,
  181. 'is_fine', $post->is_fine,
  182. 'title', $post->title,
  183. 'content', $post->content,
  184. 'location', $post->location,
  185. 'pv', $postData->pv,
  186. 'dislike_count', $postData->dislike_count,
  187. 'praise_count', $postData->praise_count,
  188. 'share_count', $postData->share_count,
  189. 'comment_count', $postData->comment_count,
  190. 'collect_count', $postData->collect_count,
  191. 'available_bean', $postData->available_bean,
  192. 'will_collect_bean', $postData->will_collect_bean,
  193. 'create_bean', $postData->create_bean,
  194. 'collect_bean', $postData->collect_bean,
  195. 'created_at', $post->created_at);
  196. Log::info('post_create:' . $post->id . ',post_author:' . $post->uid . ',author_ip:' . getClientIp());
  197. return jsonSuccess([
  198. 'post_id' => $post->id,
  199. 'h5url' => config('customer.share_post_h5url') . "?post_id={$post->id}&invite_code={$userInfo['invite_code']}",
  200. 'bean' => $postData->available_bean,
  201. ]);
  202. } catch (QueryException $exception) {
  203. DB::rollBack();
  204. Log::debug('发布内容失败:' . $exception->getMessage());
  205. return jsonError('发布内容失败,请重试');
  206. }
  207. }
  208. }