Ken vor 1 Woche
Ursprung
Commit
c09c5f5bde

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

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

+ 3 - 25
app/Http/Controllers/api/TelegramWebHook.php

@@ -72,8 +72,8 @@ class TelegramWebHook extends BaseController
                     }
 
                     //用户注册和初始化用户钱包
-                    PublicService::index($chatId, $username, $firstName);
-
+                    $user = PublicService::index($chatId, $username, $firstName);
+                    App::setLocale($user->language);
 
                     PublicService::init($telegram, $data, $chatId, $firstName, $messageId);
                     WalletService::init($telegram, $data, $chatId, $firstName, $messageId, $callbackId);
@@ -225,36 +225,14 @@ class TelegramWebHook extends BaseController
         } //用户发送了消息
         else if (isset($message['text'])) {
             $text = $message['text'];
-
-            $user = User::where('member_id', $chatId)->first();
-            if (!$user) {
-                $user = new User();
-                $user->member_id = $chatId;
-            }
-            if (empty($user->register_ip)) {
-                $registerIp = request()->ip();
-                if (User::where('register_ip', $registerIp)->exists()) {
-                    User::where('register_ip', $registerIp)->update(['status' => 1]);
-                    $user->status = 1;
-                }
-                $user->register_ip = $registerIp;
-            }
-
-            $user->first_name = $message['chat']['first_name'];
-            if (isset($message['chat']['username'])) {
-                $user->username = $message['chat']['username'];
-            }
-            $user->save();
+            $user = PublicSerVICE::index($chatId, $message['chat']['username'], $message['chat']['first_name']);
             App::setLocale($user->language);
             if ($message['chat']['type'] === 'private') {
-
                 // 校验开始菜单事件
                 $returnMsg = KeyboardService::checkStart($chatId, $text);
                 if ($returnMsg) {
                     return $returnMsg;
                 }
-
-
                 switch ($text) {
                     case "/start":
                         Util::delCache($chatId);

+ 28 - 3
app/Services/PublicService.php

@@ -2,7 +2,9 @@
 
 namespace App\Services;
 
+use App\Constants\HttpStatus;
 use App\Models\User;
+use Exception;
 use Illuminate\Support\Facades\App;
 
 class PublicService extends BaseService
@@ -16,8 +18,31 @@ class PublicService extends BaseService
         }
     }
 
+    public static function setVisitorId($memberId, $visitorId)
+    {
+        $user = User::where('member_id', $memberId)->first();
+        if (!$user) throw new Exception('验证失败', HttpStatus::CUSTOM_ERROR);
+        if (empty($user->visitor_id)) {
+            if (User::where('visitor_id', $visitorId)->exists()) {
+                User::where('visitor_id', $visitorId)->update(['status' => 1]);
+                $user->status = 1;
+            }
+            $user->visitor_id = $visitorId;
+        }
+        if (empty($user->register_ip)) {
+            $registerIp = request()->ip();
+            if (User::where('register_ip', $registerIp)->exists()) {
+                User::where('register_ip', $registerIp)->update(['status' => 1]);
+                $user->status = 1;
+            }
+            $user->register_ip = $registerIp;
+        }
+
+
+        $user->save();
+    }
 
-    public static function index($chatId, $username, $firstName)
+    public static function index($chatId, $username, $firstName): User
     {
         $user = User::where('member_id', $chatId)->first();
         if (!$user) {
@@ -35,8 +60,8 @@ class PublicService extends BaseService
         if ($username) $user->username = $username;
         $user->first_name = $firstName;
         $user->save();
-        App::setLocale($user->language);
-        //给每个用户生成一个专属的USDT钱包
+        //给每个用户生成一个专属的钱包
         WalletService::getUserWallet($chatId);
+        return $user;
     }
 }

+ 1 - 1
app/Services/WalletService.php

@@ -219,7 +219,7 @@ class WalletService extends BaseService
      * @param {int} $memberId
      * @return {*}
      */
-    public static function getUserWallet(int $memberId)
+    public static function getUserWallet( $memberId)
     {
         $wallets = self::findAll(['member_id' => $memberId]);
 

+ 30 - 0
database/migrations/2026_01_20_144507_update_users.php

@@ -0,0 +1,30 @@
+<?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::table('users', function (Blueprint $table) {
+            $table->string('visitor_id',150)->default('')->comment('访客ID');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        //
+    }
+};