Selaa lähdekoodia

fix 多公众号appid授权

zoujiajian 3 vuotta sitten
vanhempi
sitoutus
da165666b5

+ 4 - 0
netflix-dao/src/main/java/com/cyksj/mapper/gzh/UserGzhRelationMapper.java

@@ -2,6 +2,8 @@ package com.cyksj.mapper.gzh;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.cyksj.model.entity.UserGzhRelation;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 
 /*
  *项目名: netflix
@@ -10,4 +12,6 @@ import com.cyksj.model.entity.UserGzhRelation;
  *创建时间:2023/1/16 10:58
  */
 public interface UserGzhRelationMapper extends BaseMapper<UserGzhRelation> {
+	@Select("select wa.app_id from user_gzh_relation ur left join wx_app wa on wa.id = ur.wx_app_id where ur.user_id = #{userId} and ur.open_id = #{openId} order by id limit 1")
+	String selectWxAppIdByGzhRelation(@Param("userId") Long userId, @Param("openId") String openId);
 }

+ 5 - 0
netflix-dao/src/main/java/com/cyksj/model/dto/AuthRedirect.java

@@ -38,4 +38,9 @@ public class AuthRedirect {
      */
     private Long pid;
 
+    /**
+     * 微信公众号wx_app id
+     */
+    private Integer type;
+
 }

+ 1 - 1
netflix-service/src/main/java/com/cyksj/service/authorization/AuthorizationService.java

@@ -16,7 +16,7 @@ public interface AuthorizationService {
      * @param authType
      * @return
      */
-    String createUrl(String state, String authType);
+    String createUrl(String state, String authType, Integer type);
 
     JsSdkTicket getJsTicket(String appId, String url) throws Exception;
 

+ 3 - 3
netflix-service/src/main/java/com/cyksj/service/authorization/impl/AuthorizationServiceImpl.java

@@ -27,9 +27,9 @@ public class AuthorizationServiceImpl implements AuthorizationService {
 
 
     @Override
-    public String createUrl(String state, String authType) {
-        return weChatService.createUrl(state,weChatConfig.getDomain() + WeChatAuthRedirectUrl.get(authType).getUrl()
-                ,weChatConfig.getAppid(),WeChatAuthRedirectUrl.get(authType).getScope());
+    public String createUrl(String state, String authType, Integer type) {
+        return weChatService.createUrl(state, weChatConfig.getDomain() + WeChatAuthRedirectUrl.get(authType).getUrl()
+                , weChatService.getWxAppId(type), WeChatAuthRedirectUrl.get(authType).getScope());
     }
 
     @Override

+ 3 - 3
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderDonServiceImpl.java

@@ -402,7 +402,7 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		orderDon.setShopId(shopId);
 		this.updateById(orderDon);
 		H5JsPayParams params = weChatService.getJsPayParams(
-				weChatConfig.getAppid(),
+				weChatService.getWxAppIdByGzhRelation(orderDon.getUserId(), orderDon.getOpenId()),
 				shopId,
 				orderDon.getOpenId(),
 				orderDon.getMoney().intValue(),
@@ -441,7 +441,7 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 		this.updateById(orderDon);
 
 		H5JsPayParams params = weChatService.getJsPayParams(
-				weChatConfig.getAppid(),
+				weChatService.getWxAppIdByGzhRelation(orderDon.getUserId(),orderDon.getOpenId()),
 				shopId,
 				orderDon.getOpenId(),
 				orderDon.getMoney().intValue(),
@@ -933,7 +933,7 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 					.setAlgorithm(Codec.DoDigest.Algorithm.MD5)
 					.setStringData(time + String.valueOf(orderDon.getId()))
 					.toHexString();
-			weChatService.refundWxOrder(weChatConfig.getAppid(),
+			weChatService.refundWxOrder(weChatService.getWxAppIdByGzhRelation(orderDon.getUserId(), orderDon.getOpenId()),
 					Optional.ofNullable(orderDon.getShopId()).orElse(ShopConfig.WX_DEFAULT_APP_ID),
 					orderDon.getTransactionId(),
 					outRefundNo,

+ 2 - 2
netflix-service/src/main/java/com/cyksj/service/register/impl/RegisterOrderDonServiceImpl.java

@@ -262,7 +262,7 @@ public class RegisterOrderDonServiceImpl extends ServiceImpl<RegisterOrderDonMap
                 .setData(Jsons.toJson(attach)).encodeAsText();
 
         H5JsPayParams params = weChatService.getJsPayParams(
-                weChatConfig.getAppid(),
+                weChatService.getWxAppIdByGzhRelation(orderDon.getUserId(), orderDon.getOpenId()),
                 shopId,
                 orderDon.getOpenId(),
                 orderDon.getMoney().intValue(),
@@ -356,7 +356,7 @@ public class RegisterOrderDonServiceImpl extends ServiceImpl<RegisterOrderDonMap
                 .setStringData(time + String.valueOf(registerOrderDon.getId()))
                 .toHexString();
         wxOrderRefundReq.setRefundMoney(registerOrderDon.getMoney());
-        weChatService.refundWxOrder(weChatConfig.getAppid(),
+        weChatService.refundWxOrder(weChatService.getWxAppIdByGzhRelation(registerOrderDon.getUserId(), registerOrderDon.getOpenId()),
                 Optional.ofNullable(registerOrderDon.getShopId()).orElse(ShopConfig.WX_DEFAULT_APP_ID),
                 registerOrderDon.getTransactionId(),
                 outRefundNo,

+ 10 - 0
netflix-service/src/main/java/com/cyksj/service/wechat/WeChatService.java

@@ -88,4 +88,14 @@ public interface WeChatService {
      * 获取微信订单信息 校验订单是否已支付
      */
     Map<String, String> getWxOrderDetail(String shopId, String orderNo) throws Exception;
+
+    /**
+     * 获取微信公众号appid
+     */
+    String getWxAppId(Integer type);
+
+    /**
+     * 根据用户关注公众号关系,获取公众号appid
+     */
+    String getWxAppIdByGzhRelation(Long userId, String openId);
 }

+ 24 - 0
netflix-service/src/main/java/com/cyksj/service/wechat/impl/WeChatServiceImpl.java

@@ -17,6 +17,7 @@ import com.cyksj.config.WeChatFactory;
 import com.cyksj.enums.WeChatAuthRedirectUrl;
 import com.cyksj.mapper.WxAppMapper;
 import com.cyksj.mapper.channel.ShopConfigMapper;
+import com.cyksj.mapper.gzh.UserGzhRelationMapper;
 import com.cyksj.model.dto.*;
 import com.cyksj.model.entity.ShopConfig;
 import com.cyksj.model.entity.WxApp;
@@ -65,6 +66,8 @@ public class WeChatServiceImpl implements WeChatService {
 
     private final RedisService redisService;
 
+    private final UserGzhRelationMapper getUserGzhRelationMapper;
+
     @Override
     public String createUrl(String state, String redirectUrl) {
         return createUrl(state,redirectUrl,weChatConfig.getAppid(), WeChatAuthRedirectUrl.BASE.getScope());
@@ -564,6 +567,27 @@ public class WeChatServiceImpl implements WeChatService {
         return orderSearch;
     }
 
+    @Override
+    public String getWxAppId(Integer type) {
+        String appId = weChatConfig.getAppid();
+        if (type != null) {
+            WxApp wxApp = wxAppMapper.selectById(type);
+            if (wxApp != null) {
+                appId = wxApp.getAppId();
+            }
+        }
+        return appId;
+    }
+
+    @Override
+    public String getWxAppIdByGzhRelation(Long userId, String openId) {
+        if (userId == 0 || StrUtil.isEmpty(openId)) {
+            return weChatConfig.getAppid();
+        }
+        String wxAppId = Optional.ofNullable(getUserGzhRelationMapper.selectWxAppIdByGzhRelation(userId, openId)).orElse(weChatConfig.getAppid());
+        return wxAppId;
+    }
+
     /**
      * sha256_HMAC加密
      * @param message 消息

+ 10 - 13
netflix-web/src/main/java/com/cyksj/web/controller/user/AuthorizationController.java

@@ -34,11 +34,10 @@ import com.cyksj.model.response.WxGzhQrCodeTicketRep;
 import com.cyksj.redis.RedisService;
 import com.cyksj.service.authorization.AuthorizationService;
 import com.cyksj.service.distribute.DistributeService;
-import com.cyksj.service.distribute.equipment.EquipmentDistributeService;
+import com.cyksj.service.mail.MailService;
 import com.cyksj.service.template.TemplateCommonService;
 import com.cyksj.service.user.UserService;
 import com.cyksj.service.wechat.WeChatService;
-import com.cyksj.service.mail.MailService;
 import com.cyksj.web.util.StpUserUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -91,23 +90,21 @@ public class AuthorizationController {
 
     private final DistributeService distributeService;
 
-    private final EquipmentDistributeService equipDistributeService;
-
     @GetMapping(value = "/weChat")
-    public void base(String url, Long sharedId, Integer dsType, Long popularizeId, String authType, HttpServletResponse response) throws Exception {
+    public void base(String url, Long sharedId, Integer dsType, Long popularizeId, String authType, Integer type, HttpServletResponse response) throws Exception {
 
-        log.info("微信授权转跳. url: {},authType:{}",url,authType);
+        log.info("微信授权转跳. url: {},authType:{}", url, authType);
 
         // 无论是否为老用户 均转跳到授权页面
         // 若授权过 则无需再次授权 直接回调 /user/wechat/redirect
         // 新用户同意授权后 也会回调 /user/wechat/redirect
 
         Long mappingId = SEQUENCE.nextId();
-        redisService.set(mappingId.toString(),url,60*60*24L);
-        AuthRedirect redirect = new AuthRedirect().setMappingId(mappingId).setSharedId(sharedId).setDsType(dsType).setPid(popularizeId);
+        redisService.set(mappingId.toString(), url, 60 * 60 * 24L);
+        AuthRedirect redirect = new AuthRedirect().setMappingId(mappingId).setSharedId(sharedId).setDsType(dsType).setPid(popularizeId).setType(type);
         String state = Codec.DoBase64.custom().setData(Jsons.toJson(redirect)).encodeAsText();
 
-        response.sendRedirect(authorizationService.createUrl(state,authType));
+        response.sendRedirect(authorizationService.createUrl(state, authType, type));
 
     }
 
@@ -124,7 +121,7 @@ public class AuthorizationController {
         String json = Codec.DoBase64.custom().setData(state).decodeAsText();
         AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
 
-        AuthToken token = weChatService.getAuthToken(code,weChatConfig.getAppid());
+        AuthToken token = weChatService.getAuthToken(code, weChatService.getWxAppId(re.getType()));
 
         // 根据openid 与 access_token 获取用户信息
         GzhOAuth2UserInfo userinfo = weChatService.getUserInfo(token.getAccessToken(), token.getOpenid());
@@ -170,7 +167,7 @@ public class AuthorizationController {
         String json = Codec.DoBase64.custom().setData(state).decodeAsText();
         AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
 
-        AuthToken token = weChatService.getAuthToken(code,weChatConfig.getAppid());
+        AuthToken token = weChatService.getAuthToken(code, weChatService.getWxAppId(re.getType()));
 
         //  查询用户期望转跳的url
         String url = redisService.getStr(re.getMappingId().toString());
@@ -190,11 +187,11 @@ public class AuthorizationController {
      * 微信js-sdk 权限验证
      */
     @RequestMapping(value = "/jsTicket", method = RequestMethod.GET)
-    public Result<JsSdkTicket> ticket(String url) throws Exception{
+    public Result<JsSdkTicket> ticket(String url, Integer type) throws Exception {
         if (StringUtils.isBlank(url)) {
             throw BusinessRuntimeException.getInstance("请输入授权网址.");
         }
-        return GatewayResponse.SUCCESS.newBuilder().toResult(authorizationService.getJsTicket(weChatConfig.getAppid(),url));
+        return GatewayResponse.SUCCESS.newBuilder().toResult(authorizationService.getJsTicket(weChatService.getWxAppId(type), url));
 
     }