Procházet zdrojové kódy

微信公众号-回复配置API

chao před 3 roky
rodič
revize
437fdc6b95

+ 1 - 1
src/main/java/com/caimei365/manager/config/security/ConstantKey.java

@@ -18,5 +18,5 @@ public class ConstantKey {
     /**
      * Token过期时间(分钟)
      */
-    public static final Integer TOKEN_EXPIRE = 5;
+    public static final Integer TOKEN_EXPIRE = 30;
 }

+ 1 - 1
src/main/java/com/caimei365/manager/controller/wechat/WechatMenuApi.java

@@ -12,7 +12,7 @@ import javax.servlet.http.HttpServletRequest;
 import java.util.List;
 
 /**
- * 微信后台管理
+ * 微信公众号菜单
  *
  * @author : Charles
  * @date : 2022/1/4

+ 90 - 0
src/main/java/com/caimei365/manager/controller/wechat/WechatReplyApi.java

@@ -0,0 +1,90 @@
+package com.caimei365.manager.controller.wechat;
+
+import com.caimei365.manager.config.security.ConstantKey;
+import com.caimei365.manager.config.security.JwtService;
+import com.caimei365.manager.entity.PaginationVo;
+import com.caimei365.manager.entity.ResponseJson;
+import com.caimei365.manager.entity.wechat.WechatReply;
+import com.caimei365.manager.service.wechat.WechatReplyService;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 微信公众号-回复配置
+ *
+ * @author : Charles
+ * @date : 2022/1/6
+ */
+@RestController
+@RequestMapping("/wechat/reply")
+public class WechatReplyApi {
+
+    @Resource
+    private WechatReplyService wechatReplyService;
+    @Resource
+    private JwtService jwtService;
+
+    /**
+     * 获取回复配置列表
+     *
+     * @param type 类型: 1采美,2呵呵商城
+     * @param title 回复素材标题
+     * @param responseType   事件类型
+     * @param msgType   回复类型
+     * @param pageNum  页码
+     * @param pageSize 每页大小
+     */
+    @GetMapping("/list")
+    public ResponseJson<PaginationVo<WechatReply>> replyList(Integer type, String title, String responseType, String msgType,
+                                                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
+                                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
+        return wechatReplyService.getReplyList(type, title, responseType, msgType, pageNum, pageSize);
+    }
+
+    /**
+     * 根据ID获取回复配置
+     */
+    @GetMapping("/{id}")
+    public ResponseJson<WechatReply> getReply(@PathVariable("id") Integer id) {
+        if (null == id || id <= 0) {
+            return ResponseJson.error("回复配置Id不能为空!", null);
+        }
+        return wechatReplyService.getReply(id);
+    }
+
+    /**
+     * 根据ID更新回复配置
+     */
+    @PostMapping("/update/{id}")
+    public ResponseJson<Void> updateReply(HttpServletRequest request, @PathVariable("id") Integer id, @RequestBody WechatReply reply) {
+        if (null == id || id <= 0) {
+            return ResponseJson.error("回复配置Id不能为空!", null);
+        }
+        String token = request.getHeader(ConstantKey.TOKEN_NAME);
+        String username = jwtService.getUsername(token);
+        return wechatReplyService.updateReply(id, reply, username);
+    }
+
+    /**
+     * 添加回复配置
+     */
+    @PostMapping("/create")
+    public ResponseJson<Void> addReply(HttpServletRequest request, @RequestBody WechatReply reply) {
+        String token = request.getHeader(ConstantKey.TOKEN_NAME);
+        String username = jwtService.getUsername(token);
+        return wechatReplyService.addReply(reply, username);
+    }
+
+    /**
+     * 根据ID删除回复配置
+     */
+    @PostMapping("/delete/{id}")
+    public ResponseJson<Void> deleteReply(@PathVariable("id") Integer id) {
+        if (null == id || id <= 0) {
+            return ResponseJson.error("回复配置Id不能为空!", null);
+        }
+        return wechatReplyService.deleteReply(id);
+    }
+}

+ 26 - 0
src/main/java/com/caimei365/manager/dao/WeChatDao.java

@@ -1,6 +1,7 @@
 package com.caimei365.manager.dao;
 
 import com.caimei365.manager.entity.wechat.WechatMenu;
+import com.caimei365.manager.entity.wechat.WechatReply;
 import org.apache.ibatis.annotations.Mapper;
 
 import java.util.List;
@@ -40,4 +41,29 @@ public interface WeChatDao {
      * @param parentId 父级ID
      */
     Integer countChildByParentId(Integer parentId);
+
+    /**
+     * 获取微信公众号菜单列表
+     * @param wxType 公众号Id
+     * @param title 回复素材标题
+     * @param responseType   事件类型
+     * @param msgType   回复类型
+     */
+    List<WechatReply> getReplyList(String wxType, String title, String responseType, String msgType);
+    /**
+     * 根据ID获取回复配置
+     */
+    WechatReply getWechatReply(Integer id);
+    /**
+     * 根据ID更新回复配置
+     */
+    void updateWechatReply(WechatReply dbReply);
+    /**
+     * 添加回复配置
+     */
+    void insertWechatReply(WechatReply reply);
+    /**
+     * 根据ID删除微信公众号回复配置
+     */
+    void deleteWechatReply(Integer id);
 }

+ 0 - 3
src/main/java/com/caimei365/manager/entity/wechat/WechatMenu.java

@@ -64,9 +64,6 @@ public class WechatMenu {
     /**
      * 微信公众号类型
      */
-    /**
-     * 小程序页面路径
-     */
     private String wxType;
     /**
      * 操作人用户Id

+ 50 - 0
src/main/java/com/caimei365/manager/entity/wechat/WechatReply.java

@@ -0,0 +1,50 @@
+package com.caimei365.manager.entity.wechat;
+
+import lombok.Data;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/1/6
+ */
+@Data
+public class WechatReply {
+    private static final long serialVersionUID = 1L;
+    /**
+     * Id
+     */
+    private Integer id;
+    /**
+     * 关键字
+     */
+    private String keyword;
+    /**
+     * 回复类型
+     */
+    private String msgType;
+    /**
+     * 事件类型
+     */
+    private String responseType;
+    /**
+     * 素材id
+     */
+    private Long relateId;
+    /**
+     * 回复素材标题
+     */
+    private String title;
+    /**
+     * 微信公众号类型
+     */
+    private String wxType;
+    /**
+     * 操作人用户Id
+     */
+    private Integer userId;
+    /**
+     * 类型: 1采美,2呵呵商城
+     */
+    private Integer type;
+}

+ 41 - 0
src/main/java/com/caimei365/manager/service/wechat/WechatReplyService.java

@@ -0,0 +1,41 @@
+package com.caimei365.manager.service.wechat;
+
+import com.caimei365.manager.entity.PaginationVo;
+import com.caimei365.manager.entity.ResponseJson;
+import com.caimei365.manager.entity.wechat.WechatReply;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/1/6
+ */
+public interface WechatReplyService {
+    /**
+     * 获取回复配置列表
+     *
+     * @param type 类型: 1采美,2呵呵商城
+     * @param title 回复素材标题
+     * @param responseType   事件类型
+     * @param msgType   回复类型
+     * @param pageNum  页码
+     * @param pageSize 每页大小
+     */
+    ResponseJson<PaginationVo<WechatReply>> getReplyList(Integer type, String title, String responseType, String msgType, int pageNum, int pageSize);
+    /**
+     * 根据ID获取回复配置
+     */
+    ResponseJson<WechatReply> getReply(Integer id);
+    /**
+     * 根据ID更新回复配置
+     */
+    ResponseJson<Void> updateReply(Integer id, WechatReply reply, String username);
+    /**
+     * 添加回复配置
+     */
+    ResponseJson<Void> addReply(WechatReply reply, String username);
+    /**
+     * 根据ID删除回复配置
+     */
+    ResponseJson<Void> deleteReply(Integer id);
+}

+ 1 - 1
src/main/java/com/caimei365/manager/service/wechat/impl/WechatMenuServiceImpl.java

@@ -143,7 +143,7 @@ public class WechatMenuServiceImpl implements WechatMenuService {
         if (!StringUtils.hasLength(menu.getName())) {
             return ResponseJson.error("菜单标题不能为空!", null);
         }
-        if (null != menu.getParentId()) {
+        if (null == menu.getParentId()) {
             return ResponseJson.error("父级菜单Id不能为空!", null);
         }
         WechatMenu parentMenu = weChatDao.getWechatMenu(menu.getParentId());

+ 128 - 0
src/main/java/com/caimei365/manager/service/wechat/impl/WechatReplyServiceImpl.java

@@ -0,0 +1,128 @@
+package com.caimei365.manager.service.wechat.impl;
+
+import com.caimei365.manager.dao.SysDao;
+import com.caimei365.manager.dao.WeChatDao;
+import com.caimei365.manager.entity.PaginationVo;
+import com.caimei365.manager.entity.ResponseJson;
+import com.caimei365.manager.entity.wechat.WechatReply;
+import com.caimei365.manager.service.wechat.WechatReplyService;
+import com.github.pagehelper.PageHelper;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/1/6
+ */
+@Slf4j
+@Service
+public class WechatReplyServiceImpl implements WechatReplyService {
+
+    @Value("${wechat.caimei.id}")
+    private String caimeiId;
+    @Value("${wechat.hehe.id}")
+    private String heheId;
+
+    @Resource
+    private WeChatDao weChatDao;
+    @Resource
+    private SysDao sysDao;
+
+    /**
+     * 获取回复配置列表
+     *
+     * @param type 类型: 1采美,2呵呵商城
+     * @param title 回复素材标题
+     * @param responseType   事件类型
+     * @param msgType   回复类型
+     * @param pageNum  页码
+     * @param pageSize 每页大小
+     */
+    @Override
+    public ResponseJson<PaginationVo<WechatReply>> getReplyList(Integer type, String title, String responseType, String msgType, int pageNum, int pageSize) {
+        String wxType = "";
+        if (null != type && 1 == type) {
+            wxType = caimeiId;
+        } else if (null != type && 2 == type) {
+            wxType = heheId;
+        } else {
+            return ResponseJson.error("配置类型不能为空", null);
+        }
+        PageHelper.startPage(pageNum, pageSize);
+        List<WechatReply> replyList = weChatDao.getReplyList(wxType, title, responseType, msgType);
+        PaginationVo<WechatReply> pageData = new PaginationVo<>(replyList);
+        return ResponseJson.success(pageData);
+    }
+
+    /**
+     * 根据ID获取回复配置
+     */
+    @Override
+    public ResponseJson<WechatReply> getReply(Integer id) {
+        WechatReply reply = weChatDao.getWechatReply(id);
+        return ResponseJson.success(reply);
+    }
+
+    /**
+     * 根据ID更新回复配置
+     */
+    @Override
+    public ResponseJson<Void> updateReply(Integer id, WechatReply reply, String username) {
+        WechatReply dbReply = weChatDao.getWechatReply(id);
+        if (null == dbReply) {
+            return ResponseJson.error("当前回复配置异常!", null);
+        }
+        if (StringUtils.hasLength(reply.getMsgType())){
+            dbReply.setMsgType(reply.getMsgType());
+        }
+        if (StringUtils.hasLength(reply.getResponseType())){
+            dbReply.setResponseType(reply.getResponseType());
+        }
+        if (StringUtils.hasLength(reply.getKeyword())){
+            dbReply.setKeyword(reply.getKeyword());
+        }
+        if (StringUtils.hasLength(reply.getTitle())){
+            dbReply.setTitle(reply.getTitle());
+        }
+        Integer userId = sysDao.getUserIdByUsername(username);
+        dbReply.setUserId(userId);
+        // 更新
+        weChatDao.updateWechatReply(dbReply);
+        return ResponseJson.success();
+    }
+
+    /**
+     * 添加回复配置
+     */
+    @Override
+    public ResponseJson<Void> addReply(WechatReply reply, String username) {
+        if (null != reply.getType() && 1 == reply.getType()) {
+            reply.setWxType(caimeiId);
+        } else if (null != reply.getType() && 2 == reply.getType()) {
+            reply.setWxType(heheId);
+        } else {
+            return ResponseJson.error("配置类型type不正确!", null);
+        }
+        Integer userId = sysDao.getUserIdByUsername(username);
+        reply.setUserId(userId);
+        // 新增
+        weChatDao.insertWechatReply(reply);
+        return ResponseJson.success();
+    }
+
+    /**
+     * 根据ID删除回复配置
+     */
+    @Override
+    public ResponseJson<Void> deleteReply(Integer id) {
+        weChatDao.deleteWechatReply(id);
+        return ResponseJson.success();
+    }
+}

+ 39 - 3
src/main/resources/mapper/WeChatDao.xml

@@ -11,8 +11,7 @@
     <select id="getWechatMenu" resultType="com.caimei365.manager.entity.wechat.WechatMenu">
         SELECT a.id, a.parentId, a.parentIds, a.`name`, a.sort, a.type, a.`key`,
                a.url, a.mediaId, a.appid, a.pagepath AS pagePath, a.wxType
-        FROM wechat_menu a
-        WHERE a.id = #{id}
+        FROM wechat_menu a WHERE a.id = #{id}
     </select>
     <select id="countChildByParentId" resultType="java.lang.Integer">
         SELECT IFNULL(COUNT(*),0) FROM wechat_menu WHERE parentId = #{parentId}
@@ -20,7 +19,7 @@
     <update id="updateWechatMenu">
         UPDATE wechat_menu SET parentId = #{parentId}, parentIds = #{parentIds}, name = #{name}, sort = #{sort},
                                type = #{type}, `key` = #{key}, url = #{url}, mediaId = #{mediaId}, appid = #{appid},
-                               pagepath = #{pagePath}, wxType = #{wxType}, updateBy = #{userId}, updateDate = NOW()
+                               pagepath = #{pagePath}, updateBy = #{userId}, updateDate = NOW()
         WHERE id = #{id}
     </update>
     <insert id="insertWechatMenu" keyProperty="id" useGeneratedKeys="true">
@@ -30,4 +29,41 @@
     <delete id="deleteWechatMenu">
         DELETE FROM wechat_menu WHERE id = #{id}
     </delete>
+    <select id="getReplyList" resultType="com.caimei365.manager.entity.wechat.WechatReply">
+        SELECT a.cm_wxparamID AS id, a.keyword, a.title, a.responsetype AS responseType,
+               a.msgtype AS msgType, a.relateID AS relateId, a.wx_type AS wxType
+        FROM wechat_reply a
+        <where>
+            <if test=" wxType != null and  wxType != ''">
+                AND a.wx_type =#{wxType}
+            </if>
+            <if test="responseType != null and responseType != ''">
+                AND a.responsetype = #{responseType}
+            </if>
+            <if test="msgType != null and msgType != ''">
+                AND a.msgtype = #{msgType}
+            </if>
+            <if test="title != null and title != ''">
+                AND a.title LIKE concat('%',#{title},'%')
+            </if>
+        </where>
+        ORDER BY a.update_date DESC
+    </select>
+    <select id="getWechatReply" resultType="com.caimei365.manager.entity.wechat.WechatReply">
+        SELECT a.cm_wxparamID AS id, a.keyword, a.title, a.responsetype AS responseType,
+               a.msgtype AS msgType, a.relateID AS relateId, a.wx_type AS wxType
+        FROM wechat_reply a WHERE a.cm_wxparamID = #{id}
+    </select>
+    <update id="updateWechatReply">
+        UPDATE wechat_reply SET keyword = #{keyword}, title = #{title}, responsetype = #{responseType},
+                                msgtype = #{msgType}, relateID = #{relateId}, update_by = #{userId}, updatee_date = NOW()
+        WHERE cm_wxparamID = #{id}
+    </update>
+    <insert id="insertWechatReply" keyProperty="id" useGeneratedKeys="true">
+        INSERT INTO wechat_reply(keyword, title, responsetype, msgtype, relateID, wxType, create_by, create_date)
+        VALUES (#{keyword}, #{title}, #{responseType}, #{msgType}, #{relateId}, #{wxType}, #{userId}, NOW())
+    </insert>
+    <delete id="deleteWechatReply">
+        DELETE FROM wechat_reply WHERE cm_wxparamID = #{id}
+    </delete>
 </mapper>