ActivityController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Castle.Core.Internal;
  2. using Masuit.MyBlogs.Core.Extensions;
  3. using Masuit.Tools;
  4. using Masuit.Tools.AspNetCore.Mime;
  5. using Masuit.Tools.Security;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Svg;
  9. using System;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. namespace Masuit.MyBlogs.Core.Controllers
  14. {
  15. public class ActivityController : Controller
  16. {
  17. [HttpGet("sharecode")]
  18. public IActionResult GetCode(string q)
  19. {
  20. if (string.IsNullOrEmpty(q))
  21. {
  22. throw new NotFoundException("联系方式不能为空");
  23. }
  24. var enc = q.AESEncrypt();
  25. Response.Cookies.Append("ShareCode", enc, new CookieOptions()
  26. {
  27. Expires = DateTime.Now.AddYears(1),
  28. SameSite = SameSiteMode.Lax
  29. });
  30. return Content(enc);
  31. }
  32. public IActionResult ViewCount(string email)
  33. {
  34. return Ok(RedisHelper.SMembers("Share:" + email).Length);
  35. }
  36. public ActionResult Users(string type = "json", int count = 5)
  37. {
  38. var keys = RedisHelper.Keys("Share:*").ToDictionary(s => s[6..].MaskEmail(), s => RedisHelper.SMembers(s).Length).OrderByDescending(p => p.Value).ToList();
  39. switch (type)
  40. {
  41. case "svg":
  42. var svg = new SvgDocument()
  43. {
  44. Height = keys.Count * 19
  45. };
  46. var svgText = new SvgText();
  47. for (var i = 0; i < keys.Count; i++)
  48. {
  49. var s = keys[i];
  50. svgText.Children.Add(new SvgTextSpan()
  51. {
  52. Text = s.Key,
  53. Fill = new SvgColourServer(s.Value >= count ? Color.Red : Color.Black),
  54. FontWeight = SvgFontWeight.Bold,
  55. Y = new SvgUnitCollection()
  56. {
  57. new SvgUnit(18*(i+1))
  58. },
  59. X = new SvgUnitCollection()
  60. {
  61. new SvgUnit(0)
  62. },
  63. FontSize = 16
  64. });
  65. }
  66. svg.Children.Add(svgText);
  67. using (var stream = new MemoryStream())
  68. {
  69. svg.Write(stream);
  70. return File(stream.ToArray(), ContentType.Svg);
  71. }
  72. default:
  73. return Json(keys);
  74. }
  75. }
  76. public ActionResult Count(int? count)
  77. {
  78. var keys = RedisHelper.Keys("Share:*");
  79. if (count.HasValue)
  80. {
  81. keys = keys.FindAll(s => RedisHelper.SMembers(s).Length >= count);
  82. }
  83. using var stream = new MemoryStream();
  84. new SvgDocument()
  85. {
  86. Height = 20,
  87. Width = 13 * keys.Length.ToString().Length,
  88. Children =
  89. {
  90. new SvgText(keys.Length.ToString())
  91. {
  92. Fill = new SvgColourServer(Color.Red),
  93. FontWeight = SvgFontWeight.Bold,
  94. Y = new SvgUnitCollection()
  95. {
  96. new SvgUnit(18)
  97. },
  98. FontSize = 20
  99. }
  100. }
  101. }.Write(stream);
  102. return File(stream.ToArray(), ContentType.Svg);
  103. }
  104. }
  105. }