|
@@ -0,0 +1,164 @@
|
|
|
+package cn.newfeifan.mall.module.trade.utils.wechat;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+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 com.google.gson.JsonObject;
|
|
|
+import lombok.Data;
|
|
|
+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;
|
|
|
+
|
|
|
+import static cn.newfeifan.mall.module.system.constant.SystemConstants.SYSTEM_WX_ACCESS_TOKEN;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 发送微信消息的工具类
|
|
|
+ */
|
|
|
+@Data
|
|
|
+@Component
|
|
|
+public class WcChatMessageUtils {
|
|
|
+
|
|
|
+ @Value("${wx.mp.app-id}")
|
|
|
+ private String appid;
|
|
|
+ @Value("${wx.mp.secret}")
|
|
|
+ private String Wxgsecret;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+ @Resource
|
|
|
+ private WechatMsgTemplateService wechatMsgTemplateService;
|
|
|
+ @Resource
|
|
|
+ private SocialUserService socialUserService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取微信的accessToken
|
|
|
+ *
|
|
|
+ * @return accessToken
|
|
|
+ */
|
|
|
+ public String getAccessToken() {
|
|
|
+
|
|
|
+ String accessToken = stringRedisTemplate.opsForValue().get(SYSTEM_WX_ACCESS_TOKEN);
|
|
|
+
|
|
|
+ if (StrUtil.isEmpty(accessToken)) {
|
|
|
+ // 服务号的appid以及秘钥
|
|
|
+ String requestUrl = StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appid, Wxgsecret);
|
|
|
+ String returnMsg = HttpUtil.get(requestUrl);
|
|
|
+ cn.hutool.json.JSONObject responseJsonObject = JSONUtil.parseObj(returnMsg);
|
|
|
+ if (ObjectUtil.isNull(responseJsonObject)) try {
|
|
|
+ throw new Exception("响应异常:获取信息为空!");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ accessToken = responseJsonObject.getStr("access_token");
|
|
|
+
|
|
|
+ //微信的accessToken的失效时间是两个小时,这里改为一小时五十五分钟,怕双方的时间误差导致accessToken失效
|
|
|
+ long expireTime = 60 * 60 + 55 * 60; // 1小时55分钟转换为秒
|
|
|
+ stringRedisTemplate.opsForValue().set(SYSTEM_WX_ACCESS_TOKEN, accessToken, expireTime, java.util.concurrent.TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送微信消息
|
|
|
+ *
|
|
|
+ * @param userId 接口消息的用户id
|
|
|
+ * @param wechatMsgTemplateId 自定义的消息模板id
|
|
|
+ */
|
|
|
+ public void sendWxgMessage(Long userId, Long wechatMsgTemplateId,JsonObject data) {
|
|
|
+
|
|
|
+
|
|
|
+ // 组装消息内容
|
|
|
+ String userOpenId = getOpenId(userId);
|
|
|
+ String templateId = getTemplateId(wechatMsgTemplateId).getWechatMsgTemplateId(); // 模板id
|
|
|
+ String url = "https://letcgosh.newfeifan.cn/order"; // 跳转路径(小程序之外)
|
|
|
+ String client_msg_id = UUID.randomUUID().toString(); // 防重入id
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ WeChatTemplateMessage message = new WeChatTemplateMessage();
|
|
|
+ message.setTouser(userOpenId);
|
|
|
+ message.setTemplate_id(templateId);
|
|
|
+ message.setUrl(url);
|
|
|
+ message.setClient_msg_id(client_msg_id);
|
|
|
+ message.setData(data);
|
|
|
+
|
|
|
+ // 发送消息
|
|
|
+ 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("消息发送失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取模板参数
|
|
|
+ * @param orderNo 订单号
|
|
|
+ * @param logisticsName 快递公司名称
|
|
|
+ * @param logisticsNo 快递单号
|
|
|
+ * @param spuName 商品名称
|
|
|
+ * @return 模板参数
|
|
|
+ */
|
|
|
+ public JsonObject consignmentTemplate(String orderNo,String logisticsName,Long logisticsNo,String spuName){
|
|
|
+ WechatMsgTemplateDO template = getTemplateId(1L);
|
|
|
+ String messageTemplateParameters = template.getMessageTemplateParameters();
|
|
|
+
|
|
|
+ // 去除首尾的括号,并去除空格
|
|
|
+ messageTemplateParameters = messageTemplateParameters.substring(1, messageTemplateParameters.length() - 1).trim();
|
|
|
+
|
|
|
+ // 使用逗号分割字符串,并去除空格
|
|
|
+ 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);
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过自己定义消息模板id获取微信第三方模板
|
|
|
+ * @param wechatMsgTemplateId 自己定义消息模板id
|
|
|
+ * @return 微信第三方模板
|
|
|
+ */
|
|
|
+ private WechatMsgTemplateDO getTemplateId(Long wechatMsgTemplateId){
|
|
|
+ return wechatMsgTemplateService.getWechatSsgTemplateId(wechatMsgTemplateId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getOpenId(Long userId){
|
|
|
+ return socialUserService.getOpenId(userId);
|
|
|
+ }
|
|
|
+}
|