Procházet zdrojové kódy

260512 由oneapp跳轉機制

OMEGA\lulufj.ho před 1 týdnem
rodič
revize
32e90bf0ed

+ 57
- 6
app/Http/Controllers/Api/QrcodeController.php Zobrazit soubor

@@ -7,6 +7,7 @@ use App\Helpers\PhoneHelper;
7 7
 use App\Models\QrcodeRecord;
8 8
 use Illuminate\Http\Request;
9 9
 use Illuminate\Support\Facades\Http;
10
+use Illuminate\Support\Facades\Validator;
10 11
 
11 12
 class QrcodeController extends Controller
12 13
 {
@@ -58,12 +59,11 @@ class QrcodeController extends Controller
58 59
 
59 60
         // Step 4: 寫入紀錄
60 61
         QrcodeRecord::create([
61
-            'outlet_id'                 => $data['outletid'],
62
-            'outlet_name'               => $data['name'],
63
-            
64
-            'representative_id'         => $encryptedPhone,
65
-            
66
-            'qr_generated_at'           => now(),
62
+            'outlet_id'         => $data['outletid'],
63
+            'outlet_name'       => $data['name'],
64
+            'representative_id' => $encryptedPhone,
65
+            'qr_generated_at'   => now(),
66
+            'source'            => 'web',
67 67
         ]);
68 68
 
69 69
         // Step 5: 回傳
@@ -78,4 +78,55 @@ class QrcodeController extends Controller
78 78
             ],
79 79
         ]);
80 80
     }
81
+
82
+    public function generateFromOneApp(Request $request)
83
+    {
84
+        $validator = Validator::make($request->all(), [
85
+            'token' => 'required|string',
86
+        ]);
87
+
88
+        if ($validator->fails()) {
89
+            return response()->json([
90
+                'success' => false,
91
+                'message' => $validator->errors()->first(),
92
+            ], 422);
93
+        }
94
+
95
+        // Step 1: 取得 user data
96
+        $step = Http::post(config('app.oneapp_base_url') . '/API/GetUserData.ashx', [
97
+            'token' => $request->input('token'),
98
+        ]);
99
+
100
+        if (!$step->json('Success')) {
101
+            return response()->json(['success' => false, 'message' => 'GetUserData failed'], 400);
102
+        }
103
+
104
+        $data = $step->json();
105
+
106
+        // Step 2: 處理資料
107
+        $tel = $data['tel'];
108
+        $encryptedPhone = PhoneHelper::encrypt($tel);
109
+        $maskedPhone = substr($tel, 0, 2) . '*****' . substr($tel, 7);
110
+
111
+        // Step 3: 寫入紀錄
112
+        QrcodeRecord::create([
113
+            'outlet_id'         => $data['outletid'],
114
+            'outlet_name'       => $data['name'],
115
+            'representative_id' => $encryptedPhone,
116
+            'qr_generated_at'   => now(),
117
+            'source'            => 'oneapp',
118
+        ]);
119
+
120
+        // Step 4: 回傳
121
+        return response()->json([
122
+            'success' => true,
123
+            'data' => [
124
+                'otid'                     => $data['otid'],
125
+                'outletid'                 => $data['outletid'],
126
+                'name'                     => $data['name'],
127
+                'representative_id'        => $encryptedPhone,
128
+                'representative_id_masked' => $maskedPhone,
129
+            ],
130
+        ]);
131
+    }
81 132
 }

+ 1
- 0
app/Models/QrcodeRecord.php Zobrazit soubor

@@ -11,6 +11,7 @@ class QrcodeRecord extends Model
11 11
         'outlet_name',
12 12
         'representative_id',
13 13
         'qr_generated_at',
14
+        'source',
14 15
     ];
15 16
 
16 17
     protected $casts = [

+ 31
- 0
database/migrations/2026_04_29_173706_create_register_logs_table.php Zobrazit soubor

@@ -0,0 +1,31 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     */
12
+    public function up(): void
13
+    {
14
+        Schema::create('register_logs', function (Blueprint $table) {
15
+            $table->id();
16
+            $table->integer('logid');
17
+            $table->string('representative_id');
18
+            $table->string('outlet_id');
19
+            $table->dateTime('registered_at');
20
+            $table->timestamps();
21
+        });
22
+    }
23
+
24
+    /**
25
+     * Reverse the migrations.
26
+     */
27
+    public function down(): void
28
+    {
29
+        Schema::dropIfExists('register_logs');
30
+    }
31
+};

+ 25
- 0
database/migrations/2026_05_07_155754_alter_qrcode_records_add_source.php Zobrazit soubor

@@ -0,0 +1,25 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     */
12
+    public function up(): void
13
+    {
14
+        Schema::table('qrcode_records', function (Blueprint $table) {
15
+            $table->string('source')->default('web')->after('qr_generated_at');
16
+        });
17
+    }
18
+
19
+    public function down(): void
20
+    {
21
+        Schema::table('qrcode_records', function (Blueprint $table) {
22
+            $table->dropColumn('source');
23
+        });
24
+    }
25
+};

+ 8
- 1
routes/api.php Zobrazit soubor

@@ -8,6 +8,8 @@ use App\Http\Controllers\Api\HomePageController;
8 8
 use App\Http\Controllers\Api\NewsController;
9 9
 use Illuminate\Http\Request;
10 10
 use Illuminate\Support\Facades\Route;
11
+use App\Http\Controllers\Api\RegisterLogController;
12
+use App\Http\Middleware\VerifyApiKey;
11 13
 
12 14
 /*
13 15
 |--------------------------------------------------------------------------
@@ -19,4 +21,9 @@ use Illuminate\Support\Facades\Route;
19 21
 | be assigned to the "api" middleware group. Make something great!
20 22
 |
21 23
 */
22
-Route::post('/qrcode/generate', [App\Http\Controllers\Api\QrcodeController::class, 'generate']);
24
+
25
+Route::post('/qrcode/generate', [App\Http\Controllers\Api\QrcodeController::class, 'generate']);
26
+Route::post('/qrcode/generate-from-oneapp', [App\Http\Controllers\Api\QrcodeController::class, 'generateFromOneApp']);
27
+Route::middleware(VerifyApiKey::class)->group(function () {
28
+    Route::post('/register-log', [RegisterLogController::class, 'store']);
29
+});