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
{
///
/// 常用功能工具
///
public class DjyTools:ServBase
{
///
/// redis 缓存写入
///
///
///
///
/// 过期秒数
///
public static bool RedisSet(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;
}
///
/// 读取redis中的对象数据 不存在返回null
///
///
///
///
public static T RedisGet(string Key) {
var val = DbRedis.Get(Key);
if (val.IsNull())
{
return default(T);
}
else {
return Json.JsonToObject(val);
}
}
///
///
///
/// 逗号间隔的Email地址
/// Email主题
/// Email内容
/// 发件人
///
public static SendResultEntity EmailSendOnLogs(string emailArray, string Subject, string bodyhtml, string SendName, string LogsMsg)
{
var email = new List();
foreach (var item in emailArray.Split(','))
{
if (item.IsEmail())
email.Add(item);
}
return EmailSendOnLogs(email, Subject, bodyhtml, SendName, LogsMsg);
}
///
/// Email发送
///
/// 收件人
/// Email主题
/// Email内容
/// 发件人
/// 附件
public static SendResultEntity EmailSendOnLogs(List email, string Subject, string bodyhtml, string SendName, string LogsMsg, List FileList = null, SendServerConfigurationEntity MailConfig = null)
{
var get = new SendResultEntity();
try
{
var smlist = new List();
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;
}
///
/// Email发送
///
/// 收件人
/// Email主题
/// Email内容
/// 发件人
public static SendResultEntity EmailSendOnLogs(Listemail,string Subject,string bodyhtml,string SendName,string LogsMsg) {
var get = new SendResultEntity();
try
{
var smlist = new List();
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;
}
///
/// Post请求 和htmlhelp比较增加
///
/// Json时用对象, form模式用 Dictionary form支持 a=1&b=2字符串和对象模式
/// 请求地址
/// 提交数据模式 默认json模式
/// 提交的文件数据流
///
/// token
///
public static async Task PostOnLogs(T PostData, string RequestUrl,string LogMsg="", PsotType postType = PsotType.Json, List 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;
}
///
/// Get 请求
///
///
///
///
///
///
public static async Task GetOnLogs( 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;
}
}
}