From a0930151a853e712bb9b3e1f9c9b958c8b520036 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Wed, 14 Aug 2024 15:05:16 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=8E=A8=E9=80=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/shopimpl/MsgServiceImpl.java | 22 +++-- .../cn/ysk/cashier/utils/WxAccountUtil.java | 83 +++++++++++++------ .../java/cn/ysk/cashier/utils/WxMsgUtils.java | 58 +++++++++++-- 3 files changed, 121 insertions(+), 42 deletions(-) diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/MsgServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/MsgServiceImpl.java index d399c1ae..c0908595 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/MsgServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/MsgServiceImpl.java @@ -33,39 +33,37 @@ public class MsgServiceImpl implements MsgService { @Override public Page all(Integer page, Integer size, Integer shopId, String nickName, String openId, Integer state, Integer type) { - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(TbShopOpenId::getShopId, shopId); - return shopOpenIdMapper.selectAll(shopId, nickName, openId, new Page<>(page, size)); } @Override public Optional updateShopState(ShopMsgStateDTO shopMsgStateDTO) { - TbShopOpenId shopOpenId = new TbShopOpenId(); - shopOpenId.setStatus(shopMsgStateDTO.getState()); - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper().eq(TbShopOpenId::getShopId, shopMsgStateDTO.getShopId()); - if (shopMsgStateDTO.getType() != null) { - queryWrapper.eq(TbShopOpenId::getType, shopMsgStateDTO.getType()); - } +// TbShopOpenId shopOpenId = new TbShopOpenId(); +// shopOpenId.setStatus(shopMsgStateDTO.getState()); +// LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper().eq(TbShopOpenId::getShopId, shopMsgStateDTO.getShopId()); +// if (shopMsgStateDTO.getType() != null) { +// queryWrapper.eq(TbShopOpenId::getType, shopMsgStateDTO.getType()); +// } if (shopMsgStateDTO.getType() == null) { shopMsgStateDTO.setType(-1); } TbShopMsgState msgState = shopMsgStateService.lambdaQuery().eq(TbShopMsgState::getType, shopMsgStateDTO.getType()) .eq(TbShopMsgState::getShopId, shopMsgStateDTO.getShopId()).one(); + boolean flag; if (msgState == null) { msgState = new TbShopMsgState(); msgState.setShopId(shopMsgStateDTO.getShopId()); msgState.setCreateTime(DateUtil.date().toInstant()); msgState.setState(shopMsgStateDTO.getType()); - shopMsgStateService.save(msgState); + flag = shopMsgStateService.save(msgState); }else { - shopMsgStateService.lambdaUpdate().eq(TbShopMsgState::getShopId, shopMsgStateDTO.getShopId()) + flag = shopMsgStateService.lambdaUpdate().eq(TbShopMsgState::getShopId, shopMsgStateDTO.getShopId()) .eq(TbShopMsgState::getType, shopMsgStateDTO.getType()) .set(TbShopMsgState::getState, shopMsgStateDTO.getState()) .set(TbShopMsgState::getUpdateTime, DateUtil.date().toInstant()).update(); } - return Optional.of(shopOpenIdMapper.update(shopOpenId, queryWrapper) > 0); + return Optional.of(flag); } @Override diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxAccountUtil.java b/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxAccountUtil.java index 409d60ed..f870f5a1 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxAccountUtil.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxAccountUtil.java @@ -5,18 +5,21 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpUtil; +import cn.ysk.cashier.enums.ShopWxMsgTypeEnum; +import cn.ysk.cashier.mybatis.entity.TbShopMsgState; import cn.ysk.cashier.mybatis.entity.TbShopOpenId; +import cn.ysk.cashier.mybatis.mapper.TbShopMsgStateMapper; +import cn.ysk.cashier.pojo.shop.TbShopInfo; +import cn.ysk.cashier.repository.shop.TbShopInfoRepository; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Data @Slf4j @@ -27,21 +30,26 @@ public class WxAccountUtil { @Value("${wx.ysk.secrete}") private String secrete = "8492a7e8d55bbb1b57f5c8276ea1add0"; @Value("${wx.ysk.operationMsgTmpId}") - private String operationMsgTmpId ; + private String operationMsgTmpId; + @Value("${wx.ysk.warnMsgTmpId}") + private String warnMsgTmpId; - static LinkedHashMap linkedHashMap=new LinkedHashMap<>(); + private final TbShopMsgStateMapper shopMsgStateMapper; + private final TbShopInfoRepository shopInfoRepository; + + static LinkedHashMap 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","参数错误"); + 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", "参数错误"); } @@ -84,16 +92,16 @@ public class WxAccountUtil { log.info("开始发送微信模板消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); String accessToken = getAccessToken(); - JSONObject object1=new JSONObject(); + JSONObject object1 = new JSONObject(); object1.put("template_id", templateId); object1.put("touser", toUserOpenId); - object1.put("data",data); + 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")+"")){ + 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; } @@ -104,23 +112,48 @@ public class WxAccountUtil { public void sendOperationMsg(List openIds, String userName, String operationDesc) { openIds.forEach(item -> { Map data = new HashMap() {{ - put("thing19", new HashMap(){{ + put("thing19", new HashMap() {{ put("value", userName); }}); - put("thing8", new HashMap(){{ + put("thing8", new HashMap() {{ put("value", operationDesc); }}); - put("time21", new HashMap(){{ + put("time21", new HashMap() {{ put("value", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss")); }}); }}; log.info("开始发送敏感操作消息, 接收用户openId: {}, 操作用户: {}, 操作描述: {}", item.getOpenId(), userName, operationDesc); try { sendTemplateMsg(operationMsgTmpId, item.getOpenId(), data); - }catch (Exception e) { + } catch (Exception e) { log.error("发送失败, openId: {}, 响应: {}", item.getOpenId(), e.getMessage()); } }); } + + + public void sendStockMsg(String shopName, String productName, int stock, String openId, ShopWxMsgTypeEnum shopWxMsgTypeEnum, Integer shopId) { + + stock = Math.max(stock, 0); + Integer finalStock = stock; + Map data = new HashMap() {{ + put("thing22", new HashMap() {{ + put("value", shopName); + }}); + put("thing4", new HashMap() {{ + put("value", productName); + }}); + put("number5", new HashMap() {{ + put("value", finalStock); + }}); + }}; + log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", openId, data); + try { + sendTemplateMsg(warnMsgTmpId, openId, data); + } catch (Exception e) { + log.error("发送失败, openId:{}, msg: {}", openId, e.getMessage()); + } + + } } diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxMsgUtils.java b/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxMsgUtils.java index bd7525aa..94385cda 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxMsgUtils.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/utils/WxMsgUtils.java @@ -3,8 +3,12 @@ package cn.ysk.cashier.utils; import cn.ysk.cashier.enums.ShopWxMsgTypeEnum; import cn.ysk.cashier.mybatis.entity.TbShopMsgState; import cn.ysk.cashier.mybatis.entity.TbShopOpenId; +import cn.ysk.cashier.mybatis.mapper.TbShopMsgStateMapper; +import cn.ysk.cashier.mybatis.mapper.TbShopOpenIdMapper; import cn.ysk.cashier.mybatis.service.TbShopMsgStateService; import cn.ysk.cashier.mybatis.service.TbShopOpenIdService; +import cn.ysk.cashier.pojo.shop.TbShopInfo; +import cn.ysk.cashier.repository.shop.TbShopInfoRepository; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @@ -25,21 +29,46 @@ public class WxMsgUtils { private final WxAccountUtil wxAccountUtil; private final TbShopMsgStateService shopMsgStateService; + private final TbShopMsgStateMapper shopMsgStateMapper; + private final TbShopOpenIdMapper shopOpenIdMapper; + private final TbShopInfoRepository shopInfoRepository; - public WxMsgUtils(TbShopOpenIdService tbShopOpenIdService, RedisUtils redisUtils, WxAccountUtil wxAccountUtil, TbShopMsgStateService shopMsgStateService) { + public WxMsgUtils(TbShopOpenIdService tbShopOpenIdService, RedisUtils redisUtils, WxAccountUtil wxAccountUtil, TbShopMsgStateService shopMsgStateService, TbShopMsgStateMapper shopMsgStateMapper, TbShopOpenIdMapper shopOpenIdMapper, TbShopInfoRepository shopInfoRepository) { this.tbShopOpenIdService = tbShopOpenIdService; this.redisUtils = redisUtils; this.wxAccountUtil = wxAccountUtil; this.shopMsgStateService = shopMsgStateService; + this.shopMsgStateMapper = shopMsgStateMapper; + this.shopOpenIdMapper = shopOpenIdMapper; + this.shopInfoRepository = shopInfoRepository; + } + + public boolean checkIsOpen(Integer shopId, ShopWxMsgTypeEnum shopWxMsgTypeEnum) { + TbShopMsgState allState = shopMsgStateMapper.selectOne(new LambdaQueryWrapper() + .eq(TbShopMsgState::getShopId, shopId) + .eq(TbShopMsgState::getState, 1) + .eq(TbShopMsgState::getType, ShopWxMsgTypeEnum.ALL_MSG.getType())); + if (allState == null) { + log.warn("店铺全局推送开关未开启"); + return false; + } + + TbShopMsgState currentState = shopMsgStateMapper.selectOne(new LambdaQueryWrapper() + .eq(TbShopMsgState::getShopId, shopId) + .eq(TbShopMsgState::getState, 1) + .eq(TbShopMsgState::getType, shopWxMsgTypeEnum.getType())); + if (currentState == null) { + log.warn("当前推送未开启: {}", shopWxMsgTypeEnum); + return false; + } + return true; } public void aboardOperationMsg(String operationDesc, Integer shopId1) { - TbShopMsgState msgState = shopMsgStateService.lambdaQuery().eq(TbShopMsgState::getShopId, shopId1) - .eq(TbShopMsgState::getType, 2).one(); - if (msgState == null || msgState.getState().equals(0)) { - log.info("店铺未开启推送"); + if (!checkIsOpen(shopId1, ShopWxMsgTypeEnum.OPERATION_MSG)) { return; } + HttpServletRequest request = RequestHolder.getHttpServletRequest(); Object o = redisUtils.get("online-token-"+getToken(request)); @@ -56,6 +85,7 @@ public class WxMsgUtils { .eq(TbShopOpenId::getStatus, 1) .and((queryWrapper) -> queryWrapper.eq(TbShopOpenId::getType, ShopWxMsgTypeEnum.ALL_MSG.getType()) .or().eq(TbShopOpenId::getType, ShopWxMsgTypeEnum.OPERATION_MSG.getType())) + .groupBy(TbShopOpenId::getOpenId) .list(); log.info("即将开始推送敏感操作消息, 接收推送openId列表: {}", openIds); String finalNickName = nickName; @@ -65,6 +95,24 @@ public class WxMsgUtils { } } + public void aboardStockMsg(String shopName, Integer shopId, String proName, Integer stock) { + if (!checkIsOpen(shopId, ShopWxMsgTypeEnum.OPERATION_MSG)) { + return; + } + + List openIds = tbShopOpenIdService.lambdaQuery() + .eq(TbShopOpenId::getShopId, shopId) + .eq(TbShopOpenId::getStatus, 1) + .and((queryWrapper) -> queryWrapper.eq(TbShopOpenId::getType, ShopWxMsgTypeEnum.ALL_MSG.getType()) + .or().eq(TbShopOpenId::getType, ShopWxMsgTypeEnum.STOCK_MSG.getType())) + .groupBy(TbShopOpenId::getOpenId) + .list(); + log.info("即将开始推送敏感操作消息, 接收推送openId列表: {}", openIds); + openIds.parallelStream().forEach(item -> { + wxAccountUtil.sendStockMsg(shopName, proName, stock, item.getOpenId(), ShopWxMsgTypeEnum.STOCK_MSG, shopId); + }); + } + public String getToken(HttpServletRequest request) { final String requestHeader = request.getHeader("Authorization"); if (requestHeader != null && requestHeader.startsWith("Bearer")) {