1、什麼是MQTT? MQTT(Message Queuing Telemetry Transport,消息隊列遙測傳輸協議),是一種基於發佈/訂閱(publish/subscribe)模式的"輕量級"通訊協議,該協議構建於TCP/IP協議上,由IBM在1999年發佈。MQTT最大優點在於,可以以極 ...
上一節,介紹了滑動驗證碼的基本情況,也創建了一個.NET 6的工程。本節內容是創建一些基礎類。
目錄
- CaptchaData.cs
- CaptchaValidateData.cs
- ImageCaptchaInfo.cs
- Resource.cs
- SliderImageCaptchaInfo.cs
- SlideTrack.cs
- TemplatePair.cs
- Track.cs
本節創建的類全部在工程的Model目錄下:
CaptchaData.cs
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
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
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
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
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
·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
·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
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