/// <summary> /// 微信访问Api /// </summary> /// <returns></returns> public ActionResult Index(int id) { // string content = "最炫民族风"; CommonAPI api = new CommonAPI(); api.GetCar(); // GetMusic(content); path = Server.MapPath(@"/log/log.txt"); //根据ID查询企业Token employeeInfo emp = empBll.GetAEmpInfoByApi(id); CommBll.WriteLog(emp.ToKen, path); Valid(emp.ToKen); return View(); }
private void ResponseMsg(string weixinXML, string id) { string strresponse = ""; //回复消息的部分:你的代码写在这里 XmlDocument doc = new XmlDocument(); doc.LoadXml(weixinXML); XmlNodeList list = doc.GetElementsByTagName("xml"); XmlNode xn = list[0]; string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText; string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText; serverUser = ToUserName; clientUser = FromUserName; string content = "", Event = ""; try { Event = xn.SelectSingleNode("//Event").InnerText; CommBll.WriteLog("Event:" + Event, path); } catch { } try { content = xn.SelectSingleNode("//Content").InnerText; CommBll.WriteLog("Content:" + content, path); } catch { } //用户操作事件 if (Event.Equals("subscribe")) { //初次关注,检查数据库中是否存在该用户 ,无,则加入进我们的数据库 //有,则加入,公众帐号与用户的关系表 Fans fans = new Fans(); fans.FromUserName = FromUserName; UserToEmp u2e = new UserToEmp(); u2e.EmpID = int.Parse(id); u2e.UserID = FromUserName; u2e.SubscribeDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); u2e.SubscribeState = 1;//初始为关注状态 FansBll.InsertFansAndToEmp(fans, u2e); //关注的欢迎词 #region 更改日期:2013-7-20 将初次关注回复的文本内容转换为图文内容 //content = empBll.GetAEmpInfoByApi(int.Parse(id)).temp1; //strresponse = ResponseText(content); #endregion employeeInfo emp = empBll.GetAEmpInfoByApi(int.Parse(id)); strresponse = ResponsePicTextMsg("非常感谢您能关注[" + emp.Name + "]", emp.temp1, "http://weixin.cqzuxia.com/FileUpload/" + emp.temp4, "http://weixin.cqzuxia.com/Enterprise/web/index?sessionid=zuxia_SessionID"); } else if (Event.Equals("unsubscribe")) { //删除用户与公众帐号的关系表(假删除) FansBll.UpdateU2E(id, FromUserName); // CommBll.WriteLog("用户取消关注:" + FromUserName, path); } else { //查询该微信公众号的关键词进行匹配 #region 关键词进行匹配 #region CheckFansIsCunZai 判断粉丝是否存在 Fans fans = new Fans() { FromUserName = FromUserName }; UserToEmp u2e = new UserToEmp() { EmpID = int.Parse(id), UserID = FromUserName, SubscribeDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), SubscribeState = 1//初始为关注状态 }; FansBll.InsertFansAndToEmp(fans, u2e); #endregion // CommBll.WriteLog("进入关键词匹配", path); if (content != null) { sendContent = content; List<GuanJianZiHuiFu> hf = HFBll.HuiFu(content, int.Parse(id)); if (hf == null || hf.Count < 1) { List<normal_EmpExtends> listex = extendsBll.GetEmpExtends(int.Parse(id)); #region API if (listex.Count != 0) { //判断是否有插件内容 foreach (var item in listex) { if (content.Contains(item.normal_extends.BaseTable)) { //查询api CommonAPI api = new CommonAPI(); #region API switch (item.normal_extends.BaseTable) { case "天气": strresponse = ResponsePicTextMsg(content + "预报", api.GetWeather(content.Substring(0, content.Length - 2)), "http://weixin.cqzuxia.com/FileUpload/201307/26/20130726135955.jpg", ""); break; case "笑话": strresponse = ResponsePicTextMsg("笑话一箩筐", api.GetXiaoHua(), "http://weixin.cqzuxia.com/FileUpload/201307/21/20130721100709.jpg", ""); break; case "@": strresponse = ResponsePicTextMsg("翻译查询", api.GetFanYi(content.Substring(1, content.Length - 1)), "http://weixin.cqzuxia.com/FileUpload/201307/26/20130726135754.jpg", ""); break; case "gp": strresponse = ResponsePicTextMsg("股票查询", api.GetGuPiao(content.Substring(2, content.Length - 2)), "http://weixin.cqzuxia.com/FileUpload/201307/26/20130726135557.jpg", ""); break; case "会员": //判断用户是否领取我企业的会员卡 FansToCard ftc = new FansToCardService().GetFansToCardByUserID(FromUserName, int.Parse(id)); if (ftc == null) { //1、用户还未领取会员卡 strresponse = ResponsePicTextMsg("申请微信会员卡", "您尚未领取会员卡特权,快来点击领取吧", "http://weixin.cqzuxia.com/FileUpload/201307/25/20130725154809.jpg", "http://weixin.cqzuxia.com/Enterprise/web/MemberCard?userid=" + FromUserName + "&sessionid=" + WeiXin.Core.SecurityEncryption.DESEncrypt(id)); } else { //2、用户已经领取会员卡 strresponse = ResponsePicTextMsg("尊敬的会员:" + ftc.UserToEmp.Fans.TrueName, "尊敬的会员:" + ftc.UserToEmp.Fans.TrueName + ",您的会员卡号为:" + ftc.Cardid + ",会员尊享特权点击进入查询!", "http://weixin.cqzuxia.com/FileUpload/201307/25/20130725154809.jpg", "http://weixin.cqzuxia.com/Enterprise/web/MemberCard?userid=" + FromUserName + "&sessionid=" + WeiXin.Core.SecurityEncryption.DESEncrypt(id)); } break; case "kd": strresponse = ResponsePicTextMsg("快递查询", api.GetKuaiDi(content.Substring(2, content.Length - 2)), "http://weixin.cqzuxia.com/FileUpload/201307/26/20130726140210.jpg", ""); break; case "音乐": strresponse = GetMusic(content.Substring(2, content.Length - 2)); break; default: break; } #endregion break; } } CommBll.WriteLog("strresponse:" + strresponse, path); if (strresponse.Length < 2) { #region 无回复内容,更新时间:2013-7-20 更改为图文格式 //content = empBll.GetAEmpInfoByApi(int.Parse(id)).temp2; //strresponse = ResponseText(content); #endregion employeeInfo emp=empBll.GetAEmpInfoByApi(int.Parse(id)); strresponse = ResponsePicTextMsg("没有找到你需要的信息!", emp.temp2, "http://weixin.cqzuxia.com/FileUpload/" + emp.temp5, "http://weixin.cqzuxia.com/Enterprise/web/index?sessionid=zuxia_SessionID"); } } else { #region 没有数据,更新时间:2013-7-20 更改为图文格式 //content = empBll.GetAEmpInfoByApi(int.Parse(id)).temp2; //strresponse = ResponseText(content); #endregion employeeInfo emp = empBll.GetAEmpInfoByApi(int.Parse(id)); strresponse = ResponsePicTextMsg("没有找到你需要的信息!", emp.temp2, "http://weixin.cqzuxia.com/FileUpload/" + emp.temp5, "http://weixin.cqzuxia.com/Enterprise/web/index?sessionid=zuxia_SessionID"); } #endregion } else { string type = hf[0].GuanJianZiHuiFuType.name; CommBll.WriteLog("type:" + type, path); switch (type) { case "文字": //strresponse = ResponseTextMsg(hf); if (hf[0].img.Length > 1) { strresponse = ResponsePicTextMsg("", hf[0].content, "http://weixin.cqzuxia.com/FileUpload/" + hf[0].img, hf[0].temp1); } else { strresponse = ResponsePicTextMsg("", hf[0].content, "", hf[0].temp1); } break; case "图文": strresponse = ResponsePicTextMsg(hf); break; default: break; } } } #endregion } CommBll.WriteLog("ToUserName:" + ToUserName, path); CommBll.WriteLog("clientUser:" + clientUser, path); FansInteraction fi = new FansInteraction() { Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), EmpId = int.Parse(id), FansOpenId = clientUser, SendContent = sendContent, ReturnContent = strresponse, Temp1 = serverUser, }; //互动 FIBll.InsertInteraction(fi); strresponse = strresponse.Replace("zuxia_UserID", clientUser); strresponse = strresponse.Replace("zuxia_SessionID", WeiXin.Core.SecurityEncryption.DESEncrypt(id)); Response.Write(strresponse); }