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, 600 ); 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 ); } }