SendCodeController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Facade\TableName;
  4. use App\Game\GlobalUserInfo;
  5. use App\Game\Logics\SendCodeLogic;
  6. use App\Util;
  7. use App\Utility\SetNXLock;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Redis;
  12. class SendCodeController
  13. {
  14. protected $CodeType = [1 => 3, 3 => 1];
  15. // 天一宏+Kmi 短信发送
  16. public function send(Request $request)
  17. {
  18. $FPID=$request->input("bfp","");
  19. $Channel=$request->input("c",99);
  20. $UserID=0;
  21. $phone = $request->Phone ?: '';
  22. if (!is_numeric($phone)) {
  23. return apiReturnFail(['web.user.phone_fail', 'Not correct phone number'], '', 422);
  24. }
  25. $LocationCode = $request->country ?? env('COUNTRY_CODE',55);
  26. $user=$request->user();
  27. if($user){
  28. $UserID = $user->UserID;
  29. $Channel=$user->Channel;
  30. }
  31. $Type = $request->Type ?: 0; // 标识 1找回密码
  32. $ip = $request->ip();
  33. $WebRegister = $request->WebRegister ?: 0;
  34. // 并发限制
  35. $res = Redis::setnx('SendCodeController' . $FPID, 1);
  36. if (!$res) {
  37. return apiReturnFail(['web.verify.sms_too_many','Você enviou muitas mensagens de texto e foi impedido de enviar']);
  38. }
  39. Redis::expire('SendCodeController' . $FPID, 30);
  40. // 并发限制
  41. // $res = Redis::setnx('SendCodeController' . $phone, 1);
  42. // if (!$res) {
  43. // return apiReturnFail(['web.verify.sms_too_many','Você enviou muitas mensagens de texto e foi impedido de enviar']);
  44. // }
  45. // Redis::expire('SendCodeController' . $phone, 30);
  46. $phone=$LocationCode.$phone;
  47. Log::info('接收客户端发送验证码手机号:'.$phone.' Type类型:'.$Type.' 用户ID:'.$UserID.' WebRegister:'.$WebRegister);
  48. $SendCodeConfig = DB::connection('read')->table('QPAccountsDB.dbo.SysSmsConfig')
  49. ->where('Status', 1)
  50. ->get();
  51. //默认
  52. $SendCodeConfigType = rand(0,100)>50?3:1;
  53. $SendCodeConfigType=1;
  54. if ($SendCodeConfig->count() > 1) {
  55. $redis_key=$phone."_".$Channel;
  56. if (Redis::exists($redis_key)) {
  57. $currentCodeType = (int)Redis::get($redis_key);
  58. if($currentCodeType==100){
  59. //两个通道都发了,不能再发了
  60. // return apiReturnFail(['web.verify.no_wrong_later','Your number is incorrect or temporarily unavailable, please try again later']);
  61. $currentCodeType=1;
  62. }
  63. $SendCodeConfigType = $this->CodeType[$currentCodeType];
  64. // Redis::set($redis_key, $this->CodeType[$currentCodeType]);
  65. Redis::set($redis_key, $SendCodeConfigType);
  66. Redis::expire($redis_key, 3600);
  67. Log::info('补发验证码通道:'.$SendCodeConfigType);
  68. Util::WriteLog('phone_error',$phone);
  69. } else {
  70. // 默认 codeType 1 天一泓短信
  71. // 默认 codeType 3 plants
  72. Redis::set($redis_key, $SendCodeConfigType);
  73. Redis::expire($redis_key, 3600);
  74. }
  75. } else {
  76. $SendCodeConfigType = (int)$SendCodeConfig->where('Status', 1)->first()->Type ?? 1;
  77. }
  78. // $SendCodeConfigType = 1;
  79. // $redis->get(self::PHONE_SEND_LASTCONFIG_PREFIX . $phone);
  80. Log::info('发送验证码渠道:'.$SendCodeConfigType);
  81. $logic = new SendCodeLogic();
  82. if ($logic->sendVerify(trim($phone), $ip, $Type) === false) {
  83. return apiReturnFail($logic->getError());
  84. }
  85. Log::info('验证手机号通过'.$phone);
  86. // 网页注册限制注册IP
  87. // if ($WebRegister == 1) {
  88. // $webRegisterIPLimit = (new WebRegister())->ipLimit($ip);
  89. // if (!$webRegisterIPLimit) {
  90. // return apiReturnFail('Seu dispositivo foi registrado muitas vezes e o registro é proibido');
  91. // }
  92. // }
  93. $lang=[
  94. 'en'=>'Hello, {{code}} (OTP) for your mobile verification',
  95. 'pt'=>'Olá, {{code}} (OTP) para sua verificação de celular',
  96. 'es'=>'Hola, {{code}} (OTP) para tu verificación móvil ',
  97. 'ru'=>'ривет, {{code}} (OTP) для проверки вашего мобильного телефона',
  98. // 'zh'=>'您好,{{code}} (OTP),您的手機驗證碼 24680com',
  99. ];
  100. $Content = $lang['en'];
  101. $locale=GlobalUserInfo::getLocale();
  102. if($locale&&isset($lang[$locale])){
  103. $Content=$lang[$locale];
  104. }
  105. // $Content = 'Hola, Código {{code}} para su verificación móvil Slotomania';
  106. // $Services = new SendCode();
  107. $res=apiReturnSuc();
  108. if($SendCodeConfigType==3){
  109. if ($logic->send($phone, $ip, $UserID,$Content) === false) {
  110. $res= apiReturnFail($logic->getError());
  111. }
  112. }else{
  113. if ($logic->plants_send($phone, $ip, $UserID,$Content) === false) {
  114. $res= apiReturnFail($logic->getError());
  115. }
  116. }
  117. // $res = $Services->sendCode($SendCodeConfigType)->send($phone, $ip, $UserID, $Content);
  118. $query = DB::table(TableName::QPTreasureDB() . 'GamePhoneVerityCode')
  119. ->where('PhoneNum', $phone)
  120. ->select('PhoneNum', 'Code')
  121. ->first();
  122. $res['data']['cccc']=$query->Code??"";
  123. $failmsg='Você enviou muitas mensagens de texto e foi impedido de enviar';
  124. if($res['msg']==$failmsg){
  125. return apiReturnFail(['web.verify.sms_too_many',$failmsg]);
  126. }
  127. return $res;
  128. }
  129. public function notify(Request $request)
  130. {
  131. $sendResult = $request->sendResult ?: '';
  132. $to = $request->to ?: '';
  133. if ($sendResult == 'SUCCESS') {
  134. Log::info('发送验证码回调:发送成功' . $to);
  135. }
  136. Log::info('发送验证码回调:发送失败' . $to);
  137. return 'SUCCESS';
  138. }
  139. // 验证码
  140. public function send_verify(Request $request)
  141. {
  142. $phone = $request->phoneNum ?: '';
  143. $Type = $request->Type ?: 0; // 标识 1找回密码
  144. $ip = $request->ip();
  145. $logic = new SendCodeLogic();
  146. if ($logic->sendVerify(trim($phone), $ip, $Type) === false) {
  147. return apiReturnFail($logic->getError());
  148. }
  149. return apiReturnSuc();
  150. }
  151. public function getCode(Request $request){
  152. $phone = $request->PhoneNum;
  153. $query = DB::table(TableName::QPTreasureDB() . 'GamePhoneVerityCode')
  154. ->where('PhoneNum', $phone)
  155. ->select('PhoneNum', 'Code')
  156. ->first();
  157. if($request->formart){
  158. return apiReturnSuc(['code' => $query?$query->Code:0]);
  159. }
  160. return $query?$query->Code:0;
  161. }
  162. }