新增下单库存预警消息推送

This commit is contained in:
2024-06-28 14:15:42 +08:00
parent fad761b63c
commit d82387e9d1
4 changed files with 150 additions and 1 deletions

View File

@@ -0,0 +1,104 @@
package com.chaozhanggui.system.cashierservice.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@Component
@Slf4j
public class WechatUtil {
@Value("${wx.msg.appId}")
private String appId;
@Value("${wx.msg.secrete}")
private String secrete;
@Value("${wx.msg.warnMsgTmpId}")
private String msgTmpId;
static LinkedHashMap<String,String> linkedHashMap=new LinkedHashMap<>();
static {
linkedHashMap.put("40001","获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口");
linkedHashMap.put("40003","不合法的 OpenID ,请开发者确认 OpenID (该用户)是否已关注公众号,或是否是其他公众号的 OpenID");
linkedHashMap.put("40014","不合法的 access_token ,请开发者认真比对 access_token 的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口");
linkedHashMap.put("40037","不合法的 template_id");
linkedHashMap.put("43101","用户未订阅消息");
linkedHashMap.put("43107","订阅消息能力封禁");
linkedHashMap.put("43108","并发下发消息给同一个粉丝");
linkedHashMap.put("45168","命中敏感词");
linkedHashMap.put("47003","参数错误");
}
public JSONObject getAccessToken(){
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
Map<String, String> requestUrlParam = new HashMap<>();
//小程序appId
requestUrlParam.put("appid", appId);
//小程序secret
requestUrlParam.put("secret", secrete);
//默认参数
requestUrlParam.put("grant_type", "client_credential");
JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl,requestUrlParam));
return jsonObject;
}
public static void main(String[] args) {
String id ="kSxJL9TR4s_UmOmNLE";
// sendStockWarnMsg("123", "1231", "1231", "23321", id);
}
public JSONObject sendStockWarnMsg(String shopName, String productName, String stock, String note, String toUserOpenId) {
Map<String, Object> data = new HashMap<String, Object>() {{
put("thing1", new HashMap<String, Object>(){{
put("value", shopName);
}});
put("thing6", new HashMap<String, Object>(){{
put("value", productName);
}});
put("number7", new HashMap<String, Object>(){{
put("value", stock);
}});
put("thing5", new HashMap<String, Object>(){{
put("value", note);
}});
}};
log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data);
return sendTempMsg(msgTmpId, toUserOpenId, data);
}
public JSONObject sendTempMsg(String tempId, String toUserOpenId, Map<String, Object> data) {
log.info("开始发送微信模板消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data);
JSONObject object= getAccessToken();
String accessToken=object.get("access_token")+"";
JSONObject object1=new JSONObject();
object1.put("template_id", tempId);
object1.put("touser", toUserOpenId);
object1.put("data",data);
object1.put("miniprogram_state","trial");
object1.put("lang","zh_CN");
String response= HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".concat(accessToken)).body(object1.toString()).execute().body();
log.info("微信模板消息发送成功,相应内容:{}",response);
JSONObject resObj=JSONObject.parseObject(response);
if(ObjectUtil.isNotEmpty(resObj)&&ObjectUtil.isNotNull(resObj)&&"0".equals(resObj.get("errcode")+"")){
return resObj;
}
throw new RuntimeException(linkedHashMap.getOrDefault(resObj.get("errcode") + "", "未知错误"));
}
}