This commit is contained in:
2026-02-23 02:05:48 +08:00
parent c8598d3dea
commit 5b3047f675
8 changed files with 178 additions and 159 deletions

View File

@@ -1,18 +1,18 @@
package cn.iocoder.yudao.module.tik.member.mq.consumer;
import cn.iocoder.yudao.module.member.api.message.user.MemberUserCreateMessage;
import cn.iocoder.yudao.module.tik.muye.memberuserprofile.service.MemberUserProfileService;
import cn.iocoder.yudao.module.tik.quota.service.TikUserQuotaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import jakarta.annotation.Resource;
/**
* 会员用户创建事件监听器
*
* 功能:用户注册后自动初始化配额
*
* 功能:用户注册后自动初始化配额和档案
* 触发时机用户注册时MemberUserServiceImpl.createUser()
*
* @author 芋道源码
@@ -23,30 +23,28 @@ public class MemberUserCreateConsumer {
@Resource
private TikUserQuotaService quotaService;
@Resource
private MemberUserProfileService memberUserProfileService;
/**
* 监听用户创建事件(注册时触发)
*
* 同步执行,确保登录后立即可用
*
* @param message 用户创建消息
*/
@EventListener
@Async // 异步处理,避免阻塞主事务
public void onMessage(MemberUserCreateMessage message) {
log.info("[onMessage][用户注册事件,用户编号({})]", message.getUserId());
try {
Long userId = message.getUserId();
// 初始化用户配额默认VIP等级0
// 注意OSS目录采用懒加载策略在首次上传时再初始化
quotaService.initQuota(userId, 0);
log.info("[onMessage][用户({})配额初始化成功]", userId);
} catch (Exception e) {
log.error("[onMessage][用户({})配额初始化失败]", message.getUserId(), e);
// 注意:这里不抛出异常,避免影响用户注册流程
// 可以考虑记录失败日志,后续通过定时任务补偿
}
Long userId = message.getUserId();
// 初始化用户配额默认VIP等级0
quotaService.initQuota(userId, 0);
log.info("[onMessage][用户({})配额初始化成功]", userId);
// 初始化会员档案
memberUserProfileService.createIfAbsent(userId);
log.info("[onMessage][用户({})档案初始化成功]", userId);
}
}

View File

@@ -0,0 +1,37 @@
package cn.iocoder.yudao.module.tik.muye.memberuserprofile.controller.app;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.tik.muye.memberuserprofile.dal.MemberUserProfileDO;
import cn.iocoder.yudao.module.tik.muye.memberuserprofile.service.MemberUserProfileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
/**
* 用户 APP - 会员档案
*/
@Tag(name = "用户 APP - 会员档案")
@RestController
@RequestMapping("/api/muye/member-profile")
@Validated
public class AppMemberUserProfileController {
@Resource
private MemberUserProfileService memberUserProfileService;
@GetMapping("/get")
@Operation(summary = "获取当前用户档案")
public CommonResult<MemberUserProfileDO> getProfile() {
Long userId = SecurityFrameworkUtils.getLoginUserId();
MemberUserProfileDO profile = memberUserProfileService.createIfAbsent(userId);
return success(profile);
}
}

View File

@@ -40,10 +40,11 @@ public interface MemberUserProfileMapper extends BaseMapperX<MemberUserProfileDO
/**
* 根据用户ID查询档案
* @param userId 用户ID支持 String 或 Long 类型)
*/
default MemberUserProfileDO selectByUserId(String userId) {
default MemberUserProfileDO selectByUserId(Object userId) {
return selectOne(new LambdaQueryWrapperX<MemberUserProfileDO>()
.eq(MemberUserProfileDO::getUserId, userId));
.eq(MemberUserProfileDO::getUserId, String.valueOf(userId)));
}
/**

View File

@@ -60,4 +60,13 @@ public interface MemberUserProfileService {
*/
PageResult<MemberUserProfileDO> getMemberUserProfilePage(MemberUserProfilePageReqVO pageReqVO);
/**
* 根据用户ID获取或创建档案
* 如果档案不存在,则自动创建
*
* @param userId 用户ID
* @return 档案信息
*/
MemberUserProfileDO createIfAbsent(Long userId);
}

View File

@@ -10,6 +10,8 @@ import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDateTime;
import java.math.BigDecimal;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@@ -55,11 +57,9 @@ public class MemberUserProfileServiceImpl implements MemberUserProfileService {
}
@Override
public void deleteMemberUserProfileListByIds(List<Long> ids) {
// 删除
public void deleteMemberUserProfileListByIds(List<Long> ids) {
memberUserProfileMapper.deleteByIds(ids);
}
}
private void validateMemberUserProfileExists(Long id) {
if (memberUserProfileMapper.selectById(id) == null) {
@@ -77,4 +77,29 @@ public class MemberUserProfileServiceImpl implements MemberUserProfileService {
return memberUserProfileMapper.selectPage(pageReqVO);
}
@Override
public MemberUserProfileDO createIfAbsent(Long userId) {
// 1. 先查询是否存在
MemberUserProfileDO profile = memberUserProfileMapper.selectByUserId(userId);
if (profile != null) {
return profile;
}
// 2. 创建新档案
profile = new MemberUserProfileDO();
profile.setUserId(String.valueOf(userId));
profile.setRegisterTime(LocalDateTime.now());
profile.setLastLoginTime(LocalDateTime.now());
profile.setTotalPoints(0);
profile.setUsedPoints(0);
profile.setRemainingPoints(0);
profile.setTotalStorage(new BigDecimal("1.0"));
profile.setUsedStorage(BigDecimal.ZERO);
profile.setRemainingStorage(new BigDecimal("1.0"));
profile.setTotalRecharge(BigDecimal.ZERO);
profile.setStatus(1);
memberUserProfileMapper.insert(profile);
return profile;
}
}