ContactUsCustomController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Themes\kbgolf\pro\app\Http\Controllers\Page;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\CallApiService;
  5. use App\Services\MessageSendService;
  6. use Illuminate\Pagination\LengthAwarePaginator;
  7. use Illuminate\Http\Request;
  8. use DateTime;
  9. class ContactUsCustomController extends Controller
  10. {
  11. private $callApiService;
  12. public function __construct(CallApiService $callApiService, MessageSendService $messageSendService)
  13. {
  14. $this->callApiService = $callApiService;
  15. $this->messageSendService = $messageSendService;
  16. }
  17. public function getSetup()
  18. {
  19. return $this->callApiService->callApi([
  20. 'url' => 'setup-get',
  21. 'data' => [
  22. 'SetupCode' => 'contact-us',
  23. 'BrandCode' => 'main',
  24. 'LangType' => getLocale()
  25. ]
  26. ]);
  27. }
  28. public function index($id=null)
  29. {
  30. $query = "post_type_id = 58";
  31. $limit = (int)request('limit', 12);
  32. $page = (int)request('page', 1);
  33. if($id != null){
  34. $query .= " and mx.id = '$id'";
  35. }
  36. // dd($query);
  37. // api 호출 request
  38. $contactUsCustomList = $this->callApiService->callApi([
  39. 'url' => 'post-page',
  40. 'data' => [
  41. 'PageVars' => [
  42. 'Query' => $query,
  43. 'Desc' => 'created_on',
  44. 'Limit' => $limit,
  45. 'Offset' => ($page - 1) * $limit
  46. ]
  47. ]
  48. ]);
  49. // date 포맷 변경
  50. if (isset($contactUsCustomList['Page']) && !empty($contactUsCustomList['Page'])) {
  51. foreach ($contactUsCustomList['Page'] as &$contactUsCustom) {
  52. if (isset($contactUsCustom['OfficialDate'])) {
  53. $OfficialDate = DateTime::createFromFormat('Ymd', $contactUsCustom['OfficialDate']);
  54. $date = $OfficialDate->format('Y.m.d');
  55. $contactUsCustom['date'] = $date;
  56. }
  57. if(isset($contactUsCustom['Pc1'])){
  58. $Pc1Length = mb_strlen($contactUsCustom['Pc1'], 'UTF-8');
  59. if($Pc1Length > 2){
  60. $contactUsCustom['maskedName'] = mb_substr($contactUsCustom['Pc1'], 0, $Pc1Length - 2, 'UTF-8') . 'OO';
  61. }else{
  62. $contactUsCustom['maskedName'] = mb_substr($contactUsCustom['Pc1'], 0, $Pc1Length - 1, 'UTF-8') . 'O';
  63. }
  64. }
  65. }
  66. }
  67. // dd($contactUsCustomList);
  68. if ($this->callApiService->verifyApiError($contactUsCustomList)) {
  69. notify()->error($contactUsCustomList['body'], 'Error', 'bottomRight');
  70. return redirect()->back();
  71. }
  72. // pagenation
  73. $totalcnt = $contactUsCustomList['PageVars']['QueryCnt'];
  74. $contactUsCustomItems['Page'] = new LengthAwarePaginator(
  75. $contactUsCustomList['Page'],
  76. $totalcnt,
  77. $limit,
  78. $page,
  79. ['path' => request()->url()]
  80. );
  81. return view('views.page.contactus-custom-list', compact('contactUsCustomList', 'contactUsCustomItems'));
  82. }
  83. public function store()
  84. {
  85. $rules = [
  86. 'captcha' => 'required|captcha',
  87. 'Pc2' => 'required|regex:/^010(?:-?\d{4}){2}$/',
  88. ];
  89. $validator = validator()->make(request()->all(), $rules);
  90. if ($validator->fails()) {
  91. notify()->error('보안 체크 숫자 입력이 틀렸습니다. 다시 올바르게 입력해서 문의하기 해주세요', 'Error', 'bottomRight');
  92. return redirect()->to('/contactus-custom')->withInput();
  93. }
  94. $request = array_merge([
  95. 'Id' => 0,
  96. 'PostTypeId' => 58,
  97. 'Status' => '2',
  98. 'MemberId' => session('member')['MemberId'] ?? 0,
  99. ], request()->all());
  100. $response = $this->callApiService->callApi([
  101. 'url' => 'post-act',
  102. 'data' => [
  103. 'Page' => [
  104. $request
  105. ]
  106. ],
  107. ]);
  108. if ($this->callApiService->verifyApiError($response)) {
  109. notify()->error($response['body'], 'Error', 'bottomRight');
  110. return redirect()->route('index')->withInput();
  111. }
  112. $redirectTo = redirect()->to('/');
  113. $contactUs = $this->getSetup();
  114. $contactUs['Fields'] = [
  115. [
  116. 'Name' => 'Pc1',
  117. 'Caption' => '신청자 이름'
  118. ],
  119. [
  120. 'Name' => 'Pc2',
  121. 'Caption' => '연락처'
  122. ],
  123. [
  124. 'Name' => 'Pc3',
  125. 'Caption' => '거래 구분'
  126. ],
  127. [
  128. 'Name' => 'Pc4',
  129. 'Caption' => '회원권 구분'
  130. ],
  131. [
  132. 'Name' => 'Pc5',
  133. 'Caption' => '회원권명'
  134. ],
  135. [
  136. 'Name' => 'Pc6',
  137. 'Caption' => '희망 가격'
  138. ],
  139. [
  140. 'Name' => 'Pt1',
  141. 'Caption' => '요청 사항'
  142. ],
  143. [
  144. 'Name' => 'Pc7',
  145. 'Caption' => '상태'
  146. ],
  147. ];
  148. $msg = '[kbgolf] 고객지원 요청내용입니다.\n';
  149. foreach($contactUs['Fields'] ?? [] as $fields) {
  150. $msg .= $fields['Caption'] . ': ' . $request[$fields['Name']] . '\n';
  151. }
  152. if ($contactUs['OkMobile']) {
  153. $toMobiles = preg_replace('/\s+/', '', explode(',', $contactUs['ToMobile']));
  154. foreach($toMobiles ?? [] as $toMobile) {
  155. $this->messageSendService->send($msg, $msg, $toMobile);
  156. }
  157. }
  158. $toMails = preg_replace('/\s+/', '', explode(',', $contactUs['ToMail']));
  159. foreach($toMails ?? [] as $toMail) {
  160. \Mail::send('views.emails.contact-us', ['request' => $request, 'contactUs' => $contactUs],
  161. function ($message) use ($toMail) {
  162. $message->to($toMail);
  163. $message->subject('[kbgolf] 고객지원 요청내용입니다.');
  164. }
  165. );
  166. }
  167. notify()->success('성공적으로 문의하였습니다', 'Success', 'bottomRight');
  168. return $redirectTo;
  169. }
  170. }