From 9a70e49663012ea1ff5d29123e0b94d16455653f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=89=9B=E5=8F=89=E9=97=AA=E9=97=AA?= <18322780655@163.com> Date: Wed, 28 Aug 2024 16:51:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BC=9A=E5=91=98=E5=8F=98?= =?UTF-8?q?=E5=8A=A8=E4=B8=8E=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rabbit/BalanceConsumer.java | 93 +++++++++++-------- src/main/resources/application.yml | 2 +- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/BalanceConsumer.java b/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/BalanceConsumer.java index 9e39801..7a0bc84 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/BalanceConsumer.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/BalanceConsumer.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpUtil; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.chaozhanggui.system.cashierservice.dao.TbShopInfoMapper; import com.chaozhanggui.system.cashierservice.dao.TbUserInfoMapper; @@ -20,6 +21,8 @@ import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; @@ -39,7 +42,7 @@ public class BalanceConsumer { @Value("${wx.balance.secrete}") private static String secrete ; @Value("${wx.balance.warnMsgTmpId}") - private static String msgTmpId = "AV-KybUHaK3KtFVLqpy6PHccHBS7XeX_mOM4RbufnQ"; + private static String msgTmpId = "AV-KybUHaK3KtFVLqpy6PHccHBS7XeX__mOM4RbufnQ"; static LinkedHashMap linkedHashMap=new LinkedHashMap<>(); @@ -70,26 +73,46 @@ public class BalanceConsumer { public static String getAccessToken() { - String resp = HttpUtil.get(StrUtil.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}", appId, secrete)); - JSONObject respInfo = JSONObject.parseObject(resp); - if (!respInfo.containsKey("access_token")) { - log.warn("公众号获取token失败, 响应内容: {}", resp); - throw new MsgException(resp); - } - return respInfo.getString("access_token"); + String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; + url = url.replace("APPID", appId).replace("APPSECRET", secrete); + +// url = url.replace("APPID", "wxd88fffa983758a30").replace("APPSECRET", "a34a61adc0602118b49400baa8812454"); + String result = HttpUtil.get(url); + JSONObject jsonObject = JSON.parseObject(result); + return jsonObject.getString("access_token"); } - public static JSONObject sendTemplateMsg(String templateId, String toUserOpenId, Map data) { - log.info("开始发送微信模板消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); + + + public static JSONObject sendTemplateMsg(String templateId, String toUserOpenId,String shopName,BigDecimal amount,BigDecimal balance,String type,String date ) { + log.info("开始发送微信模板消息, 接收用户openId: {},templateId:{},shopName:{},amount:{},balance:{},type:{},date:{}", toUserOpenId,templateId, shopName,amount,balance,type,date); String accessToken = getAccessToken(); - JSONObject object1=new JSONObject(); + JSONObject data = new JSONObject(); + data.put("touser", toUserOpenId); + data.put("template_id", templateId); + data.put("page", "index"); + // 设置消息内容 + JSONObject thing3 = new JSONObject(); + thing3.put("value",shopName ); - object1.put("template_id", templateId); - object1.put("touser", toUserOpenId); - object1.put("data",data); + JSONObject amount1=new JSONObject(); + amount1.put("value",amount); - String response= HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".concat(accessToken)).body(object1.toString()).execute().body(); + JSONObject amount2=new JSONObject(); + amount2.put("value",balance); + + JSONObject thing4=new JSONObject(); + thing4.put("value",type); + + JSONObject time5=new JSONObject(); + time5.put("value",date); + + + data.put("data", new JSONObject().fluentPut("thing3", thing3).fluentPut("amount1", amount1).fluentPut("amount2",amount2).fluentPut("thing4",thing4).fluentPut("time5",time5)); + log.info("微信模板消息请求参数 request:{}",data.toString()); + + String response= HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".concat(accessToken)).body(data.toString()).execute().body(); log.info("微信模板消息发送成功,响应内容:{}",response); JSONObject resObj=JSONObject.parseObject(response); if(ObjectUtil.isNotEmpty(resObj)&&ObjectUtil.isNotNull(resObj)&&"0".equals(resObj.get("errcode")+"")){ @@ -106,8 +129,8 @@ public class BalanceConsumer { JSONObject object=JSONObject.parseObject(message); String shopId=object.getString("shopId"); - BigDecimal amount=object.getBigDecimal("amount"); - BigDecimal balance=object.getBigDecimal("balance"); + BigDecimal amount=object.getBigDecimal("amount").setScale(2, RoundingMode.DOWN); + BigDecimal balance=object.getBigDecimal("balance").setScale(2, RoundingMode.DOWN); String type=object.getString("type"); Date time=object.getDate("time"); String userId=object.getString("userId"); @@ -127,30 +150,8 @@ public class BalanceConsumer { String toUserOpenId=userInfo.getMiniAppOpenId(); - - Map data = new HashMap() {{ - put("thing3", new HashMap(){{ - put("value", shopInfo.getShopName()); - }}); - put("amount1", new HashMap(){{ - put("value", amount); - }}); - put("amount2", new HashMap(){{ - put("value", balance); - }}); - - put("thing4",new HashMap(){{ - put("value",type); - }}); - - put("time5",new HashMap(){{ - put("value",time); - }}); - - }}; - log.info("开始发送会员余额变动消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); try { - JSONObject resObj=sendTemplateMsg(msgTmpId, toUserOpenId, data); + JSONObject resObj=sendTemplateMsg(msgTmpId, toUserOpenId, shopInfo.getShopName(),amount,balance,type,convertDate(time)); log.info("会员变动消息发送返回:{}",resObj.toString()); }catch (Exception e) { log.error("发送失败, openId:{}, msg: {}", toUserOpenId, e.getMessage()); @@ -163,4 +164,16 @@ public class BalanceConsumer { e.printStackTrace(); } } + + + public static String convertDate(Date date){ + SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd号 HH:mm:ss"); + return sdf4.format(new Date()); + + } + + public static void main(String[] args) { +// + sendTemplateMsg("AV-KybUHaK3KtFVLqpy6PHccHBS7XeX__mOM4RbufnQ","or1l862a5N_W6eoL1-sJeewpOjZw","测试店铺",new BigDecimal(0.01),new BigDecimal(6.21),"充值",convertDate(new Date())); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d5b95b5..61e6df1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -58,7 +58,7 @@ wx: balance: appId: wxd88fffa983758a30 secrete: a34a61adc0602118b49400baa8812454 - warnMsgTmpId: IZ-l9p9yBgcvhRR0uN6cBQPkWJ5i05zyWMkfeCPaAmY + warnMsgTmpId: AV-KybUHaK3KtFVLqpy6PHccHBS7XeX__mOM4RbufnQ mybatis-plus: