银收客 模板消息修改

This commit is contained in:
2026-01-26 15:54:25 +08:00
parent 4c65d3226a
commit 782fd5a0e5
4 changed files with 61 additions and 100 deletions

View File

@@ -21,7 +21,7 @@ wx:
appId: wx212769170d2c6b2a
secrete: 8492a7e8d55bbb1b57f5c8276ea1add0
operationMsgTmpId: wFdoUG-dUT7bDRHq8bMJD9CF5TjyH9x_uJQgQByZqHg
warnMsgTmpId: C08OUr80x6wGmUN1zpFhSQ3Sv7VF5vksdZigiEx2pD0
warnMsgTmpId: C08OUr80x6wGmUN1zpFhSZyFA2G6b9_jiZgEppzLB70

View File

@@ -53,18 +53,6 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
@Resource
private WxAccountUtil wxAccountUtil;
private QueryWrapper buildQueryWrapper(ConsStockFlowDTO param) {
QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
/*if (StrUtil.isNotEmpty(param.getName())) {
queryWrapper.like(ConsStockFlow::getName, param.getName());
}*/
Long shopId = StpKit.USER.getShopId(0L);
queryWrapper.eq(ConsStockFlow::getShopId, shopId);
queryWrapper.orderBy(ConsStockFlow::getId, false);
return queryWrapper;
}
@Override
@Transactional(rollbackFor = Exception.class)
public ConsInOutStockHeadParam inStock(ConsInOutStockHeadParam param) {
@@ -72,7 +60,6 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
Long createUserId = StpKit.USER.getLoginIdAsLong();
String createUserName = StpKit.USER.getAccount();
ConsStockFlow head = BeanUtil.copyProperties(param, ConsStockFlow.class);
List<ConsStockFlow> entityList = BeanUtil.copyToList(param.getBodyList(), ConsStockFlow.class);
List<ConsStockFlow> insertList = new ArrayList<>();
List<ConsInfo> updateStockList = new ArrayList<>();
for (ConsInOutStockBodyParam entity : param.getBodyList()) {
@@ -251,15 +238,7 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
Long shopId = entity.getShopId();
BigDecimal afterNumber = entity.getAfterNumber();
ConsInfo consInfo = consInfoMapper.selectOneById(entity.getConId());
String shopName = "";
try {
shopName = StpKit.USER.getShopName();
} catch (Exception e) {
log.error("获取店铺名称失败");
}
if (StrUtil.isEmpty(shopName)) {
shopName = productMapper.getShopName(shopId);
}
String shopName = productMapper.getShopName(shopId);
BigDecimal conWarning = consInfo.getConWarning();
// 库存小于警告值,发送消息提醒
if (NumberUtil.isLess(afterNumber, conWarning)) {
@@ -268,12 +247,8 @@ public class ConsStockFlowServiceImpl extends ServiceImpl<ConsStockFlowMapper, C
return;
}
String conName = StrUtil.format("{}数量<预警值{}", consInfo.getConName(), conWarning);
String finalShopName = shopName;
ThreadUtil.execAsync(() -> {
openIdList.parallelStream().forEach(openId -> {
wxAccountUtil.sendStockMsg(finalShopName, conName, afterNumber, openId);
});
});
ThreadUtil.execAsync(() -> openIdList.parallelStream().forEach(openId ->
wxAccountUtil.sendStockMsg("耗材库存预警", shopName, conName, afterNumber, openId)));
}
}
}

View File

@@ -53,12 +53,8 @@ public class ProductStockFlowServiceImpl extends ServiceImpl<ProductStockFlowMap
}
String productName = StrUtil.sub(product.getName(), 0, 10).concat("...");
String conName = StrUtil.format("{}数量<预警值{}", productName, warnLine);
String finalShopName = shopName;
ThreadUtil.execAsync(() -> {
openIdList.parallelStream().forEach(openId -> {
wxAccountUtil.sendStockMsg(finalShopName, conName, afterNumber, openId);
});
});
ThreadUtil.execAsync(() -> openIdList.parallelStream().forEach(openId ->
wxAccountUtil.sendStockMsg("商品库存预警", shopName, conName, afterNumber, openId)));
}
}
}

View File

@@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.czg.exception.CzgException;
import com.czg.service.RedisService;
import jakarta.annotation.Resource;
import lombok.Data;
@@ -16,7 +17,6 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -32,6 +32,7 @@ import java.util.Map;
@Component
public class WxAccountUtil {
//银收客公众号推送
@Value("${wx.ysk.appId}")
private String appId = "wx212769170d2c6b2a";
@Value("${wx.ysk.secrete}")
@@ -59,84 +60,73 @@ public class WxAccountUtil {
linkedHashMap.put("47003", "参数错误");
}
public String getAccessToken() {
String accessToken = Convert.toStr(redisService.get("accessToken"));
if (StrUtil.isNotEmpty(accessToken)) {
return accessToken;
}
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 RuntimeException(resp);
}
accessToken = respInfo.getString("access_token");
int expiresIn = respInfo.getInteger("expires_in");
redisService.set("accessToken", accessToken, expiresIn - 10);
return accessToken;
}
public JSONObject sendTemplateMsg(String templateId, String toUserOpenId, Map<String, Object> data) {
log.info("开始发送微信模板消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data);
String accessToken = getAccessToken();
JSONObject object1 = new JSONObject();
object1.put("template_id", templateId);
object1.put("touser", toUserOpenId);
object1.put("data", data);
String response = HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/template/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") + "", "未知错误"));
}
public void sendOperationMsg(List<String> openIdList, String userName, String operationDesc) {
openIdList.forEach(openId -> {
Map<String, Object> data = new HashMap<String, Object>() {{
put("thing19", new HashMap<String, Object>() {{
put("value", userName);
}});
put("thing8", new HashMap<String, Object>() {{
put("value", StrUtil.sub(operationDesc,0,20));
}});
put("time21", new HashMap<String, Object>() {{
put("value", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
}});
}};
log.info("开始发送敏感操作消息, 接收用户openId: {}, 操作用户: {}, 操作描述: {}", openId, userName, operationDesc);
Map<String, Object> data = Map.of(
"thing19", Map.of("value", userName),
"thing8", Map.of("value", StrUtil.sub(operationDesc, 0, 20)),
"time21", Map.of("value", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"))
);
try {
sendTemplateMsg(operationMsgTmpId, openId, data);
sendTemplateMsg("敏感操作", operationMsgTmpId, openId, data);
} catch (Exception e) {
log.error("发送失败, openId: {}, 响应: {}", openId, e.getMessage());
}
});
}
public void sendStockMsg(String shopName, String productName, BigDecimal stockNum, String openId) {
public void sendStockMsg(String typeName, String shopName, String productName, BigDecimal stockNum, String openId) {
String stockNumStr = stockNum.toPlainString();
Map<String, Object> data = new HashMap<String, Object>() {{
put("thing22", new HashMap<String, Object>() {{
put("value", shopName);
}});
put("thing4", new HashMap<String, Object>() {{
put("value", productName);
}});
put("number5", new HashMap<String, Object>() {{
put("value", stockNumStr);
}});
}};
log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", openId, data);
Map<String, Object> data = Map.of(
//品名
"thing34", Map.of("value", typeName),
//商品名称
"thing4", Map.of("value", productName),
//商品数量
"number5", Map.of("value", stockNumStr),
//商家名称
"thing22", Map.of("value", shopName)
);
try {
sendTemplateMsg(warnMsgTmpId, openId, data);
sendTemplateMsg("库存预警", warnMsgTmpId, openId, data);
} catch (Exception e) {
log.error("发送失败, openId:{}, msg: {}", openId, e.getMessage());
}
}
public String getAccessToken() {
synchronized (this) {
String accessToken = Convert.toStr(redisService.get("accessToken"));
if (StrUtil.isNotEmpty(accessToken)) {
return accessToken;
}
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 RuntimeException(resp);
}
accessToken = respInfo.getString("access_token");
int expiresIn = respInfo.getInteger("expires_in");
redisService.set("accessToken", accessToken, expiresIn - 10);
return accessToken;
}
}
public JSONObject sendTemplateMsg(String detail, String templateId, String toUserOpenId, Map<String, Object> data) {
log.info("银收客公众号消息推送,消息类型:{} 接收用户openId: {}, 消息数据: {}", detail, toUserOpenId, data);
String accessToken = getAccessToken();
JSONObject object1 = new JSONObject();
object1.put("template_id", templateId);
object1.put("touser", toUserOpenId);
object1.put("data", data);
String response = HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/template/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 CzgException(linkedHashMap.getOrDefault(resObj.get("errcode") + "", "未知错误"));
}
}