<div style="display: none" id="ShowMessage"> <table class="tableStyle" formmode="transparent" footer="normal"> <tr> <th class="th" colspan="2" style="text-align: left" id="msgTitle">您的新消息</th> </tr> <tr> <td style="text-align: right; vertical-align: top; width: 90px">消息内容:</td> <td style="height: 312px; vertical-align: top"> <!--消息Id--> <input type="hidden" name="msgId" id="msgId" /> <!--消息发送者Id--> <input type="hidden" name="senderId" id="senderId" /> <div id="msgContentDiv" style="overflow-y: auto; height: 100%"> </div> <div id="msgReplyDiv" style="overflow-y: auto; height: 96%; width: 100%; display: none"> <textarea id="msgReplyContent" name="msgReplyContent" style="overflow-y: auto; height: 94%; width: 96%; margin: 4px auto"></textarea> </div> </td> </tr> <tr> <td colspan="2" style="text-align: center"> <div id="readOperate"> <input type="button" value="已读" id="readBut" /> <input type="button" value="回复" id="goToReplyBut" /> <input type="button" value="关闭" id="closeDialogBut" /> </div> <div id="replyOperate" style="display: none"> <input type="button" value="确认回复" id="replySureBut" /> <input type="button" value="取消返回" id="replyCancleBut" /> </div> <div id="sendOperate" style="display: none"> <input type="button" value="发送" id="sendSureBut" /> <input type="button" value="关闭" id="sendCancleBut" /> </div> <div id="closeOperate" style="display: none"> <input type="button" value="关闭" id="closeMessageBut" /> </div> </td> </tr> </table> </div>
JavaScript部分:
// 获取消息 function showMessage() { var ajaxOptions = { type: "POST", url: '/Main/ajax/Message.ashx', data: { "function": "GetListNew" }, async: true, dataType: "json", contentType: "application/x-www-form-urlencoded", beforeSend: function () { $("#messageStatusPrompt").html("正在查询..."); }, success: function (result) { if (result.Status && result.Data.length > 0) { // 有消息才显示 $("#readOperate,#msgContentDiv").show(function () { $("#replyOperate,#sendOperate,#closeOperate,#msgReplyDiv").hide(); }); $("#msgId").val(result.Data[0].Id); $("#senderId").val(result.Data[0].CreateUserId); $("#msgTitle").html("来自【" + result.Data[0].CreateCompanyName + "】公司【" + result.Data[0].CreateDepartmentName + "】部门" + result.Data[0].CreateBy + "的消息"); $("#msgContentDiv").html(result.Data[0].Contents); Dialog.open({ InvokeElementId: "ShowMessage", Title: "消息提醒", ShowCloseButton: false }); $("#messageStatusPrompt").html("请处理消息"); } else { $("#messageStatusPrompt").html("木有消息,继续"); getMessage(); } }, complete: function() { $("#messageStatusPrompt").html("完成"); }, error: function (xmlHttpRequest, textStatus, errorThrown) { } }; $.ajax(ajaxOptions); } //定时获取消息 20秒一次 var count = 20; var times = 1; var timeId; function getMessage() { count--; if (count > 0) { timeId=setTimeout(getMessage, 1000); $("#messageStatusPrompt").html(count); } else { count = 20; times++; $("#messageStatusPrompt").html("正在查询消息..."); showMessage(); } }; // 发送消息 向谁发送 function sendMessage(receiverId, receiver) { if (receiverId !== "") { clearTimeout(timeId); $("#sendOperate").show(function () { $("#msgTitle").html("向" + receiver + "发送消息"); $("#senderId").val(receiverId); $("#readOperate,#replyOperate,#closeOperate").hide(); $("#msgReplyDiv").show(function() { $("#msgContentDiv").hide(); $("#msgReplyContent").val(""); }); Dialog.open({ InvokeElementId: "ShowMessage", Title: "消息提醒", ShowCloseButton: false }); }); } } // 已读 $("#msgId").val() function read(msgId) { var ajaxOptions = { type: "POST", url: '/Main/ajax/Message.ashx', data: { "function": "Read", "Id": msgId }, async: true, dataType: "json", contentType: "application/x-www-form-urlencoded", beforeSend: function () { $("#messageStatusPrompt").html("正在查询..."); }, success: function (result) { //$("#messageStatusPrompt").html(result.StatusMessage); //Dialog.close(); getMessage(); }, complete: function () { $("#messageStatusPrompt").html("完成"); }, error: function (xmlHttpRequest, textStatus, errorThrown) { } }; $.ajax(ajaxOptions); } $(function () { getMessage(); // 关闭事件 $("#closeDialogBut,#closeOperate,#sendCancleBut").click(function () { Dialog.close(); // 关闭时调用已读消息接口 将当前消息从缓存中排除掉 read($("#msgId").val()); }); // 到回复消息按钮事件 $("#goToReplyBut").click(function () { $("#msgContentDiv").hide(function () { $("#msgReplyDiv").show(function () { $("#readOperate").hide(function () { $("#replyOperate").show(); $("#msgReplyContent").val(""); }); }); }); }); // 取消回复事件 $("#replyCancleBut").click(function () { $("#msgReplyDiv").hide(function () { $("#msgContentDiv").show(function () { $("#replyOperate").hide(function () { $("#readOperate").show(); }); }); }); }); // 确认回复及发送事件处理做成一样的即可 $("#replySureBut,#sendSureBut").click(function () { var ajaxOptions = { type: "POST", url: '/Main/ajax/Message.ashx', data: { "function": "Send", "receiverId": $("#senderId").val(), "contents": $("#msgReplyContent").val() }, async: true, dataType: "json", contentType: "application/x-www-form-urlencoded", beforeSend: function () { $("#messageStatusPrompt").html("正在处理..."); }, success: function (result) { if (result.Status) { $("#replyOperate,#sendOperate").hide(function () { $("#closeOperate").show(); // 回复的内容 $("#msgReplyContent").val("消息已发送成功!"); }); } }, complete: function () { $("#messageStatusPrompt").html("完成") }, error: function (xmlHttpRequest, textStatus, errorThrown) { } }; $.ajax(ajaxOptions); }); });
字段 | 类型 | 必须 | 示例值 | 注释 / 说明 |
---|---|---|---|---|
function | string | 是 | GetListNew | 接口调用方法:GetListNew |
UserInfo | string | 是 | 登录成功后的用户信息 |
WebClient webClient = new WebClient(); NameValueCollection postValues = new NameValueCollection(); postValues.Add("function", "GetListNew"); postValues.Add("UserInfo", "登录后的的用户信息"); byte[] responseArray = webClient.UploadValues(url, postValues); string response = Encoding.UTF8.GetString(responseArray);
{ "Status": true, "StatusCode": null, "StatusMessage": "消息获取成功。" "Data": [消息列表的序列化] }
{ "Status": false, "StatusCode": null, "StatusMessage": "消息获取失败。" }
字段 | 类型 | 必须 | 示例值 | 注释 / 说明 |
---|---|---|---|---|
function | string | 是 | Read | 接口调用方法:Read |
UserInfo | string | 是 | 登录成功后的用户信息 | |
Id | string | 是 | 消息Id |
WebClient webClient = new WebClient(); NameValueCollection postValues = new NameValueCollection(); postValues.Add("function", "Read"); postValues.Add("UserInfo", "登录后的的用户信息"); postValues.Add("Id", "消息Id"); byte[] responseArray = webClient.UploadValues(url, postValues); string response = Encoding.UTF8.GetString(responseArray);
{ "Status": true, "StatusCode": null, "StatusMessage": "消息状态已改为已读。" }
{ "Status": false, "StatusCode": null, "StatusMessage": "消息状态改变失败。" }
字段 | 类型 | 必须 | 示例值 | 注释 / 说明 |
---|---|---|---|---|
function | string | 是 | Send | 接口调用方法:Send |
UserInfo | string | 是 | 登录成功后的用户信息 | |
encrypted | bool | 是 | true | 是否加密消息接收者主键 |
receiverId | string | 是 | 消息接收者Id | |
contents | string | 是 | 消息内容 | |
functionCode | string | 否 | 消息类型 |
WebClient webClient = new WebClient(); NameValueCollection postValues = new NameValueCollection(); postValues.Add("function", "Send"); postValues.Add("UserInfo", "登录后的的用户信息"); postValues.Add("encrypted", true); postValues.Add("receiverId", "消息接收者Id,注意是否加密"); postValues.Add("contents", "消息内容"); postValues.Add("functionCode", "消息类型"); byte[] responseArray = webClient.UploadValues(url, postValues); string response = Encoding.UTF8.GetString(responseArray);
{ "Status": true, "StatusCode": null, "StatusMessage": "消息状态已改为已读。" }
{ "Status": false, "StatusCode": null, "StatusMessage": "消息状态改变失败。" }