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

220 lines
8.2 KiB
C#
Raw Normal View History

2023-07-31 18:41:23 +08:00
using Infrastructure.Extensions;
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-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;
2021-12-16 11:29:03 +08:00
using ZR.Model.System;
2023-05-15 19:54:06 +08:00
using ZR.Service.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;
2023-05-15 19:54:06 +08:00
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 Ok("看到这里页面说明你已经成功启动了本项目:)\n\n" +
2022-09-14 21:49:04 +08:00
"如果觉得项目有用,打赏作者喝杯咖啡作为奖励\n☛☛http://www.izhaorui.cn/doc/support.html\n");
2021-12-12 21:22:18 +08:00
}
2021-09-28 17:42:25 +08:00
/// <summary>
/// 企业消息测试
/// </summary>
/// <param name="msg">要发送的消息</param>
/// <param name="toUser">要发送的人@all所有xxx单独发送对个人</param>
/// <returns></returns>
[Route("/sendMsg")]
[HttpGet]
[Log(Title = "企业消息测试")]
public IActionResult SendMsg(string msg, string toUser = "")
{
WxNoticeHelper.SendMsg("消息测试", msg, toUser, WxNoticeHelper.MsgType.markdown);
return SUCCESS(msg);
}
/// <summary>
2021-09-28 17:42:25 +08:00
/// 发送邮件
/// </summary>
/// <param name="sendEmailVo">请求参数接收实体</param>
/// <returns></returns>
[ActionPermissionFilter(Permission = "tool:email:send")]
2023-06-07 13:41:42 +08:00
[Log(Title = "发送邮件")]
2021-12-03 14:13:05 +08:00
[HttpPost]
2021-09-28 17:42:25 +08:00
public IActionResult SendEmail([FromBody] SendEmailDto sendEmailVo)
{
2023-06-07 13:41:42 +08:00
if (sendEmailVo == null)
2021-09-28 17:42:25 +08:00
{
return ToResponse(ApiResult.Error($"请求参数不完整"));
}
2023-06-07 13:41:42 +08:00
if (string.IsNullOrEmpty(OptionsSetting.MailOptions.FromEmail) || string.IsNullOrEmpty(OptionsSetting.MailOptions.Password))
2021-09-28 17:42:25 +08:00
{
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);
}
2023-06-07 13:41:42 +08:00
string result = mailHelper.SendMail(toUsers, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent);
2021-09-28 17:42:25 +08:00
2023-06-07 13:41:42 +08:00
logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}, 结果{result}");
2022-01-02 15:56:23 +08:00
2023-06-07 13:41:42 +08:00
return SUCCESS(result);
2021-09-28 17:42:25 +08:00
}
2021-12-03 21:59:15 +08:00
#region
/// <summary>
/// 存储文件
/// </summary>
2022-07-12 21:12:52 +08:00
/// <param name="uploadDto">自定义文件名</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-07-12 21:12:52 +08:00
public async Task<IActionResult> UploadFile([FromForm] UploadDto uploadDto, StoreType storeType = StoreType.LOCAL)
2021-12-03 21:59:15 +08:00
{
2022-07-12 21:12:52 +08:00
IFormFile formFile = uploadDto.File;
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
2022-07-12 21:12:52 +08:00
if (OptionsSetting.Upload.NotAllowedExt.Contains(fileExt))
2022-03-26 20:23:47 +08:00
{
return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
}
2022-07-12 21:12:52 +08:00
if (uploadDto.FileNameType == 1)
{
uploadDto.FileName = Path.GetFileNameWithoutExtension(formFile.FileName);
}
else if (uploadDto.FileNameType == 3)
{
uploadDto.FileName = SysFileService.HashFileName();
}
2022-03-26 20:23:47 +08:00
switch (storeType)
{
case StoreType.LOCAL:
2022-06-09 08:37:31 +08:00
string savePath = Path.Combine(WebHostEnvironment.WebRootPath);
2022-07-12 21:12:52 +08:00
if (uploadDto.FileDir.IsEmpty())
2022-05-28 19:17:07 +08:00
{
2022-07-12 21:12:52 +08:00
uploadDto.FileDir = OptionsSetting.Upload.LocalSavePath;
2022-05-28 19:17:07 +08:00
}
2022-07-12 21:12:52 +08:00
file = await SysFileService.SaveFileToLocal(savePath, uploadDto.FileName, uploadDto.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:
2022-07-12 21:12:52 +08:00
int AlimaxContentLength = OptionsSetting.ALIYUN_OSS.MaxSize;
if (OptionsSetting.ALIYUN_OSS.REGIONID.IsEmpty())
2022-03-26 20:23:47 +08:00
{
2022-07-12 21:12:52 +08:00
return ToResponse(ResultCode.CUSTOM_ERROR, "配置文件缺失");
2022-03-26 20:23:47 +08:00
}
2022-07-12 21:12:52 +08:00
if ((fileSize / 1024) > AlimaxContentLength)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + AlimaxContentLength + " MB");
}
file = new(formFile.FileName, uploadDto.FileName, fileExt, fileSize + "kb", uploadDto.FileDir, HttpContext.GetName())
2022-03-26 20:23:47 +08:00
{
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
}
2022-07-12 21:12:52 +08:00
#endregion
2023-05-15 19:54:06 +08:00
/// <summary>
/// 初始化种子数据
/// </summary>
2023-06-11 13:16:22 +08:00
/// <param name="clean">是否清空数据</param>
2023-05-15 19:54:06 +08:00
/// <returns></returns>
[HttpGet]
[ActionPermissionFilter(Permission = "common")]
[Log(BusinessType = BusinessType.INSERT, Title = "初始化数据")]
2023-06-11 13:16:22 +08:00
public IActionResult InitSeedData(bool clean = false)
2023-05-15 19:54:06 +08:00
{
if (!WebHostEnvironment.IsDevelopment())
{
2023-06-11 13:16:22 +08:00
return ToResponse(ResultCode.CUSTOM_ERROR, "导入数据失败");
2023-05-15 19:54:06 +08:00
}
var path = Path.Combine(WebHostEnvironment.WebRootPath, "data.xlsx");
SeedDataService seedDataService = new();
2023-06-11 13:16:22 +08:00
var result = seedDataService.InitSeedData(path, clean);
2023-05-15 19:54:06 +08:00
Console.ForegroundColor = ConsoleColor.Red;
2023-06-11 13:16:22 +08:00
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ForegroundColor = ConsoleColor.White;
2023-05-15 19:54:06 +08:00
return SUCCESS(new
{
2023-06-11 13:16:22 +08:00
result
2023-05-15 19:54:06 +08:00
});
}
2022-07-12 21:12:52 +08:00
}
public class UploadDto
{
2021-12-03 21:59:15 +08:00
/// <summary>
2022-07-12 21:12:52 +08:00
/// 自定文件名
2021-12-03 21:59:15 +08:00
/// </summary>
2022-07-12 21:12:52 +08:00
public string? FileName { get; set; }
/// <summary>
/// 存储目录
/// </summary>
public string? FileDir { get; set; }
/// <summary>
/// 文件名生成类型 1 原文件名 2 自定义 3 自动生成
/// </summary>
public int FileNameType { get; set; }
2022-08-15 20:22:19 +08:00
public IFormFile? File { get; set; }
2021-08-23 16:57:25 +08:00
}
}