192 lines
7.3 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;
2022-03-10 21:39:46 +08:00
using Snowflake.Core;
2021-08-23 16:57:25 +08:00
using System;
using System.Collections.Generic;
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-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-03-22 22:01:47 +08:00
string hashFileName = FileUtil.HashFileName();
2022-01-02 17:40:19 +08:00
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);
2022-03-22 22:01:47 +08:00
double fileSize = formFile.Length / 1024.0;
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);
}
2022-03-22 22:01:47 +08:00
string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, HttpContext.GetName())
2021-12-16 11:29:03 +08:00
{
2022-03-22 22:01:47 +08:00
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
2021-12-23 21:22:38 +08:00
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 = fileId.ToString()
});
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")]
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
{
2022-03-10 21:39:46 +08:00
if (fileDir.IsEmpty()) fileDir = "uploads";
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);//文件后缀
double fileSize = formFile.Length / 1024.0;//文件大小KB
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-01-02 15:56:23 +08:00
2022-03-10 21:39:46 +08:00
(bool, string, string) result = new();
await Task.Run(() =>
{
result = SysFileService.SaveFile(fileDir, formFile, fileName, "");
});
2022-03-22 22:01:47 +08:00
long id = SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName())
2021-12-16 11:29:03 +08:00
{
2022-03-22 22:01:47 +08:00
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
2021-12-23 21:22:38 +08:00
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,
2022-03-10 21:39:46 +08:00
fileId = id
});
2021-12-03 21:59:15 +08:00
}
#endregion
2021-08-23 16:57:25 +08:00
}
}