diff --git a/app/Http/Controllers/Front/BoardController.php b/app/Http/Controllers/Front/BoardController.php index 82d2a8d..c1ce29c 100644 --- a/app/Http/Controllers/Front/BoardController.php +++ b/app/Http/Controllers/Front/BoardController.php @@ -3,8 +3,11 @@ namespace App\Http\Controllers\Front; use App\Http\Controllers\Controller; +use App\Libs\TraitSetting; use App\Libs\TraitBoard; +use App\Models\Setting; +use App\Services\SmsService; use App\Services\CrossFileService; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; @@ -12,6 +15,7 @@ use Illuminate\Contracts\View\View; class BoardController extends Controller { + use TraitSetting; use TraitBoard; protected $page = [ @@ -110,12 +114,27 @@ class BoardController extends Controller * @Path : /board/store * @param Request $request * @param CrossFileService $fileService + * @param SmsService $smsService * @return JsonResponse */ - public function store(Request $request, CrossFileService $fileService) + public function store(Request $request, CrossFileService $fileService, SmsService $smsService) { $result = $this->storePost($request, $fileService); + // 문의등록시 SMS 문자 보내기 + if ($request->get('code') == 'inquiry' && !empty($request->get('name')) && $result['code'] == 'success') { + $settings = $this->getArraySettings(); + + $message = '['. $settings['site_name'] .'] '. $request->name .'님의 문의가 접수되었습니다.'; + + $phone = '1599-3574'; + if (isset($settings['sms_phone']) && !empty($settings['sms_phone'])) { + $phone = $settings['sms_phone']; + } + + $smsResult = $smsService->sendPhone($phone, $message); + } + return $this->sendJson($result['message'], $result['code']); } diff --git a/app/Libs/TraitSetting.php b/app/Libs/TraitSetting.php index ea8a775..6216171 100644 --- a/app/Libs/TraitSetting.php +++ b/app/Libs/TraitSetting.php @@ -202,4 +202,23 @@ trait TraitSetting return $this->sendResult('변수가 삭제되었습니다.', 'success'); }); } + + /** + * 변수목록 키/배열로 가져오기 + * @return array + */ + protected function getArraySettings() + { + $all = Setting::all(); + + $data = []; + if ($all) { + foreach ($all as $row) { + $data[$row['key']] = $row['value']; + } + } + + return $data; + } + } diff --git a/app/Models/Sms.php b/app/Models/Sms.php new file mode 100644 index 0000000..85a935b --- /dev/null +++ b/app/Models/Sms.php @@ -0,0 +1,28 @@ +hasMany('App\Models\SmsDetail', 'sms_id'); + } +} diff --git a/app/Models/SmsDetail.php b/app/Models/SmsDetail.php new file mode 100644 index 0000000..bcc7a4a --- /dev/null +++ b/app/Models/SmsDetail.php @@ -0,0 +1,30 @@ + EncryptFixed::class, + ]; + +} diff --git a/app/Services/SmsService.php b/app/Services/SmsService.php new file mode 100644 index 0000000..029a269 --- /dev/null +++ b/app/Services/SmsService.php @@ -0,0 +1,264 @@ +requestSms($phone, $message); + + logger(json_encode($result)); + + $success = 0; + if ($result->result_code == 1) { + $success = 1; + } + + if ($sms_id && $sms_id > 0) { + $fields = array( + 'sms_id' => $sms_id, + 'phone' => $phone, + 'success' => $success, + 'error' => $result->message + ); + SmsDetail::query()->create($fields); + } + + if (!$success) { + return false; + } + + return true; + } + + /** + * 단일번호 문자전송 + * @param string $phone + * @param string $message + * @return bool + */ + public function sendPhone(string $phone = '', string $message = '') + { + $fields = array( + 'title' => '', + 'content' => $message, + 'callback' => $this->sender, + 'total' => 1, + 'success' => 0, + 'failure' => 0 + ); + $data = Sms::query()->create($fields); + + if ($data) { + $success = $this->sendSms($data->id, $phone, $message); + + if ($success) { + $data->success = 1; + $data->failure = 0; + } else { + $data->success = 0; + $data->failure = 1; + } + $data->save(); + + return true; + } + + return false; + } + + /** + * 단일회원 문자전송 + * @param $user + * @param $message + * @return bool + */ + public function sendUser($user, $message) + { + $fields = array( + 'title' => '', + 'content' => $message, + 'callback' => $this->sender, + 'total' => 1, + 'success' => 0, + 'failure' => 0 + ); + $data = Sms::query()->create($fields); + + if ($data) { + $success = $this->sendSms($data->id, $user->phone, $message); + if ($success) { + $data->success = 1; + $data->failure = 0; + } else { + $data->success = 0; + $data->failure = 1; + } + $data->save(); + + return true; + } + + return false; + } + + /** + * 복수회원 문자전송 + * @param array $users + * @param string $message + * @return int + */ + public function sendUsers(array $users = [], string $message = '') + { + $total = count($users); + $success = 0; + + $fields = array( + 'title' => '', + 'content' => $message, + 'callback' => $this->sender, + 'total' => $total, + 'success' => 0, + 'failure' => 0 + ); + $data = Sms::query()->create($fields); + + if ($data) { + foreach ($users as $user) { + $result = $this->sendSms($data->id, $user->phone, $message); + if ($result) { + $success++; + } + } + } + + $data->success = $success; + $data->failure = $total - $success; + $data->save(); + + return $success; + } + + /** + * 복수번호 문자전송 + * @param array $phones + * @param string $message + * @return int + */ + public function sendPhones(array $phones = [], string $message = '') + { + $total = count($phones); + $success = 0; + + $fields = array( + 'title' => '', + 'content' => $message, + 'callback' => $this->sender, + 'total' => $total, + 'success' => 0, + 'failure' => 0 + ); + $data = Sms::query()->create($fields); + + if ($data) { + foreach ($phones as $phone) { + $result = $this->sendSms($data->id, $phone, $message); + if ($result) { + $success++; + } + } + } + + $data->success = $success; + $data->failure = $total - $success; + $data->save(); + + return $success; + } + + /** + * 문자전송 요청 + * @param string $phone + * @param string $message + * @return mixed + */ + public function requestSms(string $phone = '', string $message = '') + { + $sms = []; + $sms['user_id'] = $this->user_id; + $sms['key'] = $this->api_key; + + $sms['msg'] = stripslashes($message); + $sms['receiver'] = $phone; + $sms['sender'] = $this->sender; + + $sms['rdate'] = ''; + $sms['rtime'] = ''; + $sms['testmode_yn'] = ''; + $sms['title'] = ''; + + $sms['msg_type'] = 'SMS'; + + return $this->postUrl($this->sms_url, $sms); + } + + /** + * 문자 전송 연동 모듈 함수 호출 + * @param string $url + * @param array $fields + * @return mixed + */ + private function postUrl(string $url = '', array $fields = []) + { + $schema = explode("/", $url); + $port = $schema[0] == 'https:' ? 443 : 80; + + $oCurl = curl_init(); + curl_setopt($oCurl, CURLOPT_PORT, $port); + curl_setopt($oCurl, CURLOPT_URL, $url); + curl_setopt($oCurl, CURLOPT_POST, 1); + curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($oCurl, CURLOPT_POSTFIELDS, $fields); + curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false); + $ret = curl_exec($oCurl); + curl_close($oCurl); + + // result_code : 전송성공유무 (성공:1 / 실패: -100 부터 -999) + // message : success (성공시) / reserved (예약성공시) / 그외 (실패상세사유가 포함됩니다) + // msg_id : 메세지 고유ID = 고유값을 반드시 기록해 놓으셔야 sms_list API를 통해 전화번호별 성공/실패 유무를 확인하실 수 있습니다 + // error_cnt : 에러갯수 = receiver 에 포함된 전화번호중 문자전송이 실패한 갯수 + // success_cnt : 성공갯수 = 이동통신사에 전송요청된 갯수 + // msg_type : 전송된 메세지 타입 = SMS / LMS / MMS (보내신 타입과 다른경우 로그로 기록하여 확인하셔야 합니다) + + // 결과배열 + return json_decode($ret); + } +} diff --git a/config/queue.php b/config/queue.php index 25ea5a8..e48afdd 100644 --- a/config/queue.php +++ b/config/queue.php @@ -71,6 +71,21 @@ return [ 'after_commit' => false, ], + 'redis_queue' => [ + 'driver' => 'redis', + 'connection' => 'queue', + 'queue' => env('REDIS_QUEUE_QUEUE', 'default'), + 'retry_after' => 90, + 'block_for' => null, + ], + + 'redis_push' => [ + 'driver' => 'redis', + 'connection' => env('REDIS_PUSH_CONNECTION', 'push'), + 'queue' => env('REDIS_PUSH_QUEUE', 'default'), + 'retry_after' => 90, + 'block_for' => null, + ], ], /*