123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
-
- namespace App\Http\Controllers\Web;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Http\Services\Web\Api2021Service;
- use Illuminate\Support\Facades\Validator;
- use App\Http\Services\ConstDef\GeneralConst;
- use App\Http\Services\CheckParamService;
- use Gregwar\Captcha\CaptchaBuilder;
- use Aws\CloudFront;
- use Illuminate\Support\Str;
- use Ramsey\Uuid\Uuid;
-
- class Api2021Controller extends Controller
- {
-
- private $aSv;
- private $checkParamSv;
- private $captcha;
-
- public function __construct()
- {
- session_start();
- $this->aSv = new Api2021Service();
- $this->checkParamSv = new CheckParamService();
- $this->captcha = new CaptchaBuilder();
- $this->captcha->build();
- }
-
- public function setdoc(Request $request)
- {
- // 參數驗證
- $messages = [
- 'doc.required' => '圖片必填',
- ];
- $validate = Validator::make($request->all(), [
- 'doc' => 'required',
- ], $messages);
- if ($validate->fails()) {
- $err = $validate->errors();
- $err_msg = "";
- foreach ($err->all() as $item) {
- $err_msg .= $item . "\n";
- }
-
- return response()->json(["succ" => false, "err" => $err_msg]);
- } else {
- $doc = $request->input('doc', '');
- $doc = explode(',', $doc);
- $doc = base64_decode($doc[1]);
- }
- $data = $this->aSv->setdoc($doc);
-
- // 返回
- return response()->json(["succ" => true, "err" => '', "data" => $data]);
- }
-
- public function setinfo(Request $request)
- {
- // 參數驗證
- $messages = [
- 'image_uri.nullable' => '',
- 'image_uri.regex' => '圖片路徑格式錯誤',
- 'nickname.required' => '暱稱必填',
- 'nickname.max' => '暱稱最多64字', // 格式的一半
- 'memo.nullable' => '',
- 'memo.max' => '留言最多1024字', // 格式的一半
- 'hash.nullable' => '',
- 'hash.max' => 'TAG最多64字', // 格式的一半
- 'name.required' => '姓名必填',
- 'name.max' => '姓名最多32字', // 格式的一半
- 'tel.required' => '電話必填',
- 'tel.max' => '電話最多32字',
- 'email.required' => '信箱必填',
- 'email.max' => '信箱最多128字',
- 'email.email' => '信箱格式錯誤',
- 'zone.required' => '地區必填',
- 'zone.max' => '地區最多16字', // 格式的一半
- 'utm_source.nullable' => '',
- 'utm_source.max' => 'utm_source 20字以內',
- 'utm_medium.nullable' => '',
- 'utm_medium.max' => 'utm_medium 20字以內',
- 'utm_campaign.nullable' => '',
- 'utm_campaign.max' => 'utm_campaign 20字以內',
- ];
- $validate = Validator::make($request->all(), [
- 'image_uri' => ['regex:/^pr(e|o)\/202(1|2)-[0-9]{2}-[0-9]{2}\/[0-9]{10}\.jpg$/'],
- 'nickname' => 'max:128',
- 'memo' => 'max:2048',
- 'hash' => 'max:128',
- 'name' => 'required|max:64',
- 'tel' => 'required|max:32',
- 'email' => 'required|max:128|email',
- 'zone' => 'required|max:32',
- 'utm_source' => 'nullable|max:20',
- 'utm_medium' => 'nullable|max:20',
- 'utm_campaign' => 'nullable|max:20',
- ], $messages);
- if ($validate->fails()) {
- $err = $validate->errors();
- $err_msg = "";
- foreach ($err->all() as $item) {
- $err_msg .= $item . "\n";
- }
-
- return response()->json(["succ" => false, "err" => $err_msg]);
- } else {
- $info = [
- 'image_uri' => $request->input('image_uri', ''),
- 'nickname' => $request->input('nickname', '') ?? '',
- 'memo' => $request->input('memo', ''),
- 'hash' => $request->input('hash', ''),
- 'name' => $request->input('name', ''),
- 'tel' => $request->input('tel', ''),
- 'email' => $request->input('email', ''),
- 'zone' => $request->input('zone', ''),
- 'utm_source' => $request->input('utm_source', ''),
- 'utm_medium' => $request->input('utm_medium', ''),
- 'utm_campaign' => $request->input('utm_campaign', ''),
- ];
- }
- // 業務邏輯: 保存訊息
- $this->aSv->setinfo($info);
-
- // 返回
- return response()->json(["succ" => true, "err" => '']);
- }
-
- public function counterGet($type)
- {
- // 業務邏輯: 保存訊息
- $cnt = $this->aSv->counterGet($type);
-
- // 返回
- return response()->json(["succ" => true, "err" => '', "cnt" => $cnt]);
- }
-
- public function counterSet($type)
- {
- // 業務邏輯: 設定訊息
- $this->aSv->counterSet($type);
-
- // 返回
- return response()->json(["succ" => true, "err" => '']);
- }
-
- public function reset()
- {
- $this->aSv->reset();
-
- // 返回
- return response()->json(["succ" => true, "err" => '']);
- }
-
- public function cdnInvalidation(Request $request)
- {
- $path = $request->input('path');
-
- $client = new CloudFront\CloudFrontClient([
- 'region' => env('AWS_S3_REGION'),
- 'version' => 'latest',
- 'credentials' => [
- 'key' => env('AWS_CDN_KEY'),
- 'secret' => env('AWS_CDN_SECRET'),
- ],
- ]);
-
- $result = $client->createInvalidation([
- 'DistributionId' => env('AWS_CDN_ID'),
- 'InvalidationBatch' => [
- 'CallerReference' => time() . Uuid::uuid4()->toString(),
- 'Paths' => [
- 'Items' => [$path],
- 'Quantity' => 1,
- ],
- ],
- ]);
-
- return '進行中,根據檔案數量請等待數秒至數分鐘不等。';
- }
-
- }
|