SendCodeController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\dao\SendCode\WebRegister;
  4. use App\Facade\TableName;
  5. use App\Game\GlobalUserInfo;
  6. use App\Http\logic\api\SendCodeLogic;
  7. use App\Models\AccountsInfo;
  8. use App\Services\IpCheck;
  9. use App\Services\SendCode;
  10. use App\Util;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Redis;
  15. class SendCodeController
  16. {
  17. protected $CodeType = [1 => 3, 3 => 1];
  18. public function send2(Request $request){
  19. return $this->send($request);
  20. }
  21. // 天一宏+Kmi 短信发送
  22. public function send(Request $request)
  23. {
  24. $phone = $request->Phone ?: '';
  25. $UserID = $request->UserID ?: 0;
  26. $Type = $request->Type ?: 0; // 标识 1找回密码
  27. $ip = $request->ip();
  28. $language = GlobalUserInfo::getLocaleByUserID( $UserID,'en');
  29. \App::setLocale($language);
  30. try {
  31. $pack=$request->PackageName??"com.ouro777.newplay";
  32. $v=$request->v??1;
  33. $service = new IpCheck();
  34. if(!$service->ipCheck($ip,$pack,$v)){
  35. return apiReturnFail(__('messages.send_code.too_many_sms'));
  36. }
  37. }catch (\Exception $e){
  38. }
  39. $WebRegister = $request->WebRegister ?: 0;
  40. // 并发限制
  41. $res = Redis::setnx('SendCodeController' . $UserID, 1);
  42. if (!$res) {
  43. return apiReturnFail(__('messages.send_code.too_many_sms'));
  44. }
  45. Redis::expire('SendCodeController' . $UserID, 30);
  46. // 并发限制
  47. $res = Redis::setnx('SendCodeController' . $phone, 1);
  48. if (!$res) {
  49. return apiReturnFail(__('messages.send_code.too_many_sms'));
  50. }
  51. Redis::expire('SendCodeController' . $phone, 30);
  52. Log::info('接收客户端发送验证码手机号:'.$phone.' Type类型:'.$Type.' 用户ID:'.$UserID.' WebRegister:'.$WebRegister);
  53. $SendCodeConfig = DB::connection('read')->table('QPAccountsDB.dbo.SysSmsConfig')
  54. ->where('Status', 1)
  55. ->get();
  56. //默认
  57. $SendCodeConfigType = rand(0,100)>50?3:1;
  58. $user=AccountsInfo::where("UserID",$UserID)->select('Channel','BindCountry')->first();
  59. if(!$user){
  60. return apiReturnFail(['web.error.user_not_exist','Wrong UserID!']);
  61. }
  62. $Channel=$user->Channel;
  63. // $isMultiCountry=env('MULTI_COUNTRY',0)==1;
  64. //给电话号码加区号
  65. $CountryCode=env('COUNTRY_CODE','52');
  66. if(strstr($CountryCode,','))$CountryCode=IpCheck::$countries[$user->BindCountry]['phone_code']??env('COUNTRY_CODE','52');
  67. Log::info($CountryCode.'|'.$user->BindCountry);
  68. //用了配置的多区域区号带逗号来分割
  69. if(strlen($CountryCode)>3)$CountryCode=explode(',',$CountryCode)[0];
  70. //巴基斯坦判断
  71. if($user->BindCountry=='PK'||$CountryCode==92){
  72. // 移除所有非数字字符
  73. $phone = preg_replace('/[^0-9]/', '', $phone);
  74. // 检查是否以92开头(国际格式)
  75. if (strpos($phone, '92') === 0) {
  76. $phone = substr($phone, 2);
  77. }
  78. // 检查是否以0开头(本地格式)
  79. if (strpos($phone, '0') === 0) {
  80. $phone = substr($phone, 1);
  81. }
  82. // 检查号码是否为10位且以3开头
  83. if (strlen($phone) === 10 && strpos($phone, '3') === 0) {
  84. }else{
  85. return apiReturnFail(__('messages.send_code.invalid_phone_number'));
  86. }
  87. }
  88. //巴基斯坦判断
  89. if($user->BindCountry=='BD'||$CountryCode==880){
  90. // 移除所有非数字字符
  91. $phone = preg_replace('/[^0-9]/', '', $phone);
  92. // 检查是否以92开头(国际格式)
  93. if (strpos($phone, '880') === 0) {
  94. $phone = substr($phone, 3);
  95. }
  96. // 检查是否以0开头(本地格式)
  97. if (strpos($phone, '01') === 0) {
  98. $phone = substr($phone, 1);
  99. }
  100. // 检查号码是否为10位且以3开头
  101. if (strlen($phone) == 10 && strpos($phone, '1') === 0) {
  102. Log::info('now:'.$phone);
  103. }else{
  104. return apiReturnFail(__('messages.send_code.invalid_phone_number'));
  105. }
  106. }
  107. //检查电话长度
  108. $lenSize=IpCheck::$countries[$user->BindCountry]['phone_length']??env("PHONE_LENGTH","7,10");
  109. $lenSize=explode(",",$lenSize);
  110. $len=strlen($phone);
  111. if ($len <intval($lenSize[0]) || $len > intval($lenSize[1]??$lenSize[0])) {
  112. Log::info('手机长度问题 ' . $len);
  113. return apiReturnFail(__('messages.send_code.len_fail', ['len' => $len]));
  114. }
  115. //电话补齐
  116. $phone=$CountryCode.$phone;
  117. Log::info('now:'.$phone);
  118. if ($SendCodeConfig->count() > 1) {
  119. if (Redis::exists($phone."_".$Channel)) {
  120. $currentCodeType = (int)Redis::get($phone."_".$Channel);
  121. if($currentCodeType==100){
  122. //两个通道都发了,不能再发了
  123. return apiReturnFail(__('messages.send_code.invalid_phone_number'));
  124. }
  125. $SendCodeConfigType = $this->CodeType[$currentCodeType];
  126. // Redis::set($phone."_".$Channel, $this->CodeType[$currentCodeType]);
  127. Redis::set($phone."_".$Channel, 100);
  128. Redis::expire($phone."_".$Channel, 3600);
  129. Log::info('补发验证码通道:'.$SendCodeConfigType);
  130. Util::WriteLog('phone_error',$phone);
  131. } else {
  132. // 默认 codeType 1 天一泓短信
  133. // 默认 codeType 3 plants
  134. Redis::set($phone."_".$Channel, $SendCodeConfigType);
  135. Redis::expire($phone."_".$Channel, 3600);
  136. }
  137. } else {
  138. $SendCodeConfigType = (int)$SendCodeConfig->where('Status', 1)->first()->Type ?? 1;
  139. }
  140. Log::info('发送验证码渠道:'.$SendCodeConfigType);
  141. $logic = new SendCodeLogic();
  142. if ($logic->sendVerify(trim($phone), $ip, $Type) === false) {
  143. return apiReturnFail($logic->getError());
  144. }
  145. Log::info('验证手机号通过'.$phone);
  146. $lang = [
  147. // European Languages
  148. 'en' => 'Hello, {{code}} (OTP) for your mobile verification', // English
  149. 'pt' => 'Olá, {{code}} (OTP) para sua verificação de celular', // Portuguese
  150. 'es' => 'Hola, {{code}} (OTP) para tu verificación móvil', // Spanish
  151. 'de' => 'Hallo, {{code}} (OTP) für Ihre mobile Verifizierung', // German
  152. 'fr' => 'Bonjour, {{code}} (OTP) pour votre vérification mobile', // French
  153. 'it' => 'Ciao, {{code}} (OTP) per la tua verifica mobile', // Italian
  154. 'nl' => 'Hallo, {{code}} (OTP) voor uw mobiele verificatie', // Dutch
  155. 'sv' => 'Hej, {{code}} (OTP) för din mobilverifiering', // Swedish
  156. 'no' => 'Hei, {{code}} (OTP) for din mobilbekreftelse', // Norwegian
  157. 'fi' => 'Hei, {{code}} (OTP) mobiilitodennukseesi', // Finnish
  158. 'da' => 'Hej, {{code}} (OTP) til din mobile verifikation', // Danish
  159. 'pl' => 'Cześć, {{code}} (OTP) do weryfikacji twojego telefonu komórkowego', // Polish
  160. 'cs' => 'Ahoj, {{code}} (OTP) pro ověření vašeho mobilního zařízení', // Czech
  161. 'hu' => 'Szia, {{code}} (OTP) a mobil ellenőrzéséhez', // Hungarian
  162. 'ro' => 'Salut, {{code}} (OTP) pentru verificarea mobilului tău', // Romanian
  163. 'tr' => 'Merhaba, mobil doğrulamanız için {{code}} (OTP)', // Turkish
  164. 'el' => 'Γειά, {{code}} (OTP) για την επιβεβαίωση του κινητού σας', // Greek
  165. 'uk' => 'Привіт, {{code}} (OTP) для підтвердження вашого мобільного телефону', // Ukrainian
  166. // Asian Languages
  167. 'zh' => '您好,您的手机验证验证码是 {{code}} (OTP)', // Chinese (Simplified)
  168. 'ja' => 'こんにちは、{{code}} (OTP) はあなたの携帯認証用です', // Japanese
  169. 'ko' => '안녕하세요, 모바일 인증을 위한 {{code}} (OTP)입니다', // Korean
  170. 'ar' => 'مرحباً، {{code}} (OTP) للتحقق من هاتفك المحمول', // Arabic
  171. 'hi' => 'नमस्ते, आपके मोबाइल सत्यापन के लिए {{code}} (OTP)', // Hindi
  172. 'bn' => 'হ্যালো, আপনার মোবাইল যাচাইয়ের জন্য {{code}} (OTP)', // Bengali
  173. 'th' => 'สวัสดี, รหัส {{code}} (OTP) สำหรับการยืนยันมือถือของคุณ', // Thai
  174. 'vi' => 'Xin chào, {{code}} (OTP) cho việc xác minh điện thoại di động của bạn', // Vietnamese
  175. 'id' => 'Halo, {{code}} (OTP) untuk verifikasi ponsel Anda', // Indonesian
  176. 'ms' => 'Halo, {{code}} (OTP) untuk pengesahan telefon bimbit anda', // Malay
  177. 'tl' => 'Kumusta, ang {{code}} (OTP) para sa iyong mobile na beripikasyon', // Tagalog
  178. 'fa' => 'سلام، {{code}} (OTP) برای تأیید موبایل شما', // Persian
  179. 'ur' => 'ہیلو، آپ کے موبائل تصدیق کے لیے {{code}} (OTP)', // Urdu
  180. 'ta' => 'வணக்கம், உங்கள் மொபைல் சரிபார்ப்புக்கு {{code}} (OTP)', // Tamil
  181. 'te' => '{{code}} (OTP) మీ మొబైల్ ధృవీకరణ కోసం', // Telugu
  182. 'mr' => 'नमस्कार, आपल्या मोबाइल प्रमाणिकरणासाठी {{code}} (OTP)', // Marathi
  183. 'gu' => 'હેલો, તમારા મોબાઈલ વેરિફિકેશન માટે {{code}} (OTP)', // Gujarati
  184. 'zh-tw' => '您好,您的手機驗證碼是 {{code}} (OTP)', // Chinese (Traditional)
  185. ];
  186. $Content = $lang['en'];
  187. $locale=GlobalUserInfo::getLocale();
  188. if($locale&&isset($lang[$locale])){
  189. $Content=$lang[$locale];
  190. }
  191. // $Content = 'Hola, Código {{code}} para su verificación móvil Slotomania';
  192. // $Services = new SendCode();
  193. // $res = $Services->sendCode($SendCodeConfigType)->send($phone, $ip, $UserID, $Content);
  194. $logic = new SendCodeLogic();
  195. $res=false;
  196. if($SendCodeConfigType==3){
  197. if ($logic->send($phone, $ip, $UserID,$Content) === false) {
  198. $res= $logic->getError();
  199. return apiReturnFail($res);
  200. }
  201. }else{
  202. if ($logic->plants_send($phone, $ip, $UserID,$Content) === false) {
  203. $res= $logic->getError();
  204. return apiReturnFail($res);
  205. }
  206. }
  207. return apiReturnSuc();
  208. }
  209. public function notify(Request $request)
  210. {
  211. $sendResult = $request->sendResult ?: '';
  212. $to = $request->to ?: '';
  213. if ($sendResult == 'SUCCESS') {
  214. Log::info('发送验证码回调:发送成功' . $to);
  215. }
  216. Log::info('发送验证码回调:发送失败' . $to);
  217. return 'SUCCESS';
  218. }
  219. // 验证码
  220. public function send_verify(Request $request)
  221. {
  222. $phone = $request->phoneNum ?: '';
  223. $Type = $request->Type ?: 0; // 标识 1找回密码
  224. $ip = $request->ip();
  225. $logic = new SendCodeLogic();
  226. if ($logic->sendVerify(trim($phone), $ip, $Type) === false) {
  227. $res=$logic->getError();
  228. if($res){
  229. if(is_array($res)){
  230. if(__($res[0])!=$res[0]){
  231. return apiReturnFail(__($res[0]),$res);
  232. }
  233. return apiReturnFail($res[1],$res);
  234. }
  235. return apiReturnFail($res);
  236. }
  237. return apiReturnFail($logic->getError());
  238. }
  239. return apiReturnSuc();
  240. }
  241. public function getCode(Request $request){
  242. $phone = $request->PhoneNum;
  243. $query = DB::table(TableName::QPTreasureDB() . 'GamePhoneVerityCode')
  244. ->where('PhoneNum', $phone)
  245. ->select('PhoneNum', 'Code')
  246. ->first();
  247. if($request->formart){
  248. return apiReturnSuc(['code' => $query?$query->Code:0]);
  249. }
  250. return $query?$query->Code:0;
  251. }
  252. }