문의하기: SMS 전송기능 추가

master
barunsoft 2 years ago
parent f37da33b0a
commit 9e104f6a0f

@ -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']);
}

@ -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;
}
}

@ -0,0 +1,28 @@
<?php namespace App\Models;
class Sms extends BaseSoftDeleteModel
{
protected $table = 'sms';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'content', 'callback', 'total', 'success', 'failure'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public function details()
{
return $this->hasMany('App\Models\SmsDetail', 'sms_id');
}
}

@ -0,0 +1,30 @@
<?php namespace App\Models;
use App\Casts\EncryptFixed;
class SmsDetail extends BaseSoftDeleteModel
{
protected $table = 'sms_detail';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'sms_id', 'phone', 'success', 'error'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
protected $casts = [
'phone' => EncryptFixed::class,
];
}

@ -0,0 +1,264 @@
<?php
namespace App\Services;
use App\Models\Sms;
use App\Models\SmsDetail;
class SmsService
{
private $sms_url = "https://apis.aligo.in/send/";
private $user_id = "jiasoft";
private $api_key = "0rsw46blu616mp6geidaldsy9z6o0pgi";
private $sender = '1577-5993';
/**
* Create a new job instance.
*/
public function __construct()
{
}
/**
* 문자전송
* @param int $sms_id
* @param string $phone
* @param string $message
* @return bool
*/
public function sendSms(int $sms_id = 0, string $phone = '', string $message = '')
{
// 비어 있으면 건너뛰기
if (!$sms_id || empty($phone) || empty($message)) {
return false;
}
$result = $this->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);
}
}

@ -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,
],
],
/*

Loading…
Cancel
Save