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] =?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: