Merge branch 'master' into net6.0
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Infrastructure;
|
||||
using Infrastructure.Attribute;
|
||||
using Infrastructure.Enums;
|
||||
using Infrastructure.Extensions;
|
||||
using Infrastructure.Model;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -91,26 +92,60 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// <param name="formFile"></param>
|
||||
/// <param name="fileDir">存储目录</param>
|
||||
/// <param name="fileName">自定义文件名</param>
|
||||
/// <param name="uploadType">上传类型 1、发送邮件</param>
|
||||
/// <param name="storeType">上传类型1、保存到本地 2、保存到阿里云</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost()]
|
||||
[Verify]
|
||||
[ActionPermissionFilter(Permission = "common")]
|
||||
public async Task<IActionResult> UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", int uploadType = 0)
|
||||
public async Task<IActionResult> UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", StoreType storeType = StoreType.LOCAL)
|
||||
{
|
||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||
SysFile file = new();
|
||||
string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
|
||||
double fileSize = Math.Round(formFile.Length / 1024.0, 2);//文件大小KB
|
||||
string[] NotAllowedFileExtensions = new string[] { ".bat", ".exe", ".jar", ".js" };
|
||||
int MaxContentLength = 15;
|
||||
if (NotAllowedFileExtensions.Contains(fileExt))
|
||||
{
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
|
||||
}
|
||||
switch (storeType)
|
||||
{
|
||||
case StoreType.LOCAL:
|
||||
file = await SysFileService.SaveFileToLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
|
||||
|
||||
SysFile file = await SysFileService.SaveFileToLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
|
||||
break;
|
||||
case StoreType.ALIYUN:
|
||||
if ((fileSize / 1024) > MaxContentLength)
|
||||
{
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
|
||||
}
|
||||
file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, HttpContext.GetName())
|
||||
{
|
||||
StoreType = (int)StoreType.ALIYUN,
|
||||
FileType = formFile.ContentType
|
||||
};
|
||||
file = await SysFileService.SaveFileToAliyun(file, formFile);
|
||||
|
||||
if (file.Id <= 0) { return ToResponse(ApiResult.Error("阿里云连接失败")); }
|
||||
break;
|
||||
case StoreType.TENCENT:
|
||||
break;
|
||||
case StoreType.QINIU:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return SUCCESS(new
|
||||
{
|
||||
url = uploadType == 1 ? file.FileUrl : file.AccessUrl,
|
||||
url = file.AccessUrl,
|
||||
fileName,
|
||||
fileId = file.Id.ToString()
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 存储文件到阿里云
|
||||
/// 存储文件到阿里云(已弃用)
|
||||
/// </summary>
|
||||
/// <param name="formFile"></param>
|
||||
/// <param name="fileName">自定义文件名</param>
|
||||
@@ -134,7 +169,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
|
||||
}
|
||||
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, "", HttpContext.GetName())
|
||||
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, HttpContext.GetName())
|
||||
{
|
||||
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
|
||||
FileType = formFile.ContentType
|
||||
|
||||
@@ -86,8 +86,8 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
List<string> permissions = permissionService.GetMenuPermission(user);
|
||||
|
||||
LoginUser loginUser = new(user, roles, permissions);
|
||||
CacheHelper.SetCache(GlobalConstant.UserPermKEY + user.UserId, loginUser);
|
||||
return SUCCESS(JwtUtil.GenerateJwtToken(HttpContext.AddClaims(loginUser), jwtSettings.JwtSettings));
|
||||
CacheHelper.SetCache(GlobalConstant.UserPermKEY + user.UserId, permissions);
|
||||
return SUCCESS(JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser), jwtSettings.JwtSettings));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,11 +103,11 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
// //注销登录的用户,相当于ASP.NET中的FormsAuthentication.SignOut
|
||||
// await HttpContext.SignOutAsync();
|
||||
//}).Wait();
|
||||
var id = HttpContext.GetUId();
|
||||
var userid = HttpContext.GetUId();
|
||||
var name = HttpContext.GetName();
|
||||
|
||||
CacheHelper.Remove(GlobalConstant.UserPermKEY + id);
|
||||
return SUCCESS(new { name , id});
|
||||
|
||||
CacheHelper.Remove(GlobalConstant.UserPermKEY + userid);
|
||||
return SUCCESS(new { name , id = userid });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Common;
|
||||
using ZR.Model;
|
||||
@@ -94,7 +95,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
return ToResponse(ApiResult.Error($"新增用户 '{user.UserName}'失败,登录账号已存在"));
|
||||
}
|
||||
|
||||
user.Create_by = User.Identity.Name;
|
||||
user.Create_by = HttpContext.GetName();
|
||||
user.Password = NETCore.Encrypt.EncryptProvider.Md5(user.Password);
|
||||
|
||||
return ToResponse(UserService.InsertUser(user));
|
||||
@@ -112,7 +113,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
{
|
||||
if (user == null || user.UserId <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
|
||||
|
||||
user.Update_by = User.Identity.Name;
|
||||
user.Update_by = HttpContext.GetName();
|
||||
int upResult = UserService.UpdateUser(user);
|
||||
|
||||
return ToResponse(upResult);
|
||||
|
||||
@@ -130,27 +130,6 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
return context != null ? context.Request.Path.Value : "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///组装Claims
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Claim> AddClaims(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))
|
||||
};
|
||||
|
||||
//写入Cookie
|
||||
//WhiteCookie(context, claims);
|
||||
return claims;
|
||||
}
|
||||
|
||||
private static void WhiteCookie(HttpContext context, List<Claim> claims)
|
||||
{
|
||||
//2.创建声明主题 指定认证方式 这里使用cookie
|
||||
|
||||
@@ -33,11 +33,12 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseAddTaskSchedulers(this IApplicationBuilder app)
|
||||
{
|
||||
var _tasksQzService = (ISysTasksQzService)App.GetRequiredService(typeof(ISysTasksQzService));
|
||||
//var _tasksQzService = (ISysTasksQzService)App.GetRequiredService(typeof(ISysTasksQzService));
|
||||
|
||||
ITaskSchedulerServer _schedulerServer = App.GetRequiredService<ITaskSchedulerServer>();
|
||||
|
||||
var tasks = _tasksQzService.GetList(m => m.IsStart);
|
||||
//var tasks = _tasksQzService.GetList(m => m.IsStart);
|
||||
var tasks = SqlSugar.IOC.DbScoped.SugarScope.Queryable<Model.System.SysTasksQz>().Where(m => m.IsStart).ToList();
|
||||
|
||||
//程序启动后注册所有定时任务
|
||||
foreach (var task in tasks)
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Common;
|
||||
using ZR.Model.System;
|
||||
|
||||
namespace ZR.Admin.WebApi.Framework
|
||||
@@ -124,9 +125,15 @@ namespace ZR.Admin.WebApi.Framework
|
||||
{
|
||||
try
|
||||
{
|
||||
var userData = jwtToken.FirstOrDefault(x => x.Type == ClaimTypes.UserData);
|
||||
|
||||
LoginUser loginUser = JsonConvert.DeserializeObject<LoginUser>(value: userData?.Value);
|
||||
var userData = jwtToken.FirstOrDefault(x => x.Type == ClaimTypes.UserData).Value;
|
||||
var loginUser = JsonConvert.DeserializeObject<LoginUser>(userData);
|
||||
var permissions = (List<string>)CacheHelper.GetCache(GlobalConstant.UserPermKEY + loginUser?.UserId);
|
||||
if (loginUser?.UserName == "admin")
|
||||
{
|
||||
permissions = new List<string>() { GlobalConstant.AdminPerm };
|
||||
}
|
||||
if (permissions == null) return null;
|
||||
loginUser.Permissions = permissions;
|
||||
return loginUser;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -135,5 +142,27 @@ namespace ZR.Admin.WebApi.Framework
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///组装Claims
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Claim> AddClaims(LoginUser user)
|
||||
{
|
||||
if (user?.Permissions.Count > 50)
|
||||
{
|
||||
user.Permissions = new List<string>();
|
||||
}
|
||||
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))
|
||||
};
|
||||
|
||||
return claims;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user