Procházet zdrojové kódy

微信会话内容存储开发

anderx před 6 měsíci
rodič
revize
a6150e53af

+ 1 - 2
src/main/java/com/kede/common/dao/ChatMsgMapper.java

@@ -10,7 +10,7 @@ import java.util.List;
  * 微信会话存储(ChatMsg)表数据库访问层
  *
  * @author makejava
- * @since 2025-06-17 15:13:31
+ * @since 2025-06-17 15:24:54
  */
 public interface ChatMsgMapper {
 
@@ -27,7 +27,6 @@ public interface ChatMsgMapper {
      * 查询指定行数据
      *
      * @param chatMsg  查询条件
-     * @param pageable 分页对象
      * @return 对象列表
      */
     List<ChatMsg> findChatMsgList(ChatMsg chatMsg);

+ 35 - 13
src/main/java/com/kede/common/mapper/ChatMsgMapper.xml

@@ -11,6 +11,8 @@
         <result property="tolist" column="tolist" jdbcType="VARCHAR"/>
         <result property="tolistName" column="tolist_name" jdbcType="VARCHAR"/>
         <result property="roomid" column="roomid" jdbcType="VARCHAR"/>
+        <result property="type" column="type" jdbcType="INTEGER"/>
+        <result property="roomName" column="room_name" jdbcType="VARCHAR"/>
         <result property="msgtime" column="msgtime" jdbcType="TIMESTAMP"/>
         <result property="msgtype" column="msgtype" jdbcType="VARCHAR"/>
         <result property="content" column="content" jdbcType="VARCHAR"/>
@@ -18,7 +20,7 @@
     </resultMap>
 
     <sql id="ChatMsgAllSql">
-        id, msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, msgtime, msgtype, content, create_time
+        id, msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, type, room_name, msgtime, msgtype, content, create_time
     </sql>
 
     <!--查询单个-->
@@ -31,31 +33,31 @@
 
     <!--新增所有列-->
     <insert id="insert" keyProperty="id" useGeneratedKeys="true">
-        insert into chat_msg(msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, msgtime, msgtype,
-                             content, create_time)
-        values (#{msgid}, #{actionType}, #{fromId}, #{fromName}, #{tolist}, #{tolistName}, #{roomid}, #{msgtime},
-                #{msgtype}, #{content}, #{createTime})
+        insert into chat_msg(msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, type, room_name,
+                             msgtime, msgtype, content, create_time)
+        values (#{msgid}, #{actionType}, #{fromId}, #{fromName}, #{tolist}, #{tolistName}, #{roomid}, #{type},
+                #{roomName}, #{msgtime}, #{msgtype}, #{content}, #{createTime})
     </insert>
 
     <insert id="insertBatch">
-        insert into chat_msg(msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, msgtime, msgtype,
-        content, create_time)
+        insert into chat_msg(msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, type, room_name,
+        msgtime, msgtype, content, create_time)
         values
         <foreach collection="entities" item="entity" separator=",">
             (#{entity.msgid}, #{entity.actionType}, #{entity.fromId}, #{entity.fromName}, #{entity.tolist},
-            #{entity.tolistName}, #{entity.roomid}, #{entity.msgtime}, #{entity.msgtype}, #{entity.content},
-            #{entity.createTime})
+            #{entity.tolistName}, #{entity.roomid}, #{entity.type}, #{entity.roomName}, #{entity.msgtime},
+            #{entity.msgtype}, #{entity.content}, #{entity.createTime})
         </foreach>
     </insert>
 
     <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
-        insert into chat_msg(msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, msgtime, msgtype,
-        content, create_time)
+        insert into chat_msg(msgid, action_type, from_id, from_name, tolist, tolist_name, roomid, type, room_name,
+        msgtime, msgtype, content, create_time)
         values
         <foreach collection="entities" item="entity" separator=",">
             (#{entity.msgid}, #{entity.actionType}, #{entity.fromId}, #{entity.fromName}, #{entity.tolist},
-            #{entity.tolistName}, #{entity.roomid}, #{entity.msgtime}, #{entity.msgtype}, #{entity.content},
-            #{entity.createTime})
+            #{entity.tolistName}, #{entity.roomid}, #{entity.type}, #{entity.roomName}, #{entity.msgtime},
+            #{entity.msgtype}, #{entity.content}, #{entity.createTime})
         </foreach>
         on duplicate key update
         msgid = values(msgid),
@@ -65,6 +67,8 @@
         tolist = values(tolist),
         tolist_name = values(tolist_name),
         roomid = values(roomid),
+        type = values(type),
+        room_name = values(room_name),
         msgtime = values(msgtime),
         msgtype = values(msgtype),
         content = values(content),
@@ -96,6 +100,12 @@
             <if test="roomid != null and roomid != ''">
                 roomid = #{roomid},
             </if>
+            <if test="type != null">
+                type = #{type},
+            </if>
+            <if test="roomName != null and roomName != ''">
+                room_name = #{roomName},
+            </if>
             <if test="msgtime != null">
                 msgtime = #{msgtime},
             </if>
@@ -143,6 +153,12 @@
             <if test="roomid != null and roomid != ''">
                 and roomid = #{roomid}
             </if>
+            <if test="type != null">
+                and type = #{type}
+            </if>
+            <if test="roomName != null and roomName != ''">
+                and room_name = #{roomName}
+            </if>
             <if test="msgtime != null">
                 and msgtime = #{msgtime}
             </if>
@@ -190,6 +206,12 @@
             <if test="roomid != null and roomid != ''">
                 and roomid = #{roomid}
             </if>
+            <if test="type != null">
+                and type = #{type}
+            </if>
+            <if test="roomName != null and roomName != ''">
+                and room_name = #{roomName}
+            </if>
             <if test="msgtime != null">
                 and msgtime = #{msgtime}
             </if>

+ 26 - 2
src/main/java/com/kede/common/model/ChatMsg.java

@@ -8,10 +8,10 @@ import java.io.Serializable;
  * 微信会话存储(ChatMsg)实体类
  *
  * @author makejava
- * @since 2025-06-17 15:13:31
+ * @since 2025-06-17 15:24:54
  */
 public class ChatMsg implements Serializable {
-    private static final long serialVersionUID = 610390202522406024L;
+    private static final long serialVersionUID = -17396877038535264L;
 
     private Integer id;
     /**
@@ -43,6 +43,14 @@ public class ChatMsg implements Serializable {
      */
     private String roomid;
     /**
+     * 0=单聊,1=群聊
+     */
+    private Integer type;
+    /**
+     * 群名称
+     */
+    private String roomName;
+    /**
      * 消息发送时间戳
      */
     private Date msgtime;
@@ -124,6 +132,22 @@ public class ChatMsg implements Serializable {
         this.roomid = roomid;
     }
 
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+
+    public String getRoomName() {
+        return roomName;
+    }
+
+    public void setRoomName(String roomName) {
+        this.roomName = roomName;
+    }
+
     public Date getMsgtime() {
         return msgtime;
     }

+ 13 - 8
src/main/java/com/kede/common/task/ConversationContentTask.java

@@ -57,26 +57,31 @@ public class ConversationContentTask {
         for (InputChatMsg chatMsg : list) {
             System.out.println(chatMsg);
             String from = chatMsg.getFrom();
-
+            //获取发送这名称
             String chatName = conversationContentService.pushGetChatName(from);
-            System.out.println("from"+chatName);
             String tolist = chatMsg.getTolist();
             //用逗号分割
             String[] tolistArr = tolist.split(",");
             StringJoiner stringJoiner = new StringJoiner(",");
-            for (String tolistName : tolistArr) {
-                String tolistName1 = conversationContentService.pushGetChatName(tolistName);
-                if (tolistName1 != null){
-                    stringJoiner.add(tolistName1);
+            //判定是否有群编号,判定是单聊还是群聊,群聊获取群名称
+            if (StringUtils.isEmpty(chatMsg.getRoomid())){
+                //获取接收者名称
+                for (String tolistName : tolistArr) {
+                    String tolistName1 = conversationContentService.pushGetChatName(tolistName);
+                    if (tolistName1 != null){
+                        stringJoiner.add(tolistName1);
+                    }
                 }
+            }else {
+                conversationContentService.pushGetRoomName(chatMsg.getRoomid());
             }
             if (StringUtils.isEmpty(chatName)){
                 ChatMsg chatMsg1 = chatMsgMapper.selectByMsgId(chatMsg.getMsgid());
                 if (chatMsg1 == null){
                     ChatMsg in= new ChatMsg();
                     in.setMsgid(chatMsg.getMsgid());
-                    in.setAction(chatMsg.getAction());
-                    in.setFrom(chatMsg.getFrom());
+                    in.setActionType(chatMsg.getAction());
+                    in.setFromId(chatMsg.getFrom());
                     in.setFromName(chatName);
                     in.setTolist(tolist);
                     in.setTolistName(stringJoiner.toString());

+ 4 - 0
src/main/java/com/kede/wechat/service/ConversationContentService.java

@@ -9,7 +9,11 @@ public interface ConversationContentService {
 
     String getAccessToken();
 
+    String getAddressBookAccessToken();
+
     String getPermitUserList();
 
     String pushGetChatName(String msg);
+
+    String pushGetRoomName(String roomid);
 }

+ 59 - 10
src/main/java/com/kede/wechat/service/impl/ConversationContentServiceImpl.java

@@ -35,6 +35,11 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     @Value(value = "${conversationContent.secret}")
     private String secret;
 
+    @Value(value = "${conversationContent.addressBook.secret}")
+    private String addressBookSecret;
+
+
+
     static String priKey ="-----BEGIN PRIVATE KEY-----\n" +
             "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCnh5N/kiyeNx/y\n" +
             "dDKe3FO8HWj8tuQQUCkCc8ol1Sqmcs9ey7S2189UDbZOPeY2xiiMD6AdFVUK5Y0P\n" +
@@ -147,7 +152,7 @@ public class ConversationContentServiceImpl implements ConversationContentServic
         Map<String,String> accessMap = (Map<String, String>) redisTemplate.opsForValue().get("ACCESS_TOKEN");
         if (accessMap == null){
             logger.debug("getAccessToken无缓存从接口获取");
-            Map<String, String> map = getAccessMap();
+            Map<String, String> map = getAccessMap(corpid, secret);
             //新增redis缓存数据
             redisTemplate.opsForValue().set("ACCESS_TOKEN",map);
             accessToken = map.get("accessToken");
@@ -157,7 +162,7 @@ public class ConversationContentServiceImpl implements ConversationContentServic
             Long expireDate = Long.valueOf(accessMap.get("expireDate"));
             if (expireDate < System.currentTimeMillis()){
                 logger.debug("getAccessToken缓存已过期从接口获取");
-                Map<String, String> map = getAccessMap();
+                Map<String, String> map = getAccessMap(corpid, secret);
                 accessToken = map.get("accessToken");
                 //新增redis缓存数据
                 redisTemplate.opsForValue().set("ACCESS_TOKEN",map);
@@ -167,6 +172,32 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     }
 
     @Override
+    public String getAddressBookAccessToken() {
+        //获取redis缓存数据
+        String accessToken = null;
+        Map<String,String> accessMap = (Map<String, String>) redisTemplate.opsForValue().get("ADDRESS_BOOK_ACCESS_TOKEN");
+        if (accessMap == null){
+            logger.debug("AddressBookAccessToken无缓存从接口获取");
+            Map<String, String> map = getAccessMap(corpid,secret);
+            //新增redis缓存数据
+            redisTemplate.opsForValue().set("ADDRESS_BOOK_ACCESS_TOKEN",map);
+            accessToken = map.get("accessToken");
+        }else {
+            logger.debug("AddressBookAccessToken有缓存从缓存获取");
+            accessToken = accessMap.get("accessToken");
+            Long expireDate = Long.valueOf(accessMap.get("expireDate"));
+            if (expireDate < System.currentTimeMillis()){
+                logger.debug("AddressBookAccessToken缓存已过期从接口获取");
+                Map<String, String> map = getAccessMap(corpid,secret);
+                accessToken = map.get("accessToken");
+                //新增redis缓存数据
+                redisTemplate.opsForValue().set("ADDRESS_BOOK_ACCESS_TOKEN",map);
+            }
+        }
+        return accessToken;
+    }
+
+    @Override
     public String getPermitUserList() {
         String url=String.format("https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_permit_user_list?access_token=%s",getAccessToken());
         com.alibaba.fastjson.JSONObject result = HttpUtils.httpGet(url);
@@ -192,13 +223,10 @@ public class ConversationContentServiceImpl implements ConversationContentServic
             String url=null;
             Integer type=0;
             if (msgIdSub.equals("wb")){
-                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_robot_info??access_token=%s&robot_id=%s",getAccessToken(),userId);
-            }else if (msgIdSub.equals("wo")){
+                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_robot_info??access_token=%s&robot_id=%s",getAddressBookAccessToken(),userId);
+            }else if (msgIdSub.equals("wo")||msgIdSub.equals("wm")){
                 //     https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=ACCESS_TOKEN&external_userid=EXTERNAL_USERID&cursor=CURSOR
-                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=%s&external_userid=%s",getAccessToken(),userId);
-                type=1;
-            }else if (msgIdSub.equals("wm")){
-                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=%s&external_userid=%s",getAccessToken(),userId);
+                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=%s&external_userid=%s",getAddressBookAccessToken(),userId);
                 type=1;
             }else {
                 url=String.format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s",getAccessToken(),userId);
@@ -218,6 +246,28 @@ public class ConversationContentServiceImpl implements ConversationContentServic
         }
     }
 
+    @Override
+    public String pushGetRoomName(String roomid) {
+                        //        https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=ACCESS_TOKEN
+        ChatMsgUser chatMsgUser = chatMsgUserMapper.selectByUserId(roomid);
+        if (chatMsgUser != null){
+            return chatMsgUser.getName();
+        }else {
+            String url=String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=%s",getAddressBookAccessToken());
+            Map<String,Object> map = new HashMap<String, Object>();
+            map.put("chat_id",roomid);
+            map.put("cursor",0);
+            com.alibaba.fastjson.JSONObject jsonObject = HttpUtils.httpPost(url, map);
+            System.out.println(jsonObject.toJSONString());
+            if (jsonObject.getInteger("errcode")==0){
+            }else {
+                System.out.println("获取会话内容存档群名称失败");
+            }
+        }
+
+        return null;
+    }
+
 
     private String getUserName(String url){
         com.alibaba.fastjson.JSONObject result = HttpUtils.httpGet(url);
@@ -233,9 +283,8 @@ public class ConversationContentServiceImpl implements ConversationContentServic
 
 
 
-    private Map<String, String> getAccessMap() {
+    private Map<String, String> getAccessMap(String corpid, String secret) {
         String url=String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s",corpid,secret);
-        logger.debug("API获取access_token");
         Date date = new Date();
         com.alibaba.fastjson.JSONObject jsonObject = HttpUtils.httpGet(url);
         String access_token = (String) jsonObject.get("access_token");

+ 1 - 0
src/main/resources/props/config_local.properties

@@ -20,6 +20,7 @@ static.host=//www.kedexinxi.com/client/1.0.13
 
 conversationContent.corpid=wwd1f2fb82952be74c
 conversationContent.secret=0k4XYuHUl4QB8QTB6cAOSaRIX00ur1Vyjjz4p_REP_k
+conversationContent.addressBook.secret=-iGnoz2hZEBOFjkIRMgJksoa4H8ggKFNcIUG3tjH3Ow
 
 #\u68C0\u6D4B\u6570\u636E\u5E93\u94FE\u63A5\u662F\u5426\u6709\u6548\uFF0C\u5FC5\u987B\u914D\u7F6E
 jdbc.validationQuery=SELECT 'x'

+ 1 - 0
src/main/resources/props/config_prod.properties

@@ -84,6 +84,7 @@ patentTemplate=SMS_72005286
 
 conversationContent.corpid=wwd1f2fb82952be74c
 conversationContent.secret=0k4XYuHUl4QB8QTB6cAOSaRIX00ur1Vyjjz4p_REP_k
+conversationContent.addressBook.secret=-iGnoz2hZEBOFjkIRMgJksoa4H8ggKFNcIUG3tjH3Ow
 
 easemob.client.url=https://a1.easemob.com/1117170814115609/jitao
 easemob.client.id=YXA6RUDGkIDSEeemq9_4PWzcNA