SendCodeLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace App\Game\Logics;
  3. use App\Game\Services\RouteService;
  4. use App\Http\helper\HttpCurl;
  5. use App\Http\logic\api\BaseApiLogic;
  6. use App\Models\GamePhoneVerityCode;
  7. use App\Util;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Redis;
  11. class SendCodeLogic extends BaseApiLogic
  12. {
  13. const IP_DAILY_LIMIT_PREFIX = 'ip_daily_limit_';
  14. const PHONE_DAILY_LIMIT_PREFIX = 'phone_daily_limit_';
  15. /** @var string 每分钟频率限制key前缀 */
  16. const PHONE_FREQUENT_LIMIT_PREFIX = 'phone_frequent_limit_';
  17. const PHONE_SEND_LASTCONFIG_PREFIX = 'phone_send_last_';
  18. const FREQUENT_IP_LIMIT_NUM = 20;
  19. const FREQUENT_PHONE_LIMIT_NUM = 10;
  20. protected $config; // 天一宏 -- 不卡
  21. protected $kmiConfig; // kmi
  22. protected $zrConfig; // ZhangRong
  23. protected $plantsConfig; // ZhangRong
  24. public function __construct()
  25. {
  26. // 天一宏 -- 不卡
  27. $this->config = config('InterfaceAPI.short_message');
  28. $this->kmiConfig = config('InterfaceAPI.kmi_message');
  29. $this->zrConfig = config('InterfaceAPI.zr_message');
  30. $this->plantsConfig = config('InterfaceAPI.plant_message');
  31. }
  32. public function send($phone, $ip, $UserID, $Content)
  33. {
  34. $model = new GamePhoneVerityCode();
  35. // 生成验证码
  36. $code = $this->code($phone);
  37. $content = urlencode(str_replace('{code}', $code, $Content));
  38. $appKey = $this->config['appKey'];
  39. $password = $this->config['key'];
  40. $time = time();
  41. $sign = md5($appKey . $password . $time);
  42. $data = [
  43. 'appId' => $this->config['appId'],
  44. 'numbers' => $phone,
  45. 'content' => $content,
  46. 'senderId' => $code,
  47. ];
  48. $model->addCode($UserID, $code, $phone, $ip);
  49. $build = http_build_query($data);
  50. Log::info('buka发送验证码参数 ' . $build);
  51. $gateway = $this->config['gateway'] . '?' . $build;
  52. $header = ['Sign' => $sign, 'Timestamp' =>$time, 'Api-Key' => $appKey];
  53. Log::info('bukaheader-签名 ' . json_encode($header)."###".$appKey .'-'. $password .'-'. $time);
  54. $HttpCurl = new HttpCurl();
  55. $res = $HttpCurl->curl_get($gateway, $header);
  56. Log::info('buka发送验证码结果:' . $res);
  57. try {
  58. $data = \GuzzleHttp\json_decode($res, true);
  59. }catch (\Exception $e){
  60. $data=[];
  61. }
  62. if (isset($data['status']) && $data['status'] == 0){
  63. $this->setFrequent($ip, $phone,1);
  64. return true;
  65. }
  66. $this->error = ['web.verify.send_error','Falha ao obter o código de verificação'];
  67. return false;
  68. }
  69. public function zr_send($phone, $ip, $UserID, $Content)
  70. {
  71. $model = new GamePhoneVerityCode();
  72. // 生成验证码
  73. $code = $this->code($phone);
  74. $content = urlencode(str_replace('{code}', $code, $Content));
  75. $accesskey = $this->zrConfig['accesskey'];
  76. $secret = $this->zrConfig['secret'];
  77. $time = time();
  78. $data = [
  79. 'accesskey' => $accesskey,
  80. 'secret' => $secret,
  81. 'mobile' => $phone,
  82. 'content' => $content,
  83. ];
  84. $model->addCode($UserID, $code, $phone, $ip);
  85. $build = http_build_query($data);
  86. Log::info('zr发送验证码参数 ' . $build);
  87. $gateway = $this->zrConfig['gateway'] ;
  88. // $header = ['sign' => $sign, 'Timestamp' =>$time, 'Api-Key' => $appKey];
  89. // Log::info('header-签名 ' . json_encode($header)."###".$appKey .'-'. $password .'-'. $time);
  90. $HttpCurl = new HttpCurl();
  91. $res = $HttpCurl->curlPost($gateway, $build);
  92. Log::info('zr发送验证码结果:' . $res);
  93. $data = \GuzzleHttp\json_decode($res, true);
  94. if (isset($data['status']) && $data['status'] == 0){
  95. $this->setFrequent($ip, $phone,2);
  96. return true;
  97. }
  98. $this->error = ['web.verify.send_error','Falha ao obter o código de verificação'];
  99. return false;
  100. }
  101. public function plants_send($phone, $ip, $UserID, $Content)
  102. {
  103. $model = new GamePhoneVerityCode();
  104. // 生成验证码
  105. $code = $this->code($phone);
  106. $content = urlencode(str_replace('{code}', $code, $Content));
  107. $accesskey = $this->plantsConfig['appKey'];
  108. $secret = $this->plantsConfig['secretKey'];
  109. $time = time();
  110. $data = [
  111. 'appkey' => $accesskey,
  112. 'secretkey' => $secret,
  113. 'phone' => $phone,
  114. 'content' => $content,
  115. ];
  116. $model->addCode($UserID, $code, $phone, $ip);
  117. $build = http_build_query($data);
  118. Log::info('plants发送验证码参数 ' . $build);
  119. $gateway = $this->plantsConfig['gateway'] ;
  120. // $header = ['sign' => $sign, 'Timestamp' =>$time, 'Api-Key' => $appKey];
  121. // Log::info('header-签名 ' . json_encode($header)."###".$appKey .'-'. $password .'-'. $time);
  122. $HttpCurl = new HttpCurl();
  123. $res = $HttpCurl->curlPost($gateway, $build);
  124. Log::info('plants发送验证码结果:' . $res);
  125. try {
  126. $data = \GuzzleHttp\json_decode(trim($res), true);
  127. if (isset($data['code']) && $data['code'] == 0) {
  128. $this->setFrequent($ip, $phone, 3);
  129. return true;
  130. }
  131. }catch (\Exception $exception){
  132. if (strstr($res,'"code":"0"')) {
  133. $this->setFrequent($ip, $phone, 3);
  134. return true;
  135. }
  136. }
  137. $this->error = ['web.verify.send_error','Falha ao obter o código de verificação'];
  138. return false;
  139. }
  140. public function kmiSend($phone, $ip, $UserID, $Content)
  141. {
  142. $model = new GamePhoneVerityCode();
  143. $code = $this->code($phone);
  144. $data = [
  145. 'accessKey' => $this->kmiConfig['access_key'],
  146. 'secretKey' => $this->kmiConfig['secret_key'],
  147. //'from' => $content->TemplateID ?? '',
  148. 'to' => '0055' . $phone,
  149. 'message' => str_replace('{code}', $code, $Content),
  150. 'callbackUrl' => $this->kmiConfig['callbackUrl']
  151. ];
  152. Log::info('发送验证码参数 ' . \GuzzleHttp\json_encode($data));
  153. $model->addCode($UserID, $code, $phone, $ip);
  154. $gateway = $this->kmiConfig['gateway'];
  155. $curl = new HttpCurl();
  156. $res = $curl->curlPost($gateway, $data, 'json',true);
  157. Log::info('发送验证码结果:' . $res);
  158. $data = \GuzzleHttp\json_decode($res, true);
  159. if (isset($data['code']) && $data['code'] == 200) {
  160. $this->setFrequent($ip, $phone,4);
  161. return true;
  162. }
  163. $this->error = 'No se pudo obtener el código de verificación';
  164. return false;
  165. }
  166. // 生成验证码
  167. public function code($phone)
  168. {
  169. // 如果之前发送过验证码并且还没使用,还返回之前的验证码
  170. // $query = DB::connection('write')->table('QPTreasureDB.dbo.GamePhoneVerityCode')
  171. // ->where('PhoneNum', $phone)
  172. // ->first();
  173. //
  174. // if ($query) return $query->Code;
  175. $length = $this->config['lengthNum'] ?: 4;
  176. $characters = '0123456789';
  177. if (!is_int($length) || $length < 0) {
  178. return false;
  179. }
  180. $characters_length = strlen($characters) - 1;//因为字符串的顺序是从0开始的,下面的mt_rand()从0开始取,所以最后一个字符串要取到就只能减一
  181. $string = '';
  182. for ($i = $length; $i > 0; $i--) {
  183. $string .= $characters[mt_rand(0, $characters_length)];
  184. }
  185. if ($string[0] == 0) $string[0] = mt_rand(1, 9);
  186. return $string;
  187. }
  188. // 验证码
  189. public function sendVerify($phone, $ip, $Type, $Channel = 0)
  190. {
  191. $len = strlen($phone);
  192. // 验证手机号格式
  193. if (str_starts_with($phone, '1')) {
  194. $len -= 1;
  195. if ($len < 9 || $len > 11) {
  196. Log::info('手机长度问题 ' . $len);
  197. $this->error = ['web.verify.phone_length_invalid', 'Sorry, mobile numbers with ' . $len . ' digits are not supported'];
  198. return false;
  199. }
  200. }
  201. $config=RouteService::getChannelConfig();
  202. $first = DB::connection('write')->table('QPAccountsDB.dbo.AccountPhone')
  203. ->where('PhoneNum', $phone)
  204. ->where('RegionID', $config->isRegionUnique())
  205. ->first();
  206. // 注册发送验证码 -- 已经绑定过不允许发送
  207. if ($Type == 0) {
  208. if ($first) {
  209. $this->error = ['web.verify.phone_already_bound', 'The phone number has already been bound'];
  210. return false;
  211. }
  212. } elseif ($Type == 1) { // 修改密码发送验证码 -- 判断手机号是否已经绑定
  213. if (!$first) {
  214. $this->error = ['web.verify.account_not_exist', 'Failed to send code, account does not exist'];
  215. return false;
  216. }
  217. }
  218. $redis = Redis::connection();
  219. // 限制IP一天发10次
  220. if ($redis->get(self::IP_DAILY_LIMIT_PREFIX . $ip) >= self::FREQUENT_IP_LIMIT_NUM) {
  221. $this->error = ['web.verify.too_many_ip_requests', 'You have sent too many messages from this IP, sending is blocked'];
  222. return false;
  223. }
  224. // 同一个手机号发送次数不能超过5次
  225. if ($redis->get(self::PHONE_DAILY_LIMIT_PREFIX . $phone) >= self::FREQUENT_PHONE_LIMIT_NUM) {
  226. $this->error = ['web.verify.too_many_phone_requests', 'You have sent too many messages from this phone number, sending is blocked'];
  227. return false;
  228. }
  229. // 发送间隔时间
  230. if ($redis->exists(self::PHONE_FREQUENT_LIMIT_PREFIX . $phone . '_' . $Type)) {
  231. $this->error = ['web.verify.too_frequent_requests', 'You are sending too fast, please try again later'];
  232. return false;
  233. }
  234. return apiReturnSuc();
  235. }
  236. /**
  237. * 设置频率
  238. * @param string $ip
  239. * @param string $phone
  240. */
  241. public function setFrequent($ip, $phone,$SendCodeConfigType)
  242. {
  243. Redis::set(self::PHONE_SEND_LASTCONFIG_PREFIX . $phone, $SendCodeConfigType);
  244. Redis::expire(self::PHONE_SEND_LASTCONFIG_PREFIX . $phone, 300);
  245. Redis::set(self::PHONE_FREQUENT_LIMIT_PREFIX . $phone, 1);
  246. Redis::expire(self::PHONE_FREQUENT_LIMIT_PREFIX . $phone, 118);
  247. Redis::incr(self::PHONE_DAILY_LIMIT_PREFIX . $phone);
  248. Redis::expireAt(self::PHONE_DAILY_LIMIT_PREFIX . $phone, strtotime('today +1 day'));
  249. Redis::incr(self::IP_DAILY_LIMIT_PREFIX . $ip);
  250. Redis::expireAt(self::IP_DAILY_LIMIT_PREFIX . $ip, strtotime('today +1 day'));
  251. }
  252. }