|
|
|
@ -1,6 +1,10 @@
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.SqlSugar;
|
|
|
|
|
using DS.Module.UserModule;
|
|
|
|
|
using DS.WMS.Core;
|
|
|
|
|
using DS.WMS.Core.Code.Interface;
|
|
|
|
|
using DS.WMS.MainApi.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using SqlSugar;
|
|
|
|
@ -12,25 +16,29 @@ namespace DS.WMS.MainApi.Controllers
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class MaintenanceController : ApiController
|
|
|
|
|
{
|
|
|
|
|
readonly ISqlSugarClient sugarClient;
|
|
|
|
|
readonly IConfiguration config;
|
|
|
|
|
readonly ApiFox api;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sugarClient"></param>
|
|
|
|
|
public MaintenanceController(ISqlSugarClient sugarClient)
|
|
|
|
|
/// <param name="config"></param>
|
|
|
|
|
public MaintenanceController(IConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
this.sugarClient = sugarClient;
|
|
|
|
|
this.config = config;
|
|
|
|
|
api = new ApiFox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 运行SQL脚本
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sugarClient"></param>
|
|
|
|
|
/// <param name="script">SQL脚本</param>
|
|
|
|
|
/// <param name="dbNames">运行的数据库,为空时执行全部租户库</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost, Route("RunScript"), AllowAnonymous]
|
|
|
|
|
public async Task<IActionResult> RunScriptAsync([FromForm] string script, [FromQuery] params string[] dbNames)
|
|
|
|
|
public async Task<IActionResult> RunScriptAsync([FromServices] ISqlSugarClient sugarClient,
|
|
|
|
|
[FromForm] string script, [FromQuery] params string[] dbNames)
|
|
|
|
|
{
|
|
|
|
|
bool hasError = default;
|
|
|
|
|
var linkList = await sugarClient.Queryable<SysTenantLink>().ToListAsync();
|
|
|
|
@ -72,5 +80,65 @@ namespace DS.WMS.MainApi.Controllers
|
|
|
|
|
|
|
|
|
|
return hasError ? StatusCode(500) : Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发送自定义邮件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userEmailService"></param>
|
|
|
|
|
/// <param name="user"></param>
|
|
|
|
|
/// <param name="model">邮件发送模型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost, Route("SendMail")]
|
|
|
|
|
public async Task<DataResult> SendMailAsync([FromServices] IUserEmailService userEmailService, [FromServices] IUser user,
|
|
|
|
|
[FromBody] SendMailModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(config["MailConfig:ApiUrl"]))
|
|
|
|
|
return DataResult.Failed("请联系站点管理员配置邮件服务URL");
|
|
|
|
|
|
|
|
|
|
var userEmail = await userEmailService.GetCurrentUserEmailAsync();
|
|
|
|
|
if (!userEmail.Succeeded || userEmail.Data == null)
|
|
|
|
|
return DataResult.Failed("你尚未配置邮箱设置,请到系统设置中维护后重试此操作");
|
|
|
|
|
|
|
|
|
|
if (model.Attachments?.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < model.Attachments.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var attachment = model.Attachments[i];
|
|
|
|
|
var response = await api.SendRequestAsync(HttpMethod.Get, attachment.Url);
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
return DataResult.Failed("无法从下列地址中获取文件作为附件:" + attachment.Url);
|
|
|
|
|
|
|
|
|
|
var bytes = await response.Content.ReadAsByteArrayAsync();
|
|
|
|
|
if (bytes == null || bytes.Length == 0)
|
|
|
|
|
return DataResult.Failed("下列地址中获取的文件大小为零,无法生成附件:" + attachment.Url);
|
|
|
|
|
|
|
|
|
|
attachment.Base64 = Convert.ToBase64String(bytes);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(attachment.FileName))
|
|
|
|
|
attachment.FileName = Path.GetFileName(attachment.Url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string sendTo = string.Join(",", model.Receivers);
|
|
|
|
|
dynamic[] mailParams = [new
|
|
|
|
|
{
|
|
|
|
|
SendTo = sendTo,
|
|
|
|
|
model.Title,
|
|
|
|
|
Body = model.Content,
|
|
|
|
|
ShowName = string.IsNullOrEmpty(userEmail.Data.ShowName) ? userEmail.Data.MailAccount : userEmail.Data.ShowName,
|
|
|
|
|
Account = userEmail.Data.MailAccount,
|
|
|
|
|
userEmail.Data.Password,
|
|
|
|
|
Server = userEmail.Data.SmtpServer,
|
|
|
|
|
Port = userEmail.Data.SmtpPort,
|
|
|
|
|
UseSSL = userEmail.Data.SmtpSSL.GetValueOrDefault(),
|
|
|
|
|
Attaches = model.Attachments?.Select(x => new
|
|
|
|
|
{
|
|
|
|
|
AttachName = x.FileName,
|
|
|
|
|
AttachContent = x.Base64
|
|
|
|
|
}).ToList()
|
|
|
|
|
}];
|
|
|
|
|
var mailResult = await api.SendRequestAsync(HttpMethod.Post, config["MailConfig:ApiUrl"]!, mailParams);
|
|
|
|
|
return mailResult.IsSuccessStatusCode ? DataResult.Success : DataResult.Failed("发送失败,邮件服务返回状态码为错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|