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);
|
||||
|
||||
Reference in New Issue
Block a user