Explorar o código

微信会话内容存储开发

anderx hai 6 meses
pai
achega
bd29b639d9

+ 84 - 0
src/main/java/com/kede/common/dao/ChatMsgUserMapper.java

@@ -0,0 +1,84 @@
+package com.kede.common.dao;
+
+import com.kede.common.model.ChatMsgUser;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * 微信会话存储对象名称(ChatMsgUser)表数据库访问层
+ *
+ * @author makejava
+ * @since 2025-06-17 14:44:32
+ */
+public interface ChatMsgUserMapper {
+
+    /**
+     * 通过ID查询单条数据
+     *
+     * @param id 主键
+     * @return 实例对象
+     */
+    ChatMsgUser selectById(Integer id);
+
+
+    /**
+     * 查询指定行数据
+     *
+     * @param chatMsgUser 查询条件
+     * @return 对象列表
+     */
+    List<ChatMsgUser> findChatMsgUserList(ChatMsgUser chatMsgUser);
+
+    /**
+     * 统计总行数
+     *
+     * @param chatMsgUser 查询条件
+     * @return 总行数
+     */
+    int findChatMsgUserCount(ChatMsgUser chatMsgUser);
+
+    /**
+     * 新增数据
+     *
+     * @param chatMsgUser 实例对象
+     * @return 影响行数
+     */
+    int insert(ChatMsgUser chatMsgUser);
+
+    /**
+     * 批量新增数据(MyBatis原生foreach方法)
+     *
+     * @param entities List<ChatMsgUser> 实例对象列表
+     * @return 影响行数
+     */
+    int insertBatch(@Param("entities") List<ChatMsgUser> entities);
+
+    /**
+     * 批量新增或按主键更新数据(MyBatis原生foreach方法)
+     *
+     * @param entities List<ChatMsgUser> 实例对象列表
+     * @return 影响行数
+     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
+     */
+    int insertOrUpdateBatch(@Param("entities") List<ChatMsgUser> entities);
+
+    /**
+     * 修改数据
+     *
+     * @param chatMsgUser 实例对象
+     * @return 影响行数
+     */
+    int update(ChatMsgUser chatMsgUser);
+
+    /**
+     * 通过主键删除数据
+     *
+     * @param id 主键
+     * @return 影响行数
+     */
+    int deleteById(Integer id);
+
+    ChatMsgUser selectByUserId(String userId);
+}
+

+ 139 - 0
src/main/java/com/kede/common/mapper/ChatMsgUserMapper.xml

@@ -0,0 +1,139 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.kede.common.dao.ChatMsgUserMapper">
+
+    <resultMap type="com.kede.common.model.ChatMsgUser" id="ChatMsgUserMap">
+        <result property="id" column="id" jdbcType="INTEGER"/>
+        <result property="userId" column="user_id" jdbcType="VARCHAR"/>
+        <result property="name" column="name" jdbcType="VARCHAR"/>
+        <result property="type" column="type" jdbcType="INTEGER"/>
+        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
+    </resultMap>
+
+    <sql id="ChatMsgUserAllSql">
+        id, user_id, name, type, create_time
+    </sql>
+
+    <!--查询单个-->
+    <select id="selectById" resultMap="ChatMsgUserMap">
+        select
+        <include refid="ChatMsgUserAllSql"/>
+        from chat_msg_user
+        where id = #{id}
+    </select>
+
+    <!--新增所有列-->
+    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
+        insert into chat_msg_user(user_id, name, type, create_time)
+        values (#{userId}, #{name}, #{type}, #{createTime})
+    </insert>
+
+    <insert id="insertBatch">
+        insert into chat_msg_user(user_id, name, type, create_time)
+        values
+        <foreach collection="entities" item="entity" separator=",">
+            (#{entity.userId}, #{entity.name}, #{entity.type}, #{entity.createTime})
+        </foreach>
+    </insert>
+
+    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
+        insert into chat_msg_user(user_id, name, type, create_time)
+        values
+        <foreach collection="entities" item="entity" separator=",">
+            (#{entity.userId}, #{entity.name}, #{entity.type}, #{entity.createTime})
+        </foreach>
+        on duplicate key update
+        user_id = values(user_id),
+        name = values(name),
+        type = values(type),
+        create_time = values(create_time)
+    </insert>
+
+    <!--通过主键修改数据-->
+    <update id="update">
+        update chat_msg_user
+        <set>
+            <if test="userId != null and userId != ''">
+                user_id = #{userId},
+            </if>
+            <if test="name != null and name != ''">
+                name = #{name},
+            </if>
+            <if test="type != null">
+                type = #{type},
+            </if>
+            <if test="createTime != null">
+                create_time = #{createTime},
+            </if>
+        </set>
+        where id = #{id}
+    </update>
+
+    <!--查询指定行数据-->
+    <select id="findChatMsgUserList" resultMap="ChatMsgUserMap">
+        select
+        <include refid="ChatMsgUserAllSql"/>
+
+        from chat_msg_user
+        <where>
+            <if test="id != null">
+                and id = #{id}
+            </if>
+            <if test="userId != null and userId != ''">
+                and user_id = #{userId}
+            </if>
+            <if test="name != null and name != ''">
+                and name = #{name}
+            </if>
+            <if test="type != null">
+                and type = #{type}
+            </if>
+            <if test="createTime != null">
+                and create_time = #{createTime}
+            </if>
+        </where>
+        <if test="page_sql != null">
+            ${page_sql}
+        </if>
+    </select>
+
+    <!--统计总行数-->
+    <select id="findChatMsgUserCount" resultType="java.lang.Integer">
+        select count(1)
+        from chat_msg_user
+        <where>
+            <if test="id != null">
+                and id = #{id}
+            </if>
+            <if test="userId != null and userId != ''">
+                and user_id = #{userId}
+            </if>
+            <if test="name != null and name != ''">
+                and name = #{name}
+            </if>
+            <if test="type != null">
+                and type = #{type}
+            </if>
+            <if test="createTime != null">
+                and create_time = #{createTime}
+            </if>
+        </where>
+    </select>
+
+
+    <!--通过主键删除-->
+    <delete id="deleteById">
+        delete
+        from chat_msg_user
+        where id = #{id}
+    </delete>
+
+    <select id="selectByUserId" resultMap="ChatMsgUserMap">
+        select
+        <include refid="ChatMsgUserAllSql"/>
+        from chat_msg_user
+        where user_id = #{id}
+    </select>
+
+</mapper>
+

+ 76 - 0
src/main/java/com/kede/common/model/ChatMsgUser.java

@@ -0,0 +1,76 @@
+package com.kede.common.model;
+
+import java.util.Date;
+import java.io.Serializable;
+
+
+/**
+ * 微信会话存储对象名称(ChatMsgUser)实体类
+ *
+ * @author makejava
+ * @since 2025-06-17 14:44:32
+ */
+public class ChatMsgUser implements Serializable {
+    private static final long serialVersionUID = -13552348943109897L;
+
+    private Integer id;
+    /**
+     * 编号
+     */
+    private String userId;
+    /**
+     * 称呼
+     */
+    private String name;
+    /**
+     * 会话信息编号 0=机器人,1=外部联系人,2=内部联系人
+     */
+    private Integer type;
+    /**
+     * 创建时间
+     */
+    private Date createTime;
+
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getUserId() {
+        return userId;
+    }
+
+    public void setUserId(String userId) {
+        this.userId = userId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+}
+

+ 24 - 3
src/main/java/com/kede/common/task/ConversationContentTask.java

@@ -1,5 +1,6 @@
 package com.kede.common.task;
 
+import com.kede.common.model.ChatMsg;
 import com.kede.wechat.bo.InputChatMsg;
 import com.kede.wechat.service.ConversationContentService;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
 import java.sql.SQLOutput;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.StringJoiner;
@@ -19,6 +21,8 @@ import java.util.StringJoiner;
 public class ConversationContentTask {
     @Resource
     private ConversationContentService conversationContentService;
+    @Resource
+    private com.kede.common.dao.ChatMsgMapper chatMsgMapper;
 
     /**
      * 获取企业微信聊天记录
@@ -52,15 +56,32 @@ public class ConversationContentTask {
         for (InputChatMsg chatMsg : list) {
             System.out.println(chatMsg);
             String from = chatMsg.getFrom();
-            String chatName = conversationContentService.getChatName(from);
+
+            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.getChatName(tolistName);
-                System.out.println("toList="+tolistName1);
+                String tolistName1 = conversationContentService.pushGetChatName(tolistName);
+                if (tolistName1 != null){
+                    stringJoiner.add(tolistName1);
+                }
             }
+            ChatMsg in= new ChatMsg();
+            in.setMsgid(chatMsg.getMsgid());
+            in.setAction(chatMsg.getAction());
+            in.setFrom(chatMsg.getFrom());
+            in.setFromName(chatName);
+            in.setTolist(tolist);
+            in.setTolistName(stringJoiner.toString());
+            in.setRoomid(chatMsg.getRoomid());
+            in.setMsgtime(chatMsg.getMsgtime());
+            in.setMsgtype(chatMsg.getMsgtype());
+            in.setContent(chatMsg.getContent());
+            in.setCreateTime(new Date());
+            chatMsgMapper.insert(in);
         }
     }
 

+ 9 - 0
src/main/java/com/kede/wechat/bo/InputChatMsg.java

@@ -96,7 +96,13 @@ public class InputChatMsg {
         this.roomid = roomid;
     }
 
+    public Date getMsgtime() {
+        return msgtime;
+    }
 
+    public void setMsgtime(Date msgtime) {
+        this.msgtime = msgtime;
+    }
 
     public String getMsgtype() {
         return msgtype;
@@ -114,6 +120,9 @@ public class InputChatMsg {
         this.content = content;
     }
 
+
+
+
     @Override
     public String toString() {
         return new StringJoiner(", ", InputChatMsg.class.getSimpleName() + "[", "]")

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

@@ -11,5 +11,5 @@ public interface ConversationContentService {
 
     String getPermitUserList();
 
-    String getChatName(String msg);
+    String pushGetChatName(String msg);
 }

+ 37 - 21
src/main/java/com/kede/wechat/service/impl/ConversationContentServiceImpl.java

@@ -1,8 +1,10 @@
 package com.kede.wechat.service.impl;
 
+import com.kede.common.model.ChatMsgUser;
 import com.kede.common.utils.DateUtils;
 import com.kede.common.utils.HttpUtils;
 import com.kede.common.utils.RSAEncrypt;
+import com.kede.common.utils.StringUtils;
 import com.kede.wechat.bo.InputChatMsg;
 import com.kede.wechat.service.ConversationContentService;
 import com.tencent.wework.Finance;
@@ -13,6 +15,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
+import com.kede.common.dao.ChatMsgUserMapper;
 
 import javax.annotation.Resource;
 import java.util.*;
@@ -23,6 +26,8 @@ public class ConversationContentServiceImpl implements ConversationContentServic
     Logger logger = LoggerFactory.getLogger(ConversationContentServiceImpl.class);
     @Resource
     private RedisTemplate redisTemplate;
+    @Resource
+    private ChatMsgUserMapper chatMsgUserMapper;
 
 
     @Value(value = "${conversationContent.corpid}")
@@ -173,33 +178,44 @@ public class ConversationContentServiceImpl implements ConversationContentServic
             System.out.println(result.getString("errcode"));
             System.out.println("获取会话内容存档成员列表失败");
         }
-        getChatName(result.toJSONString());
+        pushGetChatName(result.toJSONString());
         return null;
     }
 
     @Override
-    public String getChatName(String msg) {
-//        msg="{\"msgid\":\"1634016854685227651_1749628622146_external\",\"action\":\"send\"," +
-//                "\"from\":\"wopeIoCwAArS1POCA5Fiw6VidWWqkwVA\",\"tolist\":[\"KaKa\"],\"roomid\":\"\",\"msgtime\":1749628618742,\"msgtype\":\"text\",\"" +
-//                "text\":{\"content\":\"您好,请详细描述您的问题,以便更快得到解答!\"}}";
-//        com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(msg);
-//        //直接转成对象
-//        InputChatMsg chatMsg = new InputChatMsg(jsonObject);
-        String msgIdSub = msg.substring(0, 2);
-        String url=null;
-        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(),msg);
-        }else if (msgIdSub.equals("wo")){
-        //     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(),msg);
-        }else if (msgIdSub.equals("wm")){
-            url=String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=%s&external_userid=%s",getAccessToken(),msg);
+    public String pushGetChatName(String userId) {
+        ChatMsgUser chatMsgUser = chatMsgUserMapper.selectByUserId(userId);
+        if (chatMsgUser != null){
+            return chatMsgUser.getName();
         }else {
-            url=String.format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s",getAccessToken(),msg);
+            String msgIdSub = userId.substring(0, 2);
+            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")){
+                //     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);
+                type=1;
+            }else {
+                url=String.format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s",getAccessToken(),userId);
+                type=2;
+            }
+            String userName = getUserName(url);
+            System.out.println(userId+"="+userName);
+            if (StringUtils.isNotBlank(userName)){
+                chatMsgUser = new ChatMsgUser();
+                chatMsgUser.setUserId(userId);
+                chatMsgUser.setName(userName);
+                chatMsgUser.setType(type);
+                chatMsgUser.setCreateTime(new Date());
+                chatMsgUserMapper.insert(chatMsgUser);
+            }
+            return userName;
         }
-        String userName = getUserName(url);
-        System.out.println(msg+"="+userName);
-        return userName;
     }