diff --git a/luoo_common/src/main/java/constants/TaskPointIdConstants.java b/luoo_common/src/main/java/constants/TaskPointIdConstants.java index 0721e67..aa69beb 100644 --- a/luoo_common/src/main/java/constants/TaskPointIdConstants.java +++ b/luoo_common/src/main/java/constants/TaskPointIdConstants.java @@ -34,7 +34,7 @@ public class TaskPointIdConstants { public static final String DAILY_SIGN = "11"; // 分享期刊 public static final String SHARE_JOURNAL = "12"; - // 新用户邀请 todo + // 新用户邀请 public static final String NEW_USER_INVITE = "13"; // 每日评论 public static final String DAILY_COMMENT = "14"; @@ -47,13 +47,13 @@ public class TaskPointIdConstants { public static final String DAILY_SIGN_7 = "17"; // 连续签到30天 public static final String DAILY_SIGN_30 = "18"; - // 邀请用户3人 todo + // 邀请用户3人 public static final String INVITE_USER_3 = "19"; - // 邀请用户6人 todo + // 邀请用户6人 public static final String INVITE_USER_6 = "20"; - // 邀请用户10人 todo + // 邀请用户10人 public static final String INVITE_USER_10 = "21"; - // 邀请用户20人 todo + // 邀请用户20人 public static final String INVITE_USER_20 = "22"; diff --git a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java index 6f0bc4e..78649cf 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java @@ -144,7 +144,7 @@ public class LoginController extends BaseController { loginUserInfo.setLastUseDeviceId(loginReq.getDeviceId()); loginUserInfo.setLastUseDeviceBrand(loginReq.getDeviceBrand()); loginUserInfo.setLastLoginIp(getIpAddr(request)); - String token = userInfoService.loginOrRegister(loginUserInfo); + String token = userInfoService.loginOrRegisterWithInvite(loginUserInfo, loginReq); redisTemplate.delete(redisMobileCheckCodeKey); return Result.success(token); } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index 5213fe7..2442cda 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -699,5 +699,12 @@ public class MyController extends BaseController { return Result.success(userInfoService.withdrawPageResult(authorization, pageNum, pageSize)); } + @ApiOperation(value = "18 用户生成邀请码") + @PostMapping("/invitation/code") + @GlobalInterceptor(checkAppUserLogin = true) + public Result createInvitationCode(@ApiParam(value = "用户token", required = true) @RequestHeader(value = "Authorization", required = true) String authorization) { + return Result.success(userInfoService.createInvitationCode(authorization)); + } + } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java index 7a57aeb..d0e8e84 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java @@ -48,4 +48,6 @@ public interface UserInfoDao extends JpaRepository, JpaSpecifi public Integer countByUserNameAndUserNameNotNull(String userName); public List findAllByIsAuthor(int isAuthor); + + public UserInfo findByInvitationCode(String invitationCode); } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java index 44f5ab4..f77aeb4 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserPointLogDao.java @@ -21,4 +21,6 @@ public interface UserPointLogDao extends JpaRepository, Jp @Query(value = "select * from tb_user_point_log where user_id = ?1 and task_point_id = ?2 and Date(create_time) BETWEEN DATE_SUB(CURDATE(), INTERVAL ?3 DAY) AND DATE_SUB(CURDATE(), INTERVAL 1 DAY)", nativeQuery = true) public List findDaysByUserIdAndTaskPointId(String userId, String pointId, Integer days); + + public List findUserPointLogsByUserIdAndTaskPointId(String userId, String taskPointId); } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/request/LoginReq.java b/luoo_user/src/main/java/com/luoo/user/dto/request/LoginReq.java index 4e46066..0831ae9 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/request/LoginReq.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/request/LoginReq.java @@ -31,4 +31,7 @@ public class LoginReq implements Serializable { @ApiModelProperty(name = "deviceBrand", value = "设备品牌", required = false) String deviceBrand; + + @ApiModelProperty(name = "invitationCode", value = "邀请码", required = false) + String invitationCode; } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java index 05f68c4..c781e64 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java @@ -212,5 +212,15 @@ public class UserInfo implements Serializable { */ private Integer isAuthor; + /** + * 邀请码 + */ + private String invitationCode; + + /** + * 邀请人 + */ + private String invitationUserId; + } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 3d0d1a8..b688f8b 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -4,14 +4,17 @@ import api.PageResult; import client.vo.SimpleUser; import com.luoo.user.dao.UserBankDao; import com.luoo.user.dao.UserInfoDao; +import com.luoo.user.dao.UserPointLogDao; import com.luoo.user.dao.UserRealNameDao; import com.luoo.user.dao.WithdrawDao; import com.luoo.user.dto.UserQueryReq; import com.luoo.user.dto.UserRealNameCheckDto; import com.luoo.user.dto.UserRealNameFormDto; import com.luoo.user.dto.bank.UserBankAddDto; +import com.luoo.user.dto.request.LoginReq; import com.luoo.user.pojo.UserBank; import com.luoo.user.pojo.UserInfo; +import com.luoo.user.pojo.UserPointLog; import com.luoo.user.pojo.UserRealName; import com.luoo.user.pojo.Withdraw; import com.luoo.user.util.NickNameUtil; @@ -78,10 +81,13 @@ public class UserInfoService { private final UserPointLogService userPointLogService; + private final UserPointLogDao userPointLogDao; + public UserInfoService(UserInfoDao userInfoDao, IdWorker idWorker, RedisTemplate redisTemplate, RabbitTemplate rabbitTemplate, BCryptPasswordEncoder encoder, HttpServletRequest request, JwtUtil jwtUtil, UserRealNameDao userRealNameDao, UserBankDao userBankDao, - WithdrawDao withdrawDao, UserPointLogService userPointLogService) { + WithdrawDao withdrawDao, UserPointLogService userPointLogService, + UserPointLogDao userPointLogDao) { this.userInfoDao = userInfoDao; this.idWorker = idWorker; this.redisTemplate = redisTemplate; @@ -93,6 +99,7 @@ public class UserInfoService { this.userBankDao = userBankDao; this.withdrawDao = withdrawDao; this.userPointLogService = userPointLogService; + this.userPointLogDao = userPointLogDao; } /** @@ -224,6 +231,75 @@ public class UserInfoService { return userInfoDao.countByNickName(nickName); } + /** + * 注册判断是否有邀请码 + * + * @param loginUserInfo 登录用户 + * @param loginReq 邀请码 + * @return token + */ + public String loginOrRegisterWithInvite(UserInfo loginUserInfo, LoginReq loginReq) { + UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); + boolean isNewUser = false; + if (null == userInfo) { + userInfo = loginUserInfo; + userInfo.setId(String.valueOf(idWorker.nextId())); +// userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); + userInfo.setNickName(NickNameUtil2.generateRandomString()); + Date curDate = new Date(); + userInfo.setJoinTime(curDate); + userInfo.setLastLoginTime(curDate); + userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); + userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); + userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); + userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); + + isNewUser = true; + } else { +// userInfo.setAppleId(loginUserInfo.getAppleId()); + userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); + userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); + userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); + } + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } + userInfo.setLastLoginTime(new Date()); + userInfoDao.save(userInfo); + + if(isNewUser) { + // 只有新用户会获得积分 + userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, userInfo.getId()); + + if(StringUtils.isNotBlank(loginReq.getInvitationCode())) { + // 新用户,且存在邀请码 + UserInfo userInfoDaoByInvitationCode = userInfoDao.findByInvitationCode(loginReq.getInvitationCode()); + if(null != userInfoDaoByInvitationCode) { + userInfo.setInvitationUserId(userInfoDaoByInvitationCode.getId()); + userInfo.setInvitationCode(userInfoDaoByInvitationCode.getInvitationCode()); + userInfoDao.save(userInfo); + + // 邀请码有效,且新用户,则给邀请人积分 + userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_INVITE, userInfoDaoByInvitationCode.getId()); + + List list = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(userInfoDaoByInvitationCode.getId(), TaskPointIdConstants.NEW_USER_INVITE); + if (list.size() == 3) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_3, userInfoDaoByInvitationCode.getId()); + } else if (list.size() == 6) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_6, userInfoDaoByInvitationCode.getId()); + } else if (list.size() == 10) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_10, userInfoDaoByInvitationCode.getId()); + } else if (list.size() == 20) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_20, userInfoDaoByInvitationCode.getId()); + } + } + } + } + + return Constants.TOKEN_PREFIX + + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); + } + public String loginOrRegister(UserInfo loginUserInfo) { UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); boolean isNewUser = false; @@ -262,7 +338,6 @@ public class UserInfoService { + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); } - public String appleLoginOrRegister(UserInfo loginUserInfo) { UserInfo userInfo = userInfoDao.findByAppleId(loginUserInfo.getAppleId()); boolean isNewUser = false; @@ -778,4 +853,23 @@ public class UserInfoService { public List getAuthors() { return userInfoDao.findAllByIsAuthor(1); } + + @Transactional(rollbackFor = Exception.class) + public String createInvitationCode(String token) { + UserLoginDto user = jwtUtil.getUserLoginDto(token); + UserInfo byId = userInfoDao.getById(user.getUserId()); + if(StringUtils.isNotBlank(byId.getInvitationCode())) { + return byId.getInvitationCode(); + } + + String invitationCode = RandomStringUtils.randomNumeric(6); + while (userInfoDao.findByInvitationCode(invitationCode) != null) { + invitationCode = RandomStringUtils.randomNumeric(6); + } + + byId.setInvitationCode(invitationCode); + userInfoDao.save(byId); + + return invitationCode; + } } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java index 73727ad..8e9a29e 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java @@ -111,6 +111,22 @@ public class UserPointLogService { addByTask(taskPointId, userLoginDto); } + /** + * 通过任务添加积分明细,直接使用userId + * + */ + public void addByTaskDailyAndUserId(String taskPointId, String userId) { + UserPointLog userPointLog = UserPointLog.builder() + .id(String.valueOf(idWorker.nextId())) + .type(PointEnums.TASK_POINT_TYPE_ADD.getCode()) + .createUser(userId) + .taskPointId(taskPointId) + .userId(userId) + .build(); + + rabbitTemplate.convertAndSend("pointLog", userPointLog); + } + private void addByTask(String taskPointId, UserLoginDto userLoginDto) { UserPointLog userPointLog = UserPointLog.builder() diff --git a/luoo_user/src/main/resources/sql/20240802.sql b/luoo_user/src/main/resources/sql/20240802.sql index 16f0994..cb8d84e 100644 --- a/luoo_user/src/main/resources/sql/20240802.sql +++ b/luoo_user/src/main/resources/sql/20240802.sql @@ -1,2 +1,9 @@ alter table tb_lottery add draw_time datetime null comment '开奖时间' after apply_end_time; + +alter table tb_user_info + add invitation_code varchar(20) null comment '邀请码'; + +alter table tb_user_info + add invitation_user_id varchar(20) null comment '邀请人'; +