protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { if(Request.QueryString["id"]!=null) { WeixinKeFuService wkfs = new WeixinKeFuService(); WeixinKeFuInfo wkfinfo = wkfs.GetWeixinKeFuInfoByid(int.Parse(Request.QueryString["id"].ToString())); this.lbduihuamsg.Text = wkfinfo.UserOpenId.ToString(); this.lbduihua1.Text = "正在于"; this.lbduihua2.Text = "对话中......."; WxMessageService wms = new WxMessageService(); WxMessageInfo wminfo = wms.GetTopLastFaSongDateByUId(wkfinfo.UId.ToString()); if (wminfo != null) { DateTime datesystemss = DateTime.Parse(System.DateTime.Now.ToString()); DateTime lastLoingDatess = DateTime.Parse(wminfo.FaSongDate.ToString()); TimeSpan ts11 = new TimeSpan(datesystemss.Ticks); TimeSpan ts22 = new TimeSpan(lastLoingDatess.Ticks); TimeSpan ts33 = ts11.Subtract(ts22).Duration(); string chaodays = ts33.TotalDays.ToString(); if (double.Parse(chaodays) >=2) { this.LinkBtnSubSend.Enabled = false; this.errmsg.InnerText = "会话已结束!超过48小时不能主动推送信息给该用户!"; this.lbduihua1.Text = "已经于"; this.lbduihua2.Text = "失去连接.....除非该用户主动会话才能重新建立连接!"; this.txtMessage.Attributes.Add("readonly","true"); } } BindMsgList(); this.DataBind(); } } } private void BindMsgList() { string id = Request.QueryString["id"].ToString(); WxMessageService wms = new WxMessageService(); List<WxMessageInfo> wmlist = wms.GetAllMessageList(id); if(wmlist.Count>0) { this.RepeaterMessageList.DataSource = wmlist; this.RepeaterMessageList.DataBind(); } } protected void timeTick_Tick(object sender, EventArgs e) { BindMsgList(); } /// <summary> /// 推送消息到用户 /// </summary> /// <param></param> /// <param></param> protected void LinkBtnSubSend_Click(object sender, EventArgs e) { if(String.IsNullOrWhiteSpace(this.txtMessage.Value.ToString().Trim())) { this.errmsg.InnerText = "发送的内容不能为空!"; this.txtMessage.Focus(); return; } if (this.txtMessage.Value.ToString().Length < 5 || this.txtMessage.Value.ToString().Length > 200) { this.errmsg.InnerText = "发送内容应在5-200个字符之间!"; this.txtMessage.Focus(); return; } //如果存在直接保存消息记录 WxMessageService wms = new WxMessageService(); WxMessageInfo wminfo = new WxMessageInfo(); wminfo.FromUser = "我"; wminfo.ToUser = this.lbduihuamsg.Text.ToString(); wminfo.Content = this.txtMessage.Value.ToString().Trim(); wminfo.FaSongDate = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); wminfo.UId = Request.QueryString["id"].ToString(); wms.AddWxMessageInfo(wminfo); WeiXinServer wxs = new WeiXinServer(); string res = ""; ///从缓存读取accesstoken string Access_token = Cache["Access_token"] as string; if(Access_token==null) { //如果为空,重新获取 Access_token = wxs.GetAccessToken(); //设置缓存的数据7000秒后过期 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); } string Access_tokento = Access_token.Substring(17, Access_token.Length - 37); string txtcontent = this.txtMessage.Value.ToString().Replace("<p>", ""); StringBuilder sb = new StringBuilder(); sb.Append(txtcontent.Replace("</p>\r\n", "")); string posturl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Access_tokento; string postData = "{\"touser\":\"" + this.lbduihuamsg.Text.ToString() + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + sb.ToString() + "\"}}"; res = wxs.GetPage(posturl, postData); //使用前需药引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(res); ///获取返回结果的正确|true|false string isright = jsonObj["errcode"].ToString();//0 string istrueorfalse = jsonObj["errmsg"].ToString();//ok if (isright.Equals("0") && istrueorfalse.Equals("ok")) { this.errmsg.InnerText = "消息推送成功!消息已送达微信用户!"; this.txtMessage.Value = ""; } else { this.errmsg.InnerText = "消息推送失败!消息已保存至数据库!"; } } protected void RepeaterMessageList_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { WxMessageInfo wminfo = e.Item.DataItem as WxMessageInfo; Image Imagelaba = e.Item.FindControl("Imagelaba") as Image; Label lbFromUser = e.Item.FindControl("lbFromUser") as Label; lbFromUser.Text = wminfo.FromUser.ToString(); if (wminfo.FromUser.ToString().Equals("我")) { Imagelaba.ImageUrl = "images/fa.gif"; } else { Imagelaba.ImageUrl = "images/shou.gif"; } } }
本文已被整理到了《ASP.NET微信开发教程汇总》,欢迎大家学习阅读。
以上就是会话管理功能的全部核心代码,仅供参考,希望对大家的学习有所帮助。
您可能感兴趣的文章: