Ver código fonte

feat(game): 优化手机号验证码逻辑并调整渠道配置缓存

- 移除重复的验证码查询逻辑,统一使用 updateOrInsert 方法
- 从 AccountsInfo 模型获取 Channel 信息替代 URL 包名解析
- 登录控制器中为已绑定手机号返回增加用户信息参数
- 注册逻辑失败时返回用户存在的详细数据
- 发送验证码类型固定为 2,忽略请求中的 Type 参数
- 启用 WebChannelConfig 的 Redis 缓存机制并延长过期时间至 300 秒
- 移除 SendCodeLogic 中旧的复用验证码逻辑
PPPPPPP 4 semanas atrás
pai
commit
2d94007cb5

+ 5 - 5
app/Game/Logics/SendCodeLogic.php

@@ -232,11 +232,11 @@ class SendCodeLogic extends BaseApiLogic
     {
 
         // 如果之前发送过验证码并且还没使用,还返回之前的验证码
-        $query = DB::connection('write')->table('QPTreasureDB.dbo.GamePhoneVerityCode')
-            ->where('PhoneNum', $phone)
-            ->first();
-
-        if ($query) return $query->Code;
+//        $query = DB::connection('write')->table('QPTreasureDB.dbo.GamePhoneVerityCode')
+//            ->where('PhoneNum', $phone)
+//            ->first();
+//
+//        if ($query) return $query->Code;
 
 
         $length = $this->config['lengthNum'] ?: 4;

+ 5 - 5
app/Game/WebChannelConfig.php

@@ -99,16 +99,16 @@ class WebChannelConfig extends Model
     {
         $cacheKey = self::$key . $channel;
 
-//        $cachedConfig = Redis::get($cacheKey);
+        $cachedConfig = Redis::get($cacheKey);
 
-//        if ($cachedConfig) {
-//            return new self(json_decode($cachedConfig,true));
-//        }
+        if ($cachedConfig) {
+            return new self(json_decode($cachedConfig,true));
+        }
 
         $config = self::where('Channel', $channel)->first();
 
         if ($config) {
-            Redis::setex($cacheKey, 30, $config->toJson());
+            Redis::setex($cacheKey, 300, $config->toJson());
         }
 
         return $config;

+ 2 - 2
app/Http/Controllers/Game/LoginController.php

@@ -138,7 +138,7 @@ class LoginController extends Controller
         if ($first) {
             SetNXLock::release($redisKey);
             Log::info('web.verify.already_bound',[$Phone,$PhoneCode]);
-            return apiReturnFail(['web.verify.already_bound', 'O número de telefone foi vinculado']); // 电话号码已绑定
+            return apiReturnFail(['web.verify.already_bound', 'O número de telefone foi vinculado'],GlobalUserInfo::getGameUserInfoToWeb('UserID',$first->UserID)); // 电话号码已绑定
         }
 
         if (!is_numeric($PhoneCode)) {
@@ -784,7 +784,7 @@ class LoginController extends Controller
 
                     return $isExist;
                 }
-                return apiReturnFail(['web.reg.fail_phone_exist', 'O número de telefone já existe, altere-o e tente se cadastrar novamente!']);
+                return apiReturnFail(['web.reg.fail_phone_exist', 'O número de telefone já existe, altere-o e tente se cadastrar novamente!'],GlobalUserInfo::toWebData($isExist));
             }
         }
         return $Phone;

+ 32 - 29
app/Http/Controllers/Game/SendCodeController.php

@@ -32,7 +32,7 @@ class SendCodeController
             return apiReturnFail(['web.user.phone_fail', 'Not correct phone number'], '', 422);
         }
 
-        $LocationCode = $request->country ?? env('COUNTRY_CODE',1);
+        $LocationCode = $request->country ?? env('COUNTRY_CODE',55);
 
         $user=$request->user();
         if($user){
@@ -40,7 +40,8 @@ class SendCodeController
             $Channel=$user->Channel;
         }
 
-        $Type = $request->Type ?: 0;    // 标识 1找回密码
+//        $Type = $request->Type ?: 0;    // 标识 1找回密码
+        $Type = 2;//无论是否存在都发code
         $ip = $request->ip();
 
 
@@ -69,32 +70,34 @@ class SendCodeController
             ->get();
 
         //默认
-        $SendCodeConfigType=3;
-//        if ($SendCodeConfig->count() > 1) {
-//
-//            $redis_key=$phone."_".$Channel;
-//            if (Redis::exists($redis_key)) {
-//                $currentCodeType = (int)Redis::get($redis_key);
-//                if($currentCodeType==100){
-//                    //两个通道都发了,不能再发了
-////                    return apiReturnFail(['web.verify.no_wrong_later','Your number is incorrect or temporarily unavailable, please try again later']);
-//                    $currentCodeType=1;
-//                }
-//                $SendCodeConfigType = $this->CodeType[$currentCodeType];
-////                Redis::set($redis_key, $this->CodeType[$currentCodeType]);
-//                Redis::set($redis_key, $SendCodeConfigType);
-//                Redis::expire($redis_key, 3600);
-//                Log::info('补发验证码通道:'.$SendCodeConfigType);
-//                Util::WriteLog('phone_error',$phone);
-//            } else {
-//                // 默认 codeType 1 天一泓短信
-//                // 默认 codeType 3 plants
-//                Redis::set($redis_key, $SendCodeConfigType);
-//                Redis::expire($redis_key, 3600);
-//            }
-//        } else {
-//            $SendCodeConfigType = (int)$SendCodeConfig->where('Status', 1)->first()->Type ?? 1;
-//        }
+        $SendCodeConfigType = rand(0,100)>50?3:1;
+
+        $SendCodeConfigType=1;
+        if ($SendCodeConfig->count() > 1) {
+
+            $redis_key=$phone."_".$Channel;
+            if (Redis::exists($redis_key)) {
+                $currentCodeType = (int)Redis::get($redis_key);
+                if($currentCodeType==100){
+                    //两个通道都发了,不能再发了
+//                    return apiReturnFail(['web.verify.no_wrong_later','Your number is incorrect or temporarily unavailable, please try again later']);
+                    $currentCodeType=1;
+                }
+                $SendCodeConfigType = $this->CodeType[$currentCodeType];
+//                Redis::set($redis_key, $this->CodeType[$currentCodeType]);
+                Redis::set($redis_key, $SendCodeConfigType);
+                Redis::expire($redis_key, 3600);
+                Log::info('补发验证码通道:'.$SendCodeConfigType);
+                Util::WriteLog('phone_error',$phone);
+            } else {
+                // 默认 codeType 1 天一泓短信
+                // 默认 codeType 3 plants
+                Redis::set($redis_key, $SendCodeConfigType);
+                Redis::expire($redis_key, 3600);
+            }
+        } else {
+            $SendCodeConfigType = (int)$SendCodeConfig->where('Status', 1)->first()->Type ?? 1;
+        }
 //        $SendCodeConfigType = 1;
 //        $redis->get(self::PHONE_SEND_LASTCONFIG_PREFIX . $phone);
         Log::info('发送验证码渠道:'.$SendCodeConfigType);
@@ -116,7 +119,7 @@ class SendCodeController
             'pt'=>'Olá, {{code}} (OTP) para sua verificação de celular',
             'es'=>'Hola, {{code}} (OTP) para tu verificación móvil ',
             'ru'=>'ривет, {{code}} (OTP) для проверки вашего мобильного телефона',
-//            'zh'=>'您好,{{code}} (OTP),您的手機驗證碼 24680com',
+            //            'zh'=>'您好,{{code}} (OTP),您的手機驗證碼 24680com',
         ];
         $Content = $lang['en'];
         $locale=GlobalUserInfo::getLocale();

+ 11 - 24
app/Models/GamePhoneVerityCode.php

@@ -4,6 +4,7 @@
 namespace App\Models;
 
 
+use App\Game\WebChannelConfig;
 use App\Util;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Facades\DB;
@@ -14,30 +15,16 @@ class GamePhoneVerityCode extends Model
     {
 
         $now = date('Y-m-d H:i:s');
-        $query = DB::connection('write')->table('QPTreasureDB.dbo.GamePhoneVerityCode')
-            ->where('PhoneNum',$PhoneNum)
-            ->first();
-
-
-        $pack=Util::getPackageByURL();
-        ///// 如果发送过验证码,直接返回,更新手机号和过期时间
-        if ($query) {
-            DB::connection('write')->table('QPTreasureDB.dbo.GamePhoneVerityCode')
-                ->where('PhoneNum',$PhoneNum)
-                ->update(['UserID'=>$UserID,'Code'=>$query->Code,'CreateDate'=>$now,'Channel'=>$pack['channel']]);
-
-        }else{
-            $data = [
-                'UserID' => $UserID,
-                'PhoneNum' => $PhoneNum,
-                'Code' => $Code,
-                'CreateDate' => $now,
-                'Ip' => $Ip,
-                'Channel'=>$pack['channel']
-            ];
-            DB::connection('write')->table('QPTreasureDB.dbo.GamePhoneVerityCode')
-                ->updateOrInsert(['PhoneNum'=>"$PhoneNum"],$data);
-        }
+        $data = [
+            'UserID' => $UserID,
+            'PhoneNum' => $PhoneNum,
+            'Code' => $Code,
+            'CreateDate' => $now,
+            'Ip' => $Ip,
+            'Channel'=>AccountsInfo::query()->where('UserID',$UserID)->value('Channel'),
+        ];
+        DB::connection('write')->table('QPTreasureDB.dbo.GamePhoneVerityCode')
+            ->updateOrInsert(['PhoneNum'=>"$PhoneNum"],$data);
 
         return true;
     }

+ 11 - 11
app/Notification/TelegramBot.php

@@ -16,16 +16,16 @@ class TelegramBot
     public $telegram;
     public function __construct(){
         try {
-            $telegram = new Telegram(self::api_key, self::username);
-
-            $mysql=['host' => env('DB_MYSQL_HOST', '127.0.0.1'),
-                'port' => env('DB_MYSQL_PORT', '3306'),
-                'database' => 'telegram',
-                'user' => env('DB_MYSQL_USERNAME'),
-                'password' => env('DB_MYSQL_PASSWORD'),  ];
-            $telegram->enableMySql($mysql);
-
-            $this->telegram=$telegram;
+//            $telegram = new Telegram(self::api_key, self::username);
+//
+//            $mysql=['host' => env('DB_MYSQL_HOST', '127.0.0.1'),
+//                'port' => env('DB_MYSQL_PORT', '3306'),
+//                'database' => 'telegram',
+//                'user' => env('DB_MYSQL_USERNAME'),
+//                'password' => env('DB_MYSQL_PASSWORD'),  ];
+//            $telegram->enableMySql($mysql);
+//
+//            $this->telegram=$telegram;
 ////            $result = $this->telegram->setWebhook("https://".$_SERVER["HTTP_HOST"]."/api/telehook");
 //            $allowed_updates = [
 //                Update::TYPE_MESSAGE,
@@ -40,7 +40,7 @@ class TelegramBot
 
 
         }catch (TelegramException $e){
-            Util::WriteLog('telegram','error:'.$e->getMessage());
+//            Util::WriteLog('telegram','error:'.$e->getMessage());
         }
     }
     public function handle(){

+ 1 - 1
config/app.php

@@ -66,7 +66,7 @@ return [
      */
 
 //    'timezone'        => 'PRC',
-    'timezone'        => env('APP_TIMEZONE', 'America/New_York'), # ÃÀ¹ú¶«²¿Ê±Çø (EST/EDT)
+    'timezone'        => env('APP_TIMEZONE', 'America/New_York'), #
 
     /*
     |--------------------------------------------------------------------------