zoujiajian 3 лет назад
Родитель
Сommit
d41150b167

+ 5 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/WxApp.java

@@ -24,6 +24,11 @@ public class WxApp extends BaseEntity {
      */
     private String appId;
 
+    /**
+     * 公众号的原始id
+     */
+    private String gzhOriginalId;
+
     /**
      * appSecret
      */

+ 1 - 0
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -594,6 +594,7 @@ public class RedisService {
         LOTTERY_KEY("lottery_key:%s", "抽奖key", 60 * 60 * 24l),
         TASK_TIME_LIMIT_KEY("task_time_limit:%s", "一次性任务key", 30),
         WX_GZH_QRCODE_LOGIN("wx_gzh_qrcode_login:%s", "微信公众号扫码登录key", 60),
+        WX_GZH_QRCODE_SHARED_LOGIN("wx_gzh_qrcode_shared_login:%s", "微信公众号扫码分享邀请key", 60),
         ;
 
         private String name;

+ 7 - 0
netflix-service/src/main/java/com/cyksj/service/distribute/DistributeService.java

@@ -12,4 +12,11 @@ public interface DistributeService {
 	void insertDistribute(UserDistributeReq userDistributeReq);
 
 	void editDistribute(UserDistributeReq userDistributeReq);
+
+	/**
+	 * 保存分销邀请记录
+	 * sharedId: 邀请人
+	 * userId: 被邀请人
+	 */
+	void saveDistributionInvitation(Long sharedId, Long userId);
 }

+ 30 - 3
netflix-service/src/main/java/com/cyksj/service/distribute/impl/DistributeServiceImpl.java

@@ -1,16 +1,20 @@
 package com.cyksj.service.distribute.impl;
 
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.cyksj.common.constant.TaskTypeEnum;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.mapper.GoodsDonMapper;
 import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
+import com.cyksj.mapper.manage.distribute.UserDistributeSharedMapper;
 import com.cyksj.mapper.manage.distribute.UserPlatDistributeRateMapper;
-import com.cyksj.model.entity.GoodsDon;
-import com.cyksj.model.entity.UserDistribute;
-import com.cyksj.model.entity.UserPlatDistributeRate;
+import com.cyksj.mapper.market.task.TaskTypeMapper;
+import com.cyksj.model.entity.*;
 import com.cyksj.model.request.UserDistributeReq;
 import com.cyksj.service.distribute.DistributeService;
+import com.cyksj.service.market.task.TaskService;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -22,6 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
  */
 @Service
 @RequiredArgsConstructor
+@Slf4j
 public class DistributeServiceImpl implements DistributeService {
 	private final UserDistributeMapper userDistributeMapper;
 
@@ -29,6 +34,12 @@ public class DistributeServiceImpl implements DistributeService {
 
 	private final GoodsDonMapper goodsDonMapper;
 
+	private final TaskService taskService;
+
+	private final TaskTypeMapper taskTypeMapper;
+
+	private final UserDistributeSharedMapper userDistributeSharedMapper;
+
 	@Override
 	@Transactional(rollbackFor = Throwable.class)
 	public void insertDistribute(UserDistributeReq userDistributeReq) {
@@ -75,4 +86,20 @@ public class DistributeServiceImpl implements DistributeService {
 			userPlatDistributeRateMapper.updateById(rate);
 		}
 	}
+
+	@Override
+	public void saveDistributionInvitation(Long sharedId, Long userId) {
+		UserDistributeShared userDistributeShared = new UserDistributeShared();
+		userDistributeShared.setSharedId(sharedId);
+		//存在分销
+		userDistributeShared.setUserId(userId);
+		try {
+			userDistributeSharedMapper.insert(userDistributeShared);
+			TaskType taskType = taskTypeMapper.selectOne(Wrappers.lambdaQuery(TaskType.class)
+					.eq(TaskType::getName, TaskTypeEnum.share.getDesc()).last("limit 1"));
+			taskService.completeTask(taskType, sharedId);
+		} catch (DuplicateKeyException e) {
+			log.info("分销被邀请人重复插入");
+		}
+	}
 }

+ 26 - 10
netflix-service/src/main/java/com/cyksj/service/gzh/factory/EventMsgService.java

@@ -11,9 +11,11 @@ import com.cyksj.model.entity.User;
 import com.cyksj.model.entity.UserGzhRelation;
 import com.cyksj.model.entity.WxApp;
 import com.cyksj.redis.RedisService;
+import com.cyksj.service.distribute.DistributeService;
 import com.cyksj.service.wechat.WeChatService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 
 /*
@@ -36,6 +38,8 @@ public class EventMsgService implements GzhMsgService {
 
 	private final RedisService redisService;
 
+	private final DistributeService distributeService;
+
 	@Override
 	public String handleNotify(MsgEvent msgEvent) {
 		try {
@@ -58,7 +62,7 @@ public class EventMsgService implements GzhMsgService {
 			}
 			return "";
 		} catch (Exception e) {
-			log.error("接收微信公众:{},报错:{]", msgEvent.getToUserName(), e == null ? "" : e.getMessage());
+			log.error("接收微信公众:{},报错:{}", msgEvent.getToUserName(), e == null ? "" : e.getMessage());
 			return "";
 		}
 	}
@@ -89,6 +93,12 @@ public class EventMsgService implements GzhMsgService {
 			user.setUnionid(info.getUnionid());
 			userMapper.insert(user);
 
+			Object sharedIdObj = redisService.get(RedisService.key.WX_GZH_QRCODE_SHARED_LOGIN.getNameFormat(sceneKey));
+			if (sharedIdObj != null) {
+				Long sharedId = Long.parseLong(sharedIdObj.toString());
+				distributeService.saveDistributionInvitation(sharedId, user.getId());
+			}
+
 			saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid());
 		} else {
 			//之前关注过
@@ -114,15 +124,21 @@ public class EventMsgService implements GzhMsgService {
 		userGzhRelation.setUserId(userId);
 
 		WxApp wxApp = wxAppMapper.selectOne(Wrappers.lambdaQuery(WxApp.class)
-				.eq(WxApp::getAppId, msgEvent.getToUserName()).last("limit 1"));
-		userGzhRelation.setWxAppId(wxApp.getId());
-		userGzhRelation.setIsSubscribe(true);
-		userGzhRelation.setEvent(msgEvent.getEvent());
-		userGzhRelation.setOpenId(openId);
-		userGzhRelation.setUnionid(unionid);
-		if (StrUtil.isNotEmpty(msgEvent.getEventKey())) {
-			userGzhRelation.setIsQrScene(true);
+				.eq(WxApp::getGzhOriginalId, msgEvent.getToUserName()).last("limit 1"));
+		if (wxApp != null) {
+			userGzhRelation.setWxAppId(wxApp.getId());
+			userGzhRelation.setIsSubscribe(true);
+			userGzhRelation.setEvent(msgEvent.getEvent());
+			userGzhRelation.setOpenId(openId);
+			userGzhRelation.setUnionid(unionid);
+			if (StrUtil.isNotEmpty(msgEvent.getEventKey())) {
+				userGzhRelation.setIsQrScene(true);
+			}
+			try {
+				userGzhRelationMapper.insert(userGzhRelation);
+			} catch (DuplicateKeyException e) {
+
+			}
 		}
-		userGzhRelationMapper.insert(userGzhRelation);
 	}
 }

+ 2 - 2
netflix-service/src/main/java/com/cyksj/service/template/impl/TemplateCommonServiceImpl.java

@@ -60,11 +60,11 @@ public class TemplateCommonServiceImpl implements TemplateCommonService {
 	public void sendWxGzhQrCodeSuccessMsg(User user) throws Exception {
 		if (!EnvCommonService.active.equals(envCommonService.getEnv())) {
 			TemplateEnum templateEnum = TemplateEnum.WX_GZH_QR_CODE_LOGIN;
+			String url = String.format("%sauth/weChat?url=%s", envCommonService.getHost(), envCommonService.getDomain() + "yinhe/web/");
 			WxMpTemplateMessage templateMessage = new WxMpTemplateMessage()
 					.setToUser(user.getOpenId())
 					.setTemplateId(templateEnum.getTemplateId())
-					//TODO
-					.setUrl(" ");
+					.setUrl(url);
 			WxMpTemplateMessage.TemplateData data = templateMessage.getData();
 			data.setFirst(new WxMpTemplateData("您已登录成功"));
 			data.setKeyword1(new WxMpTemplateData("银河录像局"));

+ 11 - 33
netflix-service/src/main/java/com/cyksj/service/user/impl/UserServiceImpl.java

@@ -5,23 +5,20 @@ import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.cyksj.common.constant.TaskTypeEnum;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.config.WeChatConfig;
 import com.cyksj.mapper.UserMapper;
 import com.cyksj.mapper.UserTransferLogMapper;
 import com.cyksj.mapper.WxSyncLogMapper;
-import com.cyksj.mapper.manage.distribute.UserDistributeSharedMapper;
-import com.cyksj.mapper.market.task.TaskTypeMapper;
 import com.cyksj.model.dto.GzhOAuth2UserInfo;
 import com.cyksj.model.dto.UserWhoami;
-import com.cyksj.model.entity.*;
-import com.cyksj.service.market.task.TaskService;
+import com.cyksj.model.entity.User;
+import com.cyksj.model.entity.UserTransferLog;
+import com.cyksj.model.entity.WxSyncLog;
+import com.cyksj.service.distribute.DistributeService;
 import com.cyksj.service.user.UserService;
-import com.cyksj.service.wechat.WeChatService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 
@@ -35,22 +32,14 @@ import java.util.Optional;
 @Service
 @RequiredArgsConstructor
 public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
-
-
-    private final WeChatService weChatService;
-
-    private final UserDistributeSharedMapper userDistributeSharedMapper;
-
-    private final TaskService taskService;
-
-    private final TaskTypeMapper taskTypeMapper;
-
     private final WeChatConfig weChatConfig;
 
     private final UserTransferLogMapper userTransferLogMapper;
 
     private final WxSyncLogMapper wxSyncLogMapper;
 
+    private final DistributeService distributeService;
+
     @Override
     public User saveAuth(GzhOAuth2UserInfo info, Long sharedId) {
         final String unionid = info.getUnionid();
@@ -104,25 +93,14 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
         }
         user.setUnionid(unionid);
         user.setOpenId(info.getOpenid());
-        UserDistributeShared userDistributeShared = null;
+
+        Boolean isNewUser = false;
         if (user.getId() == null) {
-            User shared = this.getById(sharedId);
-            if (shared != null) {
-                userDistributeShared = new UserDistributeShared();
-                userDistributeShared.setSharedId(sharedId);
-            }
+            isNewUser = true;
         }
         this.saveOrUpdate(user);
-        if (userDistributeShared != null) {
-            userDistributeShared.setUserId(user.getId());
-            try {
-                userDistributeSharedMapper.insert(userDistributeShared);
-                TaskType taskType = taskTypeMapper.selectOne(Wrappers.lambdaQuery(TaskType.class)
-                        .eq(TaskType::getName, TaskTypeEnum.share.getDesc()).last("limit 1"));
-                taskService.completeTask(taskType, sharedId);
-            } catch (DuplicateKeyException e) {
-                log.info("分销被邀请人重复插入");
-            }
+        if (sharedId != null && isNewUser) {
+            distributeService.saveDistributionInvitation(sharedId, user.getId());
         }
         return user;
     }

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

@@ -77,5 +77,5 @@ public interface WeChatService {
      * 获取公众号带参数的二维码
      * @return
      */
-    WxGzhQrCodeTicketRep getWxGzhQrCode() throws Exception;
+    WxGzhQrCodeTicketRep getWxGzhQrCode(Long sharedId) throws Exception;
 }

+ 6 - 2
netflix-service/src/main/java/com/cyksj/service/wechat/impl/WeChatServiceImpl.java

@@ -479,12 +479,12 @@ public class WeChatServiceImpl implements WeChatService {
     }
 
     @Override
-    public WxGzhQrCodeTicketRep getWxGzhQrCode() throws Exception {
+    public WxGzhQrCodeTicketRep getWxGzhQrCode(Long sharedId) throws Exception {
         String gzhQrCodeUrl = String.format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", this.getAccessToken());
         //scene_str:场景值ID(字符串形式的ID),字符串类型,长度限制为1到64
         String scene_str = RandomUtil.randomString(16);
         DateTime dateTime = DateUtil.offsetSecond(DateTime.now(), 60);
-        String body = String.format("{\"expire_seconds\": %s, \"action_name\": \"%s\", \"action_info\": {\"scene\": {\"scene_str\": %s}}}",
+        String body = String.format("{\"expire_seconds\": %s, \"action_name\": \"%s\", \"action_info\": {\"scene\": {\"scene_str\": \"%s\"}}}",
                 60,
                 "QR_STR_SCENE",
                 scene_str);
@@ -497,6 +497,10 @@ public class WeChatServiceImpl implements WeChatService {
             throw BusinessRuntimeException.getInstance("获取微信登录二维码失败: " + response.body());
         }
 
+        if (sharedId != null) {
+            redisService.set(RedisService.key.WX_GZH_QRCODE_SHARED_LOGIN.getNameFormat(scene_str), sharedId, RedisService.key.WX_GZH_QRCODE_SHARED_LOGIN.getTimeout());
+        }
+
         JSONObject re = Jsons.parseObject(response.body(), JSONObject.class);
         String ticket = re.getStr("ticket");
         WxGzhQrCodeTicketRep wxGzhQrCodeTicketRep = new WxGzhQrCodeTicketRep();

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

@@ -10,7 +10,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.EnvCommonService;
 import com.cyksj.common.annotation.NoSubmit;
-import com.cyksj.common.constant.TaskTypeEnum;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.common.snowflake.Sequence;
 import com.cyksj.common.util.Codec;
@@ -22,24 +21,18 @@ import com.cyksj.config.wxlogin.WxOpenPlatYHLXLoginConfig;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayResponse;
 import com.cyksj.mapper.PhoneCodeMapper;
-import com.cyksj.mapper.WxAppMapper;
-import com.cyksj.mapper.manage.distribute.UserDistributeSharedMapper;
-import com.cyksj.mapper.market.task.TaskTypeMapper;
 import com.cyksj.model.dto.AuthRedirect;
 import com.cyksj.model.dto.AuthToken;
 import com.cyksj.model.dto.GzhOAuth2UserInfo;
 import com.cyksj.model.dto.JsSdkTicket;
 import com.cyksj.model.entity.PhoneCode;
-import com.cyksj.model.entity.TaskType;
 import com.cyksj.model.entity.User;
-import com.cyksj.model.entity.UserDistributeShared;
 import com.cyksj.model.request.LoginPhoneReq;
 import com.cyksj.model.response.LoginPhoneRep;
 import com.cyksj.model.response.WxGzhQrCodeTicketRep;
 import com.cyksj.redis.RedisService;
 import com.cyksj.service.authorization.AuthorizationService;
-import com.cyksj.service.market.task.TaskService;
-import com.cyksj.service.order.OrderDonService;
+import com.cyksj.service.distribute.DistributeService;
 import com.cyksj.service.template.TemplateCommonService;
 import com.cyksj.service.user.UserService;
 import com.cyksj.service.wechat.WeChatService;
@@ -48,7 +41,6 @@ import com.cyksj.web.util.StpUserUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.dao.DuplicateKeyException;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
@@ -81,8 +73,6 @@ public class AuthorizationController {
 
     private final WeChatService weChatService;
 
-    private final WxAppMapper wxAppMapper;
-
     private final WeChatConfig weChatConfig;
 
     private final RedisService redisService;
@@ -91,20 +81,14 @@ public class AuthorizationController {
 
     private final EnvCommonService envCommonService;
 
-    private final OrderDonService orderDonService;
-
     private final PhoneCodeMapper phoneCodeMapper;
 
-    private final UserDistributeSharedMapper userDistributeSharedMapper;
-
-    private final TaskService taskService;
-
-    private final TaskTypeMapper taskTypeMapper;
-
     private final MailService mailService;
 
     private final TemplateCommonService templateCommonService;
 
+    private final DistributeService distributeService;
+
     @GetMapping(value = "/weChat")
     public void base(String url,Long sharedId, String authType, HttpServletResponse response) throws Exception {
 
@@ -262,9 +246,9 @@ public class AuthorizationController {
      * 网页扫描微信公众号二维码 临时凭证ticket  关注后登录
      */
     @GetMapping("/wx/gzh/login/qrCode")
-    public Result<WxGzhQrCodeTicketRep> getWxGzhQrCode() throws Exception {
+    public Result<WxGzhQrCodeTicketRep> getWxGzhQrCode(Long sharedId) throws Exception {
         //获取gzh带参数的二维码
-        WxGzhQrCodeTicketRep result = weChatService.getWxGzhQrCode();
+        WxGzhQrCodeTicketRep result = weChatService.getWxGzhQrCode(sharedId);
         return GatewayResponse.SUCCESS.newBuilder().toResult(result);
     }
 
@@ -274,10 +258,12 @@ public class AuthorizationController {
     @GetMapping("/wx/gzh/sceneStr/checkQrCode")
     public Result<String> checkQrCodeByGzhSceneStr(String sceneStr) throws Exception {
         String key = RedisService.key.WX_GZH_QRCODE_LOGIN.getNameFormat(sceneStr);
-        Long userId = (Long) redisService.get(key);
-        if (userId == null) {
+        Object value = redisService.get(key);
+        if (value == null) {
             return GatewayResponse.SUCCESS.newBuilder().setMsg("用户未扫码").toResult();
         }
+        Long userId = Long.parseLong(value.toString());
+
         redisService.del(key);
         User user = userService.getById(userId);
         StpUserUtil.login(userId);
@@ -390,7 +376,6 @@ public class AuthorizationController {
         }
         //保存用户信息
         User user = userService.getOne(wrapper);
-        UserDistributeShared userDistributeShared = null;
         Long sharedId = loginPhoneReq.getSharedId();
         if (user == null) {
             user = new User();
@@ -412,25 +397,12 @@ public class AuthorizationController {
             userService.save(user);
             //新用户分销
             if (sharedId != null) {
-                userDistributeShared = new UserDistributeShared();
-                userDistributeShared.setSharedId(loginPhoneReq.getSharedId());
+                distributeService.saveDistributionInvitation(sharedId, user.getId());
             }
         }
         //写回登陆信息
         StpUserUtil.login(user.getId());
         LoginPhoneRep loginPhoneRep = new LoginPhoneRep(StpUserUtil.getTokenValue(), user.getId(), user.getNickname(), user.getHeadimgurl());
-        //存在分销
-        if (userDistributeShared != null) {
-            userDistributeShared.setUserId(user.getId());
-            try {
-                userDistributeSharedMapper.insert(userDistributeShared);
-                TaskType taskType = taskTypeMapper.selectOne(Wrappers.lambdaQuery(TaskType.class)
-                        .eq(TaskType::getName, TaskTypeEnum.share.getDesc()).last("limit 1"));
-                taskService.completeTask(taskType, sharedId);
-            } catch (DuplicateKeyException e) {
-                log.info("分销被邀请人重复插入");
-            }
-        }
         return GatewayResponse.SUCCESS.newBuilder().toResult(loginPhoneRep);
     }
 }