using System;
using BP.DA;
using BP.En;
using BP.Web;
using BP.Sys;
using System.Net.Mail;
namespace BP.WF
{
///
/// 消息类型
///
public class SMSMsgType
{
///
/// 自定义消息
///
public const string Self = "Self";
///
/// 抄送消息
///
public const string CC = "CC";
///
/// 待办消息
///
public const string SendSuccess = "SendSuccess";
///
/// 其他
///
public const string Etc = "Etc";
///
/// 退回消息
///
public const string ReturnAfter = "ReturnAfter";
///
/// 移交消息
///
public const string Shift = "Shift";
///
/// 加签消息
///
public const string AskFor = "AskFor";
///
/// 挂起消息
///
public const string Hungup = "HangUp";
///
/// 催办消息
///
public const string DoPress = "DoPress";
///
/// 拒绝挂起的信息
///
public const string RejectHungup = "RejectHungup";
///
/// 错误信息
///
public const string Err = "Err";
}
///
/// 消息状态
///
public enum MsgSta
{
///
/// 未开始
///
UnRun,
///
/// 成功
///
RunOK,
///
/// 失败
///
RunError,
///
/// 禁止发送
///
Disable
}
///
/// 消息属性
///
public class SMSAttr : EntityMyPKAttr
{
///
/// 消息标记(有此标记的不在发送)
///
public const string MsgFlag = "MsgFlag";
///
/// 状态 0 未发送, 1 发送成功,2发送失败.
///
public const string EmailSta = "EmailSta";
///
/// 邮件
///
public const string Email = "Email";
///
/// 邮件标题
///
public const string EmailTitle = "EmailTitle";
///
/// 邮件内容
///
public const string EmailDoc = "EmailDoc";
///
/// 发送人
///
public const string Sender = "Sender";
///
/// 发送给
///
public const string SendTo = "SendTo";
///
/// 插入日期
///
public const string RDT = "RDT";
///
/// 发送日期
///
public const string SendDT = "SendDT";
///
/// 是否读取
///
public const string IsRead = "IsRead";
///
/// 状态 0 未发送, 1 发送成功,2发送失败.
///
public const string MobileSta = "MobileSta";
///
/// 手机
///
public const string Mobile = "Mobile";
///
/// 手机信息
///
public const string MobileInfo = "MobileInfo";
///
/// 是否提示过了
///
public const string IsAlert = "IsAlert";
///
/// 消息类型
///
public const string MsgType = "MsgType";
///
/// 其他参数.
///
public const string Paras = "Paras";
///
/// 打开的连接
///
public const string OpenUrl = "OpenUrl";
///
/// 接受消息的工具 丁丁、微信
///
public const string PushModel = "PushModel";
///
/// 主键
///
public const string WorkID = "WorkID";
}
///
/// 消息
///
public class SMS : EntityMyPK
{
#region 新方法 2013
///
/// 发送消息
///
/// 接受人
/// 标题
/// 内容
/// 标记
/// 类型
/// 扩展参数
public static void SendMsg(string userNo, string msgTitle, string msgDoc, string msgFlag,
string msgType, string paras, Int64 workid, string pushModel = null, string openUrl = null)
{
SMS sms = new SMS();
sms.setMyPK(DBAccess.GenerGUID());
sms.HisEmailSta = MsgSta.UnRun;
sms.Sender = WebUser.No;
sms.SendToEmpNo = userNo;
sms.Title = msgTitle;
sms.DocOfEmail = msgDoc;
sms.MobileInfo = msgDoc;
sms.Sender = BP.Web.WebUser.No;
sms.RDT = DataType.CurrentDateTime;
sms.MsgFlag = msgFlag; // 消息标志.
sms.MsgType = msgType; // 消息类型.'
sms.AtPara = paras;
sms.WorkID = workid;
///如果没有设置模式,就设置邮件.
if (pushModel == null)
pushModel = "Email";
if (DataType.IsNullOrEmpty(openUrl) == false)
sms.SetPara("OpenUrl", openUrl);
if (DataType.IsNullOrEmpty(pushModel) == false)
sms.SetPara("PushModel", pushModel);
sms.Insert();
}
#endregion 新方法
#region 手机短信属性
///
/// 手机号码
///
public string Mobile
{
get
{
return this.GetValStringByKey(SMSAttr.Mobile);
}
set
{
SetValByKey(SMSAttr.Mobile, value);
}
}
///
/// 手机状态
///
public MsgSta HisMobileSta
{
get
{
return (MsgSta)this.GetValIntByKey(SMSAttr.MobileSta);
}
set
{
SetValByKey(SMSAttr.MobileSta, (int)value);
}
}
///
/// 手机信息
///
public string MobileInfo
{
get
{
return this.GetValStringByKey(SMSAttr.MobileInfo);
}
set
{
SetValByKey(SMSAttr.MobileInfo, value);
}
}
#endregion
#region 邮件属性
public string PushModel
{
get
{
return this.GetParaString(SMSAttr.PushModel);
}
}
///
/// 参数
///
public string AtPara
{
get
{
return this.GetValStrByKey("AtPara", "");
}
set
{
this.SetValByKey("AtPara", value);
}
}
///
/// 邮件状态
///
public MsgSta HisEmailSta
{
get
{
return (MsgSta)this.GetValIntByKey(SMSAttr.EmailSta);
}
set
{
this.SetValByKey(SMSAttr.EmailSta, (int)value);
}
}
///
/// Email
///
public string Email
{
get
{
return this.GetValStringByKey(SMSAttr.Email);
}
set
{
SetValByKey(SMSAttr.Email, value);
}
}
///
/// 发送给
///
public string SendToEmpNo
{
get
{
return this.GetValStringByKey(SMSAttr.SendTo);
}
set
{
SetValByKey(SMSAttr.SendTo, value);
}
}
public int IsRead
{
get
{
return this.GetValIntByKey(SMSAttr.IsRead);
}
set
{
this.SetValByKey(SMSAttr.IsRead, (int)value);
}
}
public int IsAlert
{
get
{
return this.GetValIntByKey(SMSAttr.IsAlert);
}
set
{
this.SetValByKey(SMSAttr.IsAlert, (int)value);
}
}
///
/// 消息标记(可以用它来避免发送重复)
///
public string MsgFlag
{
get
{
return this.GetValStringByKey(SMSAttr.MsgFlag);
}
set
{
SetValByKey(SMSAttr.MsgFlag, value);
}
}
///
/// 类型
///
public string MsgType
{
get
{
return this.GetValStringByKey(SMSAttr.MsgType);
}
set
{
SetValByKey(SMSAttr.MsgType, value);
}
}
///
/// 工作ID
///
public Int64 WorkID
{
get
{
return this.GetValInt64ByKey(SMSAttr.WorkID);
}
set
{
SetValByKey(SMSAttr.WorkID, value);
}
}
///
/// 发送人
///
public string Sender
{
get
{
return this.GetValStringByKey(SMSAttr.Sender);
}
set
{
SetValByKey(SMSAttr.Sender, value);
}
}
///
/// 记录日期
///
public string RDT
{
get
{
return this.GetValStringByKey(SMSAttr.RDT);
}
set
{
this.SetValByKey(SMSAttr.RDT, value);
}
}
///
/// 标题
///
public string Title
{
get
{
return this.GetValStringByKey(SMSAttr.EmailTitle);
}
set
{
SetValByKey(SMSAttr.EmailTitle, value);
}
}
///
/// 邮件内容
///
public string DocOfEmail
{
get
{
string doc = this.GetValStringByKey(SMSAttr.EmailDoc);
if (DataType.IsNullOrEmpty(doc))
return this.Title;
return doc.Replace('~', '\'');
}
set
{
SetValByKey(SMSAttr.EmailDoc, value);
}
}
///
/// 邮件内容.
///
public string Doc
{
get
{
string doc = this.GetValStringByKey(SMSAttr.EmailDoc);
if (DataType.IsNullOrEmpty(doc))
return this.Title;
return doc.Replace('~', '\'');
return this.DocOfEmail;
}
set
{
SetValByKey(SMSAttr.EmailDoc, value);
}
}
///
/// 打开的连接
///
public string OpenURL
{
get
{
return this.GetParaString(SMSAttr.OpenUrl);
}
set
{
this.SetPara(SMSAttr.OpenUrl, value);
}
}
#endregion
#region 构造函数
///
/// UI界面上的访问控制
///
public override UAC HisUAC
{
get
{
UAC uac = new UAC();
uac.OpenAll();
return uac;
}
}
///
/// 消息
///
public SMS()
{
}
///
/// Map
///
public override Map EnMap
{
get
{
if (this._enMap != null)
return this._enMap;
Map map = new Map("Sys_SMS", "消息");
map.AddMyPK();
map.AddTBString(SMSAttr.Sender, null, "发送人(可以为空)", false, true, 0, 200, 20);
map.AddTBString(SMSAttr.SendTo, null, "发送给(可以为空)", false, true, 0, 200, 20);
map.AddTBDateTime(SMSAttr.RDT, "写入时间", true, false);
map.AddTBString(SMSAttr.Mobile, null, "手机号(可以为空)", false, true, 0, 30, 20);
map.AddTBInt(SMSAttr.MobileSta, (int)MsgSta.UnRun, "消息状态", true, true);
map.AddTBString(SMSAttr.MobileInfo, null, "短信信息", false, true, 0, 1000, 20);
map.AddTBString(SMSAttr.Email, null, "Email(可以为空)", false, true, 0, 200, 20);
map.AddTBInt(SMSAttr.EmailSta, (int)MsgSta.UnRun, "EmaiSta消息状态", true, true);
map.AddTBString(SMSAttr.EmailTitle, null, "标题", false, true, 0, 3000, 20);
map.AddTBStringDoc(SMSAttr.EmailDoc, null, "内容", false, true);
map.AddTBDateTime(SMSAttr.SendDT, null, "发送时间", false, false);
map.AddTBInt(SMSAttr.IsRead, 0, "是否读取?", true, true);
map.AddTBInt(SMSAttr.IsAlert, 0, "是否提示?", true, true);
//用于获得数据.
map.AddTBInt(SMSAttr.WorkID, 0, "WorkID", true, true);
//消息主键.
map.AddTBString(SMSAttr.MsgFlag, null, "消息标记(用于防止发送重复)", false, true, 0, 200, 20);
map.AddTBString(SMSAttr.MsgType, null, "消息类型(CC抄送,Todolist待办,Return退回,Etc其他消息...)", false, true, 0, 200, 20);
//其他参数.
map.AddTBAtParas(500);
//map.IsShowSearchKey = false;
//map.SearchFields="Tel,Addr,"
this._enMap = map;
return this._enMap;
}
}
#endregion
///
/// 发送邮件
///
///
///
///
///
public static bool SendEmailNowAsync(string mail, string mailTitle, string mailDoc)
{
try
{
System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
//邮件地址.
string emailAddr = BP.Difference.SystemConfig.GetValByKey("SendEmailAddress", null);
if (emailAddr == null)
{
return false;
//emailAddr = "ccbpmtester@tom.com";
}
string emailPassword = BP.Difference.SystemConfig.GetValByKey("SendEmailPass", null);
if (emailPassword == null)
{
return false;
emailPassword = "ccbpm123";
}
mailDoc = DataType.ParseText2Html(mailDoc);
string displayName = BP.Difference.SystemConfig.GetValByKey("SendEmailDisplayName", "驰骋BPM");
myEmail.From = new System.Net.Mail.MailAddress(emailAddr, displayName, System.Text.Encoding.UTF8);
myEmail.To.Add(mail);
myEmail.Subject = mailTitle;
myEmail.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
myEmail.IsBodyHtml = true;
myEmail.Body = mailDoc;
myEmail.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
myEmail.IsBodyHtml = true;//是否是HTML邮件
myEmail.Priority = MailPriority.High; // 邮件优先级
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
if (BP.Difference.SystemConfig.GetValByKeyInt("SendEmailEnableSsl", 1) == 1)
client.EnableSsl = true; //经过ssl加密.
else
client.EnableSsl = false;
client.Credentials = new System.Net.NetworkCredential(emailAddr, emailPassword);
client.Port = BP.Difference.SystemConfig.GetValByKeyInt("SendEmailPort", 587); //使用的端口
client.Host = BP.Difference.SystemConfig.GetValByKey("SendEmailHost", "smtp.gmail.com");
object userState = myEmail;
//调用自带的异步方法
client.Send(myEmail);
/* client.SendMailAsync(myEmail);
client.SendAsync(myEmail, userState);*/
}
catch (Exception e)
{
BP.DA.Log.DebugWriteError(e.Message);
return false;
}
return true;
}
///
/// SAAS发送.
///
public void SendMsgToSAAS()
{
//获取设置.
string messageUrl = BP.Difference.SystemConfig.AppSettings["HandlerOfMessage"];
if (DataType.IsNullOrEmpty(messageUrl) == true)
return;
string httpUrl = messageUrl + "?Sender=" + BP.Web.WebUser.No + "&OrgNo=" + WebUser.OrgNo + "&ToUserIDs=" + this.SendToEmpNo + "&Title=" + this.Title + "&Docs=" + this.GetValDocText();
string json = "";
if (BP.Difference.SystemConfig.CustomerNo.Equals("YuTong") == true)
{
json = "{";
json += " \"token\": '34c45c2b30512e8a8e10467cee45d7ed',";
json += " \"Sender\": \"" + WebUser.No + "\",";
json += " \"OrgNo\": \"" + WebUser.OrgNo + "\",";
json += " \"userid\": \"" + this.SendToEmpNo + "\",";
json += " \"Tel\": \"" + this.Mobile + "\",";
json += " \"title\":\"" + this.Title + "\",";
json += " \"MsgFlg\":\"" + this.MsgFlag + "\",";
json += " \"MobileInfo\":\"" + this.MobileInfo + " \",";
json += " \"contents\":\"" + this.Doc + " \",";
json += " \"wx\":'true',";
json += " \"isEmail\":'true',";
json += " \"url\":\"" + this.OpenURL + " \"}";
}
else
{
json = "{";
json += " \"Sender\": \"" + WebUser.No + "\",";
json += " \"OrgNo\": \"" + WebUser.OrgNo + "\",";
json += " \"SendTo\": \"" + this.SendToEmpNo + "\",";
json += " \"Tel\": \"" + this.Mobile + "\",";
json += " \"Title\":\"" + this.Title + "\",";
json += " \"MsgFlg\":\"" + this.MsgFlag + "\",";
json += " \"MobileInfo\":\"" + this.MobileInfo + " \",";
json += " \"Doc\":\"" + this.Doc + " \",";
json += " \"Url\":\"" + this.OpenURL + " \"}";
}
//微信
if (this.PushModel.Contains("WeiXin") == true)
{
//注册到url里面去.
BP.WF.Glo.HttpPostConnect(httpUrl, json);
}
}
///
/// 插入之后执行的方法.
///
protected override void afterInsert()
{
try
{
//如果是SAAS模式.
if (BP.Difference.SystemConfig.CCBPMRunModel == CCBPMRunModel.SAAS)
{
SendMsgToSAAS();
return;
}
//if (BP.Difference.SystemConfig.CustomerNo.Equals("YuTong") == true)
//{
// DealYuTong();
// return;
//}
if (this.HisEmailSta != MsgSta.UnRun)
return;
#region 发送邮件
if (this.PushModel.Contains("Email") == true && DataType.IsNullOrEmpty(this.Email) == false)
{
string emailStrs = this.Email;
emailStrs = emailStrs.Replace(",", ";");
emailStrs = emailStrs.Replace(",", ";");
//包含多个邮箱
if (emailStrs.Contains(";") == true)
{
string[] emails = emailStrs.Split(';');
foreach (string email in emails)
{
if (DataType.IsNullOrEmpty(email) == true)
continue;
SendEmailNowAsync(email, this.Title, this.DocOfEmail);
}
}
else
{ //单个邮箱
SendEmailNowAsync(this.Email, this.Title, this.DocOfEmail);
}
}
#endregion 发送邮件
#region 发送短消息 调用接口
//发送短消息的前提必须是手机号不能为空
//if (DataType.IsNullOrEmpty(this.Mobile) == true)
// return;
//throw new Exception("发送短消息时接收人的手机号不能为空,否则接受不到消息");
string messageUrl = BP.Difference.SystemConfig.AppSettings["HandlerOfMessage"];
if (DataType.IsNullOrEmpty(messageUrl) == true)
return;
string httpUrl = "";
string json = "{";
json += " \"sender\": \"" + WebUser.No + "\",";
json += " \"sendTo\": \"" + this.SendToEmpNo + "\",";
json += " \"tel\": \"" + this.Mobile + "\",";
json += " \"title\":\"" + this.Title + "\",";
json += " \"msgFlag\":\"" + this.MsgFlag.Replace("WKAlt", "") + "\",";
json += " \"content\":\"" + this.MobileInfo + " \",";
json += " \"openUrl\":\"" + this.OpenURL + " \"}";
//soap = BP.WF.Glo.GetPortalInterfaceSoapClient();
//站内消息
if (this.PushModel.Contains("CCMsg") == true)
{
httpUrl = messageUrl + "?DoType=SendToCCMSG";
BP.WF.Glo.HttpPostConnect(httpUrl, json);
//soap.SendToCCMSG(this.MyPK, WebUser.No, this.SendToEmpNo, this.Mobile, this.MobileInfo, this.Title, this.OpenURL);
}
//短信
if (this.PushModel.Contains("SMS") == true)
{
httpUrl = messageUrl + "?DoType=SMS";
BP.WF.Glo.HttpPostConnect(httpUrl, json);
//soap.SendToWebServices(this.MyPK, WebUser.No, this.SendToEmpNo, this.Mobile, this.MobileInfo,this.Title, this.OpenURL);
}
//钉钉
if (this.PushModel.Contains("DingDing") == true)
{
httpUrl = messageUrl + "?DoType=SendToDingDing&sendTo=" + this.SendToEmpNo + "&title=" + this.Title + "&msgConten=" + this.MobileInfo;
BP.WF.Glo.HttpPostConnect(httpUrl, json);
//soap.SendToDingDing(this.MyPK, WebUser.No, this.SendToEmpNo, this.Mobile, this.MobileInfo, this.Title, this.OpenURL);
}
//微信
if (this.PushModel.Contains("WeiXin") == true)
{
httpUrl = messageUrl + "?DoType=SendToWeiXin&sendTo=" + this.SendToEmpNo;
BP.WF.Glo.HttpPostConnect(httpUrl, json);
//BP.WF.WeiXin.WeiXinMessage.SendMsgToUsers(this.SendToEmpNo, this.Title, this.Doc, WebUser.No);
}
//WebService
if (this.PushModel.Contains("WS") == true)
{
httpUrl = messageUrl + "?DoType=SendToWebServices";
BP.WF.Glo.HttpPostConnect(httpUrl, json);
//soap.SendToWebServices(this.MyPK, WebUser.No, this.SendToEmpNo, this.Mobile, this.MobileInfo, this.Title, this.OpenURL);
}
#endregion 发送短消息 调用接口
}
catch (Exception ex)
{
BP.DA.Log.DebugWriteError("@消息机制没有配置成功." + ex.Message);
}
base.afterInsert();
}
///
/// 设置已读
///
public void DoRead()
{
this.IsRead = 1;
this.Update();
}
}
///
/// 消息s
///
public class SMSs : Entities
{
///
/// 获得实体
///
public override Entity GetNewEntity
{
get
{
return new SMS();
}
}
public SMSs()
{
}
#region 为了适应自动翻译成java的需要,把实体转换成List.
///
/// 转化成 java list,C#不能调用.
///
/// List
public System.Collections.Generic.IList ToJavaList()
{
return (System.Collections.Generic.IList)this;
}
///
/// 转化成list
///
/// List
public System.Collections.Generic.List Tolist()
{
System.Collections.Generic.List list = new System.Collections.Generic.List();
for (int i = 0; i < this.Count; i++)
{
list.Add((SMS)this[i]);
}
return list;
}
#endregion 为了适应自动翻译成java的需要,把实体转换成List.
}
}