using BWP.B3WeChat.BL;
|
|
using BWP.B3WeChat.BO;
|
|
using BWP.B3WeChat.Entities;
|
|
using BWP.B3WeChat.Utils;
|
|
using BWP.Web.Utils;
|
|
using Forks.EnterpriseServices.BusinessInterfaces;
|
|
using Forks.EnterpriseServices.DomainObjects2;
|
|
using Forks.EnterpriseServices.DomainObjects2.DQuery;
|
|
using Forks.EnterpriseServices.SqlDoms;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web;
|
|
using TSingSoft.WebPluginFramework;
|
|
|
|
namespace BWP.Web.Pages
|
|
{
|
|
class WeChatReceive : IHttpHandler
|
|
{
|
|
static Forks.Utils.Logger mLogger = new Forks.Utils.Logger("WeChatReceive");
|
|
string echoStr
|
|
{
|
|
get
|
|
{
|
|
return HttpContext.Current.Request.QueryString["echoStr"];
|
|
}
|
|
}
|
|
public bool IsReusable
|
|
{
|
|
get { return true; }
|
|
}
|
|
|
|
public void ProcessRequest(HttpContext context)
|
|
{
|
|
#region 接入验证
|
|
if (!string.IsNullOrEmpty(echoStr))
|
|
{
|
|
if (InOutMessageUtil.CheckSignature())
|
|
{
|
|
HttpContext.Current.Response.Write(echoStr);
|
|
HttpContext.Current.Response.End();
|
|
}
|
|
return;
|
|
}
|
|
#endregion
|
|
|
|
object result = InOutMessageUtil.GetMessage(context.Request);
|
|
|
|
if (result is ReceivedTextMsg)
|
|
{
|
|
ReceivedTextMsg msg = result as ReceivedTextMsg;
|
|
string str = string.Empty;
|
|
if (msg != null)
|
|
{
|
|
ReplyMsg mes = new ReplyMsg()
|
|
{
|
|
CreateTime = ((int)DateTime.Now.Subtract(DateTime.Parse("1970-01-01")).TotalSeconds).ToString(),
|
|
Content = msg.Content,
|
|
ToUserName = msg.FromUserName,
|
|
FromUserName = InOutMessageUtil.config.SelfAppID,
|
|
MsgType = "text"
|
|
};
|
|
str = XmlUtil.Serializer(mes.GetType(), mes);
|
|
|
|
byte[] arr = Encoding.UTF8.GetBytes(str);
|
|
HttpContext.Current.Response.OutputStream.Write(arr, 0, arr.Length);
|
|
|
|
HttpContext.Current.Response.End();
|
|
|
|
}
|
|
}
|
|
else if (result is ReceivedEventMsg)
|
|
{
|
|
ReceivedEventMsg msg = result as ReceivedEventMsg;
|
|
if (msg.IsUnsubscribeEvent())
|
|
{
|
|
Unsubscribe(msg.FromUserName);
|
|
return;
|
|
}
|
|
|
|
int scene_id;
|
|
if (!msg.TryGetSceneID(out scene_id))
|
|
{
|
|
return;
|
|
}
|
|
var query = new DmoQuery(typeof(QRCode));
|
|
query.Where.Conditions.Add(DQCondition.EQ("ID", scene_id));
|
|
query.Range = SelectRange.Top(1);
|
|
var scene = query.EExecuteScalar<QRCode>();
|
|
|
|
if (scene == null)
|
|
{
|
|
var openID = msg.FromUserName;
|
|
SendMessageUtil.SendQRCodeNotFoundMessage(openID);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
CustomerUserBL.Instance.Follow(scene.Customer, scene.UserId, msg.FromUserName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mLogger.Error(ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
//用户取消关注时删除用户名与微信号的映射以及发给它的审批消息
|
|
private void Unsubscribe(string openID)
|
|
{
|
|
var delCustomerUser = new DQDeleteDom(typeof(CustomerUser));
|
|
delCustomerUser.Where.Conditions.Add(DQCondition.EQ("OpenID", openID));
|
|
delCustomerUser.EExecute();
|
|
|
|
var delApproveMessage = new DQDeleteDom(typeof(ApproveMessage));
|
|
delApproveMessage.Where.Conditions.Add(DQCondition.EQ("OpenID", openID));
|
|
delApproveMessage.EExecute();
|
|
}
|
|
}
|
|
}
|