using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RG3.PF.OssCaptcha.Generator
{
/// <summary>
///
/// </summary>
public enum CaptchaType
{
/// <summary>
/// 默认(英文字符大小写,数字混合)
/// </summary>
DEFAULT=0,
/// <summary>
/// 中文
/// </summary>
CHINESE=1,
/// <summary>
/// 数字
/// </summary>
NUMBER=2,
/// <summary>
/// 中文数字小写
/// </summary>
NUMBER_ZH_CN=3,
/// <summary>
/// 中文数字大写
/// </summary>
NUMBER_ZH_HK=4,
/// <summary>
/// 英文字符大小写混合
/// </summary>
WORD=5,
/// <summary>
/// 英文字符小写
/// </summary>
WORD_LOWER=6,
/// <summary>
/// 英文字符大写
/// </summary>
WORD_UPPER=7,
/// <summary>
/// 英文小写,数字混合
/// </summary>
WORD_NUMBER_LOWER=8,
/// <summary>
/// 英文大写,数字混合
/// </summary>
WORD_NUMBER_UPPER=9,
/// <summary>
/// 数字计算
/// </summary>
ARITHMETIC=10,
/// <summary>
/// 数字计算,中文
/// </summary>
ARITHMETIC_ZH=11
}
}
/// <summary>
/// 生成
/// </summary>
/// <param name="length">长度</param>
/// <returns>(渲染文本,code)</returns>
public (string renderText, string code) Generate(int length)
{
if (_captchaType == CaptchaType.ARITHMETIC)
{
return GenerateaArithmetic(length);
}
else if (_captchaType == CaptchaType.ARITHMETIC_ZH)
{
return GenerateaArithmeticZh(length);
}
else if (_captchaType == CaptchaType.NUMBER_ZH_CN)
{
return GenerateaNumberZH(length, false);
}
else if (_captchaType == CaptchaType.NUMBER_ZH_HK)
{
return GenerateaNumberZH(length, true);
}
else
{
var chars = GetCharacters(_captchaType);
var code = Pick(chars, length);
return (code, code);
}
}
private static IReadOnlyList<char> GetCharacters(CaptchaType captchaType)
{
return captchaType switch
{
CaptchaType.DEFAULT => Characters.DEFAULT,
CaptchaType.CHINESE => Characters.CHINESE,
CaptchaType.NUMBER => Characters.NUMBER,
CaptchaType.NUMBER_ZH_CN => Characters.NUMBER_ZH_CN,
CaptchaType.NUMBER_ZH_HK => Characters.NUMBER_ZH_HK,
CaptchaType.WORD_NUMBER_LOWER => Characters.WORD_NUMBER_LOWER,
CaptchaType.WORD_NUMBER_UPPER => Characters.WORD_NUMBER_UPPER,
CaptchaType.WORD => Characters.WORD,
CaptchaType.WORD_LOWER => Characters.WORD_LOWER,
CaptchaType.WORD_UPPER => Characters.WORD_UPPER,
_ => Characters.DEFAULT,
};
}
文档更新时间: 2023-09-24 11:20 作者:admin