|
@@ -7,15 +7,17 @@ import cn.hutool.json.JSONUtil;
|
|
|
import cn.newfeifan.mall.module.system.dal.dataobject.wechatmsgtemplate.WechatMsgTemplateDO;
|
|
|
import cn.newfeifan.mall.module.system.service.social.SocialUserService;
|
|
|
import cn.newfeifan.mall.module.system.service.wechatmsgtemplate.WechatMsgTemplateService;
|
|
|
+import cn.newfeifan.mall.module.trade.controller.admin.messagelog.vo.MessageLogSaveReqVO;
|
|
|
+import cn.newfeifan.mall.module.trade.service.messagelog.MessageLogService;
|
|
|
import com.google.gson.JsonObject;
|
|
|
import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
@@ -27,6 +29,7 @@ import static cn.newfeifan.mall.module.system.constant.SystemConstants.SYSTEM_WX
|
|
|
*/
|
|
|
@Data
|
|
|
@Component
|
|
|
+@Slf4j
|
|
|
public class WcChatMessageUtils {
|
|
|
|
|
|
@Value("${wx.mp.app-id}")
|
|
@@ -40,6 +43,8 @@ public class WcChatMessageUtils {
|
|
|
private WechatMsgTemplateService wechatMsgTemplateService;
|
|
|
@Resource
|
|
|
private SocialUserService socialUserService;
|
|
|
+ @Resource
|
|
|
+ private MessageLogService messageLogService;
|
|
|
|
|
|
/**
|
|
|
* 获取微信的accessToken
|
|
@@ -74,11 +79,11 @@ public class WcChatMessageUtils {
|
|
|
/**
|
|
|
* 发送微信消息
|
|
|
*
|
|
|
- * @param userId 接口消息的用户id
|
|
|
+ * @param userId 接口消息的用户id
|
|
|
+ * @param data 模板参数
|
|
|
* @param wechatMsgTemplateId 自定义的消息模板id
|
|
|
*/
|
|
|
- public void sendWxgMessage(Long userId, Long wechatMsgTemplateId,JsonObject data) {
|
|
|
-
|
|
|
+ public void sendWxgMessage(Long userId, JsonObject data, Long wechatMsgTemplateId,Long systemUserId,Long memberUserId,Long objectId) {
|
|
|
|
|
|
// 组装消息内容
|
|
|
String userOpenId = getOpenId(userId);
|
|
@@ -87,7 +92,6 @@ public class WcChatMessageUtils {
|
|
|
String client_msg_id = UUID.randomUUID().toString(); // 防重入id
|
|
|
|
|
|
|
|
|
-
|
|
|
WeChatTemplateMessage message = new WeChatTemplateMessage();
|
|
|
message.setTouser(userOpenId);
|
|
|
message.setTemplate_id(templateId);
|
|
@@ -99,19 +103,52 @@ public class WcChatMessageUtils {
|
|
|
String returnMsg = HttpUtil.post(StrUtil.format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}", getAccessToken()), message.toString());
|
|
|
cn.hutool.json.JSONObject jsonObject2 = JSONUtil.parseObj(returnMsg);
|
|
|
String errmsg = jsonObject2.getStr("errmsg");
|
|
|
- if (!StrUtil.equals("ok", errmsg)) System.out.println("消息发送失败!");
|
|
|
+
|
|
|
+ //记录发送的微信消息
|
|
|
+ MessageLogSaveReqVO messageLogSaveReqVO = MessageLogSaveReqVO.builder()
|
|
|
+ .openid(userOpenId)
|
|
|
+ .memberUserId(userId)
|
|
|
+ .systemUserId(systemUserId)
|
|
|
+ .memberUserId(memberUserId)
|
|
|
+ .wechatMsgTemplateId(wechatMsgTemplateId)
|
|
|
+ .wechatMsgTemplateParams(message.toString())
|
|
|
+ .objectId(objectId)
|
|
|
+ .sendStatus(true)
|
|
|
+ .sendTime(LocalDateTime.now())
|
|
|
+ .responseResult(returnMsg)
|
|
|
+ .build();
|
|
|
+ if (!StrUtil.equals("ok", errmsg)) {
|
|
|
+ log.info("=====消息发送失败,原因:{}", errmsg);
|
|
|
+ messageLogSaveReqVO.setSendStatus(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ messageLogService.createMessageLog(messageLogSaveReqVO);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取模板参数
|
|
|
- * @param orderNo 订单号
|
|
|
- * @param logisticsName 快递公司名称
|
|
|
- * @param logisticsNo 快递单号
|
|
|
- * @param spuName 商品名称
|
|
|
- * @return 模板参数
|
|
|
+ * 获取待发货模板参数
|
|
|
+ *
|
|
|
+ * @return 待发货模板参数
|
|
|
*/
|
|
|
- public JsonObject consignmentTemplate(String orderNo,String logisticsName,Long logisticsNo,String spuName){
|
|
|
- WechatMsgTemplateDO template = getTemplateId(1L);
|
|
|
+ public JsonObject getTemplateData(List<String> dataParams, List<String> params) {
|
|
|
+
|
|
|
+ JsonObject data = new JsonObject();
|
|
|
+
|
|
|
+ // 添加字段及其值
|
|
|
+ for (int i = 0; i < Math.min(params.size(), dataParams.size()); i++) {
|
|
|
+ String paramName = dataParams.get(i);
|
|
|
+ String paramValue = params.get(i);
|
|
|
+
|
|
|
+ JsonObject obj = new JsonObject();
|
|
|
+ obj.addProperty("value", paramValue);
|
|
|
+ data.add(paramName, obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getTemplateParams(Long userId, Long templateId, List<String> params,Long systemUserId,Long memberUserId,Long objectId) {
|
|
|
+ WechatMsgTemplateDO template = getTemplateId(templateId);
|
|
|
String messageTemplateParameters = template.getMessageTemplateParameters();
|
|
|
|
|
|
// 去除首尾的括号,并去除空格
|
|
@@ -121,44 +158,24 @@ public class WcChatMessageUtils {
|
|
|
String[] parts = messageTemplateParameters.split(",\\s*");
|
|
|
|
|
|
// 创建集合并将数组转换为集合
|
|
|
- List<String> list = Arrays.asList(parts);
|
|
|
-
|
|
|
- JsonObject data = new JsonObject();
|
|
|
-
|
|
|
- // 添加字段及其值
|
|
|
- JsonObject character_string2 = new JsonObject();
|
|
|
- character_string2.addProperty("value", orderNo);
|
|
|
- data.add(list.get(0), character_string2);
|
|
|
-
|
|
|
- JsonObject thing6 = new JsonObject();
|
|
|
- thing6.addProperty("value", logisticsName);
|
|
|
- data.add(list.get(1), thing6);
|
|
|
-
|
|
|
- JsonObject character_string7 = new JsonObject();
|
|
|
- character_string7.addProperty("value", logisticsNo);
|
|
|
- data.add(list.get(2), character_string7);
|
|
|
- JsonObject time1 = new JsonObject();
|
|
|
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
- String format = LocalDateTime.now().format(dateTimeFormatter);
|
|
|
- time1.addProperty("value", format);
|
|
|
- data.add(list.get(3), time1);
|
|
|
- JsonObject thing8 = new JsonObject();
|
|
|
- thing8.addProperty("value", spuName);
|
|
|
- data.add(list.get(4), character_string7);
|
|
|
+ List<String> dataParams = Arrays.asList(parts);
|
|
|
+ JsonObject templateData = getTemplateData(dataParams, params);
|
|
|
|
|
|
- return data;
|
|
|
+ // 发送消息
|
|
|
+ sendWxgMessage(userId, templateData, templateId,systemUserId,memberUserId,objectId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过自己定义消息模板id获取微信第三方模板
|
|
|
+ *
|
|
|
* @param wechatMsgTemplateId 自己定义消息模板id
|
|
|
* @return 微信第三方模板
|
|
|
*/
|
|
|
- private WechatMsgTemplateDO getTemplateId(Long wechatMsgTemplateId){
|
|
|
+ private WechatMsgTemplateDO getTemplateId(Long wechatMsgTemplateId) {
|
|
|
return wechatMsgTemplateService.getWechatSsgTemplateId(wechatMsgTemplateId);
|
|
|
}
|
|
|
|
|
|
- private String getOpenId(Long userId){
|
|
|
+ private String getOpenId(Long userId) {
|
|
|
return socialUserService.getOpenId(userId);
|
|
|
}
|
|
|
}
|