--hotfix修复微信改版导致小程序授权登录失败

This commit is contained in:
xuwenbo
2021-04-06 22:33:57 +08:00
parent 7115a57ca7
commit 13856de161
4 changed files with 212 additions and 211 deletions

View File

@ -94,10 +94,10 @@ public class AuthService {
WxMaUserInfo wxMpUser = wxMaService.getUserService() WxMaUserInfo wxMpUser = wxMaService.getUserService()
.getUserInfo(session.getSessionKey(), encryptedData, iv); .getUserInfo(session.getSessionKey(), encryptedData, iv);
String openid = wxMpUser.getOpenId(); String openid = session.getOpenid();
//如果开启了UnionId //如果开启了UnionId
if (StrUtil.isNotBlank(wxMpUser.getUnionId())) { if (StrUtil.isNotBlank(session.getUnionid())) {
openid = wxMpUser.getUnionId(); openid = session.getUnionid();
} }
YxUser yxUser = userService.getOne(Wrappers.<YxUser>lambdaQuery() YxUser yxUser = userService.getOne(Wrappers.<YxUser>lambdaQuery()

View File

@ -12,7 +12,7 @@
<artifactId>yshop-common</artifactId> <artifactId>yshop-common</artifactId>
<name>公共模块</name> <name>公共模块</name>
<properties> <properties>
<weixin-java.version>4.0.0</weixin-java.version> <weixin-java.version>4.0.7.B</weixin-java.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -1,206 +1,206 @@
package co.yixiang.modules.mp.service; package co.yixiang.modules.mp.service;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import co.yixiang.api.YshopException; import co.yixiang.api.YshopException;
import co.yixiang.constant.ShopConstants; import co.yixiang.constant.ShopConstants;
import co.yixiang.enums.ShopCommonEnum; import co.yixiang.enums.ShopCommonEnum;
import co.yixiang.modules.mp.domain.YxWechatTemplate; import co.yixiang.modules.mp.domain.YxWechatTemplate;
import co.yixiang.modules.user.domain.YxUser; import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.service.YxUserService; import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.modules.user.service.dto.WechatUserDto; import co.yixiang.modules.user.service.dto.WechatUserDto;
import co.yixiang.modules.mp.enums.WechatTempateEnum; import co.yixiang.modules.mp.enums.WechatTempateEnum;
import co.yixiang.modules.mp.config.WxMaConfiguration; import co.yixiang.modules.mp.config.WxMaConfiguration;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* 小程序订阅消息通知 * 小程序订阅消息通知
*/ */
@Service @Service
public class WeiXinSubscribeService { public class WeiXinSubscribeService {
@Autowired @Autowired
private YxUserService userService; private YxUserService userService;
@Autowired @Autowired
private YxWechatTemplateService yxWechatTemplateService; private YxWechatTemplateService yxWechatTemplateService;
/** /**
* 充值成功通知 * 充值成功通知
* @param time 时间 * @param time 时间
* @param price 金额 * @param price 金额
* @param uid uid * @param uid uid
*/ */
public void rechargeSuccessNotice(String time,String price,Long uid){ public void rechargeSuccessNotice(String time,String price,Long uid){
String openid = this.getUserOpenid(uid); String openid = this.getUserOpenid(uid);
if(StrUtil.isBlank(openid)) { if(StrUtil.isBlank(openid)) {
return; return;
} }
Map<String,String> map = new HashMap<>(); Map<String,String> map = new HashMap<>();
map.put("first","您的账户金币发生变动,详情如下:"); map.put("first","您的账户金币发生变动,详情如下:");
map.put("keyword1","充值"); map.put("keyword1","充值");
map.put("keyword2",time); map.put("keyword2",time);
map.put("keyword3",price); map.put("keyword3",price);
map.put("remark", ShopConstants.YSHOP_WECHAT_PUSH_REMARK); map.put("remark", ShopConstants.YSHOP_WECHAT_PUSH_REMARK);
String tempId = this.getTempId(WechatTempateEnum.RECHARGE_SUCCESS.getValue()); String tempId = this.getTempId(WechatTempateEnum.RECHARGE_SUCCESS.getValue());
if(StrUtil.isNotBlank(tempId)) { if(StrUtil.isNotBlank(tempId)) {
this.sendSubscribeMsg( openid, tempId, "/user/account",map); this.sendSubscribeMsg( openid, tempId, "/user/account",map);
} }
} }
/** /**
* 支付成功通知 * 支付成功通知
* @param orderId 订单号 * @param orderId 订单号
* @param price 金额 * @param price 金额
* @param uid uid * @param uid uid
*/ */
public void paySuccessNotice(String orderId,String price,Long uid){ public void paySuccessNotice(String orderId,String price,Long uid){
String openid = this.getUserOpenid(uid); String openid = this.getUserOpenid(uid);
if(StrUtil.isBlank(openid)) { if(StrUtil.isBlank(openid)) {
return; return;
} }
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Map<String,String> map = new HashMap<>(); Map<String,String> map = new HashMap<>();
map.put("amount1",price); map.put("amount1",price);
map.put("date2", simpleDateFormat.format(new Date())); map.put("date2", simpleDateFormat.format(new Date()));
map.put("character_string3",orderId); map.put("character_string3",orderId);
map.put("time4",simpleDateFormat.format(new Date())); map.put("time4",simpleDateFormat.format(new Date()));
map.put("thing5","yshop购买商品"); map.put("thing5","yshop购买商品");
String tempId = this.getTempId(WechatTempateEnum.PAY_SUCCESS.getValue()); String tempId = this.getTempId(WechatTempateEnum.PAY_SUCCESS.getValue());
if(StrUtil.isNotBlank(tempId)) { if(StrUtil.isNotBlank(tempId)) {
this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map);
} }
} }
/** /**
* 退款成功通知 * 退款成功通知
* @param orderId 订单号 * @param orderId 订单号
* @param price 金额 * @param price 金额
* @param uid uid * @param uid uid
* @param time 时间 * @param time 时间
*/ */
public void refundSuccessNotice(String orderId,String price,Long uid,String time){ public void refundSuccessNotice(String orderId,String price,Long uid,String time){
String openid = this.getUserOpenid(uid); String openid = this.getUserOpenid(uid);
if(StrUtil.isBlank(openid)) { if(StrUtil.isBlank(openid)) {
return; return;
} }
Map<String,String> map = new HashMap<>(); Map<String,String> map = new HashMap<>();
map.put("first","您的订单退款申请被通过,钱款将很快还至您的支付账户。"); map.put("first","您的订单退款申请被通过,钱款将很快还至您的支付账户。");
//订单号 //订单号
map.put("keyword1",orderId); map.put("keyword1",orderId);
map.put("keyword2",price); map.put("keyword2",price);
map.put("keyword3", time); map.put("keyword3", time);
map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK); map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK);
String tempId = this.getTempId(WechatTempateEnum.REFUND_SUCCESS.getValue()); String tempId = this.getTempId(WechatTempateEnum.REFUND_SUCCESS.getValue());
if(StrUtil.isNotBlank(tempId)) { if(StrUtil.isNotBlank(tempId)) {
this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map);
} }
} }
/** /**
* 发货成功通知 * 发货成功通知
* @param orderId 单号 * @param orderId 单号
* @param deliveryName 快递公司 * @param deliveryName 快递公司
* @param deliveryId 快递单号 * @param deliveryId 快递单号
* @param uid uid * @param uid uid
*/ */
public void deliverySuccessNotice(String orderId,String deliveryName, public void deliverySuccessNotice(String orderId,String deliveryName,
String deliveryId,Long uid){ String deliveryId,Long uid){
String openid = this.getUserOpenid(uid); String openid = this.getUserOpenid(uid);
if(StrUtil.isEmpty(openid)) { if(StrUtil.isEmpty(openid)) {
return; return;
} }
Map<String,String> map = new HashMap<>(); Map<String,String> map = new HashMap<>();
map.put("first","亲,宝贝已经启程了,好想快点来到你身边。"); map.put("first","亲,宝贝已经启程了,好想快点来到你身边。");
map.put("keyword2",deliveryName); map.put("keyword2",deliveryName);
map.put("keyword1",orderId); map.put("keyword1",orderId);
map.put("keyword3",deliveryId); map.put("keyword3",deliveryId);
map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK); map.put("remark",ShopConstants.YSHOP_WECHAT_PUSH_REMARK);
String tempId = this.getTempId(WechatTempateEnum.DELIVERY_SUCCESS.getValue()); String tempId = this.getTempId(WechatTempateEnum.DELIVERY_SUCCESS.getValue());
if(StrUtil.isNotBlank(tempId)) { if(StrUtil.isNotBlank(tempId)) {
this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map); this.sendSubscribeMsg( openid,tempId, "/order/detail/"+orderId,map);
} }
} }
/** /**
* 构建小程序一次性订阅消息 * 构建小程序一次性订阅消息
* @param openId 单号 * @param openId 单号
* @param templateId 模板id * @param templateId 模板id
* @param page 跳转页面 * @param page 跳转页面
* @param map map内容 * @param map map内容
* @return String * @return String
*/ */
private void sendSubscribeMsg(String openId, String templateId, String page, Map<String,String> map){ private void sendSubscribeMsg(String openId, String templateId, String page, Map<String,String> map){
WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder() WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
.toUser(openId) .toUser(openId)
.templateId(templateId) .templateId(templateId)
.page(page) .page(page)
.build(); .build();
map.forEach( (k,v)-> { wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.Data(k, v));} ); map.forEach( (k,v)-> { wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.MsgData(k, v));} );
WxMaService wxMaService = WxMaConfiguration.getWxMaService(); WxMaService wxMaService = WxMaConfiguration.getWxMaService();
try { try {
wxMaService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage); wxMaService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage);
} catch (WxErrorException e) { } catch (WxErrorException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* 获取模板消息id * 获取模板消息id
* @param key 模板key * @param key 模板key
* @return string * @return string
*/ */
private String getTempId(String key){ private String getTempId(String key){
YxWechatTemplate yxWechatTemplate = yxWechatTemplateService.lambdaQuery() YxWechatTemplate yxWechatTemplate = yxWechatTemplateService.lambdaQuery()
.eq(YxWechatTemplate::getType,"subscribe") .eq(YxWechatTemplate::getType,"subscribe")
.eq(YxWechatTemplate::getTempkey,key) .eq(YxWechatTemplate::getTempkey,key)
.one(); .one();
if (yxWechatTemplate == null) { if (yxWechatTemplate == null) {
throw new YshopException("请后台配置key:" + key + "订阅消息id"); throw new YshopException("请后台配置key:" + key + "订阅消息id");
} }
if(ShopCommonEnum.IS_STATUS_0.getValue().equals(yxWechatTemplate.getStatus())){ if(ShopCommonEnum.IS_STATUS_0.getValue().equals(yxWechatTemplate.getStatus())){
return ""; return "";
} }
return yxWechatTemplate.getTempid(); return yxWechatTemplate.getTempid();
} }
/** /**
* 获取openid * 获取openid
* @param uid uid * @param uid uid
* @return String * @return String
*/ */
private String getUserOpenid(Long uid){ private String getUserOpenid(Long uid){
YxUser yxUser = userService.getById(uid); YxUser yxUser = userService.getById(uid);
if(yxUser == null) { if(yxUser == null) {
return ""; return "";
} }
WechatUserDto wechatUserDto = yxUser.getWxProfile(); WechatUserDto wechatUserDto = yxUser.getWxProfile();
if(wechatUserDto == null) { if(wechatUserDto == null) {
return ""; return "";
} }
if(StrUtil.isBlank(wechatUserDto.getRoutineOpenid())) { if(StrUtil.isBlank(wechatUserDto.getRoutineOpenid())) {
return ""; return "";
} }
return wechatUserDto.getRoutineOpenid(); return wechatUserDto.getRoutineOpenid();
} }
} }

View File

@ -58,7 +58,8 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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 * @author hupeng