You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
9.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using Common;
using Common.Extensions;
using Common.Tools.Email;
using Common.Tools;
using Common.DJYModel;
namespace djy.Service.DjyService
{
/// <summary>
/// 常用功能工具
/// </summary>
public class DjyTools:ServBase
{
/// <summary>
/// redis 缓存写入
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="setData"></param>
/// <param name="Key"></param>
/// <param name="TimeOut">过期秒数</param>
/// <returns></returns>
public static bool RedisSet<T>(string Key, T setData,int TimeOut=0) {
var val = "";
if (typeof(T) != typeof(string))
{
val =JsonSerializer.Serialize(setData);
}
else {
val = setData.ToString();
}
if (TimeOut > 0)
{
DbRedis.Set(Key, val, TimeOut);
}
else {
DbRedis.Set(Key, val);
}
return true;
}
/// <summary>
/// 读取redis中的对象数据 不存在返回null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Key"></param>
/// <returns></returns>
public static T RedisGet<T>(string Key) {
var val = DbRedis.Get(Key);
if (val.IsNull())
{
return default(T);
}
else {
return Json.JsonToObject<T>(val);
}
}
/// <summary>
///
/// </summary>
/// <param name="emailArray">逗号间隔的Email地址</param>
/// <param name="Subject">Email主题</param>
/// <param name="bodyhtml">Email内容</param>
/// <param name="SendName">发件人</param>
/// <returns></returns>
public static SendResultEntity EmailSendOnLogs(string emailArray, string Subject, string bodyhtml, string SendName, string LogsMsg)
{
var email = new List<string>();
foreach (var item in emailArray.Split(','))
{
if (item.IsEmail())
email.Add(item);
}
return EmailSendOnLogs(email, Subject, bodyhtml, SendName, LogsMsg);
}
/// <summary>
/// Email发送
/// </summary>
/// <param name="email">收件人</param>
/// <param name="Subject">Email主题</param>
/// <param name="bodyhtml">Email内容</param>
/// <param name="SendName">发件人</param>
/// <param name="SendName">附件</param>
public static SendResultEntity EmailSendOnLogs(List<string> email, string Subject, string bodyhtml, string SendName, string LogsMsg, List<MailFile> FileList = null, SendServerConfigurationEntity MailConfig = null)
{
var get = new SendResultEntity();
try
{
var smlist = new List<string>();
foreach (var item in email)
{
smlist.Add(item.ToLower());
}
var mailBodyEntity = new MailBodyEntity(smlist, Subject, bodyhtml, SendName, null);
if (mailBodyEntity.SenderAddress.IsNull())
{
if (mailBodyEntity.Sender.IsNotNull())
{
mailBodyEntity.SenderAddress = mailBodyEntity.Sender;
}
else
{
mailBodyEntity.SenderAddress = sysOptionConfig.Webconfig.ConfigList["sys.Email_SendAccess"];
}
}
if (MailConfig == null)
{
MailConfig = new SendServerConfigurationEntity
{
SmtpHost = sysOptionConfig.Webconfig.ConfigList["sys.Email_SmtpHost"],
SmtpPort = int.Parse(sysOptionConfig.Webconfig.ConfigList["sys.Email_SmtpPort"]),
SenderAccount = sysOptionConfig.Webconfig.ConfigList["sys.Email_LoginName"],
SenderPassword = sysOptionConfig.Webconfig.ConfigList["sys.Email_Password"],
IsSsl = false
};
}
if (FileList != null)
{
mailBodyEntity.MailFiles = FileList;
}
// mailBodyEntity.Sender = MailConfig.SenderAccount;
//mailBodyEntity.SenderAddress = MailConfig.SenderAccount;
if (mailBodyEntity.Sender.IsNull())
{
mailBodyEntity.Sender = MailConfig.SenderAccount;
}
get = MailSendHelp.SendMail(mailBodyEntity, MailConfig);
if (LogsMsg.IsNull())
{
LogsMsg = "Email发送";
}
_LogsAdd(LogsMsg, "email", new { mailBodyEntity, MailConfig }, null, get);
}
catch (Exception ex)
{ get.ResultInformation=ex.Message;
get.ResultStatus = false;
}
return get;
}
/// <summary>
/// Email发送
/// </summary>
/// <param name="email">收件人</param>
/// <param name="Subject">Email主题</param>
/// <param name="bodyhtml">Email内容</param>
/// <param name="SendName">发件人</param>
public static SendResultEntity EmailSendOnLogs(List<string>email,string Subject,string bodyhtml,string SendName,string LogsMsg) {
var get = new SendResultEntity();
try
{
var smlist = new List<string>();
foreach (var item in email)
{
smlist.Add(item.ToLower());
}
var mailBodyEntity = new MailBodyEntity(smlist, Subject, bodyhtml, SendName, null);
if (mailBodyEntity.SenderAddress.IsNull())
{
//2022-3-7因赵工修改未完成导致提箱小票无法发送邮件暂注释掉修改后期再做处理
//if (mailBodyEntity.Sender.IsNotNull())
//{
// mailBodyEntity.SenderAddress = mailBodyEntity.Sender;
//}
//else
{
mailBodyEntity.SenderAddress = sysOptionConfig.Webconfig.ConfigList["sys.Email_SendAccess"];
}
}
var sendServerConfiguration = new SendServerConfigurationEntity
{
SmtpHost = sysOptionConfig.Webconfig.ConfigList["sys.Email_SmtpHost"],
SmtpPort = int.Parse(sysOptionConfig.Webconfig.ConfigList["sys.Email_SmtpPort"]),
SenderAccount = sysOptionConfig.Webconfig.ConfigList["sys.Email_LoginName"],
SenderPassword = sysOptionConfig.Webconfig.ConfigList["sys.Email_Password"],
IsSsl = false
};
get = MailSendHelp.SendMail(mailBodyEntity, sendServerConfiguration);
if (LogsMsg.IsNull())
{
LogsMsg = "Email发送";
}
_LogsAdd(LogsMsg, "paas_email", new { mailBodyEntity, sendServerConfiguration }, null, get);
}
catch (Exception ex)
{
get.ResultInformation = ex.Message;
get.ResultStatus = false;
}
return get;
}
/// <summary>
/// Post请求 和htmlhelp比较增加
/// </summary>
/// <param name="PostData">Json时用对象 form模式用 Dictionary<string, string> form支持 a=1&b=2字符串和对象模式 </param>
/// <param name="RequestUrl">请求地址</param>
/// <param name="postType">提交数据模式 默认json模式</param>
/// <paramref name="FileList">提交的文件数据流</paramref>
/// <param name="JsonNotWebOption"></param>
/// <param name="Token">token</param>
/// <returns></returns>
public static async Task<string> PostOnLogs<T>(T PostData, string RequestUrl,string LogMsg="", PsotType postType = PsotType.Json, List<PostFileListEntity> FileList = null, string Token = null,bool JsonNotWebOption = true)
{
if (LogMsg.IsNull()) {
LogMsg = RequestUrl;
}
var _starttime= DateTime.Now;
var gethtml =await HttpHelp.Post( PostData, RequestUrl, postType, FileList,Token ,JsonNotWebOption:JsonNotWebOption);
var _runtime = DateTime.Now - _starttime;
_LogsAdd(LogMsg, "paas_post", new {runtime=_runtime.TotalSeconds+"秒", RequestUrl, postType, Token, PostData }, null, gethtml);
return gethtml;
}
/// <summary>
/// Get 请求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="RequestUrl"></param>
/// <param name="Getdata"></param>
/// <param name="Token"></param>
/// <returns></returns>
public static async Task<string> GetOnLogs<T>( T Getdata,string RequestUrl, string LogMsg = "", string Token = null) {
if (LogMsg.IsNull())
{
LogMsg = RequestUrl;
}
var _starttime = DateTime.Now;
var gethtml =await HttpHelp.Get(Getdata, RequestUrl, Token);
var _runtime = DateTime.Now - _starttime;
_LogsAdd(LogMsg, "paas_get", new { runtime = _runtime.TotalSeconds + "秒", RequestUrl, Getdata, Token }, null, gethtml);
return gethtml;
}
}
}