浏览代码

邀请评价

779513719 4 年之前
父节点
当前提交
eeae5467d8

+ 105 - 0
jim-common/src/main/java/org/jim/common/packets/EvaluationReqBody.java

@@ -0,0 +1,105 @@
+package org.jim.common.packets;
+
+/**
+ * 邀请评价
+ * @author Darren
+ * @date 2020/2/13 17:58
+ */
+public class EvaluationReqBody extends Message {
+
+    /**
+     * 消息发到哪个群组;
+     */
+    private String group_id;
+    /**
+     * 聊天类型;(如公聊、私聊)
+     */
+    private Integer chatType;
+    /**
+     * 发送用户id;
+     */
+    private String from;
+
+    private EvaluationReqBody() {
+    }
+
+    private EvaluationReqBody(String group_id, Integer chatType, String from) {
+        this.group_id = group_id;
+        this.chatType = chatType;
+        this.from = from;
+    }
+
+    public static EvaluationReqBody.Builder newBuilder(){
+        return new EvaluationReqBody.Builder();
+    }
+
+    public String getGroup_id() {
+        return group_id;
+    }
+
+    public EvaluationReqBody setGroup_id(String group_id) {
+        this.group_id = group_id;
+        return this;
+    }
+
+    public Integer getChatType() {
+        return chatType;
+    }
+
+    public EvaluationReqBody setChatType(Integer chatType) {
+        this.chatType = chatType;
+        return this;
+    }
+
+    public String getFrom() {
+        return from;
+    }
+
+    public EvaluationReqBody setFrom(String from) {
+        this.from = from;
+        return this;
+    }
+
+    public static class Builder extends Message.Builder<EvaluationReqBody, EvaluationReqBody.Builder>{
+
+        /**
+         * 消息发到哪个群组;
+         */
+        private String group_id;
+        /**
+         * 聊天类型;(如公聊、私聊)
+         */
+        private Integer chatType;
+        /**
+         * 发送用户id;
+         */
+        private String from;
+
+        public Builder setGroup_id(String group_id) {
+            this.group_id = group_id;
+            return this;
+        }
+
+        public Builder setChatType(Integer chatType) {
+            this.chatType = chatType;
+            return this;
+        }
+
+        public Builder setFrom(String from) {
+            this.from = from;
+            return this;
+        }
+
+        @Override
+        protected Builder getThis() {
+            return this;
+        }
+
+        @Override
+        public EvaluationReqBody build() {
+            return new EvaluationReqBody(this.group_id, this.chatType, this.from);
+        }
+    }
+
+
+}

+ 59 - 0
jim-server/src/main/java/org/jim/server/command/handler/EvaluationReqHandler.java

@@ -0,0 +1,59 @@
+package org.jim.server.command.handler;
+
+import org.jim.common.ImAio;
+import org.jim.common.ImPacket;
+import org.jim.common.packets.*;
+import org.jim.server.command.AbstractCmdHandler;
+import org.jim.server.util.EvaluationKit;
+import org.jim.server.util.InputIndicatorKit;
+import org.tio.core.Aio;
+import org.tio.core.ChannelContext;
+
+/**
+ * 邀请评价
+ * @author Darren
+ * @date 2020/2/13 17:47
+ */
+public class EvaluationReqHandler extends AbstractCmdHandler {
+
+    @Override
+    public Command command() {
+        return Command.COMMAND_INPUT_POINTER_REQ;
+    }
+
+    /**
+     *{
+     *   'from':String来源ID,
+     *   'group_id':String群组id仅在chatType为1时需要,
+     *   'chatType':Integer聊天类型{0:未知,1:群聊,2:私聊}
+     *}
+     * @param packet
+     * @param channelContext
+     * @return {@link {@link ImPacket}}
+     * @author Darren
+     * @date 2020/2/13 20:22
+     */
+    @Override
+    public ImPacket handler(ImPacket packet, ChannelContext channelContext) throws Exception {
+        if( null == packet.getBody() ){
+            Aio.remove(channelContext, "body is null");
+            return null;
+        }
+        EvaluationReqBody reqBody = EvaluationKit.toReqBody(packet.getBody());
+        if( reqBody == null ){
+            ImPacket respChatPacket = EvaluationKit.dataInCorrectRespPacket(channelContext);
+            return respChatPacket;
+        }
+        ImPacket evaluationPacket = new ImPacket(Command.COMMAND_EVALUATION_REQ,new RespBody(Command.COMMAND_EVALUATION_RESP, reqBody).toByte());
+        evaluationPacket.setSynSeq(packet.getSynSeq());
+        if(ChatType.CHAT_TYPE_PUBLIC.getNumber() == reqBody.getChatType()){
+            String group_id = reqBody.getGroup_id();
+            //邀请评价消息
+            ImAio.sendToGroup(group_id, evaluationPacket);
+            //发送成功响应包
+            return EvaluationKit.sendSuccessRespPacket(channelContext);
+        }
+        return null;
+    }
+
+}

+ 101 - 0
jim-server/src/main/java/org/jim/server/util/EvaluationKit.java

@@ -0,0 +1,101 @@
+package org.jim.server.util;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.log4j.Logger;
+import org.jim.common.ImPacket;
+import org.jim.common.ImStatus;
+import org.jim.common.http.HttpConst;
+import org.jim.common.packets.Command;
+import org.jim.common.packets.EvaluationReqBody;
+import org.jim.common.packets.InputIndicatorReqBody;
+import org.jim.common.packets.RespBody;
+import org.jim.common.utils.ImKit;
+import org.jim.common.utils.JsonKit;
+import org.tio.core.ChannelContext;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * @author Darren
+ * @date 2020/2/13 19:24
+ */
+public class EvaluationKit {
+
+    private static Logger log = Logger.getLogger(EvaluationKit.class);
+
+    /**
+     * 转换为消息结构;
+     * @param body
+     * @return
+     */
+    public static EvaluationReqBody toReqBody(byte[] body){
+        EvaluationReqBody inputIndicatorReqBody = parseReqBody(body);
+        if( null != inputIndicatorReqBody ){
+            if(validateReqBody(inputIndicatorReqBody) ){
+                return inputIndicatorReqBody;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * 验证消息体中的值
+     * @param reqBody
+     * @return
+     */
+    private static boolean validateReqBody(EvaluationReqBody reqBody) {
+        boolean b1 = StringUtils.isNotEmpty(reqBody.getGroup_id());
+        boolean b2 = StringUtils.isNotEmpty(reqBody.getFrom());
+        boolean b3 = null != reqBody.getChatType();
+        return (b1 && b2 && b3);
+    }
+
+    /**
+     * 转换为消息结构;
+     * @param body
+     * @return
+     */
+    private static EvaluationReqBody parseReqBody(byte[] body) {
+        if(body == null) {
+            return null;
+        }
+        EvaluationReqBody reqBody = null;
+        try {
+            String text = new String(body, HttpConst.CHARSET_NAME);
+            reqBody = JsonKit.toBean(text, EvaluationReqBody.class);
+            if( null != reqBody){
+                return reqBody;
+            }
+        } catch (UnsupportedEncodingException e) {
+            log.error(e.toString());
+        }
+        return reqBody;
+    }
+
+    /**
+     * 数据格式不正确响应包
+     * @param channelContext
+     * @return imPacket
+     * @throws Exception
+     */
+    public static ImPacket dataInCorrectRespPacket(ChannelContext channelContext) {
+        RespBody respBody = new RespBody(Command.COMMAND_EVALUATION_REQ, ImStatus.C10026);
+        ImPacket respPacket = ImKit.ConvertRespPacket(respBody, channelContext);
+        respPacket.setStatus(ImStatus.C10026);
+        return respPacket;
+    }
+
+    /**
+     * 成功响应包
+     * @param channelContext
+     * @return
+     * @throws Exception
+     */
+    public static ImPacket sendSuccessRespPacket(ChannelContext channelContext) {
+        RespBody respBody = new RespBody(Command.COMMAND_EVALUATION_RESP,ImStatus.C10000);
+        ImPacket respPacket = ImKit.ConvertRespPacket(respBody, channelContext);
+        respPacket.setStatus(ImStatus.C10000);
+        return respPacket;
+    }
+
+}