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..54f1cea 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -1,5 +1,6 @@ package com.chaozhanggui.system.cashierservice.service; +import cn.hutool.core.thread.ThreadUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @@ -15,6 +16,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; @@ -29,6 +31,7 @@ import java.math.BigDecimal; import java.time.Instant; import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; /** * @author lyf @@ -62,6 +65,8 @@ public class CartService { private final TbUserShopMsgMapper tbUserShopMsgMapper; private final WechatUtil wechatUtil; + private final WxAccountUtil wxAccountUtil; + private final TbShopOpenIdMapper shopOpenIdMapper; @Autowired @@ -77,9 +82,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; @@ -457,6 +463,10 @@ public class CartService { return; } + if (product.getIsStock() == null || product.getIsStock() != 1) { + return; + } + TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(product.getShopId())); if (shopInfo == null) { log.info("商品库存预警发送失败,店铺不存在,店铺id:{}", product.getShopId()); @@ -478,9 +488,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("商品库存不足", product.getName(), + 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 { log.info("{}已在30分钟内推送过消息,跳过发送", item.getOpenId()); @@ -639,11 +649,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()); @@ -864,6 +869,18 @@ public class CartService { // PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject12.toString(), jsonObject.getString("tableId").concat("-").concat(shopId), "", false); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject12.toString(), jsonObject.getString("tableId").concat("-").concat(shopId), jsonObject.getString("userId")); redisUtil.saveMessage(RedisCst.ORDER_EXPIRED.concat(orderId.toString()), orderId.toString(), 60 * 16L); + ThreadUtil.execAsync(() -> { + ThreadUtil.sleep(5, TimeUnit.SECONDS); + 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()); + } + }); } catch (Exception e) { 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 6f5ff31..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); @@ -414,28 +436,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 +468,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); 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"); } 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..fb14ee6 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -149,24 +149,39 @@ public class ProductService { List tbProductSkus = tbProductSkuMapper.selectGroundingByProId(it.getId()); TbProductSkuResult skuResult = tbProductSkuResultMapper.selectByPrimaryKey(it.getId()); + // 上下架对应的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); 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) { 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..9182672 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,23 @@ 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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.math.BigDecimal; import java.util.*; import java.util.concurrent.TimeUnit; @@ -41,6 +54,20 @@ public class UserService { private TbSystemCouponsMapper systemCouponsMapper; @Autowired RedisUtils redisUtils; + @Qualifier("tbShopInfoMapper") + @Autowired + private TbShopInfoMapper tbShopInfoMapper; + private final FileService fileService; + + private final WxAccountUtil wxAccountUtil; + + private final ResourceLoader resourceLoader; + + public UserService(FileService fileService, WxAccountUtil wxAccountUtil, ResourceLoader resourceLoader) { + this.fileService = fileService; + this.wxAccountUtil = wxAccountUtil; + this.resourceLoader = resourceLoader; + } public JSONObject modityIntegral(IntegralVo integralVo, String userSign) { JSONObject object = (JSONObject) JSONObject.toJSON(integralVo); @@ -228,4 +255,19 @@ 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(); + Resource resource = resourceLoader.getResource("classpath:/static/logo.jpg"); + InputStream inputStream = resource.getInputStream(); + QrCodeUtil.generate(wxAccountUtil.getRadarQrCode(Integer.valueOf(shopId)), new QrConfig(500, 500). + setImg(ImageIO.read(inputStream)).setErrorCorrection(ErrorCorrectionLevel.H).setRatio(4), "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..62d2dfa --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/wxUtil/WxAccountUtil.java @@ -0,0 +1,123 @@ +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, Integer stock, String toUserOpenId) { + stock = stock < 0 ? 0 : stock; + 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: {}, 消息数据: {}", toUserOpenId, data); + try { + return sendTemplateMsg(msgTmpId, toUserOpenId, data); + }catch (Exception e) { + log.error("发送失败: {}", e.getMessage()); + return null; + } + } +} 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: diff --git a/src/main/resources/static/logo.jpg b/src/main/resources/static/logo.jpg new file mode 100644 index 0000000..3958379 Binary files /dev/null and b/src/main/resources/static/logo.jpg differ diff --git a/src/main/resources/static/logo.png b/src/main/resources/static/logo.png new file mode 100644 index 0000000..4ed2cd1 Binary files /dev/null and b/src/main/resources/static/logo.png differ