PopularityService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 人气值日报通知
  4. * User: easyboom
  5. * Date: 2019/4/26
  6. * Time: 下午3:08
  7. */
  8. namespace App\Services\Notice;
  9. use App\Http\Controllers\Miniprogram\Core;
  10. use App\Http\Controllers\Wechat\Kfaccount;
  11. use App\Models\Deed\InvitationCardModel;
  12. use App\Models\Log\FeedLogModel;
  13. use App\Models\PartnerModel;
  14. use App\Models\PraiseModel;
  15. use App\Models\User\Openid;
  16. use App\Models\User\PopularityDayLogModel;
  17. use App\Models\User\UserModel;
  18. use App\Services\Log\NoticeLogService;
  19. use App\Services\Service;
  20. use App\Services\User\HomeService;
  21. use Illuminate\Support\Facades\Redis;
  22. class PopularityService extends Service
  23. {
  24. // 加载人气值日报推送用户
  25. public function loadPushUsers()
  26. {
  27. $mktime = mktime(0, 0, 0);
  28. $partners = PartnerModel::where([['score', '>', 0], ['is_sell', 1]])->whereBetween(
  29. 'login_at',
  30. [$mktime - 2 * 86400, $mktime - 86400]
  31. )->get(['uid']);
  32. foreach ($partners as $partner) {
  33. Redis::rpush("{popularity:daiy:push}", $partner->uid);
  34. }
  35. Redis::set("popularity:daiy:push:total", $partners->count());
  36. Redis::expire("popularity:daiy:push", mktime(23, 30, 0) - time());
  37. Redis::expire("popularity:daiy:push:total", mktime(23, 30, 0) - time());
  38. }
  39. // 发送日报
  40. public function daiyPush()
  41. {
  42. $total = Redis::get("popularity:daiy:push:total") ?? 0;
  43. $cnt = $total / (15 * 1 * 12) + 1;
  44. dump($cnt);
  45. $hs = new HomeService();
  46. for (
  47. $i = 0; $i < $cnt; $i++
  48. ) {
  49. $to_uid = Redis::lpop("{popularity:daiy:push}");
  50. dump("{$i}:{$to_uid}");
  51. try {
  52. $user = UserModel::find($to_uid);
  53. if (!$user) {
  54. continue;
  55. }
  56. if ($user->login_at > mktime(0, 0, 0) - 86400) {
  57. continue;
  58. }
  59. // 获取今日人气值
  60. $pdata = $hs->getPopularity($user->uid);
  61. $base = $pdata['base'];
  62. // 获取昨日人气值
  63. $yestoday = date('Y-m-d', strtotime("-1 day"));
  64. $yesPopularitylog = PopularityDayLogModel::where([
  65. ['uid', $to_uid],
  66. ['date', $yestoday],
  67. ])->firstOrFail();
  68. // 排名+百分比
  69. $ranktotal = PopularityDayLogModel::where([
  70. ['location', $user->location],
  71. ['date', $yestoday],
  72. ['sex', $user->sex],
  73. ])->count();
  74. $lessrank_cnt = PopularityDayLogModel::where([
  75. ['location', $user->location],
  76. ['date', $yestoday],
  77. ['sex', $user->sex],
  78. ['popularity', '<', $yesPopularitylog->popularity],
  79. ])->count();
  80. $proportion = ceil($lessrank_cnt * 100 / $ranktotal);
  81. if ($yesPopularitylog->popularity > $base) {
  82. $uporlow = "下降";
  83. } else {
  84. $uporlow = "上升";
  85. }
  86. $disPopularity = abs($base - $yesPopularitylog->popularity);
  87. if (1 == $user->sex) {
  88. $sex = "男生";
  89. } else {
  90. $sex = "女生";
  91. }
  92. $title = "人气值日报通知";
  93. $uuid = uuid();
  94. $noticeLog = new NoticeLogService();
  95. try {
  96. $inviteme = InvitationCardModel::where('invite_uid', $user->uid)->whereBetween(
  97. 'created_at',
  98. [mktime(0, 0, 0) - 86400, mktime(0, 0, 0)]
  99. )->count();
  100. $thumbme = PraiseModel::where('partner_id', $user->partner_id)->whereBetween(
  101. 'created_at',
  102. [mktime(0, 0, 0) - 86400, mktime(0, 0, 0)]
  103. )->count();
  104. $feedme = FeedLogModel::where('partner_id', $user->partner_id)->whereBetween(
  105. 'created_at',
  106. [mktime(0, 0, 0) - 86400, mktime(0, 0, 0)]
  107. )->count();
  108. $remark = "";
  109. $feedme > 0 && $remark .= "{$feedme}位访客, ";
  110. $thumbme > 0 && $remark .= "{$thumbme}个心动, ";
  111. $inviteme > 0 && $remark .= "{$inviteme}个心动邀请, ";
  112. !empty($remark) && $remark = trim("昨日新增{$remark}", ", ") . "。";
  113. $content = "{$user->nickname}的人气值日报 {$yestoday}\n\n你今日人气值为{$base}(不含附加分),较昨日对比{$uporlow}了{$disPopularity},当前人气值打败了{$user->location}{$proportion}%的{$sex}。";
  114. $openid = Openid::ofPublic($to_uid)->value('openid');
  115. $page = "pages/starter/starter?launch_type=free&url=%2Fpopularity%2Fpages%2Fscore%2Fscore" . "&log_type=notice&log_id={$uuid}";
  116. // 客服消息
  117. $kfcontent = "{$content}{$remark}\n\n<a data-miniprogram-appid='wx4c1722a4055bd229' data-miniprogram-path='{$page}'>点此提升人气</a>";
  118. $response = Kfaccount::text([
  119. 'touser' => $openid,
  120. 'content' => $kfcontent,
  121. 'kf_account' => 'notice@admin',
  122. ]);
  123. if ($response['code'] == 0) {
  124. $noticeLog->record($to_uid, $title, "公众号客服消息", $uuid, 1, $kfcontent);
  125. sleep(2);
  126. continue;
  127. }
  128. // 模板消息
  129. $public_id = config('wechat.fpdx.public_id');
  130. $template_id = "kFkXgPPSqT_3GfLT_PFgCJYLMsqweWrU--GWt9zv-gE";
  131. $data = [
  132. 'first' => [
  133. 'value' => "{$user->nickname}的人气值日报 {$yestoday}\n",
  134. ],
  135. 'keyword1' => [
  136. 'value' => "{$user->nickname}",
  137. ],
  138. 'keyword2' => [
  139. 'value' => date('Y-m-d H:i:m') . "\n",
  140. ],
  141. 'remark' => [
  142. 'value' => "你今日人气值为{$base}(不含附加分),较昨日对比{$uporlow}了{$disPopularity},当前人气值打败了{$user->location}{$proportion}%的{$sex}。{$remark}",
  143. "color" => "#FF7E98",
  144. ],
  145. ];
  146. $core = new Core();
  147. $result = $core->template($to_uid, $template_id, $public_id, $page, $data, "miniprogram");
  148. if ($result) {
  149. $noticeLog->record($to_uid, $title, "公众号模板消息", $uuid, 1, $data);
  150. sleep(1);
  151. continue;
  152. }
  153. } catch (\Exception $exception) {
  154. app('sentry')->captureException($exception);
  155. }
  156. $noticeLog->record($to_uid, $title, "发送失败", $uuid);
  157. } catch (\Exception $exception) {
  158. continue;
  159. }
  160. }
  161. }
  162. }