exists()) { throw new BizException(__('The phone number is already registered')); } } if (! $this->cache->add("sms_lock_{$type}_{$phone}", 1, $decaySeconds)) { throw new BizException(__('Sending too frequently, please try again later')); } return SmsCode::create([ 'phone' => $phone, 'code' => $code, 'type' => $type, 'expires_at' => now()->addSeconds($this->expires), ]); } /** * 校验验证码是否正确 * * @param string $phone * @param int $type * @param string $code * @return void * * @throws \App\Exceptions\BizException */ public function validate(string $phone, int $type, string $code): void { $smsCode = SmsCode::where('phone', $phone) ->where('type', $type) ->latest('id') ->first(); if ($smsCode === null || $smsCode->code !== $code || $smsCode->isInvalid()) { throw new BizException(__('Invalid verification code')); } $smsCode->update([ 'is_use' => true, ]); } }