MainController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\RateLimiter;
  5. use Illuminate\Support\Facades\View;
  6. use App\Models\CoupangLib;
  7. use App\Models\Visit;
  8. use App\Models\SearchKeyword;
  9. class MainController extends Controller
  10. {
  11. protected CoupangLib $coupangLib;
  12. public function __construct(Request $request)
  13. {
  14. $this->coupangLib = new CoupangLib();
  15. $visit = new Visit();
  16. $visit->register($request); // 방문자 수 기록
  17. View::share('visitorTodayCount', $visit->todayCount());
  18. View::share('visitorYesterdayCount', $visit->yesterdayCount());
  19. View::share('visitorTotalCount', $visit->totalCount());
  20. }
  21. /**
  22. * 메인페이지
  23. */
  24. public function index()
  25. {
  26. $products = $this->coupangLib->getAllCoupangPL();
  27. $banners = [
  28. [
  29. 'title' => '쿠팡',
  30. 'href' => '4fC52',
  31. 'src' => '594227?subId=&traceId=V0-301-879dd1202e5c73b2-I594227&w=728&h=90'
  32. ],
  33. [
  34. 'title' => '골드박스',
  35. 'href' => '4hyv3',
  36. 'src' => '608229?subId=&traceId=V0-301-969b06e95b87326d-I608229&w=728&h=90'
  37. ],
  38. [
  39. 'title' => '로켓와우',
  40. 'href' => '4hyGv',
  41. 'src' => '630877?subId=&traceId=V0-301-bae0f72e5e59e45f-I630877&w=728&h=90'
  42. ],
  43. [
  44. 'title' => '로켓직구',
  45. 'href' => '4hyLY',
  46. 'src' => '630871?subId=&traceId=V0-301-50c6c2b97fba9aee-I630871&w=728&h=90'
  47. ],
  48. [
  49. 'title' => '로켓 프레시',
  50. 'href' => '4hyPc',
  51. 'src' => '630888?subId=&traceId=V0-301-371ae01f4226dec2-I630888&w=728&h=90'
  52. ],
  53. [
  54. 'title' => '로켓 패션',
  55. 'href' => '4hyTb',
  56. 'src' => '630872?subId=&traceId=V0-301-5a8c79a76485eb21-I630872&w=728&h=90'
  57. ],
  58. [
  59. 'title' => '로켓 가전/디지털',
  60. 'href' => '4hyXH',
  61. 'src' => '683768?subId=&traceId=V0-301-5f9bd61900e673c0-I683768&w=728&h=90'
  62. ],
  63. [
  64. 'title' => '로켓 도서',
  65. 'href' => '4hy6s',
  66. 'src' => '683764?subId=&traceId=V0-301-f5c692db558def48-I683764&w=728&h=90'
  67. ],
  68. [
  69. 'title' => '로켓 홈인테리어',
  70. 'href' => '4hzeh',
  71. 'src' => '684135?subId=&traceId=V0-301-2f679fc6bd8f2e58-I684135&w=728&h=90'
  72. ],
  73. [
  74. 'title' => '로켓 주방용품',
  75. 'href' => '4hzuu',
  76. 'src' => '684137?subId=&traceId=V0-301-2b8ef06377ec8f50-I684137&w=728&h=90'
  77. ]
  78. ];
  79. shuffle($banners);
  80. return view(layout('main'), [
  81. 'products' => $products,
  82. 'banners' => $banners
  83. ]);
  84. }
  85. /**
  86. * 쿠팡 상품 검색
  87. */
  88. public function search(Request $request, SearchKeyword $searchKeywordModel)
  89. {
  90. if (RateLimiter::tooManyAttempts('search:' . $request->ip(), 50)) {
  91. return '짧은 시간동안 너무 많은 요청을 하였습니다. 잠시 후 다시 시도해주세요.';
  92. }
  93. $keyword = $request->post('keyword');
  94. $products = [];
  95. if($keyword) {
  96. $products = $this->coupangLib->getSearch($keyword);
  97. $searchKeywordModel->insert([
  98. 'keyword' => $keyword,
  99. 'ip_address' => $request->ip(),
  100. 'user_agent' => $request->userAgent(),
  101. 'created_at' => now()
  102. ]);
  103. }
  104. return view(layout('search'), [
  105. 'products' => $products,
  106. 'keyword' => $keyword
  107. ]);
  108. }
  109. /**
  110. * 쿠팡 카테고리 별 Best 상품 조회
  111. */
  112. public function category(Request $request)
  113. {
  114. $categoryID = $request->route('categoryID');
  115. if (!$categoryID) {
  116. abort(400);
  117. }
  118. if(!array_key_exists($categoryID, CATEGORIES)) {
  119. abort(403);
  120. }
  121. $categories = $this->coupangLib->getBestCategories($categoryID);
  122. return view(layout('category'), [
  123. 'categories' => $categories
  124. ]);
  125. }
  126. /**
  127. * 쿠팡 PL 조회
  128. * @see /recommend
  129. */
  130. public function recommend(Request $request)
  131. {
  132. $brandID = $request->route('brandID');
  133. if (!$brandID) {
  134. abort(400);
  135. }
  136. if(!array_key_exists($brandID, BRANDS)) {
  137. abort(403);
  138. }
  139. $recommend = $this->coupangLib->getCoupangPL($brandID);
  140. return view(layout('recommend'), [
  141. 'recommend' => $recommend
  142. ]);
  143. }
  144. /**
  145. * 쿠팡 골드박스 상품 조회
  146. * @see /goldbox
  147. */
  148. public function goldBox()
  149. {
  150. $goldBox = $this->coupangLib->getGoldBox();
  151. return view(layout('goldBox'), [
  152. 'goldBox' => $goldBox
  153. ]);
  154. }
  155. /**
  156. * 쿠팡 이벤트/프로모션
  157. * @see /event
  158. */
  159. public function event()
  160. {
  161. $event = array_reverse([
  162. 1 => 'https://link.coupang.com/a/4cA18',
  163. 2 => 'https://link.coupang.com/a/4cCEp',
  164. 3 => 'https://link.coupang.com/a/4cCOa',
  165. 4 => 'https://link.coupang.com/a/4cGuZ',
  166. 5 => 'https://link.coupang.com/a/4cGzi',
  167. 6 => 'https://link.coupang.com/a/4cGCW',
  168. 7 => 'https://link.coupang.com/a/4cGGX',
  169. 8 => 'https://link.coupang.com/a/4cGLN',
  170. 9 => 'https://link.coupang.com/a/4cGQA',
  171. 10 => 'https://link.coupang.com/a/4cGVd',
  172. 11 => 'https://link.coupang.com/a/4cGX7',
  173. 12 => 'https://link.coupang.com/a/4cGbT'
  174. ], true);
  175. return view(layout('event'), [
  176. 'event' => $event
  177. ]);
  178. }
  179. }