GeneralCaptchaService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Microsoft.Extensions.Caching.Memory;
  2. using System.DrawingCore;
  3. using System.DrawingCore.Imaging;
  4. using System.Text;
  5. using YBEE.EQM.Core;
  6. namespace YBEE.EQM.Application
  7. {
  8. /// <summary>
  9. /// 常规验证码
  10. /// </summary>
  11. public class GeneralCaptchaService(IMemoryCache memoryCache) : IGeneralCaptchaService, ITransient
  12. {
  13. /// <summary>
  14. /// 生成验证码图片
  15. /// </summary>
  16. /// <param name="length">验证码长度</param>
  17. /// <param name="width">图片宽度</param>
  18. /// <param name="height">图片高度</param>
  19. /// <param name="fontSize">字体大小</param>
  20. /// <returns></returns>
  21. public GeneralCaptchaOutput CreateCaptchaImage(int length = 6, int width = 260, int height = 36, int fontSize = 22)
  22. {
  23. var rtnResult = new GeneralCaptchaOutput();
  24. var code = GenerateRandom(length); // 随机字符串集合
  25. rtnResult.ImageBase64 = Convert.ToBase64String(Draw(code, width, height, fontSize));
  26. rtnResult.Token = Guid.NewGuid().ToString();
  27. // 缓存验证码正确位置集合
  28. var cacheOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(120));
  29. memoryCache.Set(CommonConst.CACHE_KEY_CODE + rtnResult.Token, code, cacheOptions);
  30. return rtnResult;
  31. }
  32. /// <summary>
  33. /// 验证码验证
  34. /// </summary>
  35. /// <param name="input"></param>
  36. /// <returns></returns>
  37. public GeneralCaptchaOutput CheckCode(GeneralCaptchaInput input)
  38. {
  39. var res = new GeneralCaptchaOutput();
  40. var code = memoryCache.Get(CommonConst.CACHE_KEY_CODE + input.Token);
  41. if (code == null)
  42. {
  43. res.Code = 1;
  44. res.Message = "验证码已失效,请重新获取";
  45. return res;
  46. }
  47. if (string.Compare(input.CaptchaCode, (string)code, true) != 0)
  48. {
  49. res.Code = 2;
  50. res.Message = "验证码错误";
  51. return res;
  52. }
  53. memoryCache.Remove(CommonConst.CACHE_KEY_CODE + input.Token);
  54. res.Code = 0;
  55. res.Message = "验证成功";
  56. return res;
  57. }
  58. #region 私有方法
  59. /// <summary>
  60. /// 绘制验证码
  61. /// </summary>
  62. /// <param name="code"></param>
  63. /// <param name="codeW"></param>
  64. /// <param name="codeH"></param>
  65. /// <param name="fontSize"></param>
  66. /// <returns></returns>
  67. private static byte[] Draw(string code, int codeW, int codeH, int fontSize)
  68. {
  69. // 颜色列表,用于验证码、噪线、噪点
  70. Color[] color = [Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue];
  71. // 字体列表,用于验证码
  72. string[] fonts = ["Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact"];
  73. using var bmp = new Bitmap(codeW, codeH);
  74. using var g = Graphics.FromImage(bmp);
  75. using var ms = new MemoryStream();
  76. g.Clear(Color.White);
  77. var rnd = new Random();
  78. // 画噪线
  79. for (int i = 0; i < 4; i++)
  80. {
  81. int x1 = rnd.Next(codeW);
  82. int y1 = rnd.Next(codeH);
  83. int x2 = rnd.Next(codeW);
  84. int y2 = rnd.Next(codeH);
  85. var clr = color[rnd.Next(color.Length)];
  86. g.DrawLine(new Pen(clr), x1, y1, x2, y2);
  87. }
  88. // 画验证码字符串
  89. string fnt;
  90. Font ft;
  91. for (int i = 0; i < code.Length; i++)
  92. {
  93. fnt = fonts[rnd.Next(fonts.Length)];
  94. ft = new Font(fnt, fontSize);
  95. var clr = color[rnd.Next(color.Length)];
  96. g.DrawString(code[i].ToString(), ft, new SolidBrush(clr), (float)i * 24 + 3, (float)0);
  97. }
  98. // 将验证码图片写入内存流
  99. bmp.Save(ms, ImageFormat.Png);
  100. return ms.ToArray();
  101. }
  102. /// <summary>
  103. /// 生成验证码
  104. /// </summary>
  105. /// <param name="length"></param>
  106. /// <returns></returns>
  107. private static string GenerateRandom(int length)
  108. {
  109. var chars = new StringBuilder();
  110. // 验证码的字符集,去掉了一些容易混淆的字符
  111. char[] character = ['2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y'];
  112. Random rnd = new();
  113. // 生成验证码字符串
  114. for (int i = 0; i < length; i++)
  115. {
  116. chars.Append(character[rnd.Next(character.Length)]);
  117. }
  118. return chars.ToString();
  119. }
  120. #endregion
  121. }
  122. }