$mobile]; $exists = User::query()->where($where)->exists(); $category === 1 && !$exists && self::error('用户不存在!',404); $category === 0 && $exists && self::error('用户已存在!', 409); // 检测短信发送间隔 // 生成验证码 $code = 123456; // 发送短信 // 判断发送结果 // 缓存验证码 $this->save($mobile, $code); } /** * Notes : * Method : 验证手机验证码并清除缓存 * @param $mobile * @param $code * @return bool */ public function verifyCode($mobile, $code): bool { $correctCode = cache('mobile_verification_code_' . $mobile); $isValid = $correctCode === $code; $isValid && cache(['mobile_verification_code_' . $mobile => 0], 0); return $isValid; } /** * Notes : * Method : 使用缓存保存手机验证码 * @param $mobile * @param $code */ protected function save($mobile, $code): void { $sms_expires_time = env('SMS_EXPIRES_TIME', 10); $expiresAt = now()->addMinutes(floatval($sms_expires_time)); // 设置验证码有效期为10分钟 cache(['mobile_verification_code_' . $mobile => $code], $expiresAt); } }