• 通用权限管理系统接口文档V4.2 版本之消息接口介绍


    通用权限管理系统提供的消息接口可实现消息获取,消息发送,底层使用Redis对消息进行缓存,解决消息的并发请求对数据库的压力。
    前端可以通过客户端轮询来获取最新消息,前端效果截图如下:
    1、发送消息
    2、收到消息
    3、消息回复

    前端效果参考代码,前端可根据自己的需要进行功能封装,实现即时消息的功能,主要是JS实现的消息轮询和发送功能,
    Html部分:
    <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;  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%;  100%; display: none">
                            <textarea id="msgReplyContent" name="msgReplyContent" style="overflow-y: auto; height: 94%;  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" />&nbsp;&nbsp;<input type="button" value="回复" id="goToReplyBut" />&nbsp;&nbsp;<input type="button" value="关闭" id="closeDialogBut" />
                        </div>
                        <div id="replyOperate" style="display: none">
                            <input type="button" value="确认回复" id="replySureBut" />&nbsp;&nbsp;<input type="button" value="取消返回" id="replyCancleBut" />
                        </div>
                        <div id="sendOperate" style="display: none">
                             <input type="button" value="发送" id="sendSureBut" />&nbsp;&nbsp;<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);
                });
            });
    注意:在消息回复或发送时会停止对消息的轮询,知道消息发送完成才会继续轮询获取消息,消息只有在点击已读以后,下次请求时才会去掉该条消息。点击消息已读以后会将该消息从缓存中移除掉。

    接口具体内容如下(接口的安全调用会另外说明)
    1. 获取消息
    content中包含的字段定义
    字段类型必须示例值注释 / 说明
    function string GetListNew 接口调用方法:GetListNew
    UserInfo string   登录成功后的用户信息
    content格式,c#调用举例,接口地址:string url="http://host_name/UserCenterV42/MessageService.ashx";
    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);
    
    特别说明:
    • 为了增加性,此接口建议使用HTTP Post方式提交请求。
    服务器返回
    {
        "Status": true,
        "StatusCode": null,
        "StatusMessage": "消息获取成功。"
        "Data": [消息列表的序列化]
    }
    
    错误或者未找到相关数据时返回以下内容
    {
        "Status": false,
        "StatusCode": null,
        "StatusMessage": "消息获取失败。"
    }
    

    2. 改变消息状态为已读
    content中包含的字段定义
    字段类型必须示例值注释 / 说明
    function string Read 接口调用方法:Read
    UserInfo string   登录成功后的用户信息
    Id string   消息Id
    content格式,c#调用举例,接口地址:string url="http://host_name/UserCenterV42/MessageService.ashx";
    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);
    
    特别说明:
    • 为了增加性,此接口建议使用HTTP Post方式提交请求。
    服务器返回
    {
        "Status": true,
        "StatusCode": null,
        "StatusMessage": "消息状态已改为已读。"
    }
    
    错误或者未找到相关数据时返回以下内容
    {
        "Status": false,
        "StatusCode": null,
        "StatusMessage": "消息状态改变失败。"
    }
    

    3. 发送消息
    content中包含的字段定义
    字段类型必须示例值注释 / 说明
    function string Send 接口调用方法:Send
    UserInfo string   登录成功后的用户信息
    encrypted bool true 是否加密消息接收者主键
    receiverId string   消息接收者Id
    contents string   消息内容
    functionCode string   消息类型
    content格式,c#调用举例,接口地址:string url="http://host_name/UserCenterV42/MessageService.ashx";
    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);
    
    特别说明:
    • 为了增加性,此接口建议使用HTTP Post方式提交请求。
    服务器返回
    {
        "Status": true,
        "StatusCode": null,
        "StatusMessage": "消息状态已改为已读。"
    }
    
    错误或者未找到相关数据时返回以下内容
    {
        "Status": false,
        "StatusCode": null,
        "StatusMessage": "消息状态改变失败。"
    }
    
  • 相关阅读:
    本周面试总结
    本周面试总结
    本周面试题总结
    网络请求AJAX
    es6数组、对象的解构赋值
    es6箭头函数
    es6总结
    js限定输入为数字包括负数正则
    js限定输入为非负数,浮点数正则
    js数值千分隔(正则)
  • 原文地址:https://www.cnblogs.com/hnsongbiao/p/5076978.html
Copyright © 2020-2023  润新知