最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

asp.net微信開(kāi)發(fā)(自定義會(huì)話(huà)管理)

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:37:58
文檔

asp.net微信開(kāi)發(fā)(自定義會(huì)話(huà)管理)

asp.net微信開(kāi)發(fā)(自定義會(huì)話(huà)管理):和微信用戶(hù)的溝通少不了,總覺(jué)得看起來(lái)微信官網(wǎng)后臺(tái)管理中的會(huì)話(huà)回復(fù)消息有點(diǎn)呆板,所以我這里就自定義了一個(gè)會(huì)話(huà)管理功能,最終效果圖如下: 因?yàn)槲以囀褂酶晃谋疚募﨏KEDITOR來(lái)進(jìn)行編寫(xiě),你看到穩(wěn)中可能會(huì)有<P></p>字段,后臺(tái)獲取數(shù)據(jù)內(nèi)容
推薦度:
導(dǎo)讀asp.net微信開(kāi)發(fā)(自定義會(huì)話(huà)管理):和微信用戶(hù)的溝通少不了,總覺(jué)得看起來(lái)微信官網(wǎng)后臺(tái)管理中的會(huì)話(huà)回復(fù)消息有點(diǎn)呆板,所以我這里就自定義了一個(gè)會(huì)話(huà)管理功能,最終效果圖如下: 因?yàn)槲以囀褂酶晃谋疚募﨏KEDITOR來(lái)進(jìn)行編寫(xiě),你看到穩(wěn)中可能會(huì)有<P></p>字段,后臺(tái)獲取數(shù)據(jù)內(nèi)容

和微信用戶(hù)的溝通少不了,總覺(jué)得看起來(lái)微信官網(wǎng)后臺(tái)管理中的會(huì)話(huà)回復(fù)消息有點(diǎn)呆板,所以我這里就自定義了一個(gè)會(huì)話(huà)管理功能,最終效果圖如下:

因?yàn)槲以囀褂酶晃谋疚募﨏KEDITOR來(lái)進(jìn)行編寫(xiě),你看到穩(wěn)中可能會(huì)有<P></p>字段,后臺(tái)獲取數(shù)據(jù)內(nèi)容時(shí),替換為空字符即可:如下

 string txtcontent = this.txtMessage.Value.ToString().Replace("<p>", "");
 StringBuilder sb = new StringBuilder();
 sb.Append(txtcontent.Replace("</p>\r\n", ""));

在我個(gè)人理解,做會(huì)話(huà)管理,無(wú)非就是將用戶(hù)對(duì)話(huà)信息(用戶(hù)發(fā)過(guò)來(lái)的數(shù)據(jù),發(fā)給用戶(hù)的數(shù)據(jù))存入數(shù)據(jù)庫(kù),根據(jù)用戶(hù)的數(shù)據(jù)時(shí)間和回復(fù)用戶(hù)數(shù)據(jù)的時(shí)間來(lái)和當(dāng)天系統(tǒng)的時(shí)間做對(duì)比,如果大于多少分鐘或者多少個(gè)小時(shí)就不可以再主動(dòng)和用戶(hù)對(duì)話(huà),就算我這里是根據(jù)微信官網(wǎng)48小時(shí)來(lái)進(jìn)行限制的,超過(guò)48小時(shí)禁用控件,如下圖:


廢話(huà)少說(shuō),上代碼:需要用到的兩個(gè)類(lèi),數(shù)據(jù)庫(kù)也要?jiǎng)?chuàng)建和類(lèi)相同的名字(至少我試這么做的)
 

/// <summary>
 /// 微信會(huì)話(huà)記錄類(lèi),用戶(hù)存儲(chǔ)會(huì)話(huà)記錄列表
 /// </summary>
 public class WeixinKeFuInfo
 {
 public int UId { get; set; }//編號(hào)
 public string UserOpenId { get; set; }//用戶(hù)的OpenID
 public string UserContent { get; set; }//用戶(hù)內(nèi)容
 public string CreaterDate { get; set; }//創(chuàng)建時(shí)間
 }

 

 /// <summary>
 /// 與微信用戶(hù)會(huì)話(huà)的消息記錄類(lèi)
 /// </summary>
 public class WxMessageInfo
 {
 public int msgId { get; set; }//消息ID
 public string FromUser { get; set; }//發(fā)送用戶(hù)
 public string ToUser { get; set; }//接收用戶(hù)
 public string Content { get; set; }//發(fā)送內(nèi)容
 public string FaSongDate { get; set; }//發(fā)送時(shí)間
 public string UId { get; set; }//會(huì)話(huà)用戶(hù)的UId,微信會(huì)話(huà)記錄類(lèi)外鍵
 }

 /// <summary>
 /// 發(fā)送文本。。。。。。。還記得這個(gè)方法嗎?就是根據(jù)用戶(hù)發(fā)送過(guò)來(lái)的消息類(lèi)型進(jìn)行判斷后,如果是文本就回復(fù)發(fā)送此方法內(nèi)的內(nèi)容
 /// </summary>
 /// <param name="requestXML"></param>
 private void SendTextCase(RequestXML requestXML)
 {

      WeixinKeFuService wkfs = new WeixinKeFuService();//自己寫(xiě)的服務(wù)類(lèi)
 //根據(jù)openId查詢(xún)數(shù)據(jù)庫(kù)會(huì)話(huà)記錄是否存在
 WeixinKeFuInfo wkfinfoinfo = wkfs.GetWeixinKeFuInfoByOpenId(requestXML.FromUserName.ToString());
 if (wkfinfoinfo != null)
 {
 //如果存在直接保存消息記錄
 WxMessageService wms = new WxMessageService();
 WxMessageInfo wminfo = new WxMessageInfo();
 wminfo.FromUser = requestXML.FromUserName.ToString();
 wminfo.ToUser = "我";
 wminfo.Content = requestXML.Content.ToString();
 wminfo.FaSongDate = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
 wminfo.UId = wkfinfoinfo.UId.ToString();
 wms.AddWxMessageInfo(wminfo);
 }
 else
 {
 //如果不存在新建會(huì)話(huà)記錄
 WeixinKeFuInfo wkfinfo = new WeixinKeFuInfo();
 wkfinfo.UserOpenId = requestXML.FromUserName.ToString();
 wkfinfo.UserContent = requestXML.Content.ToString();
 wkfinfo.CreaterDate = System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
 wkfs.AddWeixinKeFuInfo(wkfinfo);

 string responseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName, "正在接入.請(qǐng)稍候.....");

 HttpContext.Current.Response.ContentType = "text/xml";
 HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
 HttpContext.Current.Response.Write(responseContent);
 HttpContext.Current.Response.End();
 }

  }

以上代碼實(shí)現(xiàn)了數(shù)據(jù)庫(kù)的插入,那么取出來(lái),顯示的頁(yè)面,核心代碼:WeiXinSessionList.aspx,前臺(tái)代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WeiXinSessionList.aspx.cs" Inherits="DQWebSite.Administrator.WeiXinSessionList" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title></title>
 <meta http-equiv="refresh" content="60; url=WeiXinSessionList.aspx" />
 <link href="css/style.css" rel="Stylesheet" type="text/css" />
 <style type="text/css">
 .tablestyle { width:1124px; margin:10px auto 10px auto; border:1px solid #ecd9df; text-align:center;
 }
 th { height:35px;background-image:url('images/th.gif'); background-repeat:repeat-x;
 }
 tr { height:30px;
 }
 td {
 border-left:1px dotted #a7b5bc;
 }
 .trcolor { background-color:#ecd9df;
 }
 tr:hover { cursor:pointer;
 }
 #FenPage { width:1124px; height:25px; line-height:25px; text-align:center; margin:20px auto 20px auto;
 }
 .linka { color:#0094ff; cursor:pointer;
 }
 .fenyebtn {width:60px; height:25px; border:1px solid #ced9df; border-radius:5px; text-align:center; line-height:25px; float:right;
 }
 .fenyebtn2 { width:60px; height:25px; border:1px solid #ced9df; border-radius:5px; text-align:center; line-height:25px;margin-left:10px;float:right;
 }
 .toPageIndex { width:60px;height:25px; background-image:url('images/inputbg.gif'); margin-left:10px; background-repeat:repeat-x;border-top:solid 1px #a7b5bc; border-left:solid 1px #a7b5bc; border-right:solid 1px #ced9df; border-bottom:solid 1px #ced9df; text-align:center; float:right;
 }
 .gotoPagebtn { width:60px; height:25px; border:1px solid #ced9df; border-radius:5px; text-align:center; line-height:25px;margin-left:10px;float:right; background-color:#ced9df;
 }
 .deletebtn {float:left;width:100px; color:#000; height:25px; background-color:#ced9df; border:1px solid #ced9df; border-radius:5px; text-align:center;
 }
 #BtnDeleteSelected:hover { cursor:pointer;
 }
 .publishHuoDong {background-color:#ced9df; border:1px solid #ced9df; border-radius:5px;text-align:center; width:100px; color:#000; height:25px; line-height:25px; float:left;
 }
 a { color:#08a5e0;
 }
 .droplist { background-image:url('images/inputbg.gif'); background-repeat:repeat-x; width:120px; height:25px; border:1px solid #ced9df;
 }
 </style>
 <script type="text/javascript">
 
 function EditRoster(piciNumber) {
 var url = 'MessageWindow.aspx?id=' + piciNumber; //轉(zhuǎn)向網(wǎng)頁(yè)的地址;
 var name = 'add'; //網(wǎng)頁(yè)名稱(chēng),可為空;
 var iWidth = 850; //彈出窗口的寬度;
 var iHeight = 600; //彈出窗口的高度;
 //獲得窗口的垂直位置
 var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
 //獲得窗口的水平位置
 var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
 window.open(url, name, 'height=' + iHeight + ',,innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');
 
 }
 </script>
</head>
<body style="background-image:url('images/ygbg.png'); background-repeat:repeat;">
 <form id="form1" runat="server">
 <div class="place">
 <span>位置:</span>
 <ul class="placeul">
 <li><a href="WelCome.aspx" target="rightFrame">首頁(yè)</a></li>
 <li>微信管理</li>
 <li>德橋員工服務(wù)中心--會(huì)話(huà)管理</li>
 </ul>
 </div>
 <div>
 <div style="width:1124px;margin:10px auto 0px 20px;">
 <font style="color:red;"> 根據(jù)微信公眾平臺(tái)規(guī)定,用戶(hù)主送發(fā)起會(huì)話(huà)后,48小時(shí)之內(nèi)可與該用戶(hù)進(jìn)行無(wú)限次會(huì)話(huà),超過(guò)48小時(shí)不能主動(dòng)會(huì)話(huà)。</font>
 </div>
 <asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
 <ContentTemplate>
 <table class="tablestyle">
 <asp:Repeater ID="RepeaterGustBookList" runat="server" OnItemDataBound="RepeaterGustBookList_ItemDataBound">
 <HeaderTemplate>
 <tr>
 <th style="width:50px;"><asp:CheckBox ID="CheckAll" runat="server"
 oncheckedchanged="CheckAll_CheckedChanged" /><br /></th>
 <th style="width:105px;">openId</th>
<%-- <th style="width:150px;">昵稱(chēng)</th>
 <th style="width:50px;">性別</th>--%>
 <th style="width:200px;">初始內(nèi)容</th>
 <th style="width:100px;">接入時(shí)間</th>
 <th style="width:100px;">近期會(huì)話(huà)</th>
 <th style="width:80px;">會(huì)話(huà)狀態(tài)</th>
 <th style="width:150px;">已過(guò)時(shí)間</th>
 <th style="width:100px;">會(huì)話(huà)操作</th>
 </tr>
 </HeaderTemplate>
 <ItemTemplate>
 <tr style='%#(Container.ItemIndex%2==0)?"#fff":"#ced9ff"%>' >
 <td><asp:CheckBox ID="CheckIn" runat="server" /></td>
 <td><asp:Label ID="lbUId" runat="server" Visible="false" Text="Label"></asp:Label>
 <%# Eval("UserOpenId")%>
 </td>
<%-- <td><asp:Label ID="lbNikeName" runat="server" Text="未知"></asp:Label></td>
 <td><asp:Label ID="lbSex" runat="server" Text="未知"></asp:Label></td>--%>
 <td><%# (Eval("UserContent").ToString().Length>10)?Eval("UserContent").ToString().Substring(0,10)+"..":Eval("UserContent") %></td>
 <td><%# Eval("CreaterDate")%></td>
 <td><asp:Label ID="lblastDate" runat="server" Text="未知"></asp:Label></td>
 <td><asp:Label ID="lbState" runat="server" Text="未知"></asp:Label></td>
 <td><asp:Label ID="lbChaoshi" runat="server" Text="未知"></asp:Label></td>
 <td><a onclick="EditRoster(<%# Eval("UId") %>);">啟動(dòng)會(huì)話(huà)</a> 
<%-- <asp:HyperLink ID="HyperLinkNewSession" runat="server">新建</asp:HyperLink> --%>
 </td>
 </tr>
 </ItemTemplate>
 </asp:Repeater>
 </table>
 <div id="FenPage">
 <asp:LinkButton ID="LinkBtnToPage" CssClass="gotoPagebtn" runat="server" OnClick="LinkBtnToPage_Click">確定</asp:LinkButton>
 <asp:TextBox ID="txtPageIndex" CssClass="toPageIndex" runat="server"></asp:TextBox> 
 <asp:HyperLink ID="lnkLast" runat="server"><span class="fenyebtn2">>>|</span></asp:HyperLink> 
 <asp:HyperLink ID="lnkNext" runat="server"><span class="fenyebtn2">></span></asp:HyperLink> 
 <asp:HyperLink ID="lnkTop" runat="server"><span class="fenyebtn2"><</span></asp:HyperLink> 
 <asp:HyperLink ID="lnkFist" runat="server"><span class="fenyebtn">|<<</span></asp:HyperLink> 
 <asp:Button ID="BtnDelete" runat="server" Text="刪除選中項(xiàng)" CssClass="deletebtn"
 BackColor="ButtonFace" onclick="BtnDelete_Click" />
 <span style="float:left;margin-left:20px;">當(dāng)前第</span>
 <span style="float:left; color:red;"><asp:Label ID="lbPageIndex" runat="server" Text=""></asp:Label></span>
 <span style="float:left;margin-left:5px;">頁(yè)/</span>
 <span style="float:left;margin-left:5px;">共</span>
 <span style="float:left;color:red;"><asp:Label ID="lbCountPage" runat="server" Text=""></asp:Label></span>
 <span style="float:left;margin-left:5px;">頁(yè)</span>
 <span style="float:left;margin-left:10px;"><asp:Label ID="lbPageSize" runat="server" Text=""></asp:Label></span>
 <span style="float:left;margin-left:10px;">共搜索到 </span>
 <span style="float:left;margin-left:5px; color:red;"><asp:Label ID="lbCountData" runat="server" Text=""></asp:Label></span>
 <span style="float:left;margin-left:5px;">條記錄.</span>
 </div>
 </ContentTemplate>
 </asp:UpdatePanel>
 </div>
 </form>
</body>
</html>

WeiXinSessionList.aspx.cs后臺(tái)代碼如下:

PagedDataSource pds = new PagedDataSource();
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!Page.IsPostBack)
 {
 BindGustBookList();
 this.DataBind();
 UsersInfo user = Session["Users"] as UsersInfo;
 if (user != null && user.RolsId == 1)
 {
 this.BtnDelete.Enabled = true;
 }
 else
 {
 this.BtnDelete.Enabled = false;
 }
 }
 }
 private void BindGustBookList()
 {

 WeixinKeFuService wkf = new WeixinKeFuService();
 List<WeixinKeFuInfo> wkflists = wkf.GetAllWeixinKeFuInfoList();


 //if (this.DDLState.SelectedValue.Equals("1"))
 //{
 // lists = gbs.GetAllGustBookListByState();
 //}
 //else if (this.DDLState.SelectedValue.Equals("2"))
 //{
 // lists = gbs.GetAllGustBookListByState2();
 //}
 //else
 //{
 // lists = gbs.GetAllGustBookList();
 //}

 pds.DataSource = wkflists;
 pds.AllowPaging = true;
 pds.PageSize = 20;//每頁(yè)顯示為20條
 int CurrentPage;


 if (!String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
 {

 CurrentPage = Convert.ToInt32(this.txtPageIndex.Text.ToString().Trim());
 }
 else if (Request.QueryString["Page"] != null)
 {
 CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
 }
 else
 {
 CurrentPage = 1;
 }
 pds.CurrentPageIndex = CurrentPage - 1;//當(dāng)前頁(yè)的索引就等于當(dāng)前頁(yè)碼-1;
 if (!pds.IsFirstPage)
 {
 //Request.CurrentExecutionFilePath 為當(dāng)前請(qǐng)求的虛擬路徑
 this.lnkTop.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);
 this.lnkFist.Enabled = this.lnkTop.Enabled = true;
 this.lnkNext.Enabled = this.lnkLast.Enabled = true;
 }
 else
 {
 this.lnkFist.Enabled = this.lnkTop.Enabled = false;
 this.lnkNext.Enabled = this.lnkLast.Enabled = true;
 this.lnkFist.Attributes.Add("style", "color:#ced9df;");
 this.lnkTop.Attributes.Add("style", "color:#ced9df;");
 this.lnkNext.Attributes.Remove("style");
 this.lnkLast.Attributes.Remove("style");
 }
 if (!pds.IsLastPage)
 {
 //Request.CurrentExecutionFilePath 為當(dāng)前請(qǐng)求的虛擬路徑
 this.lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);
 this.lnkFist.Enabled = this.lnkTop.Enabled = true;
 this.lnkNext.Enabled = this.lnkLast.Enabled = true;
 }
 else
 {
 this.lnkNext.Enabled = this.lnkLast.Enabled = false;
 this.lnkFist.Enabled = this.lnkTop.Enabled = true;
 this.lnkNext.Attributes.Add("style", "color:#ced9df;");
 this.lnkLast.Attributes.Add("style", "color:#ced9df;");
 this.lnkFist.Attributes.Remove("style");
 this.lnkTop.Attributes.Remove("style");
 }
 this.lnkFist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);//跳轉(zhuǎn)至首頁(yè)
 this.lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(pds.PageCount);//跳轉(zhuǎn)至末頁(yè)

 this.RepeaterGustBookList.DataSource = pds;
 this.RepeaterGustBookList.DataBind();

 this.lbCountData.Text = wkflists.Count.ToString();
 this.lbPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();
 this.lbPageSize.Text = "每頁(yè)" + pds.PageSize.ToString() + "條記錄";
 this.lbCountPage.Text = pds.PageCount.ToString();
 this.txtPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();

 if (int.Parse(wkflists.Count.ToString()) <= int.Parse(pds.PageSize.ToString()))
 {
 this.lnkFist.Visible = this.lnkTop.Visible = this.lnkNext.Visible = this.lnkLast.Visible = this.txtPageIndex.Visible = this.LinkBtnToPage.Visible = false;
 }
 else
 {
 this.lnkFist.Visible = this.lnkTop.Visible = this.lnkNext.Visible = this.lnkLast.Visible = this.txtPageIndex.Visible = this.LinkBtnToPage.Visible = true;
 }

 }
 /// <summary>
 /// 刪除選中
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void BtnDelete_Click(object sender, EventArgs e)
 {
 Boolean bools = false;
 foreach (RepeaterItem di in this.RepeaterGustBookList.Items)
 {
 CheckBox checkIn = (CheckBox)di.FindControl("CheckIn");
 if (checkIn.Checked)
 {
 bools = true;
 Label lbGustNo = di.FindControl("lbUId") as Label;
 WeixinKeFuService wkf = new WeixinKeFuService();
 int num = wkf.DeleteWeixinKeFuInfo(int.Parse(lbGustNo.Text.ToString()));
 if (num > 0)
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('刪除成功!');location='WeiXinSessionList.aspx'", true);
 }
 else
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('刪除失??!');location='WeiXinSessionList.aspx'", true);
 }
 }
 }
 if (!bools)
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('未選中刪除項(xiàng)!');location='WeiXinSessionList.aspx'", true);
 }
 }
 /// <summary>
 /// 全選全不選
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void CheckAll_CheckedChanged(object sender, EventArgs e)
 {
 CheckBox checkAll = (CheckBox)sender;
 foreach (RepeaterItem d in this.RepeaterGustBookList.Items)
 {
 CheckBox checkIn = (CheckBox)d.FindControl("CheckIn");
 checkIn.Checked = checkAll.Checked;
 }
 }
 protected void LinkBtnLook_Click(object sender, EventArgs e)
 {
 BindGustBookList();
 }
 /// <summary>
 /// 綁定事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void RepeaterGustBookList_ItemDataBound(object sender, RepeaterItemEventArgs e)
 {


 if (e.Item.ItemType == ListItemType.Header)
 {
 CheckBox checkAll = e.Item.FindControl("CheckAll") as CheckBox;
 checkAll.AutoPostBack = true;
 }
 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
 {
 WeixinKeFuInfo wkf = e.Item.DataItem as WeixinKeFuInfo;

 Label lbUId = e.Item.FindControl("lbUId") as Label;
 lbUId.Text = wkf.UId.ToString();

 WxMessageService wms = new WxMessageService();
 WxMessageInfo wminfo = wms.GetTopLastFaSongDateByUId(lbUId.Text.ToString());

 if(wminfo!=null&&!String.IsNullOrWhiteSpace(wminfo.FaSongDate.ToString()))
 {
 Label lblastDate = e.Item.FindControl("lblastDate") as Label;

 lblastDate.Text = wminfo.FaSongDate.ToString();

 DateTime datesystemss = DateTime.Parse(System.DateTime.Now.ToString());
 DateTime lastLoingDatess = DateTime.Parse(lblastDate.Text.ToString());

 TimeSpan ts11 = new TimeSpan(datesystemss.Ticks);
 TimeSpan ts22 = new TimeSpan(lastLoingDatess.Ticks);

 TimeSpan ts33 = ts11.Subtract(ts22).Duration();

 Label lbState = e.Item.FindControl("lbState") as Label;

 string chaoshifenzhong = ts33.TotalMinutes.ToString();

 if (double.Parse(chaoshifenzhong) <=10)
 {
 lbState.Text = "會(huì)話(huà)中";
 lbState.Attributes.Add("style","color:red;");
 }
 else
 {
 lbState.Text = "已結(jié)束";
 }


 Label lbChaoshi = e.Item.FindControl("lbChaoshi") as Label;

 DateTime datesystem = DateTime.Parse(System.DateTime.Now.ToString());
 DateTime lastLoingDate = DateTime.Parse(lblastDate.Text.ToString());

 TimeSpan ts1 = new TimeSpan(datesystem.Ticks);
 TimeSpan ts2 = new TimeSpan(lastLoingDate.Ticks);

 TimeSpan ts3 = ts1.Subtract(ts2).Duration();

 lbChaoshi.Text = ts3.Days.ToString() + "天" + ts3.Hours.ToString() + "小時(shí)" + ts3.Minutes.ToString() + "分鐘";
 }

 //////根據(jù)用戶(hù)的openId獲取用戶(hù)昵稱(chēng)

 //WeiXinServer wxs = new WeiXinServer();

 /////從緩存讀取accesstoken
 //string Access_token = Cache["Access_token"] as string;

 //if (Access_token == null)
 //{
 // //如果為空,重新獲取
 // Access_token = wxs.GetAccessToken();

 // //設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
 // 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 jsonres = "";


 //jsonres = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + Access_tokento + "&openid=" + wkf.UserOpenId;

 //HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
 //myRequest.Method = "GET";
 //HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
 //StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
 //string content = reader.ReadToEnd();
 //reader.Close();

 ////使用前需藥引用Newtonsoft.json.dll文件
 //JObject jsonObj = JObject.Parse(content);


 //Label lbNikeName = e.Item.FindControl("lbNikeName") as Label;
 //Label lbSex = e.Item.FindControl("lbSex") as Label;
 //lbNikeName.Text = jsonObj["nickname"].ToString();
 //if (jsonObj["sex"].ToString().Equals("1"))
 //{
 // lbSex.Text = "男";
 //}
 //else
 //{
 // lbSex.Text = "女";
 //}

 }
 }
 /// <summary>
 /// 輸入頁(yè)碼提交跳轉(zhuǎn)
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void LinkBtnToPage_Click(object sender, EventArgs e)
 {

 if (String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('頁(yè)碼不能為空!')", true);
 this.txtPageIndex.Focus();
 return;
 }
 if (IsNum(this.txtPageIndex.Text.ToString().Trim()))
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('頁(yè)碼數(shù)只能輸入數(shù)字!')", true);
 this.txtPageIndex.Focus();
 this.txtPageIndex.Text = this.lbPageIndex.Text.ToString();
 return;
 }
 if (int.Parse(this.txtPageIndex.Text.ToString().Trim()) > int.Parse(this.lbCountPage.Text.ToString().Trim()))
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('所輸頁(yè)數(shù)不能大于總頁(yè)數(shù)!')", true);
 this.txtPageIndex.Focus();
 this.txtPageIndex.Text = this.lbPageIndex.Text.ToString();
 return;
 }

 BindGustBookList();
 }
 /// <summary>
 /// 判斷是否是數(shù)字
 /// </summary>
 /// <param name="text"></param>
 /// <returns></returns>
 public static bool IsNum(string text) //
 {
 for (int i = 0; i < text.Length; i++)
 {
 if (!Char.IsNumber(text, i))
 {
 return true; //輸入的不是數(shù)字 
 }
 }
 return false; //否則是數(shù)字
 }

此代碼已包含,后臺(tái)分頁(yè)功能,仔細(xì)研究下,即可使用.
點(diǎn)擊開(kāi)啟會(huì)話(huà)的頁(yè)面:MessageWindow.aspx如下:

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeBehind="MessageWindow.aspx.cs" Inherits="DQWebSite.Administrator.MessageWindow" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title></title>
 <style type="text/css">
 .messagestyle { width:100%; height:60px; margin-top:10px;
 }
 #LinkBtnSubSend { float:left;
 }
 /*.weixiao{ float:left; background-image:url('images/qqbiaoqing.jpg'); background-repeat:no-repeat; background-position:0px 0px; width:30px; height:28px;
 }
 .piezui { float:left; background-image:url('images/qqbiaoqing.jpg'); background-repeat:no-repeat; background-position:-31px -0px; width:30px; height:28px;
 }
 .hanxiao {float:left; background-image:url('images/qqbiaoqing.jpg'); background-repeat:no-repeat; background-position:-0px -62px; width:30px; height:28px;
 }
 .zhuakuang {float:left; background-image:url('images/qqbiaoqing.jpg'); background-repeat:no-repeat; background-position:-218px -62px; width:28px; height:28px;
 }
 .shuai {float:left; background-image:url('images/qqbiaoqing.jpg'); background-repeat:no-repeat; background-position:-248px -62px; width:28px; height:28px;
 }
 .yiwen {float:left; background-image:url('images/qqbiaoqing.jpg'); background-repeat:no-repeat; background-position:-126px -62px; width:28px; height:28px;
 }
 .liuhan {float:left; background-image:url('images/qqbiaoqing.jpg'); background-repeat:no-repeat; background-position:-404px -30px; width:28px; height:28px;
 }*/
 a:hover { cursor:pointer;
 }
 .LinkBtnSubSend { margin-top:5px;
 }
 </style>
 <script type="text/javascript">
 
 function LessThan(oTextArea) {
 
 //獲得textarea的maxlength屬性
 var num = oTextArea.getAttribute("maxlength") - oTextArea.value.length;
 
 document.getElementById("errmsg").innerHTML = "還可輸入的字符數(shù):" + num;
 
 //返回文本框字符個(gè)數(shù)是否符號(hào)要求的boolean值
 return oTextArea.value.length < oTextArea.getAttribute("maxlength");
 }
 </script>
 <script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
 <script src="../ckeditor_4.5.4_full2/ckeditor/ckeditor.js"></script>
</head>
<body>
 <form id="form1" runat="server">
 <div style="height:30px; text-align:left;"> <asp:Label ID="lbduihua1" runat="server" Text="Label"></asp:Label> <span style="color:red;"><asp:Label ID="lbduihuamsg" runat="server" Text="Label"></asp:Label></span> <asp:Label ID="lbduihua2" runat="server" Text="Label"></asp:Label></div>
 <div style="height:200px; width:100%; border:2px groove #ced9df; border-top-left-radius:5px; border-top-right-radius:5px; overflow-y:auto;background-image:url('images/ygbg.png'); background-repeat:repeat;">
 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
 <ContentTemplate>
 <ul>
 <asp:Repeater ID="RepeaterMessageList" runat="server" OnItemDataBound="RepeaterMessageList_ItemDataBound" >
 <ItemTemplate>
 <li><span style="color:red;">
 <asp:Label ID="lbFromUser" runat="server" Text="Label"></asp:Label> </span>對(duì)<span style="color:red;"><%# Eval("ToUser") %></span>說(shuō):
 <asp:Image ID="Imagelaba" runat="server" />
 <br />
 <%# Eval("Content") %> [<%# Eval("FaSongDate") %>]<br />
 </li>
 </ItemTemplate>
 </asp:Repeater> 
 </ul>
 <asp:Timer ID="timeTick" runat="server" Interval="200" OnTick="timeTick_Tick"></asp:Timer>
 </ContentTemplate>
 </asp:UpdatePanel>
 </div>
 <textarea id="txtMessage" name="txtMessage" runat="server" class="ckeditor messagestyle" maxlength="200" onkeypress="return LessThan(this);" onchange="return LessThan(this);"></textarea>
 <script type="text/javascript">CKEDITOR.replace('<%=txtMessage.ClientID.Replace("_","$") %>');</script>
 <div style="height:35px; line-height:35px;">
 <asp:LinkButton ID="LinkBtnSubSend" CssClass="LinkBtnSubSend" runat="server" OnClick="LinkBtnSubSend_Click"><div style="background-image:url('images/buttonbg.png'); width:111px; height:35px; line-height:35px; font-weight:bold; text-align:center; float:left; color:#fff;">發(fā)送</div></asp:LinkButton>
 <span id="errmsg" style="color:red;" runat="server" >該推送功能直接將信息推送到對(duì)方用戶(hù)微信,謹(jǐn)慎發(fā)言</span>
 </div>
 </form>
</body>
</html>

MessageWindow.aspx.cs的核心代碼如下:

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 = "對(duì)話(huà)中.......";

 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 = "會(huì)話(huà)已結(jié)束!超過(guò)48小時(shí)不能主動(dòng)推送信息給該用戶(hù)!";
 this.lbduihua1.Text = "已經(jīng)于";
 this.lbduihua2.Text = "失去連接.....除非該用戶(hù)主動(dòng)會(huì)話(huà)才能重新建立連接!";
 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>
 /// 推送消息到用戶(hù)
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void LinkBtnSubSend_Click(object sender, EventArgs e)
 {
 if(String.IsNullOrWhiteSpace(this.txtMessage.Value.ToString().Trim()))
 {
 this.errmsg.InnerText = "發(fā)送的內(nèi)容不能為空!";
 this.txtMessage.Focus();
 return;
 }
 if (this.txtMessage.Value.ToString().Length < 5 || this.txtMessage.Value.ToString().Length > 200)
 {
 this.errmsg.InnerText = "發(fā)送內(nèi)容應(yīng)在5-200個(gè)字符之間!";
 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();

 //設(shè)置緩存的數(shù)據(jù)7000秒后過(guò)期
 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);

 ///獲取返回結(jié)果的正確|true|false
 string isright = jsonObj["errcode"].ToString();//0
 string istrueorfalse = jsonObj["errmsg"].ToString();//ok
 if (isright.Equals("0") && istrueorfalse.Equals("ok"))
 {
 this.errmsg.InnerText = "消息推送成功!消息已送達(dá)微信用戶(hù)!";
 this.txtMessage.Value = "";
 }
 else
 {
 this.errmsg.InnerText = "消息推送失?。∠⒁驯4嬷翑?shù)據(jù)庫(kù)!";
 }
 }

 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微信開(kāi)發(fā)教程匯總》,歡迎大家學(xué)習(xí)閱讀。

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

asp.net微信開(kāi)發(fā)(自定義會(huì)話(huà)管理)

asp.net微信開(kāi)發(fā)(自定義會(huì)話(huà)管理):和微信用戶(hù)的溝通少不了,總覺(jué)得看起來(lái)微信官網(wǎng)后臺(tái)管理中的會(huì)話(huà)回復(fù)消息有點(diǎn)呆板,所以我這里就自定義了一個(gè)會(huì)話(huà)管理功能,最終效果圖如下: 因?yàn)槲以囀褂酶晃谋疚募﨏KEDITOR來(lái)進(jìn)行編寫(xiě),你看到穩(wěn)中可能會(huì)有<P></p>字段,后臺(tái)獲取數(shù)據(jù)內(nèi)容
推薦度:
標(biāo)簽: 微信 自定義 管理
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top