SendCodeController.php 13 KB

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