From 13856de1613937bd25ae442576383c2de560823b Mon Sep 17 00:00:00 2001 From: xuwenbo <717567226@qq.com> Date: Tue, 6 Apr 2021 22:33:57 +0800 Subject: [PATCH] =?UTF-8?q?--hotfix=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=94=B9=E7=89=88=E5=AF=BC=E8=87=B4=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E7=99=BB=E5=BD=95=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yixiang/modules/services/AuthService.java | 6 +- yshop-common/pom.xml | 2 +- .../mp/service/WeiXinSubscribeService.java | 412 +++++++++--------- .../service/impl/YxWechatLiveServiceImpl.java | 3 +- 4 files changed, 212 insertions(+), 211 deletions(-) diff --git a/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java b/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java index 61e141fa..f389d143 100644 --- a/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java +++ b/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java @@ -94,10 +94,10 @@ public class AuthService { WxMaUserInfo wxMpUser = wxMaService.getUserService() .getUserInfo(session.getSessionKey(), encryptedData, iv); - String openid = wxMpUser.getOpenId(); + String openid = session.getOpenid(); //如果开启了UnionId - if (StrUtil.isNotBlank(wxMpUser.getUnionId())) { - openid = wxMpUser.getUnionId(); + if (StrUtil.isNotBlank(session.getUnionid())) { + openid = session.getUnionid(); } YxUser yxUser = userService.getOne(Wrappers.lambdaQuery() diff --git a/yshop-common/pom.xml b/yshop-common/pom.xml index ed3913ca..fc005329 100644 --- a/yshop-common/pom.xml +++ b/yshop-common/pom.xml @@ -12,7 +12,7 @@ yshop-common 公共模块 - 4.0.0 + 4.0.7.B diff --git a/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/WeiXinSubscribeService.java b/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/WeiXinSubscribeService.java index 20afb2b8..748db1ad 100644 --- a/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/WeiXinSubscribeService.java +++ b/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/WeiXinSubscribeService.java @@ -1,206 +1,206 @@ -package co.yixiang.modules.mp.service; - -import cn.binarywang.wx.miniapp.api.WxMaService; -import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; -import cn.hutool.core.util.StrUtil; -import co.yixiang.api.YshopException; -import co.yixiang.constant.ShopConstants; -import co.yixiang.enums.ShopCommonEnum; -import co.yixiang.modules.mp.domain.YxWechatTemplate; -import co.yixiang.modules.user.domain.YxUser; -import co.yixiang.modules.user.service.YxUserService; -import co.yixiang.modules.user.service.dto.WechatUserDto; -import co.yixiang.modules.mp.enums.WechatTempateEnum; -import co.yixiang.modules.mp.config.WxMaConfiguration; -import me.chanjar.weixin.common.error.WxErrorException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -/** - * 小程序订阅消息通知 - */ -@Service -public class WeiXinSubscribeService { - - @Autowired - private YxUserService userService; - @Autowired - private YxWechatTemplateService yxWechatTemplateService; - /** - * 充值成功通知 - * @param time 时间 - * @param price 金额 - * @param uid uid - */ - public void rechargeSuccessNotice(String time,String price,Long uid){ - String openid = this.getUserOpenid(uid); - - if(StrUtil.isBlank(openid)) { - return; - } - - Map map = new HashMap<>(); - map.put("first","您的账户金币发生变动,详情如下:"); - map.put("keyword1","充值"); - map.put("keyword2",time); - map.put("keyword3",price); - map.put("remark", ShopConstants.YSHOP_WECHAT_PUSH_REMARK); - String tempId = this.getTempId(WechatTempateEnum.RECHARGE_SUCCESS.getValue()); - if(StrUtil.isNotBlank(tempId)) { - this.sendSubscribeMsg( openid, tempId, "/user/account",map); - } - } - - - /** - * 支付成功通知 - * @param orderId 订单号 - * @param price 金额 - * @param uid uid - */ - public void paySuccessNotice(String orderId,String price,Long uid){ - - String openid = this.getUserOpenid(uid); - if(StrUtil.isBlank(openid)) { - return; - } - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); - Map map = new HashMap<>(); - map.put("amount1",price); - map.put("date2", simpleDateFormat.format(new Date())); - map.put("character_string3",orderId); - map.put("time4",simpleDateFormat.format(new Date())); - map.put("thing5","yshop购买商品"); - String tempId = this.getTempId(WechatTempateEnum.PAY_SUCCESS.getValue()); - if(StrUtil.isNotBlank(tempId)) { - this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); - } - } - - /** - * 退款成功通知 - * @param orderId 订单号 - * @param price 金额 - * @param uid uid - * @param time 时间 - */ - public void refundSuccessNotice(String orderId,String price,Long uid,String time){ - - String openid = this.getUserOpenid(uid); - - if(StrUtil.isBlank(openid)) { - return; - } - - Map map = new HashMap<>(); - map.put("first","您的订单退款申请被通过,钱款将很快还至您的支付账户。"); - //订单号 - map.put("keyword1",orderId); - map.put("keyword2",price); - map.put("keyword3", time); - map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK); - String tempId = this.getTempId(WechatTempateEnum.REFUND_SUCCESS.getValue()); - if(StrUtil.isNotBlank(tempId)) { - this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); - } - } - - /** - * 发货成功通知 - * @param orderId 单号 - * @param deliveryName 快递公司 - * @param deliveryId 快递单号 - * @param uid uid - */ - public void deliverySuccessNotice(String orderId,String deliveryName, - String deliveryId,Long uid){ - - String openid = this.getUserOpenid(uid); - - if(StrUtil.isEmpty(openid)) { - return; - } - - Map map = new HashMap<>(); - map.put("first","亲,宝贝已经启程了,好想快点来到你身边。"); - map.put("keyword2",deliveryName); - map.put("keyword1",orderId); - map.put("keyword3",deliveryId); - map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK); - String tempId = this.getTempId(WechatTempateEnum.DELIVERY_SUCCESS.getValue()); - if(StrUtil.isNotBlank(tempId)) { - this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); - } - } - - - /** - * 构建小程序一次性订阅消息 - * @param openId 单号 - * @param templateId 模板id - * @param page 跳转页面 - * @param map map内容 - * @return String - */ - private void sendSubscribeMsg(String openId, String templateId, String page, Map map){ - WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder() - .toUser(openId) - .templateId(templateId) - .page(page) - .build(); - map.forEach( (k,v)-> { wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.Data(k, v));} ); - WxMaService wxMaService = WxMaConfiguration.getWxMaService(); - try { - wxMaService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage); - } catch (WxErrorException e) { - e.printStackTrace(); - } - } - - /** - * 获取模板消息id - * @param key 模板key - * @return string - */ - private String getTempId(String key){ - YxWechatTemplate yxWechatTemplate = yxWechatTemplateService.lambdaQuery() - .eq(YxWechatTemplate::getType,"subscribe") - .eq(YxWechatTemplate::getTempkey,key) - .one(); - if (yxWechatTemplate == null) { - throw new YshopException("请后台配置key:" + key + "订阅消息id"); - } - if(ShopCommonEnum.IS_STATUS_0.getValue().equals(yxWechatTemplate.getStatus())){ - return ""; - } - return yxWechatTemplate.getTempid(); - } - - - /** - * 获取openid - * @param uid uid - * @return String - */ - private String getUserOpenid(Long uid){ - YxUser yxUser = userService.getById(uid); - if(yxUser == null) { - return ""; - } - - WechatUserDto wechatUserDto = yxUser.getWxProfile(); - if(wechatUserDto == null) { - return ""; - } - if(StrUtil.isBlank(wechatUserDto.getRoutineOpenid())) { - return ""; - } - return wechatUserDto.getRoutineOpenid(); - - } -} +package co.yixiang.modules.mp.service; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; +import cn.hutool.core.util.StrUtil; +import co.yixiang.api.YshopException; +import co.yixiang.constant.ShopConstants; +import co.yixiang.enums.ShopCommonEnum; +import co.yixiang.modules.mp.domain.YxWechatTemplate; +import co.yixiang.modules.user.domain.YxUser; +import co.yixiang.modules.user.service.YxUserService; +import co.yixiang.modules.user.service.dto.WechatUserDto; +import co.yixiang.modules.mp.enums.WechatTempateEnum; +import co.yixiang.modules.mp.config.WxMaConfiguration; +import me.chanjar.weixin.common.error.WxErrorException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 小程序订阅消息通知 + */ +@Service +public class WeiXinSubscribeService { + + @Autowired + private YxUserService userService; + @Autowired + private YxWechatTemplateService yxWechatTemplateService; + /** + * 充值成功通知 + * @param time 时间 + * @param price 金额 + * @param uid uid + */ + public void rechargeSuccessNotice(String time,String price,Long uid){ + String openid = this.getUserOpenid(uid); + + if(StrUtil.isBlank(openid)) { + return; + } + + Map map = new HashMap<>(); + map.put("first","您的账户金币发生变动,详情如下:"); + map.put("keyword1","充值"); + map.put("keyword2",time); + map.put("keyword3",price); + map.put("remark", ShopConstants.YSHOP_WECHAT_PUSH_REMARK); + String tempId = this.getTempId(WechatTempateEnum.RECHARGE_SUCCESS.getValue()); + if(StrUtil.isNotBlank(tempId)) { + this.sendSubscribeMsg( openid, tempId, "/user/account",map); + } + } + + + /** + * 支付成功通知 + * @param orderId 订单号 + * @param price 金额 + * @param uid uid + */ + public void paySuccessNotice(String orderId,String price,Long uid){ + + String openid = this.getUserOpenid(uid); + if(StrUtil.isBlank(openid)) { + return; + } + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); + Map map = new HashMap<>(); + map.put("amount1",price); + map.put("date2", simpleDateFormat.format(new Date())); + map.put("character_string3",orderId); + map.put("time4",simpleDateFormat.format(new Date())); + map.put("thing5","yshop购买商品"); + String tempId = this.getTempId(WechatTempateEnum.PAY_SUCCESS.getValue()); + if(StrUtil.isNotBlank(tempId)) { + this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); + } + } + + /** + * 退款成功通知 + * @param orderId 订单号 + * @param price 金额 + * @param uid uid + * @param time 时间 + */ + public void refundSuccessNotice(String orderId,String price,Long uid,String time){ + + String openid = this.getUserOpenid(uid); + + if(StrUtil.isBlank(openid)) { + return; + } + + Map map = new HashMap<>(); + map.put("first","您的订单退款申请被通过,钱款将很快还至您的支付账户。"); + //订单号 + map.put("keyword1",orderId); + map.put("keyword2",price); + map.put("keyword3", time); + map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK); + String tempId = this.getTempId(WechatTempateEnum.REFUND_SUCCESS.getValue()); + if(StrUtil.isNotBlank(tempId)) { + this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); + } + } + + /** + * 发货成功通知 + * @param orderId 单号 + * @param deliveryName 快递公司 + * @param deliveryId 快递单号 + * @param uid uid + */ + public void deliverySuccessNotice(String orderId,String deliveryName, + String deliveryId,Long uid){ + + String openid = this.getUserOpenid(uid); + + if(StrUtil.isEmpty(openid)) { + return; + } + + Map map = new HashMap<>(); + map.put("first","亲,宝贝已经启程了,好想快点来到你身边。"); + map.put("keyword2",deliveryName); + map.put("keyword1",orderId); + map.put("keyword3",deliveryId); + map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK); + String tempId = this.getTempId(WechatTempateEnum.DELIVERY_SUCCESS.getValue()); + if(StrUtil.isNotBlank(tempId)) { + this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); + } + } + + + /** + * 构建小程序一次性订阅消息 + * @param openId 单号 + * @param templateId 模板id + * @param page 跳转页面 + * @param map map内容 + * @return String + */ + private void sendSubscribeMsg(String openId, String templateId, String page, Map map){ + WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder() + .toUser(openId) + .templateId(templateId) + .page(page) + .build(); + map.forEach( (k,v)-> { wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.MsgData(k, v));} ); + WxMaService wxMaService = WxMaConfiguration.getWxMaService(); + try { + wxMaService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage); + } catch (WxErrorException e) { + e.printStackTrace(); + } + } + + /** + * 获取模板消息id + * @param key 模板key + * @return string + */ + private String getTempId(String key){ + YxWechatTemplate yxWechatTemplate = yxWechatTemplateService.lambdaQuery() + .eq(YxWechatTemplate::getType,"subscribe") + .eq(YxWechatTemplate::getTempkey,key) + .one(); + if (yxWechatTemplate == null) { + throw new YshopException("请后台配置key:" + key + "订阅消息id"); + } + if(ShopCommonEnum.IS_STATUS_0.getValue().equals(yxWechatTemplate.getStatus())){ + return ""; + } + return yxWechatTemplate.getTempid(); + } + + + /** + * 获取openid + * @param uid uid + * @return String + */ + private String getUserOpenid(Long uid){ + YxUser yxUser = userService.getById(uid); + if(yxUser == null) { + return ""; + } + + WechatUserDto wechatUserDto = yxUser.getWxProfile(); + if(wechatUserDto == null) { + return ""; + } + if(StrUtil.isBlank(wechatUserDto.getRoutineOpenid())) { + return ""; + } + return wechatUserDto.getRoutineOpenid(); + + } +} diff --git a/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/impl/YxWechatLiveServiceImpl.java b/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/impl/YxWechatLiveServiceImpl.java index d4531e59..637ea009 100644 --- a/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/impl/YxWechatLiveServiceImpl.java +++ b/yshop-weixin/src/main/java/co/yixiang/modules/mp/service/impl/YxWechatLiveServiceImpl.java @@ -58,7 +58,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import static cn.binarywang.wx.miniapp.api.WxMaLiveService.CREATE_ROOM; +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Broadcast.Room.CREATE_ROOM; + /** * @author hupeng