You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\Service\v3\Implementations;
use App\Constants\v3\ErrorCode;use App\Constants\v3\SsdbKeys;use App\Exception\BusinessException;use App\Exception\ErrorCodeException;use App\Service\v3\Interfaces\HelperServiceInterface;use App\Service\v3\Interfaces\SmsSendServiceInterface;use App\Service\v3\Interfaces\VerifyCodeServiceInterface;use App\TaskWorker\SSDBTask;use Hyperf\Utils\ApplicationContext;use Hyperf\Di\Annotation\Inject;
class VerifyCodeService implements VerifyCodeServiceInterface{ /** * @Inject * @var SmsSendServiceInterface */ protected $smsService;
/** * @Inject * @var HelperServiceInterface */ protected $helperService;
public function do($userId, $tel) { // 获取验证码
$verifyCode = $this->helperService->makeNumCode(6);
// 存到SSDB
$ssdbClient = ApplicationContext::getContainer()->get(SSDBTask::class); $setRes = $ssdbClient->exec( "setnx", SsdbKeys::VERIFY_CODE.$userId.'_'.$tel, $verifyCode ); if (!$setRes) { throw new ErrorCodeException(ErrorCode::VERIFY_CODE_SENDED); }
$expireRes = $ssdbClient->exec( 'expire', SsdbKeys::VERIFY_CODE.$userId.'_'.$tel, 900 ); if (!$expireRes) { throw new ErrorCodeException(ErrorCode::VERIFY_CODE_ERROR); }
// 发送短信
$smsRes = $this->smsService->doVerifyCode($tel, $verifyCode); if (!$smsRes) { throw new ErrorCodeException(ErrorCode::VERIFY_CODE_ERROR); }
return true; }
public function check($userId, $tel, $verifyCode) { // 获取验证码并验证
$ssdbClient = ApplicationContext::getContainer()->get(SSDBTask::class); $code = $ssdbClient->exec( "get", SsdbKeys::VERIFY_CODE.$userId.'_'.$tel ); if (empty($code) || $verifyCode!=$code) { throw new ErrorCodeException(ErrorCode::INVALID_VERIFY_CODE); }
$this->undo($userId, $tel); return true;
}
public function undo($userId, $tel) { $ssdbClient = ApplicationContext::getContainer()->get(SSDBTask::class); return $ssdbClient->exec( 'del', SsdbKeys::VERIFY_CODE.$userId.'_'.$tel ); }}
|