diff --git a/Infrastructure/Helper/FileUtil.cs b/Infrastructure/Helper/FileUtil.cs index 9d3b3aa..1a3c000 100644 --- a/Infrastructure/Helper/FileUtil.cs +++ b/Infrastructure/Helper/FileUtil.cs @@ -21,11 +21,11 @@ namespace Infrastructure int day = date.Day; int hour = date.Hour; - string timeDir = $"{year}/{month}/{day}/{hour}";// date.ToString("yyyyMM/dd/HH/"); + string timeDir = $"{year}{month}{day}";// date.ToString("yyyyMM/dd/HH/"); if (!string.IsNullOrEmpty(path)) { - timeDir = $"{path}/{timeDir}"; + timeDir = $"{path}/{timeDir}/"; } return timeDir; } diff --git a/Infrastructure/OptionsSetting.cs b/Infrastructure/OptionsSetting.cs index 6a876d1..84ef25d 100644 --- a/Infrastructure/OptionsSetting.cs +++ b/Infrastructure/OptionsSetting.cs @@ -27,7 +27,7 @@ namespace Infrastructure { public string From { get; set; } public string Password { get; set; } - public string Host { get; set; } + public string Smtp { get; set; } public int Port { get; set; } } /// diff --git a/ZR.Admin.WebApi/Controllers/HomeController.cs b/ZR.Admin.WebApi/Controllers/HomeController.cs index 96bb587..c670ea5 100644 --- a/ZR.Admin.WebApi/Controllers/HomeController.cs +++ b/ZR.Admin.WebApi/Controllers/HomeController.cs @@ -1,12 +1,25 @@ using Infrastructure; using Infrastructure.Attribute; +using Infrastructure.Model; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; using System; +using ZR.Admin.WebApi.Filters; +using ZR.Common; namespace ZR.Admin.WebApi.Controllers { public class HomeController : BaseController { + private OptionsSetting OptionsSetting; + private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + + public HomeController(IOptions options) + { + OptionsSetting = options.Value; + } + /// /// 心跳 /// @@ -31,7 +44,7 @@ namespace ZR.Admin.WebApi.Controllers } string key = ConfigUtils.Instance.GetConfig("DbKey"); string encryptTxt = NETCore.Encrypt.EncryptProvider.DESEncrypt(content, key); - return Ok(new { content, encryptTxt }); + return Ok(new { content, encryptTxt }); } /// @@ -49,5 +62,30 @@ namespace ZR.Admin.WebApi.Controllers string encryptTxt = NETCore.Encrypt.EncryptProvider.DESDecrypt(content, key); return Ok(new { content, encryptTxt }); } + + /// + /// 发送邮件 + /// + /// 请求参数接收实体 + /// + [ActionPermissionFilter(Permission = "tool:email:send")] + 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 MailHelper(OptionsSetting.MailOptions.From, OptionsSetting.MailOptions.Smtp, OptionsSetting.MailOptions.Port, OptionsSetting.MailOptions.Password); + + mailHelper.SendMail(sendEmailVo.ToUser, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent); + + logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}"); + + return SUCCESS(true); + } } } diff --git a/ZR.Admin.WebApi/Controllers/UploadController.cs b/ZR.Admin.WebApi/Controllers/UploadController.cs index 07855f7..f1dece4 100644 --- a/ZR.Admin.WebApi/Controllers/UploadController.cs +++ b/ZR.Admin.WebApi/Controllers/UploadController.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using System; using System.IO; +using ZR.Admin.WebApi.Filters; using ZR.Common; namespace ZR.Admin.WebApi.Controllers @@ -26,24 +27,28 @@ namespace ZR.Admin.WebApi.Controllers /// /// [HttpPost] + [Verify] + [ActionPermissionFilter(Permission = "system")] public IActionResult SaveFile([FromForm(Name = "file")] IFormFile formFile) { if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传图片不能为空"); string fileExt = Path.GetExtension(formFile.FileName); - string savePath = Path.Combine(WebHostEnvironment.WebRootPath, FileUtil.GetdirPath("uploads")); - - if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } - string fileName = FileUtil.HashFileName(Guid.NewGuid().ToString()).ToLower() + fileExt; - string finalFilePath = Path.Combine(savePath, fileName); + string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, FileUtil.GetdirPath("uploads"), fileName); + finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/"); + + if (!Directory.Exists(Path.GetDirectoryName(finalFilePath))) + { + Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath)); + } using (var stream = new FileStream(finalFilePath, FileMode.Create)) { formFile.CopyToAsync(stream); } - string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{finalFilePath.Replace("wwwroot", "").Replace("\\", "/")}"; - return ToResponse(ToJson(1, accessPath)); + string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{FileUtil.GetdirPath("uploads").Replace("\\", " /")}{fileName}"; + return ToResponse(ResultCode.SUCCESS, new { accessPath, fullPath = finalFilePath }); } } } diff --git a/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs b/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs index 88258d7..be6684e 100644 --- a/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs +++ b/ZR.Admin.WebApi/Filters/ActionPermissionFilter.cs @@ -54,12 +54,12 @@ namespace ZR.Admin.WebApi.Filters bool isDemoMode = ConfigUtils.Instance.GetAppConfig("DemoMode", false); //演示公开环境屏蔽权限 - string[] denyPerms = new string[] { "update", "add", "remove", "add", "edit", "delete", "import", "run", "start", "stop", "clear" }; + string[] denyPerms = new string[] { "update", "add", "remove", "add", "edit", "delete", "import", "run", "start", "stop", "clear", "send" }; if (isDemoMode && (denyPerms.Any(f => Permission.ToLower().Contains(f.ToLower())) || Permission.Equals("system"))) { context.Result = new JsonResult(new { code = ResultCode.FORBIDDEN, msg = "演示模式 , 不允许操作" }); } - if (!HasPermi) + if (!HasPermi && !Permission.Equals("system")) { logger.Info($"用户{info.NickName}没有权限访问{context.HttpContext.Request.Path},当前权限[{Permission}]"); context.Result = new JsonResult(new { code = ResultCode.FORBIDDEN, msg = "你没有权限访问" }); diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index 4416867..3b2e17c 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -13,7 +13,7 @@ "urls": "http://localhost:8888", //Ŀurl "sysConfig": { "DBCommandTimeout": 10, - "tokenExpire": 1440,//Jwt tokenʱʱ䣨֣ + "tokenExpire": 1440, //Jwt tokenʱʱ䣨֣ "cors": "http://localhost:8887" //ַ"," }, "DemoMode": false, //Ƿʾģʽ @@ -22,7 +22,7 @@ "UploadDirectory": "/", "UploadUrl": "http://localhost:8888" }, - "ALYUN_OCS": { + "QIQIU_OSS": { "REGIONID": "cn-hangzhou", "KEY": "XX", "SECRET": "XX" @@ -33,5 +33,15 @@ "autoPre": true, //Զȥǰ׺ "author": "zr", "tablePrefix": "live_,sys_" //"ǰ׺ǰ׺öŷָ", + }, + //Ϣ + "MailOptions": { + // + "From": "xxxx@qq.com", + // + "Password": "123456", + //Э + "Smtp": "smtp.qq.com", + "Port": 587 } } diff --git a/ZR.Common/ZR.Common.csproj b/ZR.Common/ZR.Common.csproj index bb64769..1e5d7f1 100644 --- a/ZR.Common/ZR.Common.csproj +++ b/ZR.Common/ZR.Common.csproj @@ -1,10 +1,11 @@ - + net5.0 + diff --git a/ZR.Vue/src/api/common.js b/ZR.Vue/src/api/common.js index 51044b3..b106f3b 100644 --- a/ZR.Vue/src/api/common.js +++ b/ZR.Vue/src/api/common.js @@ -8,3 +8,16 @@ export function upload(data) { headers: { "Content-Type": "multipart/form-data" }, }) } + +/** + * 发送邮件 + * @param {*} data + * @returns + */ +export function sendEmail(data) { + return request({ + url: '/home/SendEmail', + method: 'POST', + data: data, + }) +} diff --git a/ZR.Vue/src/views/index.vue b/ZR.Vue/src/views/index.vue index 6772d69..5439aa5 100644 --- a/ZR.Vue/src/views/index.vue +++ b/ZR.Vue/src/views/index.vue @@ -1,24 +1,28 @@