package com.ruoyi.common.utils;

import java.util.Random;

/**
 * 验证码生成工具类
 */
public class VerifyCodeUtils {

    private static final String NUMBERS = "0123456789";  // 数字验证码字符集
    private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";  // 字母验证码字符集
    private static final String MIXED = NUMBERS + LETTERS;  // 混合字符集

    /**
     * 生成纯数字验证码
     * @param length 验证码长度
     * @return 数字验证码
     */
    public static String generateNumberCode(int length) {
        return generateCode(NUMBERS, length);
    }

    /**
     * 生成字母+数字混合验证码
     * @param length 验证码长度
     * @return 混合验证码
     */
    public static String generateMixedCode(int length) {
        return generateCode(MIXED, length);
    }

    /**
     * 生成自定义字符集的验证码
     * @param charSet 字符集
     * @param length 长度
     * @return 验证码
     */
    private static String generateCode(String charSet, int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("验证码长度必须大于0");
        }
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(charSet.length());
            sb.append(charSet.charAt(index));
        }
        return sb.toString();
    }
}
