LuluFJ.Ho 2 anni fa
parent
commit
c9e12c7bd8

+ 114
- 0
app/Http/Controllers/Api/SeminarSignUpController.php Vedi File

@@ -0,0 +1,114 @@
1
+<?php
2
+
3
+namespace App\Http\Controllers\Api;
4
+
5
+use App\Http\Controllers\Api\ApiController;
6
+use App\Http\Services\Web\SeminarSignUpService;
7
+use App\Http\Requests\Api\SeminarSignUp\StoreRequest;
8
+
9
+use Log;
10
+
11
+class SeminarSignUpController extends ApiController
12
+{
13
+    
14
+    private $seminarSignUpSv;
15
+    
16
+    public function __construct()
17
+    {
18
+        $this->seminarSignUpSv = new SeminarSignUpService();
19
+
20
+    }
21
+
22
+    // save data to db
23
+    public function insertData(StoreRequest $request)
24
+    {
25
+        $name = $this->safeEncrypt($request->input('name', 'name'), 'arm');
26
+        $email = $this->safeEncrypt($request->input('email', 'email'), 'arm');
27
+        $mobile = $this->safeEncrypt($request->input('mobile', 'mobile'), 'arm');
28
+        $trackId = $request->input('trackId', 'T0052');
29
+        
30
+        $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackId);
31
+        if ($overOrNot) {
32
+
33
+            $this->seminarSignUpSv->insertData($name, $email, $mobile, $trackId);
34
+            $res = '報名成功';
35
+        } else {
36
+
37
+            $res = '已達報名上限';
38
+        }
39
+        $data = [
40
+            'res' => $res,
41
+        ];
42
+
43
+        return $this->apiResponse($data);
44
+    }
45
+
46
+    public function getData()
47
+    {
48
+        
49
+        $Data = $this->seminarSignUpSv->getData();
50
+        
51
+        $data = [
52
+            'list' => $Data
53
+        ];
54
+        
55
+        return $this->apiResponse($data);
56
+    }
57
+
58
+    /**
59
+     * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
60
+     */
61
+    public function safeEncrypt(string $message, string $skey): string
62
+    {
63
+        // if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
64
+        //     throw new RangeException('Key is not the correct size (must be 32 bytes).');
65
+        // }
66
+        // $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
67
+        
68
+        // $cipher = base64_encode(
69
+        //     $nonce .
70
+        //     sodium_crypto_secretbox(
71
+        //         $message,
72
+        //         $nonce,
73
+        //         $key
74
+        //     )
75
+        // );
76
+        // sodium_memzero($message);
77
+        // sodium_memzero($key);
78
+        // return $cipher;
79
+        $strArr = str_split(base64_encode($message));
80
+        $strCount = count($strArr);
81
+        foreach (str_split($skey) as $key => $value)
82
+        $key < $strCount && $strArr[$key].=$value;
83
+        return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
84
+    }
85
+    
86
+    /**
87
+     * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
88
+     */
89
+    public function safeDecrypt(string $encrypted, string $skey): string
90
+    {
91
+        // $decoded = base64_decode($encrypted);
92
+        // $nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
93
+        // $ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
94
+        
95
+        // $plain = sodium_crypto_secretbox_open(
96
+        //     $ciphertext,
97
+        //     $nonce,
98
+        //     $key
99
+        // );
100
+        // if (!is_string($plain)) {
101
+        //     throw new Exception('Invalid MAC');
102
+        // }
103
+        // sodium_memzero($ciphertext);
104
+        // sodium_memzero($key);
105
+        
106
+        // return $plain;
107
+        $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
108
+        $strCount = count($strArr);
109
+        foreach (str_split($skey) as $key => $value)
110
+        $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
111
+        return base64_decode(join('', $strArr));
112
+    }
113
+    
114
+}

+ 32
- 0
app/Http/Requests/Api/SeminarSignUp/StoreRequest.php Vedi File

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+namespace App\Http\Requests\Api\SeminarSignUp;
4
+
5
+use Illuminate\Foundation\Http\FormRequest;
6
+
7
+class StoreRequest extends FormRequest
8
+{
9
+    public function rules(): array
10
+    {
11
+        // $name, $email, $mobile, $trackId
12
+        return [
13
+            'name'   => 'required',
14
+            'email' => 'required|email',
15
+            'mobile' => 'required',
16
+            'trackId' => 'required'
17
+        ];
18
+    }
19
+
20
+    public function messages(): array
21
+    {
22
+        return [
23
+            'name.required'     => 'column [name] is required.',
24
+            'email.required' => 'column [email] is required.',
25
+            'mobile.required'  => 'column [mobile] is required.',
26
+            'email.email'   => 'column [email] format error',
27
+            'trackId.required'  => 'column [trackId] is required.',
28
+        ];
29
+    }
30
+}
31
+
32
+?>

+ 68
- 0
app/Http/Services/Web/SeminarSignUpService.php Vedi File

@@ -0,0 +1,68 @@
1
+<?php
2
+
3
+namespace App\Http\Services\Web;
4
+
5
+use App\Models\SignupData;
6
+use App\Models\TrackData;
7
+
8
+use DB;
9
+use GuzzleHttp\Client;
10
+
11
+class SeminarSignUpService
12
+{
13
+    protected $signupDb;
14
+    protected $trackDataDb;
15
+    
16
+    public function __construct()
17
+    {
18
+        $this->signupDb = new SignupData();
19
+        $this->trackDataDb = new TrackData();
20
+        
21
+    }
22
+    
23
+    public function insertData($name, $email, $mobile, $trackId)
24
+    {
25
+        
26
+        $this->signupDb
27
+            ->insert([
28
+                'name' => $name,
29
+                'email' => $email,
30
+                'mobile' => $mobile,
31
+                'trackId' => $trackId,
32
+                'createDate' => date("Y-m-d H:i:s"),
33
+            ]);
34
+        
35
+    }
36
+    
37
+    public function overLimitOrNot($trackId)
38
+    {
39
+        $nowCount = $this->signupDb
40
+            ->where('trackId', '=', $trackId)
41
+            ->count();
42
+        
43
+        $limit = $this->trackDataDb
44
+            ->select('trackLimit')
45
+            ->where('trackNo', '=', $trackId)
46
+            ->first();
47
+        
48
+        if ($limit->trackLimit>$nowCount) {
49
+            return true;
50
+        } else {
51
+            return false;
52
+        }
53
+    }
54
+    
55
+    public function getData()
56
+    {
57
+        $res = $this->trackDataDb
58
+        ->select([
59
+            '*'
60
+        ])
61
+        ->get();
62
+        
63
+        // 整理返回值並返回
64
+        return $res;
65
+
66
+    }
67
+
68
+}

+ 14
- 0
app/Models/SignupData.php Vedi File

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use Illuminate\Database\Eloquent\Model;
6
+
7
+class SignupData extends Model
8
+{
9
+    protected $connection = 'mysql';                  // 參照 config/database.php 的連線名稱
10
+    protected $table      = 'signupData';
11
+    protected $primaryKey = 'id';                     // PK 的欄位名稱
12
+    public    $timestamps = false;                    // 保持 false
13
+
14
+}

+ 14
- 0
app/Models/TrackData.php Vedi File

@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+namespace App\Models;
4
+
5
+use Illuminate\Database\Eloquent\Model;
6
+
7
+class TrackData extends Model
8
+{
9
+    protected $connection = 'mysql';                  // 參照 config/database.php 的連線名稱
10
+    protected $table      = 'trackData';
11
+    protected $primaryKey = 'id';                     // PK 的欄位名稱
12
+    public    $timestamps = false;                    // 保持 false
13
+
14
+}

+ 19
- 1
routes/api.php Vedi File

@@ -1,3 +1,21 @@
1 1
 <?php
2 2
 
3
-use Illuminate\Support\Facades\Route;
3
+use Illuminate\Support\Facades\Route;
4
+use App\Http\Controllers\Api\SeminarSignUpController;
5
+// use App\Http\Controllers\Api\PlayerController;
6
+
7
+Route::prefix('signup')->group(function () {
8
+    Route::post('/insertData', [SeminarSignUpController::class, 'insertData']);
9
+    Route::post('/getData', [SeminarSignUpController::class, 'getData']);
10
+});
11
+
12
+
13
+/*// API
14
+Route::group(['prefix' => 'web', 'namespace' => 'Web'], function () {
15
+    
16
+    Route::post('/insertData', 'SeminarSignUpController@insertData');
17
+    Route::post('/getData', 'SeminarSignUpController@getData');
18
+    
19
+});*/
20
+
21
+?>