SendCodeLogic.php 11 KB

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