Przeglądaj źródła

微信公众号-文本素材API

chao 3 lat temu
rodzic
commit
24d35f667b

+ 14 - 3
db.sql

@@ -106,8 +106,19 @@ CREATE TABLE `wechat_reply` (
   `update_date` datetime DEFAULT NULL COMMENT '最后更新时间',
   PRIMARY KEY (`cm_wxparamID`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='回复配置表';
-
-
-
+-- 微信公众号-文本素材(原表:caimei_weixin.cm_wxtext)
+DROP TABLE IF EXISTS `wechat_text`;
+CREATE TABLE `wechat_text` (
+  `cm_wxtextID` int(11) NOT NULL AUTO_INCREMENT,
+  `title` varchar(128) DEFAULT NULL COMMENT '标题',
+  `content` text COMMENT '内容',
+  `addTime` varchar(19) DEFAULT NULL,
+  `wx_type` varchar(20) DEFAULT NULL COMMENT '微信公众号类型',
+  `create_by` bigint(11) DEFAULT NULL COMMENT '创建人',
+  `create_date` datetime DEFAULT NULL COMMENT '创建时间',
+  `update_by` bigint(11) DEFAULT NULL COMMENT '最后更新人',
+  `update_date` datetime DEFAULT NULL COMMENT '最后更新时间',
+  PRIMARY KEY (`cm_wxtextID`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='微信文本素材表';
 
 -- ============================================== 微信表 end ===============================

+ 88 - 0
src/main/java/com/caimei365/manager/controller/wechat/WechatTextApi.java

@@ -0,0 +1,88 @@
+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.WechatText;
+import com.caimei365.manager.service.wechat.WechatTextService;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 微信公众号-文本素材
+ *
+ * @author : Charles
+ * @date : 2022/1/6
+ */
+@RestController
+@RequestMapping("/wechat/text")
+public class WechatTextApi {
+
+    @Resource
+    private WechatTextService wechatTextService;
+    @Resource
+    private JwtService jwtService;
+
+    /**
+     * 获取文本素材列表
+     *
+     * @param type 类型: 1采美,2呵呵商城
+     * @param title 标题
+     * @param pageNum  页码
+     * @param pageSize 每页大小
+     */
+    @GetMapping("/list")
+    public ResponseJson<PaginationVo<WechatText>> textList(Integer type, String title,
+                                                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
+                                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
+        return wechatTextService.getTextList(type, title, pageNum, pageSize);
+    }
+
+    /**
+     * 根据ID获取文本素材
+     */
+    @GetMapping("/{id}")
+    public ResponseJson<WechatText> getText(@PathVariable("id") Integer id) {
+        if (null == id || id <= 0) {
+            return ResponseJson.error("文本素材Id不能为空!", null);
+        }
+        return wechatTextService.getText(id);
+    }
+
+    /**
+     * 根据ID更新文本素材
+     */
+    @PostMapping("/update/{id}")
+    public ResponseJson<Void> updateText(HttpServletRequest request, @PathVariable("id") Integer id, @RequestBody WechatText text) {
+        if (null == id || id <= 0) {
+            return ResponseJson.error("文本素材Id不能为空!", null);
+        }
+        String token = request.getHeader(ConstantKey.TOKEN_NAME);
+        String username = jwtService.getUsername(token);
+        return wechatTextService.updateText(id, text, username);
+    }
+
+    /**
+     * 添加文本素材
+     */
+    @PostMapping("/create")
+    public ResponseJson<Void> addText(HttpServletRequest request, @RequestBody WechatText text) {
+        String token = request.getHeader(ConstantKey.TOKEN_NAME);
+        String username = jwtService.getUsername(token);
+        return wechatTextService.addText(text, username);
+    }
+
+    /**
+     * 根据ID删除文本素材
+     */
+    @PostMapping("/delete/{id}")
+    public ResponseJson<Void> deleteText(@PathVariable("id") Integer id) {
+        if (null == id || id <= 0) {
+            return ResponseJson.error("文本素材Id不能为空!", null);
+        }
+        return wechatTextService.deleteText(id);
+    }
+}

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

@@ -2,6 +2,7 @@ package com.caimei365.manager.dao;
 
 import com.caimei365.manager.entity.wechat.WechatMenu;
 import com.caimei365.manager.entity.wechat.WechatReply;
+import com.caimei365.manager.entity.wechat.WechatText;
 import org.apache.ibatis.annotations.Mapper;
 
 import java.util.List;
@@ -66,4 +67,26 @@ public interface WeChatDao {
      * 根据ID删除微信公众号回复配置
      */
     void deleteWechatReply(Integer id);
+
+    /**
+     * 获取文本素材列表
+     * @param wxType 公众号Id
+     */
+    List<WechatText> getTextList(String wxType, String title);
+    /**
+     * 根据ID获取文本素材
+     */
+    WechatText getWechatText(Integer id);
+    /**
+     * 根据ID更新文本素材
+     */
+    void updateWechatText(WechatText dbText);
+    /**
+     * 添加文本素材
+     */
+    void insertWechatText(WechatText text);
+    /**
+     * 根据ID删除文本素材
+     */
+    void deleteWechatText(Integer id);
 }

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

@@ -1,7 +1,10 @@
 package com.caimei365.manager.entity.wechat;
 
+import com.fasterxml.jackson.annotation.JsonFormat;
 import lombok.Data;
 
+import java.util.Date;
+
 /**
  * Description
  *
@@ -47,4 +50,9 @@ public class WechatReply {
      * 类型: 1采美,2呵呵商城
      */
     private Integer type;
+    /**
+     * 更新时间
+     */
+    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
+    private Date updateTime;
 }

+ 46 - 0
src/main/java/com/caimei365/manager/entity/wechat/WechatText.java

@@ -0,0 +1,46 @@
+package com.caimei365.manager.entity.wechat;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/1/6
+ */
+@Data
+public class WechatText {
+    private static final long serialVersionUID = 1L;
+    /**
+     * Id
+     */
+    private Integer id;
+    /**
+     * 标题
+     */
+    private String title;
+    /**
+     * 内容
+     */
+    private String content;
+    /**
+     * 微信公众号类型
+     */
+    private String wxType;
+    /**
+     * 操作人用户Id
+     */
+    private Integer userId;
+    /**
+     * 类型: 1采美,2呵呵商城
+     */
+    private Integer type;
+    /**
+     * 更新时间
+     */
+    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
+    private Date updateTime;
+}

+ 39 - 0
src/main/java/com/caimei365/manager/service/wechat/WechatTextService.java

@@ -0,0 +1,39 @@
+package com.caimei365.manager.service.wechat;
+
+import com.caimei365.manager.entity.PaginationVo;
+import com.caimei365.manager.entity.ResponseJson;
+import com.caimei365.manager.entity.wechat.WechatText;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2022/1/6
+ */
+public interface WechatTextService {
+    /**
+     * 获取文本素材列表
+     *
+     * @param type 类型: 1采美,2呵呵商城
+     * @param title 标题
+     * @param pageNum  页码
+     * @param pageSize 每页大小
+     */
+    ResponseJson<PaginationVo<WechatText>> getTextList(Integer type, String title, int pageNum, int pageSize);
+    /**
+     * 根据ID获取文本素材
+     */
+    ResponseJson<WechatText> getText(Integer id);
+    /**
+     * 根据ID更新文本素材
+     */
+    ResponseJson<Void> updateText(Integer id, WechatText text, String username);
+    /**
+     * 添加文本素材
+     */
+    ResponseJson<Void> addText(WechatText text, String username);
+    /**
+     * 根据ID删除文本素材
+     */
+    ResponseJson<Void> deleteText(Integer id);
+}

+ 125 - 0
src/main/java/com/caimei365/manager/service/wechat/impl/WechatTextServiceImpl.java

@@ -0,0 +1,125 @@
+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.WechatText;
+import com.caimei365.manager.service.wechat.WechatTextService;
+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 WechatTextServiceImpl implements WechatTextService {
+
+    @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 pageNum  页码
+     * @param pageSize 每页大小
+     */
+    @Override
+    public ResponseJson<PaginationVo<WechatText>> getTextList(Integer type, String title, int pageNum, int pageSize) {
+        String wxType = "";
+        if (null != type && 1 == type) {
+            wxType = caimeiId;
+        } else if (null != type && 2 == type) {
+            wxType = heheId;
+        }
+        PageHelper.startPage(pageNum, pageSize);
+        List<WechatText> textList = weChatDao.getTextList(wxType, title);
+        PaginationVo<WechatText> pageData = new PaginationVo<>(textList);
+        return ResponseJson.success(pageData);
+    }
+
+    /**
+     * 根据ID获取文本素材
+     *
+     * @param id
+     */
+    @Override
+    public ResponseJson<WechatText> getText(Integer id) {
+        WechatText text = weChatDao.getWechatText(id);
+        return ResponseJson.success(text);
+    }
+
+    /**
+     * 根据ID更新文本素材
+     */
+    @Override
+    public ResponseJson<Void> updateText(Integer id, WechatText text, String username) {
+        WechatText dbText = weChatDao.getWechatText(id);
+        if (null == dbText) {
+            return ResponseJson.error("当前回复配置异常!", null);
+        }
+        if (StringUtils.hasLength(text.getTitle())){
+            dbText.setTitle(text.getTitle());
+        }
+        if (StringUtils.hasLength(text.getContent())){
+            dbText.setContent(text.getContent());
+        }
+        if (null != text.getType() && 1 == text.getType()) {
+            dbText.setWxType(caimeiId);
+        } else if (null != text.getType() && 2 == text.getType()) {
+            dbText.setWxType(heheId);
+        }
+        Integer userId = sysDao.getUserIdByUsername(username);
+        dbText.setUserId(userId);
+        // 更新
+        weChatDao.updateWechatText(dbText);
+        return ResponseJson.success();
+    }
+
+    /**
+     * 添加文本素材
+     */
+    @Override
+    public ResponseJson<Void> addText(WechatText text, String username) {
+        if (null != text.getType() && 1 == text.getType()) {
+            text.setWxType(caimeiId);
+        } else if (null != text.getType() && 2 == text.getType()) {
+            text.setWxType(heheId);
+        } else {
+            return ResponseJson.error("配置类型type不正确!", null);
+        }
+        Integer userId = sysDao.getUserIdByUsername(username);
+        text.setUserId(userId);
+        // 新增
+        weChatDao.insertWechatText(text);
+        return ResponseJson.success();
+    }
+
+    /**
+     * 根据ID删除文本素材
+     */
+    @Override
+    public ResponseJson<Void> deleteText(Integer id) {
+        weChatDao.deleteWechatText(id);
+        return ResponseJson.success();
+    }
+}

+ 34 - 4
src/main/resources/mapper/WeChatDao.xml

@@ -31,7 +31,7 @@
     </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
+               a.msgtype AS msgType, a.relateID AS relateId, a.wx_type AS wxType, IFNULL(a.updatee_date,a.create_date) AS updateTime
         FROM wechat_reply a
         <where>
             <if test=" wxType != null and  wxType != ''">
@@ -47,11 +47,11 @@
                 AND a.title LIKE concat('%',#{title},'%')
             </if>
         </where>
-        ORDER BY a.update_date DESC
+        ORDER BY updateTime 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
+               a.msgtype AS msgType, a.relateID AS relateId, a.wx_type AS wxType, IFNULL(a.updatee_date,a.create_date) AS updateTime
         FROM wechat_reply a WHERE a.cm_wxparamID = #{id}
     </select>
     <update id="updateWechatReply">
@@ -60,10 +60,40 @@
         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)
+        INSERT INTO wechat_reply(keyword, title, responsetype, msgtype, relateID, wx_type, 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>
+    <select id="getTextList" resultType="com.caimei365.manager.entity.wechat.WechatText">
+        SELECT a.cm_wxtextID AS id, a.title, a.content, a.wx_type AS wxType,
+               IFNULL(a.updatee_date,a.create_date) AS updateTime
+        FROM wechat_text a
+        <where>
+            <if test=" wxType != null and  wxType != ''">
+                AND a.wx_type =#{wxType}
+            </if>
+            <if test="title != null and title != ''">
+                AND a.title LIKE concat('%',#{title},'%')
+            </if>
+        </where>
+        ORDER BY updateTime DESC
+    </select>
+    <select id="getWechatText" resultType="com.caimei365.manager.entity.wechat.WechatText">
+        SELECT a.cm_wxtextID AS id, a.title, a.content, a.wx_type AS wxType,
+               IFNULL(a.updatee_date,a.create_date) AS updateTime
+        FROM wechat_text a WHERE a.cm_wxtextID = #{id}
+    </select>
+    <update id="updateWechatText">
+        UPDATE wechat_text SET title = #{title}, content = #{content}, wx_type = #{wxType}, update_by = #{userId}, updatee_date = NOW()
+        WHERE cm_wxtextID = #{id}
+    </update>
+    <insert id="insertWechatText" keyProperty="id" useGeneratedKeys="true">
+        INSERT INTO wechat_text(title, content, wx_type, create_by, create_date)
+        VALUES (#{title}, #{content}, #{wxType}, #{userId}, NOW())
+    </insert>
+    <delete id="deleteWechatText">
+        DELETE FROM wechat_text WHERE cm_wxtextID = #{id}
+    </delete>
 </mapper>