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

202 lines
7.6 KiB
C#
Raw Normal View History

2021-08-23 16:57:25 +08:00
using Infrastructure;
using Infrastructure.Attribute;
2022-01-02 17:40:19 +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;
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-08-23 16:57:25 +08:00
/// <summary>
/// 心跳
/// </summary>
/// <returns></returns>
2021-12-07 16:51:14 +08:00
[HttpGet]
2021-08-23 16:57:25 +08:00
public IActionResult Health()
{
return SUCCESS(true);
}
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-01-02 15:56:23 +08:00
/// <param name="uploadType">上传类型 1、发送邮件</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-01-02 17:40:19 +08:00
public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", int uploadType = 0)
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, "上传文件不能为空");
2021-12-03 21:59:15 +08:00
string fileExt = Path.GetExtension(formFile.FileName);
2022-01-02 17:40:19 +08:00
string hashFileName = FileUtil.HashFileName(Guid.NewGuid().ToString()).ToLower();
fileName = (fileName.IsEmpty() ? hashFileName : fileName) + fileExt;
fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
2021-12-16 11:29:03 +08:00
string filePath = FileUtil.GetdirPath(fileDir);
string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, filePath, fileName);
2021-12-03 21:59:15 +08:00
finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/");
2021-12-16 11:29:03 +08:00
double fileSize = formFile.Length / 1024;
2021-12-03 21:59:15 +08:00
if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
}
using (var stream = new FileStream(finalFilePath, FileMode.Create))
{
formFile.CopyTo(stream);
}
2021-12-16 11:29:03 +08:00
string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{filePath.Replace("\\", " /")}{fileName}";
SysFile file = new()
{
AccessUrl = accessPath,
Create_by = HttpContext.GetName(),
FileExt = fileExt,
FileName = fileName,
FileSize = fileSize + "kb",
StoreType = 1,
FileUrl = finalFilePath,
2021-12-23 21:22:38 +08:00
RealName = formFile.FileName,
Create_time = DateTime.Now,
FileType = formFile.ContentType
2021-12-16 11:29:03 +08:00
};
long fileId = SysFileService.InsertFile(file);
2022-01-11 10:49:38 +08:00
return SUCCESS(new
{
2022-01-02 15:56:23 +08:00
url = uploadType == 1 ? finalFilePath : accessPath,
2021-12-16 11:29:03 +08:00
fileName,
fileId
});
2021-12-03 21:59:15 +08:00
}
/// <summary>
/// 存储文件到阿里云
/// </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")]
public 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, "上传文件不能为空");
string fileExt = Path.GetExtension(formFile.FileName);
string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".jpeg", ".webp", ".svga", ".xls", ".doc", ".zip", ".json", ".txt", ".bundle" };
2021-12-23 21:22:38 +08:00
int MaxContentLength = 1024 * 1024 * 15;
2021-12-16 11:29:03 +08:00
double fileSize = formFile.Length / 1024;
2021-12-03 21:59:15 +08:00
if (!AllowedFileExtensions.Contains(fileExt))
{
return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
}
if (formFile.Length > MaxContentLength)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + (MaxContentLength / 1024).ToString() + " MB");
}
2022-01-02 15:56:23 +08:00
(bool, string, string) result = SysFileService.SaveFile(fileDir, formFile, fileName);
2021-12-16 11:29:03 +08:00
long fileId = SysFileService.InsertFile(new SysFile()
{
AccessUrl = result.Item2,
Create_by = HttpContext.GetName(),
FileExt = fileExt,
FileName = result.Item3,
FileSize = fileSize + "kb",
StoreType = 2,
2021-12-23 21:22:38 +08:00
StorePath = fileDir,
RealName = formFile.FileName,
Create_time = DateTime.Now,
FileType = formFile.ContentType
2021-12-16 11:29:03 +08:00
});
2022-01-11 10:49:38 +08:00
return SUCCESS(new
{
url = result.Item2,
2021-12-16 11:29:03 +08:00
fileName = result.Item3,
fileId
});
2021-12-03 21:59:15 +08:00
}
#endregion
2021-08-23 16:57:25 +08:00
}
}