| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /**
- * 短信服务.
- *
- * @author nj <nj@gmail.com>
- * @date 2020-01-22 11:36:11
- */
- declare(strict_types=1);
- namespace Modules\Api\Services;
- use App\Exception\ApiException;
- use App\Model\Mongo\SmsLogModel;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\Service\CommonService;
- use Carbon\Carbon;
- use Psr\SimpleCache\CacheInterface;
- class SmsService
- {
- /**
- * 启瑞云
- * @param $phone number
- * @param $content string
- * @return boolean
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function sendByQiRui($phone, $content)
- {
- if (empty($phone) || empty($content)) {
- return false;
- }
- $account = make(ConfigRepoistory::class)->get('sms_account', 'sys', '');
- $password = make(ConfigRepoistory::class)->get('sms_password', 'sys', '');
- if (empty($account) || empty($password)) {
- return false;
- }
- $url = 'http://api.qirui.com:7891/mt';
- $content = mb_convert_encoding($content, 'utf-8', 'auto');
- $postData = array(
- 'un' => $account,
- 'pw' => $password,
- 'da' => $phone,
- 'tf' => 3,
- 'sm' => $content,
- 'rf' => 2,
- 'dc' => 15,
- 'rd' => 1,
- );
- $url .= '?' . http_build_query($postData);
- $result = make(CommonService::class)->httpGet($url);
- $result = json_decode($result);
- if ($result && $result->success) {
- return true;
- }
- $data = array(
- 'phone' => $phone . '',
- 'content' => $content . '',
- 'status' => -1,
- 'ip' => make(CommonService::class)->getIp(),
- 'message' => $result->error,
- );
- make(SmsLogModel::class)->db()->insertOne($data);
- return false;
- }
- /**
- * 發送短信
- * @param $phone
- * @param $code
- * @return boolean
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function sendCommon($phone, $code)
- {
- $content = "【云亿特科技】您好,您的验证码是{code},10分钟内输入有效,不要将验证码告诉任何人哦。";
- $content = str_replace("{code}", $code, $content);
- return $this->send($phone, $content);
- }
- /**
- * 發送驗證碼
- * @param $phone
- * @param $content
- * @return boolean
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function send($phone, $content)
- {
- $data = array(
- 'phone' => $phone . '',
- 'content' => $content . '',
- 'status' => 1,
- 'ip' => make(CommonService::class)->getIP(),
- 'message' => '',
- );
- $cacheObj = make(CacheInterface::class);
- $cacheKey = 'dy_sms_log_' . $phone;
- //次数控制
- //同账号二分钟三次(绑定手机、找回账号、修改安全码)
- $mobileSend = $cacheObj->get($cacheKey);
- $now = Carbon::now()->timestamp;
- $mobileSendNum = 0;
- $mobileSendTime = $now;
- if ($mobileSend) {
- $mobileSend = json_decode($mobileSend);
- $mobileSendNum = $mobileSend->number;
- $mobileSendTime = $mobileSend->time;
- }
- if ($mobileSendNum >= 3) {
- throw new ApiException('发送过于频繁,请稍后再试');
- }
- $mobileSendNum++;
- $cacheData = [
- 'number' => $mobileSendNum,
- 'time' => $mobileSendTime
- ];
- $ttl = 120 - ($now - $mobileSendTime);
- $cacheObj->set($cacheKey, json_encode($cacheData), $ttl);
- //同ip源1分钟10次
- $cacheKey = md5('dy_sms_log_' . $data['ip']);
- $ipSend = $cacheObj->get($cacheKey);
- $ipSendNum = 0;
- $ipSendTime = $now;
- if ($ipSend) {
- $ipSend = json_decode($ipSend);
- $ipSendNum = $ipSend->number;
- $ipSendTime = $ipSend->time;
- }
- if ($ipSendNum >= 10) {
- throw new ApiException('发送过于频繁,请稍后再试');
- }
- $ipSendNum++;
- $cacheData = [
- 'number' => $ipSendNum,
- 'time' => $ipSendTime
- ];
- $ttl = 60 - ($now - $ipSendTime);
- $cacheObj->set($cacheKey, json_encode($cacheData), $ttl);
- $result = $this->sendByQirui($phone, $content);
- if ($result) {
- make(SmsLogModel::class)->db()->insertOne($data);
- return true;
- }
- throw new ApiException('短信发送错误!');
- }
- }
|