* @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('短信发送错误!'); } }