|
|
@@ -2,18 +2,26 @@ package com.kym.service.wechat.impl;
|
|
|
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.util.CharsetUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.core.util.XmlUtil;
|
|
|
import com.kym.common.utils.CommUtil;
|
|
|
+import com.kym.entity.miniapp.MpMsgTemplate;
|
|
|
import com.kym.entity.miniapp.MpRelation;
|
|
|
+import com.kym.service.miniapp.MpMsgTemplateService;
|
|
|
import com.kym.service.miniapp.impl.MpRelationServiceImpl;
|
|
|
import com.kym.service.wechat.WeixinMPService;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+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.stereotype.Service;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.PrintWriter;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
@@ -25,10 +33,16 @@ import java.util.Map;
|
|
|
@Slf4j
|
|
|
public class WeixinMPServiceImpl implements WeixinMPService {
|
|
|
|
|
|
+ private final MpMsgTemplateService mpMsgTemplateService;
|
|
|
+
|
|
|
private final MpRelationServiceImpl mpRelationService;
|
|
|
|
|
|
- public WeixinMPServiceImpl(MpRelationServiceImpl mpRelationService) {
|
|
|
+ private final WxMpService wxMpService;
|
|
|
+
|
|
|
+ public WeixinMPServiceImpl(MpMsgTemplateService mpMsgTemplateService, MpRelationServiceImpl mpRelationService, WxMpService wxMpService) {
|
|
|
+ this.mpMsgTemplateService = mpMsgTemplateService;
|
|
|
this.mpRelationService = mpRelationService;
|
|
|
+ this.wxMpService = wxMpService;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -104,4 +118,43 @@ public class WeixinMPServiceImpl implements WeixinMPService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void sendMPTemplateMessage(String mpOpenid, String templateTitle, List<String> dataList) throws WxErrorException {
|
|
|
+ var template = getMPMsgTemplate(templateTitle);
|
|
|
+ CommUtil.asserts(template != null, "模板消息不存在");
|
|
|
+ var templateKeys = getMPMsgTemplateKeys(template);
|
|
|
+ if (dataList.size() == templateKeys.length) {
|
|
|
+ WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
|
|
|
+ .toUser(mpOpenid)
|
|
|
+ .templateId(template.getTemplateId())
|
|
|
+ .url("")
|
|
|
+ .build();
|
|
|
+ for (int i = 0; i < templateKeys.length; i++) {
|
|
|
+ templateMessage.addData(new WxMpTemplateData(templateKeys[i], dataList.get(i)));
|
|
|
+ }
|
|
|
+ wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取模板消息的key
|
|
|
+ *
|
|
|
+ * @param template
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String[] getMPMsgTemplateKeys(MpMsgTemplate template) {
|
|
|
+ return StrUtil.removeAll(template.getContent().replace(".DATA", ""), '{', '}').split(",");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取模板消息
|
|
|
+ *
|
|
|
+ * @param title
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private MpMsgTemplate getMPMsgTemplate(String title) {
|
|
|
+ return mpMsgTemplateService.lambdaQuery().eq(MpMsgTemplate::getTitle, title).oneOpt()
|
|
|
+ .isPresent() ? mpMsgTemplateService.lambdaQuery().eq(MpMsgTemplate::getTitle, title).one() : null;
|
|
|
+ }
|
|
|
+
|
|
|
}
|