WxCommentRepositores.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace App\Http\Controllers\Api\Repositories;
  3. use App\Jobs\AttachIpAddressJob;
  4. use App\Jobs\Circle\UpdateCircleCountJob;
  5. use App\Models\Posts\WxPostsTag;
  6. use App\Models\Circle\WxCircle;
  7. use App\Wen\Utils\CommentUtils;
  8. use App\Wen\Utils\FieldUtils;
  9. use App\Wen\Utils\PostUtils;
  10. use App\Wen\Utils\Settings;
  11. use App\Wen\Utils\UserUtils;
  12. use App\Wen\Utils\Utils;
  13. use App\Models\Posts\WxComment;
  14. use App\Models\Posts\WxPost;
  15. use App\Models\WxCommentLike;
  16. use App\Models\WxSyncRelation;
  17. use App\Models\User\WxUser;
  18. use Illuminate\Support\Facades\Cache;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Redis;
  21. class WxCommentRepositores
  22. {
  23. /**
  24. * @param $posts_id 文章id
  25. * @param $comment_id 评论id
  26. * @param $user_id 评论的用户id
  27. * @param $comment_content 评论内容
  28. */
  29. public static function add($posts_id, $comment_id, $reply_user_id, $user_id, $comment_content, $comment_img_url, $comment_state, $device = '', $is_anonymous = 0)
  30. {
  31. DB::beginTransaction();
  32. try{
  33. $posts = WxPost::where('id', $posts_id)->first();
  34. $user = UserUtils::get_cached_user($user_id);
  35. $agentUser = null;
  36. if(_abs($reply_user_id) > 0){
  37. if($reply_user_id > 0){
  38. $agentUser = WxUser::where('id', $reply_user_id)->first();
  39. }else{
  40. $agentUser = WxUser::where('id', _abs($reply_user_id))->first();
  41. $anonymous_info = UserUtils::get_anonymous_info($agentUser);
  42. if($anonymous_info){
  43. $agentUser['id'] = _abs($reply_user_id);
  44. $agentUser['user_name'] = $anonymous_info['anonymous_name'];
  45. $agentUser['user_avatar'] = $anonymous_info['anonymous_avatar'];
  46. }
  47. }
  48. }
  49. $circle_id = $posts->circle_id;
  50. $circle_user_id = WxCircle::where('id', $circle_id)->value('user_id');
  51. global $__MINI_GLOBAL_IP__;
  52. // 0:审核中,1:审核通过,2:驳回
  53. if (!_empty_($comment_id)) {
  54. $CommentModel = new WxComment();
  55. $CommentModel->posts_id = $posts_id;
  56. $CommentModel->posts_user_id = $posts->is_anonymous == 1 ? (-1 * $posts['user_id']) : $posts['user_id'];
  57. $CommentModel->user_id = $user_id;
  58. $CommentModel->user_name = $user['user_name'];
  59. $CommentModel->user_avatar = $user['user_avatar'];
  60. if($agentUser){
  61. $CommentModel->comment_agent_id = $agentUser['id'];
  62. $CommentModel->comment_agent_name = $agentUser['user_name'];
  63. $CommentModel->comment_agent_avatar = $agentUser['user_avatar'];
  64. }
  65. $CommentModel->comment_id = $comment_id;
  66. $CommentModel->is_sticky = 0;
  67. $CommentModel->comment_img_url = $comment_img_url;
  68. $CommentModel->comment_content = warp2br(_mini_phone(_mini_emoji( _mini_aite_replace($comment_content) )));
  69. $CommentModel->comment_state = $comment_state;
  70. if($is_anonymous == 1){
  71. $CommentModel->is_anonymous = 1;
  72. }
  73. if($device){
  74. $CommentModel->device = $device;
  75. }
  76. if($__MINI_GLOBAL_IP__){
  77. $CommentModel->ip = $__MINI_GLOBAL_IP__;
  78. }
  79. $r = $CommentModel->save();
  80. $comment_id = $CommentModel->id;
  81. if ($comment_state == 1) {
  82. $comment_count = WxCommentRepositores::commentCount($posts_id) ?: 0;//评论数量
  83. $weight = WxPost::where('id', $posts_id)->value('weight');
  84. $weight += ( Utils::getDecayCoefficient($comment_count, 5) * 1000);
  85. if($weight > time()){
  86. $weight = time();
  87. }
  88. WxPost::withoutSyncingToSearch(function () use ($posts_id, $weight) {
  89. WxPost::where('id', $posts_id)->update(['last_reply_at' => current_time(), 'weight' => timetostr($weight)]);
  90. });
  91. Redis::sadd('realtime:others:set', json_encode([$reply_user_id, 4, 1]));
  92. if($reply_user_id != $user['id']){
  93. if($is_anonymous == 1){
  94. $anonymous = [
  95. 'anonymous_name' => $user['anonymous_name'] ?? '',
  96. 'anonymous_avatar' => $user['anonymous_avatar'] ?? ''
  97. ];
  98. if(_empty_($anonymous['anonymous_name'])){
  99. $the_user = WxUser::find($user['id']);
  100. $anonymous = UserUtils::get_anonymous_info($the_user);
  101. }
  102. $user['id'] = $user['id'] * -1;
  103. $user['user_name'] = $anonymous['anonymous_name'] ?: '匿名';
  104. $user['user_avatar'] = $anonymous['anonymous_avatar'] ?: Settings::get('img_default_avatar', '');
  105. }
  106. UserUtils::add_user_notice(3002, $reply_user_id, '你的评论收到了一个回复快去看看吧', '「<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>」' . '回复了您的评论。', 102, $posts_id);
  107. }
  108. _mini_aite($user_id, $comment_content, 2, false, $posts_id, $comment_id);
  109. }
  110. Cache::forget('post:commentCount:'.$posts_id);
  111. if($__MINI_GLOBAL_IP__){
  112. AttachIpAddressJob::dispatch(3, $comment_id)->delay(2);
  113. }
  114. DB::commit();
  115. return $comment_id;
  116. } else {
  117. $CommentModel = new WxComment();
  118. $CommentModel->posts_id = $posts_id;
  119. $CommentModel->posts_user_id = $posts->is_anonymous == 1 ? (-1 * $posts['user_id']) : $posts['user_id'];
  120. $CommentModel->user_id = $user_id;
  121. $CommentModel->user_name = $user['user_name'];
  122. $CommentModel->user_avatar = $user['user_avatar'];
  123. $CommentModel->comment_content = _mini_phone(_mini_emoji( _mini_aite_replace($comment_content) ));
  124. $CommentModel->comment_img_url = $comment_img_url;
  125. $CommentModel->is_sticky = 0;
  126. $CommentModel->comment_state = $comment_state;
  127. if($is_anonymous == 1){
  128. $CommentModel->is_anonymous = 1;
  129. }
  130. if($device){
  131. $CommentModel->device = $device;
  132. }
  133. if($__MINI_GLOBAL_IP__){
  134. $CommentModel->ip = $__MINI_GLOBAL_IP__;
  135. }
  136. $r = $CommentModel->save();
  137. $comment_id = $CommentModel->id;
  138. if ($comment_state == 1) {
  139. $comment_count = WxCommentRepositores::commentCount($posts_id) ?: 0;//评论数量
  140. $weight = WxPost::where('id', $posts_id)->value('weight');
  141. $weight += ( Utils::getDecayCoefficient($comment_count, 5) * 1000);
  142. if($weight > time()){
  143. $weight = time();
  144. }
  145. WxPost::withoutSyncingToSearch(function () use ($posts_id, $weight) {
  146. WxPost::where('id', $posts_id)->update(['last_reply_at' => current_time(), 'weight' => timetostr($weight)]);
  147. });
  148. if($posts['user_id'] != $user['id']){
  149. if($is_anonymous == 1){
  150. $anonymous = [
  151. 'anonymous_name' => $user['anonymous_name'] ?? '',
  152. 'anonymous_avatar' => $user['anonymous_avatar'] ?? ''
  153. ];
  154. if(_empty_($anonymous['anonymous_name'])){
  155. $the_user = WxUser::find($user['id']);
  156. $anonymous = UserUtils::get_anonymous_info($the_user);
  157. }
  158. $user['id'] = $user['id'] * -1;
  159. $user['user_name'] = $anonymous['anonymous_name'] ?: '匿名';
  160. $user['user_avatar'] = $anonymous['anonymous_avatar'] ?: Settings::get('img_default_avatar', '');
  161. }
  162. UserUtils::add_user_notice(2005, $posts['user_id'], '收到了一个评论快去看看吧', '「<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>」' . '评论了您的笔记。', 102, $posts_id);
  163. }
  164. _mini_aite($user_id, $comment_content, 2, false, $posts_id, $comment_id);
  165. }
  166. }
  167. Redis::sadd('realtime:others:set', json_encode([$circle_id, 2, 5]));
  168. Redis::sadd('realtime:others:set', json_encode([$posts['user_id'], 4, 5]));
  169. $tagsIds = WxPostsTag::where('posts_id', $posts_id)->pluck('tags_id');//话题ID列表
  170. if($tagsIds){
  171. foreach ($tagsIds as $tag_id){
  172. Redis::sadd('realtime:others:set', json_encode([$tag_id, 5, 5]));
  173. }
  174. }
  175. if($circle_id > 0){
  176. UpdateCircleCountJob::dispatch($circle_id);
  177. }
  178. if($circle_user_id){
  179. Redis::sadd('realtime:others:set', json_encode([$circle_user_id, 4, 5]));
  180. }
  181. Redis::sadd('realtime:post:set', $posts_id);
  182. Cache::forget('post:commentCount:'.$posts_id);
  183. if($__MINI_GLOBAL_IP__){
  184. AttachIpAddressJob::dispatch(3, $comment_id)->delay(2);
  185. }
  186. DB::commit();
  187. return $comment_id;
  188. } catch (\Exception $e) {
  189. DB::rollBack();
  190. _logger_(__file__, __line__, $e->getLine().' '.$e->getMessage());
  191. return 0;
  192. }
  193. }
  194. /**
  195. * @param $posts_id 文章id
  196. * @param $comment_id 评论id
  197. * @param $user_id 评论的用户id
  198. * @param $comment_content 评论内容
  199. */
  200. public static function admin_add($domain, $posts_id, $comment_id, $reply_user_id, $user_id, $comment_content, $comment_img_url, $external_id = '', $date = '')
  201. {
  202. DB::beginTransaction();
  203. try{
  204. if($domain && $external_id){
  205. $inner_id = WxSyncRelationRepositories::wx_comment_id($domain, $external_id);
  206. if($inner_id){
  207. return true;
  208. }
  209. }
  210. $posts = WxPost::where('id', $posts_id)->first();
  211. $user = UserUtils::get_cached_user($user_id);
  212. $agentUser = WxUser::where('id', $reply_user_id)->first();
  213. if (!_empty_($comment_id)) {
  214. $CommentModel = new WxComment();
  215. $CommentModel->posts_id = $posts_id;
  216. $CommentModel->posts_user_id = $posts['user_id'];
  217. $CommentModel->user_id = $user_id;
  218. $CommentModel->user_name = $user['user_name'];
  219. $CommentModel->user_avatar = $user['user_avatar'];
  220. $CommentModel->comment_agent_id = $agentUser['id'];
  221. $CommentModel->comment_agent_name = $agentUser['user_name'];
  222. $CommentModel->comment_agent_avatar = $agentUser['user_avatar'];
  223. $CommentModel->comment_id = $comment_id;
  224. $CommentModel->is_sticky = 0;
  225. $CommentModel->comment_img_url = $comment_img_url;
  226. $CommentModel->comment_content = _mini_phone(_mini_emoji( _mini_aite_replace($comment_content) ));
  227. $CommentModel->comment_state = 1;
  228. $CommentModel->device = ['app', 'mp'][(int)mini_rand(0,1)];
  229. if($date){
  230. $CommentModel->updated_at = strtotime($date);
  231. $CommentModel->created_at = strtotime($date);
  232. }
  233. $CommentModel->save();
  234. $commentId = $CommentModel->id;
  235. if($domain && $external_id){
  236. $sync = new WxSyncRelation();
  237. $sync->domain = $domain;
  238. $sync->type = 'comment';
  239. $sync->inner_id = $commentId;
  240. $sync->external_id = $external_id;
  241. $sync->status = 1;
  242. $sync->save();
  243. }
  244. if($reply_user_id != $user['id']) {
  245. UserUtils::add_user_notice(3002, $reply_user_id, '你的评论收到了一个回复快去看看吧', '「<a href="/pages/user/user?id=' . $user['id'] . '">' . $user['user_name'] . '</a>」' . '回复了您的评论。', 102, $posts_id);
  246. }
  247. _mini_aite($user_id, $comment_content, 2, false, $posts_id, $comment_id);
  248. } else {
  249. $CommentModel = new WxComment();
  250. $CommentModel->posts_id = $posts_id;
  251. $CommentModel->posts_user_id = $posts['user_id'];
  252. $CommentModel->user_id = $user_id;
  253. $CommentModel->user_name = $user['user_name'];
  254. $CommentModel->user_avatar = $user['user_avatar'];
  255. $CommentModel->comment_content = _mini_phone(_mini_emoji( _mini_aite_replace($comment_content) ));
  256. $CommentModel->comment_img_url = $comment_img_url;
  257. $CommentModel->is_sticky = 0;
  258. // 判断是否需要审核评论
  259. $CommentModel->comment_state = 1;
  260. if($date){
  261. $CommentModel->updated_at = strtotime($date);
  262. $CommentModel->created_at = strtotime($date);
  263. }
  264. $CommentModel->save();
  265. $commentId = $CommentModel->id;
  266. if($domain && $external_id){
  267. $sync = new WxSyncRelation();
  268. $sync->domain = $domain;
  269. $sync->type = 'comment';
  270. $sync->inner_id = $commentId;
  271. $sync->external_id = $external_id;
  272. $sync->status = 1;
  273. $sync->save();
  274. }
  275. if($posts['user_id'] != $user['id']) {
  276. UserUtils::add_user_notice(2005, $posts['user_id'], '收到了一个评论快去看看吧', '「<a href="/pages/user/user?id=' . $user['id'] . '">' . $user['user_name'] . '</a>」' . '评论了您的笔记。', 102, $posts_id);
  277. }
  278. _mini_aite($user_id, $comment_content, 2, false, $posts_id, $comment_id);
  279. }
  280. Redis::sadd('realtime:post:set', $posts_id);
  281. update_user_visit($user_id, 1, $posts_id);
  282. DB::commit();
  283. return $commentId;
  284. } catch (\Exception $e) {
  285. DB::rollBack();
  286. _logger_(__file__, __line__, $e->getMessage());
  287. return 0;
  288. }
  289. }
  290. /**
  291. * 通过笔记ID获取评论列表
  292. * @param $posts_id
  293. * @param $limit
  294. * @return mixed
  295. */
  296. public static function getCommentByPostsId($posts_id, $uid = 0, $limit = 10)
  297. {
  298. $data = WxComment::where('comment_state', 1)->where('posts_id', $posts_id)
  299. ->where('comment_id', null)->orderBy('is_sticky', 'desc')->orderBy('created_at', 'desc')
  300. ->simplePaginate($limit, FieldUtils::commentInfoColums());
  301. $post_user_id = WxPost::where('id', $posts_id)->value('user_id');
  302. $data->map(function ($v, $k) use ($uid, $posts_id, $post_user_id) {
  303. return CommentUtils::comment_process($k, $v, $posts_id, $post_user_id, $uid, 1, 0);
  304. });
  305. $data->append(['format_time', 'ip_address']);
  306. if($uid > 0){
  307. update_user_visit($uid, 1, $posts_id);
  308. }
  309. return $data;
  310. }
  311. /**
  312. * 获取评论数量
  313. */
  314. public static function commentCount($postId)
  315. {
  316. return (int)(Cache::remember('post:commentCount:'.$postId, 36000, function () use ($postId){
  317. return WxComment::where([['posts_id', '=', $postId], ['comment_state', '=', 1]])->count();
  318. }));
  319. }
  320. }