SendCodeController.php 6.9 KB

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