| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using Castle.Core.Internal;
- using Masuit.MyBlogs.Core.Extensions;
- using Masuit.Tools;
- using Masuit.Tools.AspNetCore.Mime;
- using Masuit.Tools.Security;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Svg;
- using System;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- namespace Masuit.MyBlogs.Core.Controllers
- {
- public class ActivityController : Controller
- {
- [HttpGet("sharecode")]
- public IActionResult GetCode(string q)
- {
- if (string.IsNullOrEmpty(q))
- {
- throw new NotFoundException("联系方式不能为空");
- }
- var enc = q.AESEncrypt();
- Response.Cookies.Append("ShareCode", enc, new CookieOptions()
- {
- Expires = DateTime.Now.AddYears(1),
- SameSite = SameSiteMode.Lax
- });
- return Content(enc);
- }
- public IActionResult ViewCount(string email)
- {
- return Ok(RedisHelper.SMembers("Share:" + email).Length);
- }
- public ActionResult Users(string type = "json", int count = 5)
- {
- var keys = RedisHelper.Keys("Share:*").ToDictionary(s => s[6..].MaskEmail(), s => RedisHelper.SMembers(s).Length).OrderByDescending(p => p.Value).ToList();
- switch (type)
- {
- case "svg":
- var svg = new SvgDocument()
- {
- Height = keys.Count * 19
- };
- var svgText = new SvgText();
- for (var i = 0; i < keys.Count; i++)
- {
- var s = keys[i];
- svgText.Children.Add(new SvgTextSpan()
- {
- Text = s.Key,
- Fill = new SvgColourServer(s.Value >= count ? Color.Red : Color.Black),
- FontWeight = SvgFontWeight.Bold,
- Y = new SvgUnitCollection()
- {
- new SvgUnit(18*(i+1))
- },
- X = new SvgUnitCollection()
- {
- new SvgUnit(0)
- },
- FontSize = 16
- });
- }
- svg.Children.Add(svgText);
- using (var stream = new MemoryStream())
- {
- svg.Write(stream);
- return File(stream.ToArray(), ContentType.Svg);
- }
- default:
- return Json(keys);
- }
- }
- public ActionResult Count(int? count)
- {
- var keys = RedisHelper.Keys("Share:*");
- if (count.HasValue)
- {
- keys = keys.FindAll(s => RedisHelper.SMembers(s).Length >= count);
- }
- using var stream = new MemoryStream();
- new SvgDocument()
- {
- Height = 20,
- Width = 13 * keys.Length.ToString().Length,
- Children =
- {
- new SvgText(keys.Length.ToString())
- {
- Fill = new SvgColourServer(Color.Red),
- FontWeight = SvgFontWeight.Bold,
- Y = new SvgUnitCollection()
- {
- new SvgUnit(18)
- },
- FontSize = 20
- }
- }
- }.Write(stream);
- return File(stream.ToArray(), ContentType.Svg);
- }
- }
- }
|