SendCodeController.php 7.1 KB

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