不做码农 973c3281b2 Squashed commit of the following:
commit 1daa137d98a3a814e3617606b14c737dbb611f4f
Author: 不做码农 <599854767@qq.com>
Date:   Sat Apr 9 14:51:29 2022 +0800

    IPRatelimit添加白名单接口

commit d05690654b889ae3ab8f4add1b76a5859e1ab89a
Author: 不做码农 <599854767@qq.com>
Date:   Fri Apr 8 20:20:11 2022 +0800

    添加页签openPage支持传递参数

commit 861710079a26294c64d70eeadb7630bf83e3bfc4
Author: 不做码农 <599854767@qq.com>
Date:   Fri Apr 8 12:44:28 2022 +0800

    update FileHelper.cs

commit a0cf47c099533fabaf5bd389cf498f08eead2ef4
Author: 不做码农 <599854767@qq.com>
Date:   Thu Apr 7 13:30:47 2022 +0800

    优化代码生成模板

commit 5b376614d02c2d27b6e90252c2a3169adfb0e612
Author: 不做码农 <599854767@qq.com>
Date:   Thu Apr 7 13:30:13 2022 +0800

    IP限制增加百名单接口

commit 939ec56d1db4cad52f7b64555e16d52b5b061360
Author: 不做码农 <599854767@qq.com>
Date:   Mon Apr 4 21:48:27 2022 +0800

    fix 基础sql脚本bug

commit 19c738b974e14b62929dc69c2c6ac1e8540aad98
Author: 不做码农 <599854767@qq.com>
Date:   Mon Apr 4 18:53:02 2022 +0800

    新增加IPRateLimit限制

commit 6b0e6b11b3fdbb5ab53606cf4d0910ae30886365
Author: 不做码农 <599854767@qq.com>
Date:   Mon Apr 4 12:09:39 2022 +0800

    格式化代码

commit 1024471c64beef683b257b38d4904ab7cd509c82
Author: 不做码农 <599854767@qq.com>
Date:   Mon Apr 4 12:02:32 2022 +0800

    自定义异常新增获取LogAttribute属性
2022-04-09 15:06:45 +08:00

188 lines
7.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Common;
using ZR.Model.System;
using ZR.Service.System.IService;
namespace ZR.Admin.WebApi.Controllers
{
/// <summary>
/// 公共模块
/// </summary>
[Route("[controller]/[action]")]
public class CommonController : BaseController
{
private OptionsSetting OptionsSetting;
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private IWebHostEnvironment WebHostEnvironment;
private ISysFileService SysFileService;
public CommonController(IOptions<OptionsSetting> options, IWebHostEnvironment webHostEnvironment, ISysFileService fileService)
{
WebHostEnvironment = webHostEnvironment;
SysFileService = fileService;
OptionsSetting = options.Value;
}
/// <summary>
/// hello
/// </summary>
/// <returns></returns>
[Route("/")]
[HttpGet]
public IActionResult Index()
{
return Content("Hello看到这里页面说明你已经成功启动了本项目加油吧 少年。");
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="sendEmailVo">请求参数接收实体</param>
/// <returns></returns>
[ActionPermissionFilter(Permission = "tool:email:send")]
[Log(Title = "发送邮件", IsSaveRequestData = false)]
[HttpPost]
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($"请配置邮箱信息"));
}
MailHelper mailHelper = new();
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);
logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}");
return SUCCESS(true);
}
#region
/// <summary>
/// 存储文件
/// </summary>
/// <param name="formFile"></param>
/// <param name="fileDir">存储目录</param>
/// <param name="fileName">自定义文件名</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", 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);
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 = file.AccessUrl,
fileName = file.FileName,
fileId = file.Id.ToString()
});
}
/// <summary>
/// 存储文件到阿里云(已弃用)
/// </summary>
/// <param name="formFile"></param>
/// <param name="fileName">自定义文件名</param>
/// <param name="fileDir">上传文件夹路径</param>
/// <returns></returns>
[HttpPost]
[Verify]
[ActionPermissionFilter(Permission = "common")]
public async Task<IActionResult> UploadFileAliyun([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "")
{
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
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, "上传失败,未经允许上传类型");
}
if ((fileSize / 1024) > MaxContentLength)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
}
SysFile 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("阿里云连接失败")); }
return SUCCESS(new
{
url = file.AccessUrl,
fileName = file.FileName,
fileId = file.Id.ToString()
});
}
#endregion
}
}