Bladeren bron

注册队列

xiongzhu 3 jaren geleden
bovenliggende
commit
e82aeada73

+ 2 - 0
src/main/java/com/izouma/nineth/config/GeneralProperties.java

@@ -30,4 +30,6 @@ public class GeneralProperties {
     private String  updateQuotaTopic;
     private String  broadcastEventGroup;
     private String  broadcastEventTopic;
+    private String  registerGroup;
+    private String  registerTopic;
 }

+ 7 - 7
src/main/java/com/izouma/nineth/domain/User.java

@@ -28,18 +28,18 @@ import java.util.Set;
 @Entity
 @Table(indexes = {
         @Index(columnList = "phone"),
+        @Index(columnList = "collectionId"),
+        @Index(columnList = "collectionInvitor"),
+        @Index(columnList = "admin"),
+        @Index(columnList = "minter"),
+        @Index(columnList = "createdAt"),
+        @Index(columnList = "settleAccountId")
 })
 @AllArgsConstructor
 @NoArgsConstructor
 @Builder
 @ApiModel(value = "用户", description = "用户")
-public class User extends BaseEntityNoID implements Serializable {
-
-    @Id
-    @GeneratedValue(strategy = GenerationType.IDENTITY)
-//    @GeneratedValue(generator = "snowFlakeId")
-//    @GenericGenerator(name = "snowFlakeId", strategy = "com.izouma.nineth.utils.SnowflakeIdGenerator")
-    private Long id;
+public class User extends BaseEntity implements Serializable {
 
     //    @Pattern(regexp = Constants.Regex.USERNAME)
     @Size(min = 1, max = 50)

+ 17 - 0
src/main/java/com/izouma/nineth/event/RegisterEvent.java

@@ -0,0 +1,17 @@
+package com.izouma.nineth.event;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class RegisterEvent {
+    private String phone;
+    private String code;
+    private String password;
+    private String inviteCode;
+    private Long   invitor;
+    private Long   collectionId;
+}

+ 42 - 0
src/main/java/com/izouma/nineth/listener/RegisterListener.java

@@ -0,0 +1,42 @@
+package com.izouma.nineth.listener;
+
+import com.izouma.nineth.domain.User;
+import com.izouma.nineth.event.RegisterEvent;
+import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.service.UserService;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.spring.annotation.ConsumeMode;
+import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
+import org.apache.rocketmq.spring.core.RocketMQListener;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+import java.time.Duration;
+
+@Service
+@Slf4j
+@AllArgsConstructor
+@RocketMQMessageListener(
+        consumerGroup = "${general.register-group}",
+        topic = "${general.register-topic}",
+        consumeMode = ConsumeMode.ORDERLY)
+@ConditionalOnProperty(value = "general.notify-server", havingValue = "false", matchIfMissing = true)
+public class RegisterListener implements RocketMQListener<RegisterEvent> {
+    private UserService                   userService;
+    private RedisTemplate<String, Object> redisTemplate;
+
+    @Override
+    public void onMessage(RegisterEvent registerEvent) {
+        try {
+            User user = userService.phoneRegister(registerEvent.getPhone(), registerEvent.getCode(),
+                    registerEvent.getPassword(), registerEvent.getInviteCode(),
+                    registerEvent.getInvitor(), registerEvent.getCollectionId());
+            redisTemplate.opsForValue().set("register::" + registerEvent.getPhone(), user, Duration.ofSeconds(600));
+        } catch (Exception e) {
+            redisTemplate.opsForValue()
+                    .set("register::" + registerEvent.getPhone(), new BusinessException(e.getMessage()), Duration.ofSeconds(600));
+        }
+    }
+}

+ 35 - 18
src/main/java/com/izouma/nineth/service/UserService.java

@@ -6,12 +6,14 @@ import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
 import com.huifu.adapay.core.exception.BaseAdaPayException;
 import com.izouma.nineth.TokenHistory;
 import com.izouma.nineth.config.Constants;
+import com.izouma.nineth.config.GeneralProperties;
 import com.izouma.nineth.domain.Collection;
 import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.*;
 import com.izouma.nineth.enums.AuthStatus;
 import com.izouma.nineth.enums.AuthorityName;
 import com.izouma.nineth.event.AccountCreatedEvent;
+import com.izouma.nineth.event.RegisterEvent;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.*;
 import com.izouma.nineth.security.Authority;
@@ -32,6 +34,7 @@ import me.chanjar.weixin.mp.bean.result.WxMpUser;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
 import org.springframework.beans.BeanUtils;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
@@ -40,6 +43,7 @@ import org.springframework.core.env.Environment;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.jpa.domain.Specification;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
@@ -56,24 +60,27 @@ import java.util.stream.Collectors;
 @Slf4j
 @AllArgsConstructor
 public class UserService {
-    private UserRepo              userRepo;
-    private WxMaService           wxMaService;
-    private WxMpService           wxMpService;
-    private SmsService            smsService;
-    private StorageService        storageService;
-    private JwtTokenUtil          jwtTokenUtil;
-    private FollowService         followService;
-    private FollowRepo            followRepo;
-    private IdentityAuthRepo      identityAuthRepo;
-    private SysConfigService      sysConfigService;
-    private UserBankCardRepo      userBankCardRepo;
-    private InviteRepo            inviteRepo;
-    private NFTService            nftService;
-    private CacheService          cacheService;
-    private TokenHistoryRepo      tokenHistoryRepo;
-    private CollectionRepo        collectionRepo;
-    private AdapayMerchantService adapayMerchantService;
-    private Environment           env;
+    private UserRepo                      userRepo;
+    private WxMaService                   wxMaService;
+    private WxMpService                   wxMpService;
+    private SmsService                    smsService;
+    private StorageService                storageService;
+    private JwtTokenUtil                  jwtTokenUtil;
+    private FollowService                 followService;
+    private FollowRepo                    followRepo;
+    private IdentityAuthRepo              identityAuthRepo;
+    private SysConfigService              sysConfigService;
+    private UserBankCardRepo              userBankCardRepo;
+    private InviteRepo                    inviteRepo;
+    private NFTService                    nftService;
+    private CacheService                  cacheService;
+    private TokenHistoryRepo              tokenHistoryRepo;
+    private CollectionRepo                collectionRepo;
+    private AdapayMerchantService         adapayMerchantService;
+    private Environment                   env;
+    private RocketMQTemplate              rocketMQTemplate;
+    private GeneralProperties             generalProperties;
+    private RedisTemplate<String, Object> redisTemplate;
 
     public User update(User user) {
         if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN)) {
@@ -236,6 +243,16 @@ public class UserService {
         return user;
     }
 
+    public String mqRegister(String phone, String code, String password, String inviteCode, Long invitor, Long collectionId) {
+        rocketMQTemplate.convertAndSend(generalProperties.getRegisterTopic(),
+                new RegisterEvent(phone, code, password, inviteCode, invitor, collectionId));
+        return phone;
+    }
+
+    public Object getRegisterResult(String phone) {
+        return redisTemplate.opsForValue().get("register::" + phone);
+    }
+
     public User testPhoneRegister() {
         String phone = "19" + RandomStringUtils.randomNumeric(11);
         String password = "123456";

+ 31 - 0
src/main/java/com/izouma/nineth/web/AuthenticationController.java

@@ -22,6 +22,8 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Objects;
 
 @Slf4j
@@ -77,6 +79,35 @@ public class AuthenticationController {
         return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
     }
 
+    @PostMapping("/mqPhoneRegister")
+    @ApiOperation(value = "手机号密码注册")
+    public String mqPhoneRegister(String phone, String code, String password, String inviteCode, Long invitor,
+                                  Long collectionId, String captcha, String captchaKey) {
+//        boolean verify = captchaService.verify(captchaKey, captcha);
+//        if (!verify) {
+//            throw new BusinessException("验证码错误");
+//        }
+        return userService.mqRegister(phone, code, password, inviteCode, invitor, collectionId);
+    }
+
+    @GetMapping("/registerResult")
+    public Object registerResult(String phone) {
+        Object res = userService.getRegisterResult(phone);
+        Map<String, Object> map = new HashMap<>();
+        if (res instanceof User) {
+            map.put("status", "success");
+            map.put("token", jwtTokenUtil.generateToken(JwtUserFactory.create((User) res)));
+            return map;
+        } else if (res == null) {
+            map.put("status", "pending");
+            return map;
+        } else {
+            map.put("status", "fail");
+            map.put("error", res);
+            return map;
+        }
+    }
+
     @PostMapping("/testphoneRegister")
     @ApiOperation(value = "手机号密码注册")
     public String phonePwdLogin() {

+ 12 - 0
src/main/resources/application.yaml

@@ -158,6 +158,8 @@ general:
   update-quota-topic: update-quota-topic-dev
   broadcast-event-group: broadcast-event-group-dev
   broadcast-event-topic: broadcast-event-topic-dev
+  register-group: register-group-dev
+  register-topic: register-topic-dev
 mychain:
   rest:
     bizid: a00e36c5
@@ -238,6 +240,8 @@ general:
   update-quota-topic: update-quota-topic-test
   broadcast-event-group: broadcast-event-group-test
   broadcast-event-topic: broadcast-event-topic-test
+  register-group: register-group-test
+  register-topic: register-topic-test
 ---
 
 spring:
@@ -269,6 +273,8 @@ general:
   update-quota-topic: update-quota-topic-test1
   broadcast-event-group: broadcast-event-group-test1
   broadcast-event-topic: broadcast-event-topic-test1
+  register-group: register-group-test1
+  register-topic: register-topic-test1
 wx:
   pay:
     notify-url: https://test1.raex.vip/notify/order/weixin
@@ -325,6 +331,8 @@ general:
   update-quota-topic: update-quota-topic
   broadcast-event-group: broadcast-event-group
   broadcast-event-topic: broadcast-event-topic
+  register-group: register-group
+  register-topic: register-topic
 wx:
   pay:
     notify-url: https://www.raex.vip/notify/order/weixin
@@ -371,6 +379,8 @@ general:
   update-activity-stock-topic: update-activity-stock-topic
   update-quota-group: update-quota-group
   update-quota-topic: update-quota-topic
+  register-group: register-group
+  register-topic: register-topic
 wx:
   pay:
     notify-url: https://www.raex.vip/notify/order/weixin
@@ -417,6 +427,8 @@ general:
   update-quota-topic: update-quota-topic-test
   broadcast-event-group: broadcast-event-group-test
   broadcast-event-topic: broadcast-event-topic-test
+  register-group: register-group-test
+  register-topic: register-topic-test
 rocketmq:
   name-server: 172.29.50.102:9876
   producer: