SmsService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * 短信服务.
  4. *
  5. * @author nj <nj@gmail.com>
  6. * @date 2020-01-22 11:36:11
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Services;
  10. use App\Exception\ApiException;
  11. use App\Model\Mongo\SmsLogModel;
  12. use App\Repositories\ConfigRepoistory;
  13. use App\Repositories\Service\CommonService;
  14. use Carbon\Carbon;
  15. use Psr\SimpleCache\CacheInterface;
  16. class SmsService
  17. {
  18. /**
  19. * 启瑞云
  20. * @param $phone number
  21. * @param $content string
  22. * @return boolean
  23. * @throws \Psr\SimpleCache\InvalidArgumentException
  24. */
  25. public function sendByQiRui($phone, $content)
  26. {
  27. if (empty($phone) || empty($content)) {
  28. return false;
  29. }
  30. $account = make(ConfigRepoistory::class)->get('sms_account', 'sys', '');
  31. $password = make(ConfigRepoistory::class)->get('sms_password', 'sys', '');
  32. if (empty($account) || empty($password)) {
  33. return false;
  34. }
  35. $url = 'http://api.qirui.com:7891/mt';
  36. $content = mb_convert_encoding($content, 'utf-8', 'auto');
  37. $postData = array(
  38. 'un' => $account,
  39. 'pw' => $password,
  40. 'da' => $phone,
  41. 'tf' => 3,
  42. 'sm' => $content,
  43. 'rf' => 2,
  44. 'dc' => 15,
  45. 'rd' => 1,
  46. );
  47. $url .= '?' . http_build_query($postData);
  48. $result = make(CommonService::class)->httpGet($url);
  49. $result = json_decode($result);
  50. if ($result && $result->success) {
  51. return true;
  52. }
  53. $data = array(
  54. 'phone' => $phone . '',
  55. 'content' => $content . '',
  56. 'status' => -1,
  57. 'ip' => make(CommonService::class)->getIp(),
  58. 'message' => $result->error,
  59. );
  60. make(SmsLogModel::class)->db()->insertOne($data);
  61. return false;
  62. }
  63. /**
  64. * 發送短信
  65. * @param $phone
  66. * @param $code
  67. * @return boolean
  68. * @throws \Psr\SimpleCache\InvalidArgumentException
  69. */
  70. public function sendCommon($phone, $code)
  71. {
  72. $content = "【云亿特科技】您好,您的验证码是{code},10分钟内输入有效,不要将验证码告诉任何人哦。";
  73. $content = str_replace("{code}", $code, $content);
  74. return $this->send($phone, $content);
  75. }
  76. /**
  77. * 發送驗證碼
  78. * @param $phone
  79. * @param $content
  80. * @return boolean
  81. * @throws \Psr\SimpleCache\InvalidArgumentException
  82. */
  83. public function send($phone, $content)
  84. {
  85. $data = array(
  86. 'phone' => $phone . '',
  87. 'content' => $content . '',
  88. 'status' => 1,
  89. 'ip' => make(CommonService::class)->getIP(),
  90. 'message' => '',
  91. );
  92. $cacheObj = make(CacheInterface::class);
  93. $cacheKey = 'dy_sms_log_' . $phone;
  94. //次数控制
  95. //同账号二分钟三次(绑定手机、找回账号、修改安全码)
  96. $mobileSend = $cacheObj->get($cacheKey);
  97. $now = Carbon::now()->timestamp;
  98. $mobileSendNum = 0;
  99. $mobileSendTime = $now;
  100. if ($mobileSend) {
  101. $mobileSend = json_decode($mobileSend);
  102. $mobileSendNum = $mobileSend->number;
  103. $mobileSendTime = $mobileSend->time;
  104. }
  105. if ($mobileSendNum >= 3) {
  106. throw new ApiException('发送过于频繁,请稍后再试');
  107. }
  108. $mobileSendNum++;
  109. $cacheData = [
  110. 'number' => $mobileSendNum,
  111. 'time' => $mobileSendTime
  112. ];
  113. $ttl = 120 - ($now - $mobileSendTime);
  114. $cacheObj->set($cacheKey, json_encode($cacheData), $ttl);
  115. //同ip源1分钟10次
  116. $cacheKey = md5('dy_sms_log_' . $data['ip']);
  117. $ipSend = $cacheObj->get($cacheKey);
  118. $ipSendNum = 0;
  119. $ipSendTime = $now;
  120. if ($ipSend) {
  121. $ipSend = json_decode($ipSend);
  122. $ipSendNum = $ipSend->number;
  123. $ipSendTime = $ipSend->time;
  124. }
  125. if ($ipSendNum >= 10) {
  126. throw new ApiException('发送过于频繁,请稍后再试');
  127. }
  128. $ipSendNum++;
  129. $cacheData = [
  130. 'number' => $ipSendNum,
  131. 'time' => $ipSendTime
  132. ];
  133. $ttl = 60 - ($now - $ipSendTime);
  134. $cacheObj->set($cacheKey, json_encode($cacheData), $ttl);
  135. $result = $this->sendByQirui($phone, $content);
  136. if ($result) {
  137. make(SmsLogModel::class)->db()->insertOne($data);
  138. return true;
  139. }
  140. throw new ApiException('短信发送错误!');
  141. }
  142. }