Files
shgx_tz_mes_backend_sync/ZR.Admin.WebApi/Controllers/CommonController.cs

195 lines
7.8 KiB
C#
Raw Normal View History

2021-08-23 16:57:25 +08:00
using Infrastructure;
using Infrastructure.Attribute;
2022-03-26 20:23:47 +08:00
using Infrastructure.Enums;
2022-05-28 16:17:02 +08:00
using Infrastructure.Extensions;
2021-09-28 17:42:25 +08:00
using Infrastructure.Model;
2021-12-03 21:59:15 +08:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
2021-08-23 16:57:25 +08:00
using Microsoft.AspNetCore.Mvc;
2021-09-28 17:42:25 +08:00
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
2021-08-23 16:57:25 +08:00
using System;
2021-12-03 21:59:15 +08:00
using System.IO;
using System.Linq;
2022-03-10 21:39:46 +08:00
using System.Threading.Tasks;
2021-12-16 11:29:03 +08:00
using ZR.Admin.WebApi.Extensions;
2021-09-28 17:42:25 +08:00
using ZR.Admin.WebApi.Filters;
using ZR.Common;
2021-12-16 11:29:03 +08:00
using ZR.Model.System;
2021-12-03 21:59:15 +08:00
using ZR.Service.System.IService;
2021-08-23 16:57:25 +08:00
namespace ZR.Admin.WebApi.Controllers
{
2021-12-01 21:50:38 +08:00
/// <summary>
/// 公共模块
/// </summary>
[Route("[controller]/[action]")]
public class CommonController : BaseController
2021-08-23 16:57:25 +08:00
{
2021-09-28 17:42:25 +08:00
private OptionsSetting OptionsSetting;
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
2021-12-03 21:59:15 +08:00
private IWebHostEnvironment WebHostEnvironment;
private ISysFileService SysFileService;
public CommonController(IOptions<OptionsSetting> options, IWebHostEnvironment webHostEnvironment, ISysFileService fileService)
2021-09-28 17:42:25 +08:00
{
2021-12-03 21:59:15 +08:00
WebHostEnvironment = webHostEnvironment;
SysFileService = fileService;
2021-09-28 17:42:25 +08:00
OptionsSetting = options.Value;
}
2021-12-23 21:22:38 +08:00
/// <summary>
/// hello
/// </summary>
/// <returns></returns>
2021-12-12 21:22:18 +08:00
[Route("/")]
2021-12-21 17:59:12 +08:00
[HttpGet]
2021-12-12 21:22:18 +08:00
public IActionResult Index()
{
return Content("Hello看到这里页面说明你已经成功启动了本项目加油吧 少年。");
}
2021-09-28 17:42:25 +08:00
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="sendEmailVo">请求参数接收实体</param>
/// <returns></returns>
[ActionPermissionFilter(Permission = "tool:email:send")]
2021-12-01 21:50:38 +08:00
[Log(Title = "发送邮件", IsSaveRequestData = false)]
2021-12-03 14:13:05 +08:00
[HttpPost]
2021-09-28 17:42:25 +08:00
public IActionResult SendEmail([FromBody] SendEmailDto sendEmailVo)
{
if (sendEmailVo == null || string.IsNullOrEmpty(sendEmailVo.Subject) || string.IsNullOrEmpty(sendEmailVo.ToUser))
{
return ToResponse(ApiResult.Error($"请求参数不完整"));
}
if (string.IsNullOrEmpty(OptionsSetting.MailOptions.From) || string.IsNullOrEmpty(OptionsSetting.MailOptions.Password))
{
return ToResponse(ApiResult.Error($"请配置邮箱信息"));
}
2022-01-02 15:56:23 +08:00
2022-01-02 10:48:27 +08:00
MailHelper mailHelper = new();
2021-09-28 17:42:25 +08:00
2021-12-21 21:29:55 +08:00
string[] toUsers = sendEmailVo.ToUser.Split(",", StringSplitOptions.RemoveEmptyEntries);
if (sendEmailVo.SendMe)
{
toUsers.Append(mailHelper.FromEmail);
}
mailHelper.SendMail(toUsers, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent);
2021-09-28 17:42:25 +08:00
logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}");
2022-01-02 15:56:23 +08:00
2021-09-28 17:42:25 +08:00
return SUCCESS(true);
}
2021-12-03 21:59:15 +08:00
#region
/// <summary>
/// 存储文件
/// </summary>
/// <param name="formFile"></param>
2021-12-16 11:29:03 +08:00
/// <param name="fileDir">存储目录</param>
/// <param name="fileName">自定义文件名</param>
2022-03-26 20:23:47 +08:00
/// <param name="storeType">上传类型1、保存到本地 2、保存到阿里云</param>
2021-12-03 21:59:15 +08:00
/// <returns></returns>
[HttpPost()]
[Verify]
2021-12-12 16:39:33 +08:00
[ActionPermissionFilter(Permission = "common")]
2022-05-28 16:17:02 +08:00
public async Task<IActionResult> UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "", StoreType storeType = StoreType.LOCAL)
2021-12-03 21:59:15 +08:00
{
2021-12-09 22:21:44 +08:00
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
2022-03-26 20:23:47 +08:00
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:
2022-06-09 08:37:31 +08:00
string savePath = Path.Combine(WebHostEnvironment.WebRootPath);
if (fileDir.IsEmpty())
2022-05-28 16:17:02 +08:00
{
2022-06-09 08:37:31 +08:00
fileDir = AppSettings.App(new string[] { "Upload", "localSavePath" });
2022-05-28 16:17:02 +08:00
}
file = await SysFileService.SaveFileToLocal(savePath, fileName, fileDir, HttpContext.GetName(), formFile);
2022-03-26 20:23:47 +08:00
break;
2022-06-09 08:37:31 +08:00
case StoreType.REMOTE:
break;
2022-03-26 20:23:47 +08:00
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;
}
2022-01-11 10:49:38 +08:00
return SUCCESS(new
{
2022-03-26 20:23:47 +08:00
url = file.AccessUrl,
fileName = file.FileName,
2022-03-23 10:22:25 +08:00
fileId = file.Id.ToString()
});
2021-12-03 21:59:15 +08:00
}
/// <summary>
2022-03-27 14:06:34 +08:00
/// 存储文件到阿里云(已弃用)
2021-12-03 21:59:15 +08:00
/// </summary>
/// <param name="formFile"></param>
/// <param name="fileName">自定义文件名</param>
/// <param name="fileDir">上传文件夹路径</param>
2021-12-03 21:59:15 +08:00
/// <returns></returns>
[HttpPost]
[Verify]
2021-12-12 16:39:33 +08:00
[ActionPermissionFilter(Permission = "common")]
2022-03-10 21:39:46 +08:00
public async Task<IActionResult> UploadFileAliyun([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "")
2021-12-03 21:59:15 +08:00
{
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
2022-03-10 21:39:46 +08:00
string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
2022-03-23 14:01:45 +08:00
double fileSize = Math.Round(formFile.Length / 1024.0, 2);//文件大小KB
2022-03-10 21:39:46 +08:00
string[] NotAllowedFileExtensions = new string[] { ".bat", ".exe", ".jar", ".js" };
int MaxContentLength = 15;
if (NotAllowedFileExtensions.Contains(fileExt))
2021-12-03 21:59:15 +08:00
{
return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
}
2022-03-10 21:39:46 +08:00
if ((fileSize / 1024) > MaxContentLength)
2021-12-03 21:59:15 +08:00
{
2022-03-10 21:39:46 +08:00
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
2021-12-03 21:59:15 +08:00
}
2022-03-26 20:23:47 +08:00
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, HttpContext.GetName())
2021-12-16 11:29:03 +08:00
{
StoreType = (int)StoreType.ALIYUN,
2021-12-23 21:22:38 +08:00
FileType = formFile.ContentType
2022-03-23 14:01:45 +08:00
};
file = await SysFileService.SaveFileToAliyun(file, formFile);
if (file.Id <= 0) { return ToResponse(ApiResult.Error("阿里云连接失败")); }
2022-01-11 10:49:38 +08:00
return SUCCESS(new
{
2022-03-23 14:01:45 +08:00
url = file.AccessUrl,
fileName = file.FileName,
fileId = file.Id.ToString()
});
2021-12-03 21:59:15 +08:00
}
#endregion
2021-08-23 16:57:25 +08:00
}
}