Ken 3 giorni fa
parent
commit
4beda9b20d

+ 23 - 7
app/Http/Controllers/api/ActivityReward.php

@@ -7,6 +7,7 @@ use App\Models\ActivityReward as ActivityRewardModel;
 use App\Models\ActivityUser;
 use App\Services\ActivityRewardService;
 use App\Services\ActivityUserService;
+use App\Services\PhoneCodeService;
 use App\Services\SmsService;
 use App\Services\UserService;
 use Illuminate\Http\JsonResponse;
@@ -19,8 +20,26 @@ use Exception;
 class ActivityReward extends BaseController
 {
 
+    public function verifyPhone(): JsonResponse
+    {
+        try {
+            $params = request()->validate([
+                'member_id' => ['required', 'integer'],
+                'phone' => ['required', 'string'],
+                'code' => ['required', 'string'],
+                'visitor_id' => ['required', 'string', 'min:1', 'max:32'],
+            ]);
+            PhoneCodeService::verify($params['phone'], $params['code'], $params['member_id'], $params['visitor_id']);
+        } catch (ValidationException $e) {
+            return $this->error($e->validator->errors()->first());
+        } catch (Exception $e) {
+            return $this->error($e->getMessage(), [], $e->getCode());
+        }
+        return $this->success();
+    }
+
     //发送手机验证码
-    public function sendPhoneCode()
+    public function sendPhoneCode(): JsonResponse
     {
         try {
             $params = request()->validate([
@@ -36,14 +55,9 @@ class ActivityReward extends BaseController
             if (!Cache::has($key)) {
                 Cache::put($key, true, 60);
                 $code = SmsService::sendPhoneCode($params['phone']);
+                PhoneCodeService::add($params['phone'], $code);
             }
-
-
-
-
-
         } catch (ValidationException $e) {
-
             return $this->error($e->validator->errors()->first());
         } catch (Exception $e) {
             return $this->error($e->getMessage(), [], $e->getCode());
@@ -51,6 +65,8 @@ class ActivityReward extends BaseController
         return $this->success();
     }
 
+
+    //参与活动
     public function participate(): JsonResponse
     {
         DB::beginTransaction();

+ 0 - 34
app/Http/Controllers/api/Fingerprint.php

@@ -1,34 +0,0 @@
-<?php
-
-namespace App\Http\Controllers\api;
-
-use App\Constants\HttpStatus;
-use App\Services\PublicService;
-use Illuminate\Validation\ValidationException;
-use Exception;
-
-class Fingerprint extends BaseController
-{
-
-    public function index()
-    {
-        $memberId = request()->input('member_id');
-        return view('login');
-    }
-
-    public function setVisitorId()
-    {
-        try {
-            $params = request()->validate([
-                'member_id' => ['required', 'string', 'min:1'],
-                'visitor_id' => ['required', 'string', 'min:1', 'max:32'],
-            ]);
-            PublicService:: setVisitorId($params['member_id'], $params['visitor_id']);
-        } catch (ValidationException $e) {
-            return $this->error($e->validator->errors()->first());
-        } catch (Exception $e) {
-            return $this->error($e->getMessage());
-        }
-        return $this->success();
-    }
-}

+ 9 - 0
app/Models/PhoneCode.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Models;
+
+class PhoneCode extends BaseModel
+{
+    protected $table = 'phone_code';
+    protected $fillable = ['phone', 'code', 'ext', 'status'];
+}

+ 81 - 0
app/Services/PhoneCodeService.php

@@ -0,0 +1,81 @@
+<?php
+
+namespace App\Services;
+
+use App\Constants\HttpStatus;
+use App\Models\PhoneCode;
+use App\Models\User;
+use Exception;
+
+class PhoneCodeService extends BaseService
+{
+    public static string $MODEL = PhoneCode::class;
+
+    public static function getWhere(array $search = []): array
+    {
+        $where = [];
+        if (isset($search['phone']) && !empty($search['phone'])) {
+            $where[] = ['phone', '=', $search['phone']];
+        }
+
+        if (isset($search['code']) && !empty($search['code'])) {
+            $where[] = ['code', '=', $search['code']];
+        }
+        return $where;
+    }
+
+    public static function findOne(array $search = []): ?PhoneCode
+    {
+        return self::$MODEL::where(static::getWhere($search))->first();
+    }
+
+    /**
+     * @param $phone
+     * @param $code
+     * @param $memberId
+     * @param $visitorId
+     * @return void
+     * @throws Exception
+     */
+    public static function verify($phone, $code, $memberId, $visitorId): void
+    {
+        $search = [
+            'phone' => $phone,
+            'code' => $code,
+        ];
+        $phoneCode = static::$MODEL::where(static::getWhere($search))->first();
+        if (!$phoneCode) throw new Exception("验证码错误", HttpStatus::CUSTOM_ERROR);
+        $time = time();
+        if ($phoneCode->ext < $time) throw new Exception("验证码过期", HttpStatus::CUSTOM_ERROR);
+
+        $user = UserService::findOne(['member_id' => $memberId]);
+        if (!$user) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
+        $registerIp = request()->ip();
+        $user->register_ip = $registerIp;
+        $user->phone = $phone;
+        $user->visitor_id = $visitorId;
+        if (User::where('phone', $phone)->where('member_id', '!=', $memberId)->exists()) {
+            $user->status = 1;
+        }
+        if (User::where('register_ip', $registerIp)->where('member_id', '!=', $memberId)->exists()) {
+            $user->status = 1;
+        }
+
+        if (User::where('visitor_id', $visitorId)->where('member_id', '!=', $memberId)->exists()) {
+            $user->status = 1;
+        }
+        $user->save();
+    }
+
+    public static function add($phone, $code): PhoneCode
+    {
+        $time = time();
+        $ext = $time + 600;
+        $data = [
+            'phone' => $phone,
+            'code' => $code,
+            'ext' => $ext,
+        ];
+        return static::$MODEL::create($data);
+    }
+}

+ 36 - 0
database/migrations/2026_01_28_140034_create_phone_code.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration {
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('phone_code', function (Blueprint $table) {
+            $table->engine = 'InnoDB';
+            $table->collation = 'utf8mb4_general_ci';
+            $table->id();
+            $table->string('phone', 64)->comment('手机号');
+            $table->string('code', 16)->comment('验证码');
+            $table->integer('ext')->comment('过期时间');
+            $table->tinyInteger('status')->default(0)->comment('0未使用,1已使用');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('phone_code');
+    }
+};

+ 1 - 5
routes/api.php

@@ -22,11 +22,7 @@ Route::prefix('/ActivityReward')->group(function () {
     Route::get('/', [ActivityReward::class, 'index']);
     Route::post('/participate', [ActivityReward::class, 'participate']);
     Route::post('/sendPhoneCode', [ActivityReward::class, 'sendPhoneCode']);
-});
-
-
-Route::prefix('/fingerprint')->group(function () {
-    Route::post('/setVisitorId', [Fingerprint::class, 'setVisitorId']);
+    Route::post('/verifyPhone', [ActivityReward::class, 'verifyPhone']);
 });