From 4feb821f7009512ebeaacb430e0d2f0ff8fa22f4 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Tue, 23 Jul 2024 10:40:28 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BC=9A=E5=91=98=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cashierservice/service/LoginService.java | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java index 6f5ff31..a8aebc1 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java @@ -414,28 +414,20 @@ public class LoginService { @Transactional public Result upPhone(String openId, String phone, String shopId) { TbUserInfo userInfo = tbUserInfoMapper.selectByOpenId(openId); - boolean isup = false; - if(StringUtils.isNotBlank(userInfo.getTelephone())){ - List tbShopUsers = tbShopUserMapper.selectByPhone(userInfo.getTelephone()); - for (TbShopUser tbShopUser : tbShopUsers) { - if (tbShopUser.getShopId().equals(shopId)) { - isup = true; - } - tbShopUser.setTelephone(phone); - tbShopUser.setUpdatedAt(System.currentTimeMillis()); - tbShopUserMapper.updateByPrimaryKey(tbShopUser); - } - if(!isup){ - registerShopUser(userInfo,shopId); - } + // 同一个手机号 同一个店 userId 不为空 + TbShopUser tbShopUserPC = tbShopUserMapper.selectPCByPhoneAndShopId(userInfo.getTelephone(),shopId); + if (tbShopUserPC != null) { + TbShopUser tbShopUserSM = tbShopUserMapper.selectByUserIdAndShopId(userInfo.getId().toString(),shopId); + tbShopUserMapper.deleteByPrimaryKey(tbShopUserSM.getId()); + tbShopUserPC.setUserId(userInfo.getId().toString()); + tbShopUserMapper.updateByPrimaryKey(tbShopUserPC); }else { - TbShopUser shopUser = tbShopUserMapper.selectByUserIdAndShopId(userInfo.getId().toString(), shopId); - if (shopUser != null) { - shopUser.setTelephone(phone); - shopUser.setUpdatedAt(System.currentTimeMillis()); - tbShopUserMapper.updateByPrimaryKey(shopUser); - }else { - registerShopUser(userInfo,shopId); + TbShopUser tbShopUserSM = tbShopUserMapper.selectByUserIdAndShopId(userInfo.getId().toString(),shopId); + if (tbShopUserSM != null) { + tbShopUserSM.setIsVip(Byte.parseByte("1")); + tbShopUserSM.setTelephone(phone); + tbShopUserSM.setUpdatedAt(System.currentTimeMillis()); + tbShopUserMapper.updateByPrimaryKey(tbShopUserSM); } } userInfo.setTelephone(phone); @@ -454,7 +446,7 @@ public class LoginService { shopUser.setCode(dynamicCode); shopUser.setTelephone(userInfo.getTelephone()); shopUser.setAmount(BigDecimal.ZERO); - shopUser.setIsVip(Byte.parseByte("0")); + shopUser.setIsVip(Byte.parseByte("1")); shopUser.setCreditAmount(BigDecimal.ZERO); shopUser.setConsumeAmount(BigDecimal.ZERO); shopUser.setConsumeNumber(0); From afb920f3600a29c60bc91f6d3400798abadfbc63 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Tue, 23 Jul 2024 11:13:08 +0800 Subject: [PATCH 2/8] =?UTF-8?q?1.=E5=82=A8=E5=80=BC=E5=8D=A1=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=90=8C=E6=AD=A5=E4=BF=9D=E5=AD=98=E5=BA=93=E5=AD=98?= =?UTF-8?q?=E6=B6=88=E8=80=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/cashierservice/service/PayService.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java index b28db1d..cf77531 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/PayService.java @@ -405,6 +405,12 @@ public class PayService { producer.printMechine(orderId); sendOrderToClient(orderInfo); + + // 发送mq消息并保存库存记录 + JSONObject data = new JSONObject(); + data.put("orderId", orderInfo.getId()); + data.put("plat", "miniApp"); + mQUtils.sendStockSaleMsg(data); return Result.success(CodeEnum.SUCCESS, "1"); } From 954bfb00ac9d6a6e40734654919f4866bdc2dea4 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Tue, 23 Jul 2024 14:10:30 +0800 Subject: [PATCH 3/8] =?UTF-8?q?1.=E5=A4=9A=E8=A7=84=E6=A0=BC=E5=95=86?= =?UTF-8?q?=E5=93=81snap=E6=8B=BC=E6=8E=A5=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/cashierservice/service/ProductService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index dd1db47..32c2a03 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -149,6 +149,7 @@ public class ProductService { List tbProductSkus = tbProductSkuMapper.selectGroundingByProId(it.getId()); TbProductSkuResult skuResult = tbProductSkuResultMapper.selectByPrimaryKey(it.getId()); + // 上下架对应的sku HashSet specSet = new HashSet<>(); tbProductSkus.forEach(item -> { String specSnap = item.getSpecSnap(); @@ -162,11 +163,12 @@ public class ProductService { JSONObject snapJSON; JSONArray finalSnap = new JSONArray(); - String finalValues = ""; for (Object snap : tagSnaps) { snapJSON = (JSONObject) snap; String values = snapJSON.getString("value"); + String finalValues = ""; + if (StrUtil.isNotBlank(values)) { String[] valueList = values.split(","); for (String value : valueList) { From e87537850a776dfeab0788581ff4f878777d1a10 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Thu, 25 Jul 2024 10:10:54 +0800 Subject: [PATCH 4/8] =?UTF-8?q?1.=E6=96=B0=E5=A2=9Ephp=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E8=AE=A2=E9=98=85=E8=80=85openid=E6=8E=A5=E5=8F=A3=202.?= =?UTF-8?q?=E5=BA=93=E5=AD=98=E9=A2=84=E8=AD=A6=E6=9B=B4=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E6=8E=A8=E9=80=81=203.sku=E5=95=86?= =?UTF-8?q?=E5=93=81=E4=B8=8A=E4=B8=8B=E6=9E=B6=E6=9C=80=E4=BD=8E=E4=BB=B7?= =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E7=A1=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cashierservice/auth/LoginFilter.java | 5 +- .../controller/CommonController.java | 23 +++- .../controller/UserContoller.java | 37 +++--- .../entity/dto/WxMsgSubDTO.java | 9 ++ .../cashierservice/service/CartService.java | 12 +- .../cashierservice/service/FileService.java | 14 +++ .../cashierservice/service/LoginService.java | 22 ++++ .../service/ProductService.java | 17 ++- .../cashierservice/service/UserService.java | 30 +++++ .../cashierservice/wxUtil/WechatUtil.java | 2 + .../cashierservice/wxUtil/WxAccountUtil.java | 116 ++++++++++++++++++ src/main/resources/application.yml | 6 + 12 files changed, 263 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java create mode 100644 src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/auth/LoginFilter.java b/src/main/java/com/chaozhanggui/system/cashierservice/auth/LoginFilter.java index 6525ba2..d2d0f9c 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/auth/LoginFilter.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/auth/LoginFilter.java @@ -61,8 +61,11 @@ public class LoginFilter implements Filter { "/cashierService/song/detail", "/cashierService/song/record", "/cashierService/song", + "/cashierService/song/**", "/cashierService/song/singing", - "/cashierService/login/test" + "/cashierService/login/test", + "cashierService/subMsg", + "cashierService/user/subQrCode" ); @Autowired diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java index ba65f22..33217e3 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/CommonController.java @@ -2,11 +2,13 @@ package com.chaozhanggui.system.cashierservice.controller; import com.chaozhanggui.system.cashierservice.dao.TbPlatformDictMapper; import com.chaozhanggui.system.cashierservice.entity.TbPlatformDict; +import com.chaozhanggui.system.cashierservice.entity.dto.WxMsgSubDTO; import com.chaozhanggui.system.cashierservice.entity.vo.DistrictVo; import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.redis.RedisCst; import com.chaozhanggui.system.cashierservice.redis.RedisUtil; import com.chaozhanggui.system.cashierservice.service.FileService; +import com.chaozhanggui.system.cashierservice.service.LoginService; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.util.LocationUtils; @@ -44,6 +46,10 @@ public class CommonController { private TbPlatformDictMapper platformDictMapper; @Resource private FileService fileService; + + private final LoginService loginService;; + + /** * 一分钟 */ @@ -198,4 +204,19 @@ public class CommonController { // 将手机号码的请求次数加1 redisUtil.getIncrNum(key, "2"); } -} \ No newline at end of file + + + /** + * 订阅消息 + * @param wxMsgSubDTO + * @return + */ + @PostMapping("/subMsg") + public Result subscribeWxAccount(@RequestBody WxMsgSubDTO wxMsgSubDTO) { + if (wxMsgSubDTO.getOpenId() == null || wxMsgSubDTO.getShopId() == null) { + return Result.fail("shopId或openId缺失"); + } + loginService.addShopId(wxMsgSubDTO.getOpenId(), wxMsgSubDTO.getShopId()); + return Result.success(CodeEnum.SUCCESS); + } +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/UserContoller.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/UserContoller.java index 56588d0..2908836 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/UserContoller.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/UserContoller.java @@ -1,48 +1,28 @@ package com.chaozhanggui.system.cashierservice.controller; -import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.RandomUtil; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.chaozhanggui.system.cashierservice.dao.TbMerchantAccountMapper; import com.chaozhanggui.system.cashierservice.dao.TbShopInfoMapper; import com.chaozhanggui.system.cashierservice.dao.TbShopUserMapper; import com.chaozhanggui.system.cashierservice.dao.TbUserInfoMapper; -import com.chaozhanggui.system.cashierservice.entity.TbMerchantAccount; import com.chaozhanggui.system.cashierservice.entity.TbShopInfo; import com.chaozhanggui.system.cashierservice.entity.TbShopUser; import com.chaozhanggui.system.cashierservice.entity.TbUserInfo; -import com.chaozhanggui.system.cashierservice.entity.dto.AuthUserDto; -import com.chaozhanggui.system.cashierservice.entity.dto.OnlineUserDto; import com.chaozhanggui.system.cashierservice.entity.vo.IntegralFlowVo; import com.chaozhanggui.system.cashierservice.entity.vo.IntegralVo; -import com.chaozhanggui.system.cashierservice.entity.vo.OrderVo; -import com.chaozhanggui.system.cashierservice.service.LoginService; -import com.chaozhanggui.system.cashierservice.service.OnlineUserService; import com.chaozhanggui.system.cashierservice.service.UserService; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; -import com.chaozhanggui.system.cashierservice.util.IpUtil; -import com.chaozhanggui.system.cashierservice.util.MD5Utils; -import com.chaozhanggui.system.cashierservice.util.StringUtil; import com.chaozhanggui.system.cashierservice.util.TokenUtil; -import com.chaozhanggui.system.cashierservice.wxUtil.WechatUtil; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; -import javax.servlet.http.HttpServletRequest; +import java.io.IOException; import java.math.BigDecimal; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.UUID; @CrossOrigin(origins = "*") @RestController @@ -142,7 +122,7 @@ public class UserContoller { return userService.modityIntegral(integralVo, userSign); } - @PostMapping("/userIntegral") + @PostMapping("/userIntegral") public JSONObject userIntegral(@RequestHeader String token, @RequestBody IntegralFlowVo integralFlowVo) throws Exception { JSONObject jsonObject = TokenUtil.parseParamFromToken(token); String userSign = jsonObject.getString("userSign"); @@ -164,4 +144,17 @@ public class UserContoller { } + /** + * 获取订阅当前用户店库存预警消息的二维码 + * + * @param shopId + * @return + */ + @GetMapping("/subQrCode") + public Result getSubQrCode(String shopId) throws Exception { + String url = userService.getSubQrCode(shopId); + return Result.successWithData(url); + } + + } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java new file mode 100644 index 0000000..de1eb52 --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/WxMsgSubDTO.java @@ -0,0 +1,9 @@ +package com.chaozhanggui.system.cashierservice.entity.dto; + +import lombok.Data; + +@Data +public class WxMsgSubDTO { + private String shopId; + private String openId; +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index f089af2..707c000 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -15,6 +15,7 @@ import com.chaozhanggui.system.cashierservice.util.JSONUtil; import com.chaozhanggui.system.cashierservice.util.LockUtils; import com.chaozhanggui.system.cashierservice.util.N; import com.chaozhanggui.system.cashierservice.wxUtil.WechatUtil; +import com.chaozhanggui.system.cashierservice.wxUtil.WxAccountUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -62,6 +63,8 @@ public class CartService { private final TbUserShopMsgMapper tbUserShopMsgMapper; private final WechatUtil wechatUtil; + private final WxAccountUtil wxAccountUtil; + private final TbShopOpenIdMapper shopOpenIdMapper; @Autowired @@ -77,9 +80,10 @@ public class CartService { private final RedisTemplate redisTemplate; - public CartService(TbUserShopMsgMapper tbUserShopMsgMapper, WechatUtil wechatUtil, TbShopOpenIdMapper shopOpenIdMapper, ProductService productService, TbProductMapper tbProductMapper, RedisTemplate redisTemplate) { + public CartService(TbUserShopMsgMapper tbUserShopMsgMapper, WechatUtil wechatUtil, WxAccountUtil wxAccountUtil, TbShopOpenIdMapper shopOpenIdMapper, ProductService productService, TbProductMapper tbProductMapper, RedisTemplate redisTemplate) { this.tbUserShopMsgMapper = tbUserShopMsgMapper; this.wechatUtil = wechatUtil; + this.wxAccountUtil = wxAccountUtil; this.shopOpenIdMapper = shopOpenIdMapper; this.productService = productService; this.tbProductMapper = tbProductMapper; @@ -478,9 +482,9 @@ public class CartService { shopOpenIds.forEach(item -> { String message = redisUtil.getMessage(RedisCst.SEND_STOCK_WARN_MSG + product.getId() + ":" + item.getOpenId()); if (message == null) { - wechatUtil.sendStockWarnMsg(shopInfo.getShopName(), product.getName(), - product.getIsDistribute() == 1 ? String.valueOf(product.getStockNumber() - num) : String.valueOf(productSku.getStockNumber() - num), - "商品库存不足,请及时补充。", item.getOpenId()); + wxAccountUtil.sendStockWarnMsg(shopInfo.getShopName(), product.getName(), + product.getIsDistribute() == 1 ? String.valueOf(product.getStockNumber() - num) : String.valueOf(productSku.getStockNumber() - num) + , item.getOpenId()); redisUtil.saveMessage(RedisCst.SEND_STOCK_WARN_MSG + product.getId() + ":" + item.getOpenId(), product.getId().toString(), 60 * 30L); }else { log.info("{}已在30分钟内推送过消息,跳过发送", item.getOpenId()); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/FileService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/FileService.java index 0d079ae..7e8e15d 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/FileService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/FileService.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.PostConstruct; +import java.io.InputStream; import java.util.UUID; @Service @@ -51,6 +52,19 @@ public class FileService { return "https://" + bucketName + "." + endpoint + "/" + path; } + public String uploadFileByInputStream(String suffix, InputStream inputStream) throws Exception { + String path = getPath("upload", suffix); + OSS client = new OSSClientBuilder().build(endpoint, credentialsProvider); + try { + client.putObject(bucketName, path, inputStream); + client.shutdown(); + } catch (Exception e) { + throw new Exception("上传异常"); + } + + return "https://" + bucketName + "." + endpoint + "/" + path; + } + public String getPath(String prefix, String suffix) { //生成uuid String uuid = UUID.randomUUID().toString().replaceAll("-", ""); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java index a8aebc1..3f612c3 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/LoginService.java @@ -64,6 +64,28 @@ public class LoginService { this.shopOpenIdMapper = shopOpenIdMapper; } + public void addShopId(String openId,String shopId) { + TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByShopIdAndOpenId(Integer.valueOf(shopId),openId); + if(Objects.isNull(shopMsg)){ + shopMsg=new TbUserShopMsg(); + shopMsg.setShopId(Integer.valueOf(shopId)); + shopMsg.setOpenId(openId); + shopMsg.setCreateTime(new Date()); + shopMsg.setStatus("1"); + tbUserShopMsgMapper.insert(shopMsg); + } + + // 为商家绑定openid + if (shopOpenIdMapper.countByOpenId(openId, Integer.valueOf(shopId)) == null) { + TbShopOpenId shopOpenId = new TbShopOpenId(); + shopOpenId.setOpenId(openId); + shopOpenId.setCreateTime(DateUtil.date()); + shopOpenId.setShopId(Integer.valueOf(shopId)); + shopOpenId.setStatus(1); + shopOpenIdMapper.insert(shopOpenId); + } + } + public Result wxBusinessLogin(String openId,String shopId){ TbUserShopMsg shopMsg= tbUserShopMsgMapper.selectByShopIdAndOpenId(Integer.valueOf(shopId),openId); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index 32c2a03..fb14ee6 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -151,12 +151,25 @@ public class ProductService { // 上下架对应的sku HashSet specSet = new HashSet<>(); - tbProductSkus.forEach(item -> { + + BigDecimal lowerPrice = null; + for (TbProductSku item : tbProductSkus) { + if (lowerPrice == null || lowerPrice.compareTo(item.getSalePrice()) > 0) { + lowerPrice = item.getSalePrice(); + } + String specSnap = item.getSpecSnap(); if (specSnap != null) { specSet.addAll(Arrays.asList(specSnap.split(","))); } - }); + } + + if (lowerPrice == null) { + lowerPrice = BigDecimal.ZERO; + } + + it.setLowPrice(lowerPrice); + String tagSnap = skuResult != null ? skuResult.getTagSnap() : null; if (tagSnap != null) { JSONArray tagSnaps = JSONObject.parseArray(tagSnap); diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/UserService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/UserService.java index c39c22d..49819fa 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/UserService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/UserService.java @@ -1,7 +1,10 @@ package com.chaozhanggui.system.cashierservice.service; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.RandomUtil; +import cn.hutool.extra.qrcode.QrCodeUtil; +import cn.hutool.extra.qrcode.QrConfig; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.chaozhanggui.system.cashierservice.dao.*; @@ -14,13 +17,18 @@ import com.chaozhanggui.system.cashierservice.redis.RedisUtil; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.util.*; +import com.chaozhanggui.system.cashierservice.wxUtil.WxAccountUtil; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.math.BigDecimal; import java.util.*; import java.util.concurrent.TimeUnit; @@ -41,6 +49,17 @@ public class UserService { private TbSystemCouponsMapper systemCouponsMapper; @Autowired RedisUtils redisUtils; + @Qualifier("tbShopInfoMapper") + @Autowired + private TbShopInfoMapper tbShopInfoMapper; + private final FileService fileService; + + private final WxAccountUtil wxAccountUtil; + + public UserService(FileService fileService, WxAccountUtil wxAccountUtil) { + this.fileService = fileService; + this.wxAccountUtil = wxAccountUtil; + } public JSONObject modityIntegral(IntegralVo integralVo, String userSign) { JSONObject object = (JSONObject) JSONObject.toJSON(integralVo); @@ -228,4 +247,15 @@ public class UserService { int sysNum = systemCouponsMapper.selectByAmount(orderNum); return userNum+sysNum; } + + public String getSubQrCode(String shopId) throws Exception { + TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(shopId)); + if (shopInfo == null) { + throw new MsgException("店铺不存在"); + } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + QrCodeUtil.generate(wxAccountUtil.getRadarQrCode(Integer.valueOf(shopId)), new QrConfig(100, 100), "png", outputStream); + + return fileService.uploadFileByInputStream(".png", new ByteArrayInputStream(outputStream.toByteArray())); + } } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WechatUtil.java b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WechatUtil.java index 8d42fcc..286a947 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WechatUtil.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WechatUtil.java @@ -160,4 +160,6 @@ public class WechatUtil { throw new RuntimeException(linkedHashMap.getOrDefault(resObj.get("errcode") + "", "未知错误")); } + + } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java new file mode 100644 index 0000000..b83a833 --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java @@ -0,0 +1,116 @@ +package com.chaozhanggui.system.cashierservice.wxUtil; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.extra.qrcode.QrCodeUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpUtil; +import com.alibaba.fastjson.JSONObject; +import com.chaozhanggui.system.cashierservice.exception.MsgException; +import lombok.Data; +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; + +@Data +@Slf4j +@Component +public class WxAccountUtil { + @Value("${wx.ysk.appId}") + private static String appId = "wx212769170d2c6b2a"; + @Value("${wx.ysk.secrete}") + private static String secrete = "8492a7e8d55bbb1b57f5c8276ea1add0"; + @Value("${wx.ysk.warnMsgTmpId}") + private static String msgTmpId = "C08OUr80x6wGmUN1zpFhSQ3Sv7VF5vksdZigiEx2pD0"; + + 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","参数错误"); + + } + + public static void main(String[] args) { + sendStockWarnMsg("13213", "31123", "234", "ojC-S6n2DDlpj52iVMoiLL0Ry4HI"); + } + + public static String getRadarQrCode(Integer shopId) { + HashMap req = new HashMap<>(); + req.put("expire_seconds", 300); + req.put("action_name", "QR_STR_SCENE"); + HashMap actionInfo = new HashMap<>(); + HashMap scene = new HashMap<>(); + scene.put("scene_str", "msg" + shopId); + actionInfo.put("scene", scene); + req.put("action_info", actionInfo); + log.info("开始获取公众号二维码, 请求数据: {}", req); + String resp = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + getAccessToken(), JSONObject.toJSONString(req)); + JSONObject respInfo = JSONObject.parseObject(resp); + log.warn("获取微信公众号二维码结束,响应: {}", resp); + if (!respInfo.containsKey("url")) { + log.warn("获取微信公众号二维码失败,响应: {}", resp); + throw new MsgException(resp); + } + return respInfo.getString("url"); + } + + + 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"); + } + + public static JSONObject sendTemplateMsg(String templateId, String toUserOpenId, Map 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 static JSONObject sendStockWarnMsg(String shopName, String productName, String stock, String toUserOpenId) { + Map data = new HashMap() {{ + put("thing22", new HashMap(){{ + put("value", shopName); + }}); + put("thing4", new HashMap(){{ + put("value", productName); + }}); + put("number5", new HashMap(){{ + put("value", stock); + }}); + }}; + log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); + return sendTemplateMsg(msgTmpId, toUserOpenId, data); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9ef5971..b31734b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -19,6 +19,12 @@ wx: appId: wxcf0fe8cdba153fd6 secrete: c33e06467c6879a62af633d50ed6b720 warnMsgTmpId: IZ-l9p9yBgcvhRR0uN6cBQPkWJ5i05zyWMkfeCPaAmY + + ysk: + appId: wx212769170d2c6b2a + secrete: 8492a7e8d55bbb1b57f5c8276ea1add0 + warnMsgTmpId: C08OUr80x6wGmUN1zpFhSQ3Sv7VF5vksdZigiEx2pD0 + # spring: profiles: From 93f5c51d4451b34a2137349483358f020cabfee1 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Thu, 25 Jul 2024 11:26:07 +0800 Subject: [PATCH 5/8] =?UTF-8?q?1.=E6=8E=A8=E9=80=81=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=81=9C=E6=AD=A2=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/cashierservice/wxUtil/WxAccountUtil.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java index b83a833..0bf0132 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java @@ -111,6 +111,11 @@ public class WxAccountUtil { }}); }}; log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); - return sendTemplateMsg(msgTmpId, toUserOpenId, data); + try { + return sendTemplateMsg(msgTmpId, toUserOpenId, data); + }catch (Exception e) { + log.error("发送失败: {}", e.getMessage()); + return null; + } } } From e4c3478f8ec809b474db8b6cfd1829421b2fb288 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Thu, 25 Jul 2024 15:03:01 +0800 Subject: [PATCH 6/8] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E6=A0=87=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chaozhanggui/system/cashierservice/service/CartService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index 707c000..8ebaf84 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -482,7 +482,7 @@ public class CartService { shopOpenIds.forEach(item -> { String message = redisUtil.getMessage(RedisCst.SEND_STOCK_WARN_MSG + product.getId() + ":" + item.getOpenId()); if (message == null) { - wxAccountUtil.sendStockWarnMsg(shopInfo.getShopName(), product.getName(), + wxAccountUtil.sendStockWarnMsg("商品库存不足", product.getName(), product.getIsDistribute() == 1 ? String.valueOf(product.getStockNumber() - num) : String.valueOf(productSku.getStockNumber() - num) , item.getOpenId()); redisUtil.saveMessage(RedisCst.SEND_STOCK_WARN_MSG + product.getId() + ":" + item.getOpenId(), product.getId().toString(), 60 * 30L); From de2300311deb4e65138388d1758c535f811740f5 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Thu, 25 Jul 2024 17:06:07 +0800 Subject: [PATCH 7/8] =?UTF-8?q?1.=E6=A8=A1=E6=9D=BF=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E8=B4=9F=E5=80=BC=E4=B8=8D=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/cashierservice/service/CartService.java | 2 +- .../system/cashierservice/wxUtil/WxAccountUtil.java | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index 8ebaf84..6d6f849 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -483,7 +483,7 @@ public class CartService { String message = redisUtil.getMessage(RedisCst.SEND_STOCK_WARN_MSG + product.getId() + ":" + item.getOpenId()); if (message == null) { wxAccountUtil.sendStockWarnMsg("商品库存不足", product.getName(), - product.getIsDistribute() == 1 ? String.valueOf(product.getStockNumber() - num) : String.valueOf(productSku.getStockNumber() - num) + product.getIsDistribute() == 1 ? product.getStockNumber() - num : (int) (productSku.getStockNumber() - num) , item.getOpenId()); redisUtil.saveMessage(RedisCst.SEND_STOCK_WARN_MSG + product.getId() + ":" + item.getOpenId(), product.getId().toString(), 60 * 30L); }else { diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java index 0bf0132..62d2dfa 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java @@ -44,7 +44,7 @@ public class WxAccountUtil { } public static void main(String[] args) { - sendStockWarnMsg("13213", "31123", "234", "ojC-S6n2DDlpj52iVMoiLL0Ry4HI"); +// sendStockWarnMsg("13213", "31123", "234", "ojC-S6n2DDlpj52iVMoiLL0Ry4HI"); } public static String getRadarQrCode(Integer shopId) { @@ -98,7 +98,9 @@ public class WxAccountUtil { throw new RuntimeException(linkedHashMap.getOrDefault(resObj.get("errcode") + "", "未知错误")); } - public static JSONObject sendStockWarnMsg(String shopName, String productName, String stock, String toUserOpenId) { + public static JSONObject sendStockWarnMsg(String shopName, String productName, Integer stock, String toUserOpenId) { + stock = stock < 0 ? 0 : stock; + Integer finalStock = stock; Map data = new HashMap() {{ put("thing22", new HashMap(){{ put("value", shopName); @@ -107,7 +109,7 @@ public class WxAccountUtil { put("value", productName); }}); put("number5", new HashMap(){{ - put("value", stock); + put("value", finalStock); }}); }}; log.info("开始发送库存预警消息, 接收用户openId: {}, 消息数据: {}", toUserOpenId, data); From 95f529e4d7aac0dd4e8a098054634cfbdcf5aa94 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Fri, 26 Jul 2024 10:11:38 +0800 Subject: [PATCH 8/8] =?UTF-8?q?1.=E8=80=97=E6=9D=90=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E9=A2=84=E8=AD=A6=E5=80=BC=E6=94=B9=E4=B8=BA=E7=AD=89=E4=BA=8E?= =?UTF-8?q?=202.=20=E8=80=97=E6=9D=90=E6=95=B0=E9=87=8F=E4=B8=BA=E6=B6=88?= =?UTF-8?q?=E8=80=97=E5=AE=8C=E6=AF=95=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cashierservice/service/CartService.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index 6d6f849..72d6b4b 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -643,11 +643,6 @@ public class CartService { return; } - // 发送判断耗材是否耗尽消息 - JSONObject objectMsg = new JSONObject(); - objectMsg.put("skuId", Integer.valueOf(cashierCart.getSkuId())); - objectMsg.put("shopId", Integer.valueOf(cashierCart.getShopId())); - producer.con_msg(objectMsg.toString()); totalAmount = totalAmount.add(cashierCart.getTotalAmount()); packAMount = packAMount.add(cashierCart.getPackFee()); @@ -849,6 +844,17 @@ public class CartService { log.info("开始发送mq消息,消耗耗材,消息内容:{}", jsonObject2); producer.cons(jsonObject2.toString()); + for (int i = 0; i < array.size(); i++) { + JSONObject object = array.getJSONObject(i); + TbCashierCart cashierCart = JSONUtil.parseJSONStr2T(object.toJSONString(), TbCashierCart.class); + // 发送判断耗材是否耗尽消息 + JSONObject objectMsg = new JSONObject(); + objectMsg.put("skuId", Integer.valueOf(cashierCart.getSkuId())); + objectMsg.put("shopId", Integer.valueOf(cashierCart.getShopId())); + producer.con_msg(objectMsg.toString()); + } + + redisUtil.saveMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId")).concat("-").concat(shopId), array.toJSONString()); orderInfo.setDetailList(orderDetails); JSONObject jsonObject1 = new JSONObject();