first commit
This commit is contained in:
75
ZR.Admin.WebApi/Extensions/EntityExtension.cs
Normal file
75
ZR.Admin.WebApi/Extensions/EntityExtension.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Snowflake.Core;
|
||||
using System;
|
||||
|
||||
namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
public static class EntityExtension
|
||||
{
|
||||
public static TSource ToCreate<TSource>(this TSource source, HttpContext context = null)
|
||||
{
|
||||
var types = source.GetType();
|
||||
|
||||
var worker = new IdWorker(1, 1);
|
||||
if (types.GetProperty("ID") != null)
|
||||
{
|
||||
long id = worker.NextId();
|
||||
|
||||
types.GetProperty("ID").SetValue(source, id.ToString(), null);
|
||||
}
|
||||
|
||||
if (types.GetProperty("CreateTime") != null)
|
||||
{
|
||||
types.GetProperty("CreateTime").SetValue(source, DateTime.Now, null);
|
||||
}
|
||||
|
||||
if (types.GetProperty("UpdateTime") != null)
|
||||
{
|
||||
types.GetProperty("UpdateTime").SetValue(source, DateTime.Now, null);
|
||||
}
|
||||
|
||||
if (types.GetProperty("Create_by") != null && context != null)
|
||||
{
|
||||
types.GetProperty("Create_by").SetValue(source, context.GetName(), null);
|
||||
|
||||
// types.GetProperty("CreateName").SetValue(source, userSession.UserName, null);
|
||||
}
|
||||
if (types.GetProperty("UserId") != null && context != null)
|
||||
{
|
||||
types.GetProperty("UserId").SetValue(source, context.GetUId(), null);
|
||||
}
|
||||
//if (types.GetProperty("UpdateID") != null)
|
||||
//{
|
||||
// types.GetProperty("UpdateID").SetValue(source, userSession.UserID, null);
|
||||
|
||||
// types.GetProperty("UpdateName").SetValue(source, userSession.UserName, null);
|
||||
//}
|
||||
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
//public static TSource ToUpdate<TSource>(this TSource source, UserSessionVM userSession)
|
||||
//{
|
||||
// var types = source.GetType();
|
||||
|
||||
// if (types.GetProperty("UpdateTime") != null)
|
||||
// {
|
||||
// types.GetProperty("UpdateTime").SetValue(source, DateTime.Now, null);
|
||||
// }
|
||||
|
||||
// if (types.GetProperty("UpdateID") != null)
|
||||
// {
|
||||
// types.GetProperty("UpdateID").SetValue(source, userSession.UserID, null);
|
||||
// }
|
||||
|
||||
// if (types.GetProperty("UpdateName") != null)
|
||||
// {
|
||||
// types.GetProperty("UpdateName").SetValue(source, userSession.UserName, null);
|
||||
// }
|
||||
|
||||
// return source;
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
160
ZR.Admin.WebApi/Extensions/HttpContextExtension.cs
Normal file
160
ZR.Admin.WebApi/Extensions/HttpContextExtension.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using UAParser;
|
||||
using ZR.Model.System;
|
||||
|
||||
namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// HttpContext扩展类
|
||||
/// </summary>
|
||||
public static class HttpContextExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否是ajax请求
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAjaxRequest(this HttpRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
}
|
||||
|
||||
//return request.Headers.ContainsKey("X-Requested-With") &&
|
||||
// request.Headers["X-Requested-With"].Equals("XMLHttpRequest");
|
||||
|
||||
return request.Headers["X-Requested-With"] == "XMLHttpRequest" || (request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取客户端IP
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetClientUserIp(this HttpContext context)
|
||||
{
|
||||
if (context == null) return "";
|
||||
var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
result = context.Connection.RemoteIpAddress.ToString();
|
||||
}
|
||||
if (string.IsNullOrEmpty(result) || result.Contains("::1"))
|
||||
result = "127.0.0.1";
|
||||
|
||||
result = result.Replace("::ffff:", "127.0.0.1");
|
||||
result = IsIP(result) ? result : "127.0.0.1";
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool IsIP(string ip)
|
||||
{
|
||||
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
||||
}
|
||||
|
||||
public static long GetUId(this HttpContext context)
|
||||
{
|
||||
var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid);
|
||||
|
||||
return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0 ;
|
||||
}
|
||||
public static string GetName(this HttpContext context)
|
||||
{
|
||||
var uid = context.User.Identity.Name;
|
||||
|
||||
return uid;
|
||||
}
|
||||
//public static int GetRole(this HttpContext context)
|
||||
//{
|
||||
// var roleid = context.User.FindFirstValue(ClaimTypes.Role) ?? "0";
|
||||
|
||||
// return int.Parse(roleid);
|
||||
//}
|
||||
|
||||
public static string GetUserAgent(this HttpContext context)
|
||||
{
|
||||
var str = context.Request.Headers["User-Agent"];
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求令牌
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetToken(this HttpContext context)
|
||||
{
|
||||
var str = context.Request.Headers["Token"];
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static ClientInfo GetClientInfo(this HttpContext context)
|
||||
{
|
||||
var str = GetUserAgent(context);
|
||||
var uaParser = Parser.GetDefault();
|
||||
ClientInfo c = uaParser.Parse(str);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
public static string GetRequestUrl(this HttpContext context)
|
||||
{
|
||||
return context != null ? context.Request.Path.Value : "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录cookie写入
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Claim> WriteCookies(this HttpContext context, LoginUser user)
|
||||
{
|
||||
//1、创建Cookie保存用户信息,使用claim
|
||||
var claims = new List<Claim>()
|
||||
{
|
||||
new Claim(ClaimTypes.PrimarySid, user.UserId.ToString()),
|
||||
new Claim(ClaimTypes.Name, user.UserName),
|
||||
new Claim(ClaimTypes.UserData, JsonConvert.SerializeObject(user))
|
||||
};
|
||||
if (user.RoleIds != null)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, string.Join(",", user.RoleIds)));
|
||||
}
|
||||
if (user.Permissions != null)
|
||||
{
|
||||
claims.Add(new Claim("perm", string.Join(",", user.Permissions)));
|
||||
}
|
||||
//2.创建声明主题 指定认证方式 这里使用cookie
|
||||
var claimsIdentity = new ClaimsIdentity(claims, "Login");
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await context.SignInAsync(
|
||||
CookieAuthenticationDefaults.AuthenticationScheme,//这里要注意的是HttpContext.SignInAsync(AuthenticationType,…) 所设置的Scheme一定要与前面的配置一样,这样对应的登录授权才会生效。
|
||||
new ClaimsPrincipal(claimsIdentity),
|
||||
new AuthenticationProperties()
|
||||
{
|
||||
IsPersistent = true,
|
||||
AllowRefresh = true,
|
||||
ExpiresUtc = DateTimeOffset.Now.AddDays(1),//有效时间
|
||||
});
|
||||
}).Wait();
|
||||
return claims;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
53
ZR.Admin.WebApi/Extensions/TasksExtension.cs
Normal file
53
ZR.Admin.WebApi/Extensions/TasksExtension.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Quartz.Spi;
|
||||
using System;
|
||||
using ZR.Service.IService;
|
||||
using ZR.Tasks;
|
||||
|
||||
namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 定时任务扩展方法
|
||||
/// </summary>
|
||||
public static class TasksExtension
|
||||
{
|
||||
public static void AddTaskSchedulers(this IServiceCollection services)
|
||||
{
|
||||
if (services == null) throw new ArgumentNullException(nameof(services));
|
||||
|
||||
//添加Quartz服务
|
||||
services.AddSingleton<IJobFactory, JobFactory>();
|
||||
//添加我们的服务
|
||||
services.AddTransient<Job_SyncTest>();
|
||||
|
||||
services.AddTransient<ITaskSchedulerServer, TaskSchedulerServer>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 程序启动后添加任务计划
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseAddTaskSchedulers(this IApplicationBuilder app)
|
||||
{
|
||||
var _tasksQzService = (ISysTasksQzService)App.GetRequiredService(typeof(ISysTasksQzService));
|
||||
|
||||
//此写法不通过有待研究
|
||||
//var _tasksQzService2 = (ISysTasksQzService)services.GetRequiredService(typeof(SysTasksQzService));
|
||||
ITaskSchedulerServer _schedulerServer = App.GetRequiredService<ITaskSchedulerServer>();
|
||||
|
||||
var tasks = _tasksQzService.GetWhere(m => m.IsStart);
|
||||
|
||||
//程序启动后注册所有定时任务
|
||||
foreach (var task in tasks)
|
||||
{
|
||||
_schedulerServer.AddTaskScheduleAsync(task);
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user