上一節,介紹了滑動驗證碼的基本情況,也建立了一個.NET 6的工程。本節內容是建立一些基礎類。
本節建立的類全部在工程的Model目錄下:
CaptchaData.cs:驗證碼的資料類實體
namespace SlideCaptcha.Model
{
public class CaptchaData
{
/// <summary>
/// id
/// </summary>
public string Id { get; set; }
/// <summary>
/// 背景圖(含凹槽)
/// </summary>
public string BackgroundImage { get; set; }
/// <summary>
/// 滑動塊圖
/// </summary>
public string SliderImage { get; set; }
public CaptchaData(string id, string backgroundImage, string sliderImage)
{
Id = id;
BackgroundImage = backgroundImage;
SliderImage = sliderImage;
}
}
}
id表示驗證碼的id值,用於校驗驗證碼,BackgroundImage 是驗證碼圖片base64,包含了凹槽。凹槽模板在templates目錄下。SliderImage 是滾軸圖片。模板在templates下。
CaptchaValidateData.cs:驗證碼驗證資料實體
namespace SlideCaptcha.Model
{
public class CaptchaValidateData
{
/// <summary>
/// 滑動比例
/// </summary>
public float Percent { get; set; }
/// <summary>
/// 容錯值(校驗時用,缺口位置與實際滑動位置匹配容錯範圍)
/// </summary>
public float Tolerant { get; set; }
public CaptchaValidateData(float percent, float tolerant)
{
Percent = percent;
Tolerant = tolerant;
}
}
}
ImageCaptchaInfo.cs:證碼圖片資訊實體
namespace SlideCaptcha.Model
{
public class ImageCaptchaInfo
{
/// <summary>
/// 背景圖寬
/// </summary>
public int BackgroundImageWidth { get; set; }
/// <summary>
/// 背景圖高
/// </summary>
public int BackgroundImageHeight { get; set; }
/// <summary>
/// 背景圖
/// </summary>
public string BackgroundImageBase64 { get; set; }
/// <summary>
/// 滑動塊圖寬
/// </summary>
public int SliderImageWidth { get; set; }
/// <summary>
/// 滑動塊圖高
/// </summary>
public int SliderImageHeight { get; set; }
/// <summary>
/// 滑動塊圖
/// </summary>
public string SliderImageBase64 { get; set; }
}
}
驗證碼實體類的基礎類別,滑動驗證碼、旋轉驗證碼、點選驗證碼、拼接驗證碼都基於這個類。
Resource.cs :資源實體類
namespace SlideCaptcha.Model
{
public class Resource
{
public Resource(){}
/// <summary>
/// 資源資料
/// </summary>
public string Data { get; set; }
/// <summary>
/// 資源型別
/// </summary>
public string Type { get; set; }
/// <summary>
/// 建構函式
/// </summary>
/// <param name="type">資源型別</param>
/// <param name="data">資源資料</param>
public Resource(string type,string data)
{
Type = type;
Data = data;
}
}
}
資源資料,Data為要生成驗證碼圖片路徑。Type為型別,目前僅為本地檔案,後期將擴充套件URL模式。
SliderImageCaptchaInfo.cs:滑動驗證碼實體類
using SlideCaptcha.Exceptions;
namespace SlideCaptcha.Model
{
public class SliderImageCaptchaInfo : ImageCaptchaInfo
{
/// <summary>
/// 凹槽x座標
/// </summary>
public int X { get; set; }
/// <summary>
/// 凹槽y座標
/// </summary>
public int Y { get; set; }
/// <summary>
/// 凹槽位置百分比
/// </summary>
public float Percent
{
get
{
if (BackgroundImageWidth <= 0) return 0;
return 1.0f * X / BackgroundImageWidth;
}
}
public void Check()
{
// 校驗
if (this.X <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(X)}小於等於0");
if (this.Y <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(Y)}小於等於0");
if (this.BackgroundImageWidth <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(BackgroundImageWidth)}小於等於0");
if (this.BackgroundImageHeight <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(BackgroundImageHeight)}小於等於0");
if (this.SliderImageWidth <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(SliderImageWidth)}小於等於0");
if (this.SliderImageHeight <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(SliderImageHeight)}小於等於0");
if (string.IsNullOrWhiteSpace(this.BackgroundImageBase64)) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(BackgroundImageBase64)}為空");
if (string.IsNullOrWhiteSpace(this.SliderImageBase64)) throw new SlideCaptchaException($"SliderImageCaptchaInfo資料異常: {nameof(SliderImageBase64)}為空");
}
}
}
·SlideTrack.cs:滑動軌跡·
using SlideCaptcha.Exceptions;
using System;
using System.Collections.Generic;
namespace SlideCaptcha.Model
{
/// <summary>
/// 滑動軌跡
/// </summary>
public class SlideTrack
{
/// <summary>
/// 背景圖片寬度(可能經過縮放,不是原始圖片寬高)
/// </summary>
public int BackgroundImageWidth { get; set; }
/// <summary>
/// 背景圖片高度(可能經過縮放,不是原始圖片寬高)
/// </summary>
public int BackgroundImageHeight { get; set; }
/// <summary>
/// 滾軸圖片寬度(可能經過縮放,不是原始圖片寬高)
/// </summary>
public int SliderImageWidth { get; set; }
/// <summary>
/// 滾軸圖片高度(可能經過縮放,不是原始圖片寬高)
/// </summary>
public int SliderImageHeight { get; set; }
/// <summary>
/// 滑動開始時間(可能經過縮放,不是原始圖片寬高)
/// </summary>
public DateTime StartTime { get; set; }
/// <summary>
/// 滑動結束時間
/// </summary>
public DateTime EndTime { get; set; }
/// <summary>
/// 軌跡
/// </summary>
public List<Track> Tracks { get; set; }
/// <summary>
/// 滑動比例
/// </summary>
public float Percent
{
get
{
if (this.BackgroundImageWidth <= 0) return -1;
if (this.Tracks.Count <= 0) return -1;
var lastTrack = this.Tracks[this.Tracks.Count - 1];
return 1.0f * lastTrack.X / this.BackgroundImageWidth;
}
}
public void Check()
{
// 校驗
if (this.BackgroundImageWidth <= 0) throw new SlideCaptchaException($"SlideTrack資料異常: {nameof(BackgroundImageWidth)}小於等於0");
if (this.BackgroundImageHeight <= 0) throw new SlideCaptchaException($"SlideTrack資料異常: {nameof(BackgroundImageHeight)}小於等於0");
if (this.SliderImageWidth <= 0) throw new SlideCaptchaException($"SlideTrack資料異常: {nameof(SliderImageWidth)}小於等於0");
if (this.SliderImageHeight <= 0) throw new SlideCaptchaException($"SlideTrack資料異常: {nameof(SliderImageHeight)}小於等於0");
if (this.StartTime == DateTime.MinValue) throw new SlideCaptchaException($"SlideTrack資料異常: {nameof(StartTime)}為空");
if (this.EndTime == DateTime.MinValue) throw new SlideCaptchaException($"SlideTrack資料異常: {nameof(EndTime)}為空");
}
public void CheckTracks()
{
if (this.Tracks == null || this.Tracks.Count == 0) throw new SlideCaptchaException($"SlideTrack資料異常: {nameof(Tracks)}為空");
}
}
}
·TemplatePair.cs:模板類·
namespace SlideCaptcha.Model
{
public class TemplatePair
{
public TemplatePair() { }
/// <summary>
/// 滾軸資源
/// </summary>
public Resource Slider { get; set; }
/// <summary>
/// 凹槽資源
/// </summary>
public Resource Notch { get; set; }
/// <summary>
/// 模板型別
/// </summary>
public string TYPE { get; set; }
/// <summary>
/// 建構函式
/// </summary>
/// <param name="slider">滾軸資源</param>
/// <param name="notch">凹槽資源</param>
/// <param name="type">末班型別</param>
public TemplatePair(Resource slider, Resource notch,string type)
{
Slider = slider;
Notch = notch;
TYPE = type;
}
/// <summary>
/// 建立模板
/// </summary>
/// <param name="slider">滾軸資源</param>
/// <param name="notch">凹槽資源</param>
/// <returns></returns>
public static TemplatePair Create(Resource slider, Resource notch,string type)
{
return new TemplatePair(slider, notch,type);
}
}
}
Track.cs :滑動資料實體類
namespace SlideCaptcha.Model
{
public class Track
{
/// <summary>
/// X軸
/// </summary>
public int X { get; set; }
/// <summary>
/// Y軸
/// </summary>
public int Y { get; set; }
/// <summary>
/// 時間
/// </summary>
public int T { get; set; }
}
}
程式碼中SlideCaptchaException是自定義錯誤類。自定義錯誤類在Extensions資料夾下,這個後面的程式碼會進行說明。
下載地址:https://pan.baidu.com/s/19mx24FXrnqz9u2mmFqlr6g?pwd=7636