HomepageService.php 6.5 KB

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