NoticeService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace App\Services\Deed;
  3. use App\Exceptions\ApiException;
  4. use App\Jobs\NoticeJob;
  5. use App\Models\Common\NoticeManageModel;
  6. use App\Models\Deed\InvitationCardModel;
  7. use App\Models\Deed\NoticeReadModel;
  8. use App\Models\Log\FeedLogModel;
  9. use App\Models\NoticeModel;
  10. use App\Models\PraiseModel;
  11. use App\Models\User\UserModel;
  12. use App\Services\Log\NoticeLogService;
  13. use App\Services\Notice\ManageService;
  14. use App\Services\Service;
  15. use Illuminate\Support\Facades\Redis;
  16. use PocketBE\MsyPush\Jobs\RegistEventJob;
  17. class NoticeService extends Service
  18. {
  19. /**
  20. * 重置未读次数
  21. * @param $uid
  22. */
  23. public function readNotice($uid)
  24. {
  25. NoticeReadModel::updateOrCreate(
  26. ['uid' => $uid],
  27. ['unread_cnt' => 0]
  28. );
  29. }
  30. /**
  31. * 订阅开关
  32. * @param int $uid
  33. * @return float|int
  34. */
  35. public function subscribe(int $uid)
  36. {
  37. $profile = NoticeManageModel::firstOrCreate([
  38. 'uid' => $uid,
  39. 'key' => 'flipped_notice',
  40. ], [
  41. 'group' => 'flipped',
  42. 'isopen' => 0,
  43. ]);
  44. $profile->isopen = abs($profile->isopen - 1);
  45. $profile->save();
  46. return $profile->isopen;
  47. }
  48. /**
  49. * 心动邀请通知延迟
  50. */
  51. public function invitationDelay()
  52. {
  53. $json = Redis::lpop("{fpdx:invitation:notice:dely}");
  54. while (!empty($json)) {
  55. try {
  56. $data = json_decode($json, true);
  57. $this->invitation($data['uid'], $data['invite_uid'], $data['invite_id'], $data['question_type'], true);
  58. $json = Redis::lpop("{fpdx:invitation:notice:dely}");
  59. sleep(rand(10, 300));
  60. } catch (\Exception $exception) {
  61. }
  62. }
  63. }
  64. /**
  65. * 发送申请通知
  66. * @param int $uid 触发用户
  67. * @param int $invite_uid 通知用户
  68. * @param int $invite_id 邀请id
  69. * @param int $question_type
  70. * @param bool $is_callback 是否作为回调函数
  71. * @return bool
  72. * @throws ApiException
  73. * @throws \App\Exceptions\AlertException
  74. */
  75. public function invitation($uid, $invite_uid, $invite_id, $question_type, $is_callback = true)
  76. {
  77. $title = "心动邀请通知";
  78. $uuid = uuid();
  79. $noticeLog = new NoticeLogService();
  80. $to_uid = $invite_uid;
  81. // 系统通知 type=13
  82. NoticeModel::create([
  83. 'uid' => $to_uid,
  84. 'type' => 13,
  85. 'title' => "收到新的好友申请",
  86. 'content' => "收到1条新的好友申请",
  87. ]);
  88. // 通知开关
  89. $nms = new ManageService();
  90. if (!$nms->getNoticeByKey($to_uid, "flipped_notice")) {
  91. $noticeLog->record($to_uid, $title, "取消订阅", $uuid);
  92. return false;
  93. }
  94. if (time() < mktime(8, 0, 0) || time() > mktime(23, 0, 0)) {
  95. // 延迟发送
  96. Redis::rpush("{fpdx:invitation:notice:dely}", [
  97. json_encode([
  98. 'uid' => $uid,
  99. 'invite_uid' => $invite_uid,
  100. 'invite_id' => $invite_id,
  101. 'question_type' => $question_type,
  102. ]),
  103. ]);
  104. return true;
  105. }
  106. /** @var UserModel $user */
  107. $user = UserModel::findOrFail($uid);
  108. /** @var UserModel $to_user */
  109. $to_user = UserModel::findOrFail($to_uid);
  110. // 三天内访问次数
  111. $visitor_count = FeedLogModel::where([
  112. ['partner_id', $to_user->partner_id],
  113. ['created_at', '>', strtotime("-3 day")],
  114. ])->count();
  115. // 三天内的点赞次数
  116. $praise_count = PraiseModel::where([
  117. ['partner_id', $to_user->partner_id],
  118. ['created_at', '>', strtotime("-3 day")],
  119. ])->count();
  120. $invite_count = InvitationCardModel::where([
  121. ['invite_uid', $to_user->uid],
  122. ['created_at', '>', strtotime("-3 day")],
  123. ])->count();
  124. $description = "在过去的3天里";
  125. $description .= $visitor_count ? "共有{$visitor_count}个访客访问了你" : "";
  126. $description .= $praise_count ? ",收到{$praise_count}个「心动」" : "";
  127. $description .= $invite_count ? ",收到{$invite_count}个「好友申请」" : "";
  128. $description .= "。";
  129. // 通知队列
  130. $payload = [
  131. 'to_user' => $to_user->getAuth(),
  132. 'user' => $to_user->toArray(),
  133. 'from_user' => $user->toArray(),
  134. 'meta' => [
  135. 'visitor_count' => $visitor_count,
  136. 'praise_count' => $praise_count,
  137. 'invite_count' => $invite_count,
  138. 'description' => $description,
  139. 'date' => date('Y-m-d'),
  140. 'datetime' => date('Y-m-d H:i:s'),
  141. ],
  142. ];
  143. if ($is_callback) {
  144. // QQ通知事件
  145. dispatch(new RegistEventJob(100013, $payload))->onQueue("{push}");
  146. NoticeJob::dispatch(
  147. $to_uid,
  148. array(new self(), __FUNCTION__),
  149. array($uid, $to_uid, $invite_id, $question_type)
  150. );
  151. return true;
  152. }
  153. // 通知事件
  154. dispatch(new RegistEventJob(100002, $payload))->onQueue("{push}");
  155. return true;
  156. }
  157. /**
  158. * 好友申请成功提醒
  159. * @param int $to_uid 邀请者|发送者
  160. * @param int $be_uid 被邀请者
  161. * @return bool
  162. */
  163. public function invitationSuccess($to_uid, $be_uid)
  164. {
  165. // 通知队列
  166. $to_user = UserModel::find($to_uid);
  167. $be_user = UserModel::find($be_uid);
  168. $payload = [
  169. 'to_user' => $to_user->getAuth(),
  170. 'user' => $to_user->toArray(),
  171. 'be_user' => $be_user->toArray(),
  172. 'meta' => [
  173. 'date' => date('Y-m-d'),
  174. ],
  175. ];
  176. dispatch(new RegistEventJob(100003, $payload))->onQueue("{push}");
  177. }
  178. /**
  179. * 相互心动通知
  180. * @param int $to_uid
  181. * @param int $be_uid
  182. * @return bool
  183. */
  184. public function eachLike(int $to_uid, int $be_uid)
  185. {
  186. /** @var UserModel $user */
  187. $user = UserModel::findOrFail($to_uid);
  188. $be_user = UserModel::findOrFail($be_uid);
  189. // 通知队列
  190. $payload = [
  191. 'to_user' => $user->getAuth(),
  192. 'user' => $user->toArray(),
  193. 'be_user' => $be_user->toArray(),
  194. 'meta' => [
  195. 'date' => date('Y-m-d'),
  196. ],
  197. ];
  198. dispatch(new RegistEventJob(100004, $payload))->onQueue("{push}");
  199. }
  200. }