SendCodeLogic.php 10 KB

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