HomepageService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Http\Services\V1_1;
  3. use App\Generated\Exceptions\DailyFeedLimitException;
  4. use App\Generated\V1_1\Messages\Homepage\ActivityTimeTableMessage;
  5. use App\Http\Controllers\Core\Auth;
  6. use App\Jobs\FeedCleanPartnerJob;
  7. use App\Jobs\FeedMockPushJob;
  8. use App\Models\ArticleLookModel;
  9. use App\Models\ArticleModel;
  10. use App\Models\Fpdx\ActivityModel;
  11. use App\Models\User\OpenidUserModel;
  12. use App\Models\User\UserModel;
  13. use App\Models\Fpdx\PairModel;
  14. use App\Services\Home\FeedService;
  15. use App\Generated\V1_1\Messages\Homepage\OpenidFeedMessage;
  16. use App\Generated\V1_1\Messages\Homepage\FeedMessage;
  17. use App\Generated\V1_1\Messages\Homepage\FeedScoreMessage;
  18. class HomepageService
  19. {
  20. protected $feedService;
  21. public function __construct(FeedService $feedService)
  22. {
  23. $this->feedService = $feedService;
  24. }
  25. public function activityTimeTable(ActivityTimeTableMessage $message)
  26. {
  27. $uid = Auth::auth();
  28. if (PairModel::where(['uid' => $uid, 'activity_type' => 'qbj', 'stage_id' => 78])->first()) {
  29. $qbj_pair = ActivityModel::where('qbj_stage_id', '>', 0)->OrderBy(
  30. 'qbj_stage_id',
  31. 'asc'
  32. )->value('signend_time');
  33. } else {
  34. $qbj_pair = ActivityModel::where('qbj_stage_id', '>', 0)->where(
  35. 'signend_time',
  36. '>',
  37. time()
  38. )->OrderBy('qbj_stage_id', 'asc')->value('signend_time');
  39. }
  40. $wx_game = \App\Models\Gteam\ActivityModel::where('match_end_at', '>', time())->where(
  41. 'type',
  42. 'wz_wx'
  43. )->OrderBy('id', 'asc')->value('match_end_at');
  44. $cj_game = \App\Models\Gteam\ActivityModel::where('match_end_at', '>', time())->where(
  45. 'type',
  46. 'cj_wx'
  47. )->OrderBy('id', 'asc')->value('match_end_at');
  48. $goodnight = \App\Models\Goodnight\ActivityModel::where('closed_at', '>', time())->OrderBy(
  49. 'id',
  50. 'asc'
  51. )->value('closed_at');
  52. $looked_at = ArticleLookModel::where('uid', $uid)->value('looked_at') ?: 0;
  53. $unlook_article_count = ArticleModel::whereBetween("showed_at", [$looked_at, time()])->count();
  54. $pair = [
  55. 'sign_end_time' => ActivityModel::where('signend_time', '>', time())->OrderBy(
  56. 'stage_id',
  57. 'asc'
  58. )->value('signend_time'),
  59. 'is_enter' => PairModel::where('uid', $uid)->value('id') ? true : false,
  60. ];
  61. $message->setResponse([
  62. 'pair' => $pair,
  63. 'qbj_pair' => $qbj_pair,
  64. 'bqj_pair' => $qbj_pair,
  65. 'wx_game' => $wx_game,
  66. 'cj_game' => $cj_game,
  67. 'goodnight' => $goodnight,
  68. 'unlook_article_count' => $unlook_article_count
  69. ]);
  70. }
  71. public function openidFeed(OpenidFeedMessage $message)
  72. {
  73. $openid = $message->getOpenid();
  74. $take = $message->getTake() ?? 5;
  75. /** @var OpenidUserModel $openuser */
  76. $openuser = OpenidUserModel::where('openid', $openid)->firstOrFail();
  77. // 滑动的性别
  78. if (in_array($openuser->feed_sex, [1, 2])) {
  79. $sxo = $openuser->feed_sex;
  80. } else {
  81. $sxo = 2;
  82. }
  83. $data = $openuser->toArray();
  84. $data['uid'] = $openuser->openid;
  85. $data['partner_id'] = 0;
  86. $data['feed_level'] = 'A';
  87. $data['only_last'] = 0;
  88. $res = $this->feedService->feed($data, $sxo, $take);
  89. $message->setResponse($res);
  90. }
  91. /**
  92. * @param FeedMessage $message
  93. * @throws DailyFeedLimitException
  94. * @throws \App\Exceptions\AlertException
  95. */
  96. public function feed(FeedMessage $message)
  97. {
  98. $take = $message->getTake() ?? 5;
  99. $uid = Auth::auth();
  100. // 流量控制
  101. if ("production" == config('app.env') && !$this->feedService->limit($uid)) {
  102. throw new DailyFeedLimitException('每日限流200张');
  103. }
  104. /** @var UserModel $user */
  105. $user = UserModel::findOrFail($uid);
  106. // 滑动的性别
  107. if (in_array($user->feed_sex, [1, 2])) {
  108. $sxo = $user->feed_sex;
  109. } else {
  110. $sxo = 2;
  111. }
  112. $userArr = $user->toArray();
  113. // 定义用户分级
  114. if ($user->feed_cnt < 30) {
  115. $userArr['feed_level'] = 'A';
  116. } elseif ($user->feed_cnt >= 500) {
  117. $userArr['feed_level'] = 'E';
  118. if (1 == $user->sex) {
  119. if (floatval($user->feed_like / $user->feed_cnt) < 0.1) {
  120. $userArr['feed_level'] = 'E';
  121. } elseif (floatval($user->feed_like / $user->feed_cnt) > 0.8) {
  122. $userArr['feed_level'] = 'G';
  123. } else {
  124. $userArr['feed_level'] = 'F';
  125. }
  126. } else {
  127. if (floatval($user->feed_like / $user->feed_cnt) < 0.05) {
  128. $userArr['feed_level'] = 'E';
  129. } elseif (floatval($user->feed_like / $user->feed_cnt) > 0.3) {
  130. $userArr['feed_level'] = 'G';
  131. } else {
  132. $userArr['feed_level'] = 'F';
  133. }
  134. }
  135. } else {
  136. if (1 == $user->sex) {
  137. if (floatval($user->feed_like / $user->feed_cnt) < 0.1) {
  138. $userArr['feed_level'] = 'B';
  139. } elseif (floatval($user->feed_like / $user->feed_cnt) > 0.8) {
  140. $userArr['feed_level'] = 'D';
  141. } else {
  142. $userArr['feed_level'] = 'C';
  143. }
  144. } else {
  145. if (floatval($user->feed_like / $user->feed_cnt) < 0.05) {
  146. $userArr['feed_level'] = 'B';
  147. } elseif (floatval($user->feed_like / $user->feed_cnt) > 0.3) {
  148. $userArr['feed_level'] = 'D';
  149. } else {
  150. $userArr['feed_level'] = 'C';
  151. }
  152. }
  153. }
  154. $res = $this->feedService->feed($userArr, $sxo, $take);
  155. // 清理我看过的缓存
  156. 1 == rand(0, 15) && FeedCleanPartnerJob::dispatch($uid);
  157. $message->setResponse($res);
  158. }
  159. public function feedScore(FeedScoreMessage $message)
  160. {
  161. $uid = Auth::auth();
  162. $partnerId = $message->getPartnerId();
  163. $fs = new FeedService();
  164. list($location, $dis, $score, $info) = $fs->getScoreUid2Partner($uid, $partnerId);
  165. $message->setResponse([
  166. 'location' => [
  167. 'location' => $location,
  168. 'dis' => $dis
  169. ],
  170. 'score' => $score,
  171. 'info' => $info
  172. ]);
  173. }
  174. }