企业号回调模式php,.net开发微信企业号之回调模式触发功能

1 首先服务器跟域名指向的80端口必须不能缺少。

2 然后在企业号后台配置里选择回调模式,选择回调模式时候需要配置域名。这时候就跟我们的代码相关了,这一次的配置,对于服务器来说,属于get的请求,我们要在服务器上面写上get请求的方法,以及返回值,如果通过,则配置成功,否则无法往下面开发。

贴上代码:#region 回调模式下发送任务

public static void ProcessRequest()

{

Logger.Info(HttpContext.Current.Request.HttpMethod.ToUpper());

if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")

{

GetInitsetInfo();

}

else if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")

{

EvenClick();

}

}

public static PipelineCountForWebchat GetTaskCountForWebchat(int PositionID, String EID)

{

var context = HttpContext.Current;

string url = string.Format("{0}Api/Pipeline/GetTaskCountForWebchat?PositionID={1}&EID={2}", ConfigurationManager.AppSettings["WebAPI"], PositionID, EID);

PipelineCountForWebchat wxMsg = ApiProxy.Call(url, "GET", null, null);

context.Session.Timeout = 24 * 60;

return wxMsg;

}

public static void EvenClick()

{

Logger.Info("Post数据");

string echoString = HttpContext.Current.Request.QueryString["echoStr"];

string sReqMsgSig = HttpContext.Current.Request.QueryString["msg_signature"];//企业号的 msg_signature

string sReqTimeStamp = HttpContext.Current.Request.QueryString["timestamp"];

string sReqNonce = HttpContext.Current.Request.QueryString["nonce"];

Logger.Info("signature=" + sReqMsgSig);

Logger.Info("echoString=" + echoString);

Logger.Info("timestamp=" + sReqTimeStamp);

Logger.Info("nonce=" + sReqNonce);

//读取发过来的信息到inputXml变量中

Stream sin = HttpContext.Current.Request.InputStream;

if (sin.Length == 0)

{

HttpContext.Current.Response.ContentType = "text/xml";

HttpContext.Current.Response.Write("error");

return;

}

byte[] readBytes;

readBytes = new byte[sin.Length];

sin.Read(readBytes, 0, readBytes.Length);

string inputXml = Encoding.UTF8.GetString(readBytes);

Logger.Info("input xml:{0}" + inputXml);

Logger.Info("1");

//使用XMLDocument加载信息结构

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(inputXml);

Logger.Info("2");

string sMsg = ""; //解析之后的明文

Logger.Info("3");

WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpId);

Logger.Info("4");

int ret = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, inputXml, ref sMsg);

Logger.Info(ret.ToString());

if (ret != 0)

{

Logger.Info("ERR: Decrypt fail, ret: " + ret);

return;

}

Logger.Info("解析后数据: " + sMsg);

XmlDocument _nxmlDoc = new XmlDocument();

_nxmlDoc.LoadXml(sMsg);

//把传过来的XML数据各个字段区分出来,并且填到fields这个字典变量中去

Dictionary fields = new Dictionary();

Logger.Info("Xml里的数据有多少个 " + _nxmlDoc.SelectSingleNode("/xml").ChildNodes.Count);

foreach (XmlNode x in _nxmlDoc.SelectSingleNode("/xml").ChildNodes)

{

Logger.Info(x.Name + "*****" + x.InnerText);

fields.Add(x.Name, x.InnerText);

}

// 发送方帐号(open_id)

String fromUserName = fields["FromUserName"];

// 公众帐号

String toUserName = fields["ToUserName"];

// 消息类型

String msgType = fields["MsgType"];

//事件类型

String Event = fields["Event"];

//消息Key

String EventKey = fields["EventKey"];

Logger.Info("用户开始登陆" + fromUserName);

McdPMTContext.GetLoginUserByEmportId(fromUserName);

String Content = "";

Logger.Info("登陆成功继续执行");

//string host = HttpContext.Current.Request.Url.Host;

//string path = HttpContext.Current.Request.Path;

//string redirect_uri = HttpUtility.UrlEncode("http://" + host + path);

string redirect_uri = "http://pmtstg02.mcd.com.cn";

switch (EventKey)

{

case "Task":

if (McdPMTContext.CurrentUser != null)

{

PipelineCountForWebchat Wxmsg = McdPMTContext.GetTaskCountForWebchat(McdPMTContext.CurrentUser.PositionID, McdPMTContext.CurrentUser.EID);

Content = @"

";

//发送消息

SendTextReplyMessage(fromUserName, toUserName, Content);

}

else

{

Content = "您的账号不在我们的服务内,请联系管理员进行设置";

SendTextReplyMessage(fromUserName, toUserName, Content);

}

// String Content = @"

//

//

//

//

//

//

//

//

//

//

// ";

// SendTextReplyMessage(fromUserName, toUserName, Content);

break;

default:

Logger.Info("开始跳转到OnAir页面" + EventKey);

break;

}

}

///

/// 获取基本配置信息

///

public static void GetInitsetInfo()

{

string echoString = HttpContext.Current.Request.QueryString["echoStr"];

string signature = HttpContext.Current.Request.QueryString["msg_signature"];//企业号的 msg_signature

string timestamp = HttpContext.Current.Request.QueryString["timestamp"];

string nonce = HttpContext.Current.Request.QueryString["nonce"];

string sPostData = HttpContext.Current.Request.QueryString["sPostData"];

string sMsg = HttpContext.Current.Request.QueryString["sMsg"];

Logger.Info("signature=" + signature);

Logger.Info("echoString=" + echoString);

Logger.Info("timestamp=" + timestamp);

Logger.Info("nonce=" + nonce);

Logger.Info("sPostData=" + sPostData);

Logger.Info("sMsg=" + sMsg);

Logger.Info("以下是配置信息");

Logger.Info("token=" + token);

Logger.Info("encodingAESKey=" + encodingAESKey);

Logger.Info("corpId=" + corpId);

string decryptEchoString = "";

if (CheckSignature(token, signature, timestamp, nonce, corpId, encodingAESKey, echoString, ref decryptEchoString))

{

if (!string.IsNullOrEmpty(decryptEchoString))

{

HttpContext.Current.Response.Write(decryptEchoString);

HttpContext.Current.Response.End();

}

}

}

public static bool CheckSignature(string token, string signature, string timestamp, string nonce, string corpId, string encodingAESKey, string echostr, ref string retEchostr)

{

WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpId);

int result = wxcpt.VerifyURL(signature, timestamp, nonce, echostr, ref retEchostr);

if (result != 0)

{

Logger.Error("ERR: VerifyURL fail, ret: " + result);

return false;

}

return true;

//ret==0表示验证成功,retEchostr参数表示明文,用户需要将retEchostr作为get请求的返回参数,返回给企业号。

// HttpUtils.SetResponse(retEchostr);

}

//当前语言

public static String Language

{

get

{

return CultureInfo.CurrentCulture.Name;

}

}

#region 新的方法

//发送被动响应文本消息:

///

/// 发送被动响应文本消息,需要先加密在发送

///

/// 发送方

/// 接收方

/// 文本内容

public static void SendTextReplyMessage(string fromUserName, string toUserName, string content)

{

DateTime newTime = Convert.ToDateTime(DateTime.Now.ToLocalTime().ToString());

long strTime = newTime.Ticks;

TextReplyMessage msg = new TextReplyMessage()

{

CreateTime = strTime,

FromUserName = fromUserName,

ToUserName = toUserName,

Content = content

};

/* LogInfo.Info("发送信息2sMsg=" + content);//也可以使用微信的接口发送消息

TextMsg data = new TextMsg(content);

data.agentid = "7";

data.safe = "0";

// data.toparty = "@all";

// data.totag = "@all";

data.touser = toUserName;

BLLMsg.SendMessage(data);*/

WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpId);

string msg_signature = HttpContext.Current.Request.QueryString["msg_signature"];

string timestamp = HttpContext.Current.Request.QueryString["timestamp"];

string nonce = HttpContext.Current.Request.QueryString["nonce"];

string encryptResponse = "";//加密后的文字

string sMsg = msg.ToXmlString();//加密前的文字

int isok = wxcpt.EncryptMsg(sMsg, timestamp, nonce, ref encryptResponse);//

Logger.Info("发送信息sMsg=" + sMsg);

if (isok == 0 && !string.IsNullOrEmpty(encryptResponse))

{

HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

HttpContext.Current.Response.Write(encryptResponse);//被动相应消息不需要调用微信接口

}

else

{

Logger.Info("发送信息失败isok=" + isok);

}

}

//被动响应消息实体

///

/// 被动响应消息类

///

public abstract class ReplyMessage

{

public string ToUserName { get; set; }

public string FromUserName { get; set; }

public long CreateTime { get; set; }

///

/// 将对象转化为Xml消息

///

///

public abstract string ToXmlString();

}

///

/// 被动响应文本消息

///

public class TextReplyMessage : ReplyMessage

{

///

/// 回复的消息内容(换行:在content中能够换行,微信客户端就支持换行显示)

///

public string Content { get; set; }

///

/// 将对象转化为Xml消息

///

///

public override string ToXmlString()

{

string s = @"

{2}

4

{4}

";

s = string.Format(s,

ToUserName ?? string.Empty,

FromUserName ?? string.Empty,

CreateTime.ToString(),

"news",

Content ?? string.Empty

);

return s;

}

}

#endregion

#endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值