SeminarSignUpController.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\ApiController;
  4. use App\Http\Services\Api\SeminarSignUpService;
  5. use App\Http\Requests\Api\SeminarSignUp\StoreRequest;
  6. use Log;
  7. class SeminarSignUpController extends ApiController
  8. {
  9. private $seminarSignUpSv;
  10. public function __construct()
  11. {
  12. $this->seminarSignUpSv = new SeminarSignUpService();
  13. }
  14. // save data to db
  15. public function insertData(StoreRequest $request)
  16. {
  17. $trackNo = $request->input('trackNo', '');
  18. $lang = substr($trackNo, 0, 2);
  19. $firstName_orig = $request->input('firstName', '');
  20. $firstName = $this->safeEncrypt($firstName_orig, 'arm');
  21. $lastName_orig = $request->input('lastName', '');
  22. $lastName = $this->safeEncrypt($request->input('lastName_orig', ''), 'arm');
  23. $companyName_orig = $request->input('companyName', '');
  24. $companyName = $this->safeEncrypt($request->input('companyName_orig', ''), 'arm');
  25. $companyEmail_orig = $request->input('companyEmail', '');
  26. $companyEmail = $this->safeEncrypt($request->input('companyEmail_orig', ''), 'arm');
  27. $backupEmail = $request->input('backupEmail', '');
  28. if (!is_null($backupEmail)) {
  29. $backupEmail_orig = $request->input('backupEmail', '');
  30. $backupEmail = $this->safeEncrypt($backupEmail_orig, 'arm');
  31. } else {
  32. $backupEmail_orig = '';
  33. }
  34. $phoneNumber_orig = $request->input('phoneNumber', '');
  35. $phoneNumber = $this->safeEncrypt($request->input('phoneNumber_orig', ''), 'arm');
  36. $country = $request->input('country', '');
  37. $registeredSession = $request->input('registeredSession', '');
  38. $lunchOptions = $request->input('lunchOptions', '');
  39. $typeOfIndustry = $request->input('typeOfIndustry', '');
  40. $typeOfJob = $request->input('typeOfJob', '');
  41. $jobTitle = $request->input('jobTitle', '');
  42. $trackOfInterest = $request->input('trackOfInterest', '');
  43. $areaOfInterest = $request->input('areaOfInterest', '');
  44. $howToKnowAboutTheEvent = $request->input('howToKnowAboutTheEvent', '');
  45. $consentAcceptEmail = $request->input('consentAcceptEmail', '');
  46. $consentPrivacyPolicy = $request->input('consentPrivacyPolicy', '');
  47. $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackNo); // true: 可報名 / false: 已額滿
  48. $duplicatedOrNot = $this->seminarSignUpSv->duplicatedOrNot($trackNo, $companyEmail, $phoneNumber); // true: 可報名 / false: 已重複
  49. if ($overOrNot&&$duplicatedOrNot) {
  50. $this->seminarSignUpSv->insertData(
  51. $firstName,
  52. $lastName,
  53. $companyName,
  54. $companyEmail,
  55. $backupEmail,
  56. $phoneNumber,
  57. $country,
  58. $trackNo,
  59. $registeredSession,
  60. $lunchOptions,
  61. $typeOfIndustry,
  62. $typeOfJob,
  63. $jobTitle,
  64. $trackOfInterest,
  65. $areaOfInterest,
  66. $howToKnowAboutTheEvent,
  67. $consentAcceptEmail,
  68. $consentPrivacyPolicy,
  69. );
  70. $res = '報名成功';
  71. if ($lang=='TP'||$lang=='HS') {
  72. $this->mailToUser_TW($firstName_orig, $lastName_orig, $companyName_orig, $companyEmail_orig, $backupEmail_orig,
  73. $phoneNumber_orig, $country, $registeredSession, $lunchOptions, $typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest);
  74. } elseif ($lang=='JP') {
  75. $this->mailToUser_JP($firstName_orig, $companyEmail_orig, $backupEmail_orig);
  76. } elseif ($lang=='KR') {
  77. $this->mailToUser_KR($firstName_orig, $companyEmail_orig, $backupEmail_orig);
  78. } else {
  79. $this->mailToUser_EN($firstName_orig, $companyEmail_orig, $backupEmail_orig);
  80. }
  81. } elseif (!$overOrNot) {
  82. $res = '已達報名上限';
  83. } elseif (!$duplicatedOrNot) {
  84. $res = '已重複報名';
  85. }
  86. $data = [
  87. 'res' => $res,
  88. ];
  89. return $this->apiResponse($data);
  90. }
  91. public function getData()
  92. {
  93. $list = $this->seminarSignUpSv->getData();
  94. $data = [
  95. 'list' => $list
  96. ];
  97. return $this->apiResponse($data);
  98. }
  99. /**
  100. * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
  101. */
  102. public function safeEncrypt(string $message, string $skey): string
  103. {
  104. $strArr = str_split(base64_encode($message));
  105. $strCount = count($strArr);
  106. foreach (str_split($skey) as $key => $value)
  107. $key < $strCount && $strArr[$key].=$value;
  108. return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
  109. }
  110. /**
  111. * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
  112. */
  113. public function safeDecrypt(string $encrypted, string $skey): string
  114. {
  115. $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
  116. $strCount = count($strArr);
  117. foreach (str_split($skey) as $key => $value)
  118. $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
  119. return base64_decode(join('', $strArr));
  120. }
  121. public function mailToUser_TW(
  122. string $firstName='',
  123. string $lastName='',
  124. string $companyName='',
  125. string $companyEmail='',
  126. string $backupEmail='',
  127. string $phoneNumber='',
  128. string $country='',
  129. string $registeredSession='',
  130. string $lunchOptions='',
  131. string $typeOfIndustry='',
  132. string $typeOfJob='',
  133. string $jobTitle='',
  134. string $trackOfInterest=''
  135. )
  136. {
  137. $data = array(
  138. 'firstName' => $firstName,
  139. 'lastName' => $lastName,
  140. 'companyName' => $companyName,
  141. 'companyEmail' => $companyEmail,
  142. 'backupEmail' => $backupEmail,
  143. 'phoneNumber' => $phoneNumber,
  144. 'country' => $country,
  145. 'registeredSession' => $registeredSession,
  146. 'lunchOptions' => $lunchOptions,
  147. 'typeOfIndustry' => $typeOfIndustry,
  148. 'typeOfJob' => $typeOfJob,
  149. 'jobTitle' => $jobTitle,
  150. 'trackOfInterest' => $trackOfInterest,
  151. );
  152. if (strlen($backupEmail)>0) {
  153. \Mail::send(['text'=>'mailTW'], $data, function($message) use ($firstName, $companyEmail, $backupEmail) {
  154. $message->to($companyEmail, $firstName)->subject('報名成功');
  155. $message->cc($backupEmail, $firstName)->subject('報名成功');
  156. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  157. });
  158. } else {
  159. \Mail::send(['text'=>'mailTW'], $data, function($message) use ($companyEmail, $firstName) {
  160. $message->to($companyEmail, $firstName)->subject('報名成功');
  161. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  162. });
  163. }
  164. }
  165. public function mailToUser_JP(string $firstName='', string $companyEmail='', string $backupEmail='')
  166. {
  167. $data = array('name'=>$firstName);
  168. if ($backupEmail) {
  169. \Mail::send(['text'=>'mailJP'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
  170. $message->to($companyEmail, $firstName)->subject('登録完了');
  171. $message->cc($backupEmail, $firstName)->subject('登録完了');
  172. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  173. });
  174. } else {
  175. \Mail::send(['text'=>'mailJP'], $data, function($message) use ($companyEmail, $firstName) {
  176. $message->to($companyEmail, $firstName)->subject('登録完了');
  177. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  178. });
  179. }
  180. }
  181. public function mailToUser_KR(string $firstName='', string $companyEmail='', string $backupEmail='')
  182. {
  183. $data = array('name'=>$firstName);
  184. if ($backupEmail) {
  185. \Mail::send(['text'=>'mailKR'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
  186. $message->to($companyEmail, $firstName)->subject('등록이 완료되었습니다');
  187. $message->cc($backupEmail, $firstName)->subject('등록이 완료되었습니다');
  188. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  189. });
  190. } else {
  191. \Mail::send(['text'=>'mailKR'], $data, function($message) use ($companyEmail, $firstName) {
  192. $message->to($companyEmail, $firstName)->subject('등록이 완료되었습니다');
  193. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  194. });
  195. }
  196. }
  197. public function mailToUser_EN(string $firstName='', string $companyEmail='', string $backupEmail='')
  198. {
  199. $data = array('name'=>$firstName);
  200. if ($backupEmail) {
  201. \Mail::send(['text'=>'mailEN'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
  202. $message->to($companyEmail, $firstName)->subject('Registration is complete');
  203. $message->cc($backupEmail, $firstName)->subject('Registration is complete');
  204. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  205. });
  206. } else {
  207. \Mail::send(['text'=>'mailEN'], $data, function($message) use ($companyEmail, $firstName) {
  208. $message->to($companyEmail, $firstName)->subject('Registration is complete');
  209. $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
  210. });
  211. }
  212. }
  213. }