HomeController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace App\Http\Controllers\Home;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Controllers\Core\Auth;
  6. use App\Http\Resources\Home\ArticleCollection;
  7. use App\Jobs\FeedCleanPartnerJob;
  8. use App\Jobs\FeedMockPushJob;
  9. use App\Jobs\HomeHdLogJob;
  10. use App\Models\ArticleLookModel;
  11. use App\Models\ArticleModel;
  12. use App\Models\Fpdx\ActivityModel;
  13. use App\Models\PraiseModel;
  14. use App\Models\User\UserModel;
  15. use App\Models\Fpdx\PairModel;
  16. use App\Models\Gnight\UserModel as GnightUserModel;
  17. use App\Models\Goodnight\UserModel as GoodnightUserModel;
  18. use App\Models\Log\FeedLogModel;
  19. use App\Services\Home\FeedService;
  20. use Exception;
  21. use Illuminate\Contracts\Routing\ResponseFactory;
  22. use Illuminate\Http\Request;
  23. use Illuminate\Support\Facades\Config;
  24. use Illuminate\Support\Facades\DB;
  25. use Illuminate\Support\Facades\Redis;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Tymon\JWTAuth\Exceptions\JWTException;
  28. /**
  29. * 信息流API
  30. * Class HomeController
  31. * @package App\Http\Controllers\Home
  32. * @deprecated
  33. */
  34. class HomeController extends Controller
  35. {
  36. /**
  37. * 首页feed
  38. * @param FeedService $fs
  39. * @return array
  40. * @throws AlertException
  41. * @deprecated Homepage/Feed
  42. */
  43. public function feed(FeedService $fs)
  44. {
  45. $uid = Auth::auth();
  46. // 流量控制
  47. if (!$fs->limit($uid)) {
  48. throw new AlertException("每日限流200张", 202);
  49. }
  50. /** @var UserModel $user */
  51. $user = UserModel::findOrFail($uid);
  52. // 滑动的性别
  53. if (in_array($user->feed_sex, [1, 2])) {
  54. $sxo = $user->feed_sex;
  55. } else {
  56. $sxo = 2;
  57. }
  58. $userArr = $user->toArray();
  59. // 定义用户分级
  60. if ($user->feed_cnt < 30) {
  61. $userArr['feed_level'] = 'A';
  62. } elseif ($user->feed_cnt >= 500) {
  63. $userArr['feed_level'] = 'E';
  64. } else {
  65. if (1 == $user->sex) {
  66. if (floatval($user->feed_like / $user->feed_cnt) < 0.1) {
  67. $userArr['feed_level'] = 'B';
  68. } elseif (floatval($user->feed_like / $user->feed_cnt) > 0.8) {
  69. $userArr['feed_level'] = 'D';
  70. } else {
  71. $userArr['feed_level'] = 'C';
  72. }
  73. } else {
  74. if (floatval($user->feed_like / $user->feed_cnt) < 0.05) {
  75. $userArr['feed_level'] = 'B';
  76. } elseif (floatval($user->feed_like / $user->feed_cnt) > 0.3) {
  77. $userArr['feed_level'] = 'D';
  78. } else {
  79. $userArr['feed_level'] = 'C';
  80. }
  81. }
  82. }
  83. $res = $fs->feed($userArr, $sxo);
  84. return array(
  85. 'code' => 200,
  86. 'message' => 'success',
  87. 'data' => $res
  88. );
  89. }
  90. /**
  91. * 获取匹配分
  92. * @param int $partner_id
  93. * @return array
  94. * @throws JWTException
  95. */
  96. public function score(int $partner_id)
  97. {
  98. $uid = Auth::auth();
  99. $fs = new FeedService();
  100. list($location, $dis, $score, $info) = $fs->getScoreUid2Partner($uid, $partner_id);
  101. return array(
  102. 'code' => 200,
  103. 'message' => 'success',
  104. 'data' => [
  105. 'location' => [
  106. 'location' => $location,
  107. 'dis' => $dis
  108. ],
  109. 'score' => $score,
  110. 'info' => $info
  111. ],
  112. );
  113. }
  114. /**
  115. * 提醒TA补全信息
  116. * @param Request $request
  117. * @return array
  118. * @throws JWTException
  119. */
  120. public function remindType4(Request $request)
  121. {
  122. $this->validate($request, [
  123. 'remind_uid' => 'required'
  124. ]);
  125. $remind_uid = $request->get("remind_uid");
  126. $type = $request->get('type', 1) ?: 1;
  127. /** @var integer $uid */
  128. $uid = Auth::auth();
  129. $fs = new FeedService();
  130. $fs->remindType4($uid, $remind_uid, $type);
  131. return array(
  132. 'code' => 200,
  133. 'message' => 'success'
  134. );
  135. }
  136. /**
  137. * 获取点赞队列存量
  138. */
  139. public function getHasThumb()
  140. {
  141. $uid = Auth::auth();
  142. $fs = new FeedService();
  143. $cnt = $fs->getHasThumb($uid);
  144. return array(
  145. 'code' => 200,
  146. 'message' => 'success',
  147. 'data' => $cnt
  148. );
  149. }
  150. /**
  151. * 游乐场模块人数
  152. */
  153. public function modelCnt()
  154. {
  155. $data = Redis::get('app:model:cnt');
  156. if (empty($data)) {
  157. $pair = PairModel::count(DB::raw('DISTINCT(`uid`)')) * 5;
  158. $goodnight = GoodnightUserModel::count() * 5;
  159. $voice = GnightUserModel::count() * 5;
  160. $data = [
  161. 'pair' => $pair,
  162. 'voice' => $voice,
  163. 'goodnight' => $goodnight
  164. ];
  165. } else {
  166. $data = json_decode($data, true);
  167. }
  168. return array(
  169. 'code' => 200,
  170. 'message' => 'success',
  171. 'data' => $data
  172. );
  173. }
  174. /**
  175. * 记录每日滑动数量
  176. * @param Request $request
  177. * @param int $partner_id
  178. * @return array
  179. */
  180. public function hdCnt(Request $request, int $partner_id)
  181. {
  182. try {
  183. $uid = Auth::auth();
  184. } catch (JWTException $exception) {
  185. $uid = $request['uid'];
  186. }
  187. Redis::hincrby("session_msy_{$uid}", "hd_cnt", 1);
  188. if ($partner_id > 0) {
  189. $post = $request->post();
  190. if (!empty($post)) {
  191. $detail = $post['type_detail'];
  192. $data = [
  193. 'created_at' => time(),
  194. 'created_date' => date('Y-m-d'),
  195. 'uid' => $uid,
  196. 'partner_id' => $partner_id,
  197. 'type' => $post['type'],
  198. 'attach' => isset($post['attach']) ? $post['attach'] : null,
  199. 'photo_num' => 0,
  200. 'voice' => 0,
  201. 'remind' => 0,
  202. 'retention_time' => is_null($post['retention_time']) ? 0 : $post['retention_time'],
  203. 'like' => $post['like'],
  204. 'each_like' => isset($post['each_like']) ? $post['each_like'] : 0,
  205. 'page' => isset($post['page']) ? $post['page'] : "",
  206. 'app' => Config::get("platform", 'wx')
  207. ];
  208. if (in_array($data['type'], [1, 2])) {
  209. $data['photo_num'] = $detail['photo_num'];
  210. $data['voice'] = $detail['voice'];
  211. } else {
  212. if (!is_array($detail)) {
  213. $detail = json_decode($detail, true);
  214. }
  215. $data['remind'] = isset($detail['remind']) ? $detail['remind'] : 0;
  216. }
  217. HomeHdLogJob::dispatch($uid, $data);
  218. // 递交模拟拉卡片任务
  219. FeedMockPushJob::dispatch($uid);
  220. }
  221. }
  222. return array(
  223. 'code' => 200,
  224. 'message' => 'success'
  225. );
  226. }
  227. /**
  228. * 发现页
  229. * @return ResponseFactory|Response
  230. * @deprecated Homepage/ActivityTimeTable
  231. */
  232. public function activityTimetable()
  233. {
  234. try {
  235. $uid = Auth::auth();
  236. } catch (Exception $e) {
  237. $uid = 0;
  238. }
  239. if (PairModel::where(['uid' => $uid, 'activity_type' => 'qbj', 'stage_id' => 78])->first()) {
  240. $qbj_pair = ActivityModel::where('qbj_stage_id', '>', 0)->OrderBy(
  241. 'qbj_stage_id',
  242. 'asc'
  243. )->value('signend_time');
  244. } else {
  245. $qbj_pair = ActivityModel::where('qbj_stage_id', '>', 0)->where(
  246. 'signend_time',
  247. '>',
  248. time()
  249. )->OrderBy('qbj_stage_id', 'asc')->value('signend_time');
  250. }
  251. $wx_game = \App\Models\Gteam\ActivityModel::where('match_end_at', '>', time())->where(
  252. 'type',
  253. 'wz_wx'
  254. )->OrderBy('id', 'asc')->value('match_end_at');
  255. $cj_game = \App\Models\Gteam\ActivityModel::where('match_end_at', '>', time())->where(
  256. 'type',
  257. 'cj_wx'
  258. )->OrderBy('id', 'asc')->value('match_end_at');
  259. $goodnight = \App\Models\Goodnight\ActivityModel::where('closed_at', '>', time())->OrderBy(
  260. 'id',
  261. 'asc'
  262. )->value('closed_at');
  263. $looked_at = ArticleLookModel::where('uid', $uid)->value('looked_at') ?: 0;
  264. $unlook_article_count = ArticleModel::whereBetween("showed_at", [$looked_at, time()])->count();
  265. $activity = ActivityModel::OrderBy('stage_id', 'desc')->first();
  266. $enroll_number = PairModel::where([
  267. ['stage_id', $activity->stage_id],
  268. ['state', '>', 100]
  269. ])->count() + 1;
  270. $pair = [
  271. 'activity' => $activity,
  272. 'sign_end_time' => $activity->signend_time,
  273. 'is_enter' => PairModel::where('uid', $uid)->value('id') ? true : false,
  274. 'enroll_number' => $enroll_number * 6,
  275. ];
  276. return response([
  277. "code" => 200,
  278. "message" => "OK",
  279. "data" => [
  280. 'pair' => $pair,
  281. 'qbj_pair' => $qbj_pair,
  282. 'bqj_pair' => $qbj_pair,
  283. 'wx_game' => $wx_game,
  284. 'cj_game' => $cj_game,
  285. 'goodnight' => $goodnight,
  286. 'unlook_article_count' => $unlook_article_count
  287. ]
  288. ]);
  289. }
  290. /**
  291. * 文章列表
  292. * @param Request $request
  293. * @return ArticleCollection
  294. */
  295. public function articles(Request $request)
  296. {
  297. $articles = ArticleModel::where("showed_at", '<=', time())
  298. ->orderBy('showed_at', 'desc')->paginate($request->get('per_page', 10));
  299. // 阅读
  300. try {
  301. $uid = Auth::auth();
  302. ArticleLookModel::updateOrCreate(
  303. ['uid' => $uid],
  304. ['looked_at' => time()]
  305. );
  306. } catch (Exception $e) {
  307. }
  308. return new ArticleCollection($articles);
  309. }
  310. }