Api2021Controller.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Http\Controllers\Web;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Http\Services\Web\Api2021Service;
  6. use Illuminate\Support\Facades\Validator;
  7. use App\Http\Services\ConstDef\GeneralConst;
  8. use App\Http\Services\CheckParamService;
  9. use Gregwar\Captcha\CaptchaBuilder;
  10. use Aws\CloudFront;
  11. use Illuminate\Support\Str;
  12. use Ramsey\Uuid\Uuid;
  13. class Api2021Controller extends Controller
  14. {
  15. private $aSv;
  16. private $checkParamSv;
  17. private $captcha;
  18. public function __construct()
  19. {
  20. session_start();
  21. $this->aSv = new Api2021Service();
  22. $this->checkParamSv = new CheckParamService();
  23. $this->captcha = new CaptchaBuilder();
  24. $this->captcha->build();
  25. }
  26. public function setdoc(Request $request)
  27. {
  28. // 參數驗證
  29. $messages = [
  30. 'doc.required' => '圖片必填',
  31. ];
  32. $validate = Validator::make($request->all(), [
  33. 'doc' => 'required',
  34. ], $messages);
  35. if ($validate->fails()) {
  36. $err = $validate->errors();
  37. $err_msg = "";
  38. foreach ($err->all() as $item) {
  39. $err_msg .= $item . "\n";
  40. }
  41. return response()->json(["succ" => false, "err" => $err_msg]);
  42. } else {
  43. $doc = $request->input('doc', '');
  44. $doc = explode(',', $doc);
  45. $doc = base64_decode($doc[1]);
  46. }
  47. $data = $this->aSv->setdoc($doc);
  48. // 返回
  49. return response()->json(["succ" => true, "err" => '', "data" => $data]);
  50. }
  51. public function setinfo(Request $request)
  52. {
  53. // 參數驗證
  54. $messages = [
  55. 'image_uri.nullable' => '',
  56. 'image_uri.regex' => '圖片路徑格式錯誤',
  57. 'nickname.required' => '暱稱必填',
  58. 'nickname.max' => '暱稱最多64字', // 格式的一半
  59. 'memo.nullable' => '',
  60. 'memo.max' => '留言最多1024字', // 格式的一半
  61. 'hash.nullable' => '',
  62. 'hash.max' => 'TAG最多64字', // 格式的一半
  63. 'name.required' => '姓名必填',
  64. 'name.max' => '姓名最多32字', // 格式的一半
  65. 'tel.required' => '電話必填',
  66. 'tel.max' => '電話最多32字',
  67. 'email.required' => '信箱必填',
  68. 'email.max' => '信箱最多128字',
  69. 'email.email' => '信箱格式錯誤',
  70. 'zone.required' => '地區必填',
  71. 'zone.max' => '地區最多16字', // 格式的一半
  72. 'utm_source.nullable' => '',
  73. 'utm_source.max' => 'utm_source 20字以內',
  74. 'utm_medium.nullable' => '',
  75. 'utm_medium.max' => 'utm_medium 20字以內',
  76. 'utm_campaign.nullable' => '',
  77. 'utm_campaign.max' => 'utm_campaign 20字以內',
  78. ];
  79. $validate = Validator::make($request->all(), [
  80. 'image_uri' => ['regex:/^pr(e|o)\/202(1|2)-[0-9]{2}-[0-9]{2}\/[0-9]{10}\.jpg$/'],
  81. 'nickname' => 'max:128',
  82. 'memo' => 'max:2048',
  83. 'hash' => 'max:128',
  84. 'name' => 'required|max:64',
  85. 'tel' => 'required|max:32',
  86. 'email' => 'required|max:128|email',
  87. 'zone' => 'required|max:32',
  88. 'utm_source' => 'nullable|max:20',
  89. 'utm_medium' => 'nullable|max:20',
  90. 'utm_campaign' => 'nullable|max:20',
  91. ], $messages);
  92. if ($validate->fails()) {
  93. $err = $validate->errors();
  94. $err_msg = "";
  95. foreach ($err->all() as $item) {
  96. $err_msg .= $item . "\n";
  97. }
  98. return response()->json(["succ" => false, "err" => $err_msg]);
  99. } else {
  100. $info = [
  101. 'image_uri' => $request->input('image_uri', ''),
  102. 'nickname' => $request->input('nickname', '') ?? '',
  103. 'memo' => $request->input('memo', ''),
  104. 'hash' => $request->input('hash', ''),
  105. 'name' => $request->input('name', ''),
  106. 'tel' => $request->input('tel', ''),
  107. 'email' => $request->input('email', ''),
  108. 'zone' => $request->input('zone', ''),
  109. 'utm_source' => $request->input('utm_source', ''),
  110. 'utm_medium' => $request->input('utm_medium', ''),
  111. 'utm_campaign' => $request->input('utm_campaign', ''),
  112. ];
  113. }
  114. // 業務邏輯: 保存訊息
  115. $this->aSv->setinfo($info);
  116. // 返回
  117. return response()->json(["succ" => true, "err" => '']);
  118. }
  119. public function counterGet($type)
  120. {
  121. // 業務邏輯: 保存訊息
  122. $cnt = $this->aSv->counterGet($type);
  123. // 返回
  124. return response()->json(["succ" => true, "err" => '', "cnt" => $cnt]);
  125. }
  126. public function counterSet($type)
  127. {
  128. // 業務邏輯: 設定訊息
  129. $this->aSv->counterSet($type);
  130. // 返回
  131. return response()->json(["succ" => true, "err" => '']);
  132. }
  133. public function reset()
  134. {
  135. $this->aSv->reset();
  136. // 返回
  137. return response()->json(["succ" => true, "err" => '']);
  138. }
  139. public function cdnInvalidation(Request $request)
  140. {
  141. $path = $request->input('path');
  142. $client = new CloudFront\CloudFrontClient([
  143. 'region' => env('AWS_S3_REGION'),
  144. 'version' => 'latest',
  145. 'credentials' => [
  146. 'key' => env('AWS_CDN_KEY'),
  147. 'secret' => env('AWS_CDN_SECRET'),
  148. ],
  149. ]);
  150. $result = $client->createInvalidation([
  151. 'DistributionId' => env('AWS_CDN_ID'),
  152. 'InvalidationBatch' => [
  153. 'CallerReference' => time() . Uuid::uuid4()->toString(),
  154. 'Paths' => [
  155. 'Items' => [$path],
  156. 'Quantity' => 1,
  157. ],
  158. ],
  159. ]);
  160. return '進行中,根據檔案數量請等待數秒至數分鐘不等。';
  161. }
  162. }