Explorar el Código

微信公众号消息模版配置小程序跳转

skyline hace 1 año
padre
commit
6b5f3a0fef

+ 7 - 1
entity/src/main/java/com/kym/entity/miniapp/MpMsgTemplate.java

@@ -2,7 +2,6 @@ package com.kym.entity.miniapp;
 
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.kym.entity.BaseEntity;
-import java.io.Serializable;
 import lombok.Getter;
 import lombok.Setter;
 
@@ -50,4 +49,11 @@ public class MpMsgTemplate extends BaseEntity {
      * 模板示例
      */
     private String example;
+
+    /**
+     * 小程序跳转页面路径
+     */
+    private String pagePath;
+
+
 }

+ 2 - 1
mapper/src/main/resources/mappers/miniapp/MpMsgTemplateMapper.xml

@@ -11,13 +11,14 @@
         <result column="deputy_industry" property="deputyIndustry" />
         <result column="content" property="content" />
         <result column="example" property="example" />
+        <result column="page_path" property="pagePath" />
         <result column="create_time" property="createTime" />
         <result column="update_time" property="updateTime" />
     </resultMap>
 
     <!-- 通用查询结果列 -->
     <sql id="Base_Column_List">
-        id,template_id, title, primary_industry, deputy_industry, content, example,create_time,update_time
+        id,template_id, title, primary_industry, deputy_industry, content, example,page_path,create_time,update_time
     </sql>
 
 </mapper>

+ 12 - 2
service/src/main/java/com/kym/service/wechat/impl/WeixinMPServiceImpl.java

@@ -17,6 +17,8 @@ import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
 import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
@@ -34,11 +36,15 @@ import java.util.Map;
 public class WeixinMPServiceImpl implements WeixinMPService {
 
     private final MpMsgTemplateService mpMsgTemplateService;
-
     private final MpRelationServiceImpl mpRelationService;
-
     private final WxMpService wxMpService;
 
+    /**
+     * 小程序appid
+     */
+    @Value("${wechat.miniapp.appid}")
+    public String appid;
+
     public WeixinMPServiceImpl(MpMsgTemplateService mpMsgTemplateService, MpRelationServiceImpl mpRelationService, WxMpService wxMpService) {
         this.mpMsgTemplateService = mpMsgTemplateService;
         this.mpRelationService = mpRelationService;
@@ -119,6 +125,7 @@ public class WeixinMPServiceImpl implements WeixinMPService {
     }
 
     @Override
+    @Async
     public void sendMPTemplateMessage(String mpOpenid, String templateTitle, List<String> dataList) throws WxErrorException {
         var template = getMPMsgTemplate(templateTitle);
         CommUtil.asserts(template != null, "模板消息不存在");
@@ -132,6 +139,9 @@ public class WeixinMPServiceImpl implements WeixinMPService {
             for (int i = 0; i < templateKeys.length; i++) {
                 templateMessage.addData(new WxMpTemplateData(templateKeys[i], dataList.get(i)));
             }
+            if (CommUtil.isNotEmptyAndNull(template.getPagePath())) {
+                templateMessage.setMiniProgram(new WxMpTemplateMessage.MiniProgram(appid, template.getPagePath(), false));
+            }
             wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
         }
     }