package cn.iocoder.yudao.util; import org.springframework.stereotype.Component; import java.util.Random; /** * 手机验证码通用util * @author vinjor-M * @date 14:08 2024/10/16 **/ @Component public class PhoneCodeUtil { /** * 生成登录验证码 * @author vinjor-M * @date 14:09 2024/10/16 * @return java.lang.String **/ public String generateVerificationCode() { // 设置验证码长度 int codeLength = 6; // 设置验证码字符源 String codeSource = "0123456789"; // 生成随机数对象 Random random = new Random(); StringBuilder verificationCode = new StringBuilder(); // 随机生成验证码 for (int i = 0; i < codeLength; i++) { int index = random.nextInt(codeSource.length()); char codeChar = codeSource.charAt(index); verificationCode.append(codeChar); } return verificationCode.toString(); } }