Browse Source

抽奖代码

懒得勤快 5 years ago
parent
commit
65d7d9cf4c

+ 41 - 35
src/Masuit.MyBlogs.Core/Controllers/ActivityController.cs

@@ -1,39 +1,45 @@
-//using Masuit.MyBlogs.Core.Extensions;
-//using Masuit.Tools.Security;
-//using Microsoft.AspNetCore.Http;
-//using Microsoft.AspNetCore.Mvc;
-//using System;
+using Masuit.MyBlogs.Core.Extensions;
+using Masuit.Tools.Security;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using System;
 
-//namespace Masuit.MyBlogs.Core.Controllers
-//{
-//    public class ActivityController : Controller
-//    {
-//        [HttpGet("sharecode")]
-//        public IActionResult GetCode(string q)
-//        {
-//            if (string.IsNullOrEmpty(q))
-//            {
-//                throw new NotFoundException("联系方式不能为空");
-//            }
+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);
-//        }
+            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 IActionResult ViewCount(string email)
+        {
+            return Ok(RedisHelper.SMembers("Share:" + email).Length);
+        }
 
-//        public ActionResult GetActivityUsers()
-//        {
-//            var keys = RedisHelper.Keys("Share:*");
-//            return Json(keys);
-//        }
-//    }
-//}
+        public ActionResult GetActivityUsers()
+        {
+            var keys = RedisHelper.Keys("Share:*");
+            return Json(keys);
+        }
+
+        public ActionResult Count()
+        {
+            var keys = RedisHelper.Keys("Share:*");
+            return Ok(keys.Length);
+        }
+    }
+}

+ 9 - 10
src/Masuit.MyBlogs.Core/Controllers/ToolsController.cs

@@ -5,9 +5,10 @@ using Masuit.Tools.Models;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Newtonsoft.Json;
-using System.Collections.Generic;
+using System;
 using System.Net.Http;
 using System.Threading.Tasks;
+using TimeZoneConverter;
 
 #if DEBUG
 #endif
@@ -41,27 +42,25 @@ namespace Masuit.MyBlogs.Core.Controllers
             }
 
             ViewBag.IP = ip;
+            var location = CommonHelper.MaxmindReader.City(ip).Location;
             var address = await ip.GetPhysicsAddressInfo() ?? new PhysicsAddress()
             {
                 Status = 0,
                 AddressResult = new AddressResult()
                 {
-                    Pois = new List<Pois>()
-                    {
-                        new Pois()
-                        {
-                            AddressDetail = ip.GetIPLocation() + "(本地数据库)"
-                        }
-                    },
                     AddressComponent = new AddressComponent(),
                     FormattedAddress = ip.GetIPLocation(),
                     Location = new Location()
                     {
-                        Lng = CommonHelper.MaxmindReader.City(ip).Location.Longitude.Value,
-                        Lat = CommonHelper.MaxmindReader.City(ip).Location.Latitude.Value
+                        Lng = location.Longitude.Value,
+                        Lat = location.Latitude.Value
                     }
                 }
             };
+            address.AddressResult.Pois.Add(new Pois
+            {
+                AddressDetail = $"{ip.GetIPLocation()}(UTC{TZConvert.GetTimeZoneInfo(location.TimeZone).BaseUtcOffset.Hours:+#;-#;0},本地数据库)"
+            });
             if (Request.Method.Equals(HttpMethods.Get))
             {
                 return View(address);

+ 44 - 44
src/Masuit.MyBlogs.Core/Extensions/ActivityMiddleware.cs

@@ -1,49 +1,49 @@
-//using Masuit.MyBlogs.Core.Common;
-//using Masuit.Tools.Security;
-//using Microsoft.AspNetCore.Http;
-//using System;
-//using System.Threading.Tasks;
+using Masuit.MyBlogs.Core.Common;
+using Masuit.Tools.Security;
+using Microsoft.AspNetCore.Http;
+using System;
+using System.Threading.Tasks;
 
-//namespace Masuit.MyBlogs.Core.Extensions
-//{
-//    /// <summary>
-//    /// 请求拦截器
-//    /// </summary>
-//    public class ActivityMiddleware
-//    {
-//        private readonly RequestDelegate _next;
+namespace Masuit.MyBlogs.Core.Extensions
+{
+    /// <summary>
+    /// 请求拦截器
+    /// </summary>
+    public class ActivityMiddleware
+    {
+        private readonly RequestDelegate _next;
 
-//        /// <summary>
-//        /// 构造函数
-//        /// </summary>
-//        /// <param name="next"></param>
-//        public ActivityMiddleware(RequestDelegate next)
-//        {
-//            _next = next;
-//        }
+        /// <summary>
+        /// 构造函数
+        /// </summary>
+        /// <param name="next"></param>
+        public ActivityMiddleware(RequestDelegate next)
+        {
+            _next = next;
+        }
 
-//        public async Task Invoke(HttpContext context)
-//        {
-//            var req = context.Request;
-//            var share = req.Query["share"].ToString();
-//            if (req.IsRobot() || string.IsNullOrEmpty(share) || share == req.Cookies["ShareCode"])
-//            {
-//                await _next.Invoke(context);
-//                return;
-//            }
+        public async Task Invoke(HttpContext context)
+        {
+            var req = context.Request;
+            var share = req.Query["share"].ToString();
+            if (req.IsRobot() || string.IsNullOrEmpty(share) || share == req.Cookies["ShareCode"])
+            {
+                await _next.Invoke(context);
+                return;
+            }
 
-//            var mail = share.AESDecrypt();
-//            if (string.IsNullOrEmpty(mail))
-//            {
-//                await _next.Invoke(context);
-//                return;
-//            }
+            var mail = share.AESDecrypt();
+            if (string.IsNullOrEmpty(mail))
+            {
+                await _next.Invoke(context);
+                return;
+            }
 
-//            var ip = context.Connection.RemoteIpAddress.MapToIPv4().ToString();
-//            RedisHelper.SAddAsync("Share:" + mail, ip).ContinueWith(task => RedisHelper.Expire("Share:" + mail, TimeSpan.FromDays(30)));
-//            //var query = req.Query.Where(x => x.Key != "share").Select(x => x.Key + "=" + x.Value).Join("&");
-//            //context.Response.Redirect((req.Path + "?" + query).Trim('?'));
-//            await _next.Invoke(context);
-//        }
-//    }
-//}
+            var ip = context.Connection.RemoteIpAddress.MapToIPv4().ToString();
+            RedisHelper.SAddAsync("Share:" + mail, ip).ContinueWith(task => RedisHelper.Expire("Share:" + mail, TimeSpan.FromDays(30)));
+            //var query = req.Query.Where(x => x.Key != "share").Select(x => x.Key + "=" + x.Value).Join("&");
+            //context.Response.Redirect((req.Path + "?" + query).Trim('?'));
+            await _next.Invoke(context);
+        }
+    }
+}

+ 4 - 4
src/Masuit.MyBlogs.Core/Extensions/IApplicationBuilderExtensions.cs

@@ -8,9 +8,9 @@ namespace Masuit.MyBlogs.Core.Extensions
         {
             return builder.UseMiddleware<RequestInterceptMiddleware>();
         }
-        //public static IApplicationBuilder UseActivity(this IApplicationBuilder builder)
-        //{
-        //    return builder.UseMiddleware<ActivityMiddleware>();
-        //}
+        public static IApplicationBuilder UseActivity(this IApplicationBuilder builder)
+        {
+            return builder.UseMiddleware<ActivityMiddleware>();
+        }
     }
 }

+ 1 - 1
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj

@@ -56,7 +56,7 @@
         <PackageReference Include="TimeZoneConverter" Version="3.2.0" />
         <PackageReference Include="WilderMinds.RssSyndication" Version="1.6.0" />
         <PackageReference Include="WinInsider.System.Net.Http.Formatting" Version="1.0.14" />
-        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="3.0.59" />
+        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="3.0.60" />
     </ItemGroup>
     <ItemGroup>
         <Content Update="appsettings.json">

+ 1 - 0
src/Masuit.MyBlogs.Core/Startup.cs

@@ -218,6 +218,7 @@ namespace Masuit.MyBlogs.Core
                 }
             }); //配置hangfire
             app.UseResponseCaching().UseResponseCompression(); //启动Response缓存
+            app.UseActivity();// 抽奖活动
             app.UseRouting(); // 放在 UseStaticFiles 之后
             app.UseEndpoints(endpoints =>
             {