Merge remote-tracking branch 'origin/hph' into test

This commit is contained in:
牛叉闪闪
2024-08-28 16:51:44 +08:00
2 changed files with 54 additions and 41 deletions

View File

@@ -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<String,String> 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<String, Object> 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<String, Object> data = new HashMap<String, Object>() {{
put("thing3", new HashMap<String, Object>(){{
put("value", shopInfo.getShopName());
}});
put("amount1", new HashMap<String, Object>(){{
put("value", amount);
}});
put("amount2", new HashMap<String, Object>(){{
put("value", balance);
}});
put("thing4",new HashMap<String,Object>(){{
put("value",type);
}});
put("time5",new HashMap<String,Object>(){{
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()));
}
}

View File

@@ -58,7 +58,7 @@ wx:
balance:
appId: wxd88fffa983758a30
secrete: a34a61adc0602118b49400baa8812454
warnMsgTmpId: IZ-l9p9yBgcvhRR0uN6cBQPkWJ5i05zyWMkfeCPaAmY
warnMsgTmpId: AV-KybUHaK3KtFVLqpy6PHccHBS7XeX__mOM4RbufnQ
mybatis-plus: