gongzuming
2024-10-27 eff1dfad3cb6bbf7fbcd987d31703b8084f4190b
重复点击问题
已修改1个文件
78 ■■■■■ 文件已修改
src/main/java/com/mzl/flower/service/register/RegisterService.java 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mzl/flower/service/register/RegisterService.java
@@ -1,7 +1,6 @@
package com.mzl.flower.service.register;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.register.RegisterCustomerDTO;
@@ -13,6 +12,7 @@
import com.mzl.flower.mapper.system.UserWechatMapper;
import com.mzl.flower.service.customer.CustomerService;
import com.mzl.flower.service.partner.PartnerService;
import com.mzl.flower.service.payment.RedisLockService;
import com.mzl.flower.utils.UUIDGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
@@ -36,15 +36,19 @@
    private final UserWechatMapper wechatMapper;
    private final RedisLockService lockService;
    public RegisterService(UserMapper userMapper,
                           PasswordEncoder passwordEncoder,
                           PartnerService partnerService,
                           CustomerService customerService, UserWechatMapper wechatMapper) {
                           CustomerService customerService, UserWechatMapper wechatMapper,
                           RedisLockService lockService) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
        this.partnerService = partnerService;
        this.customerService = customerService;
        this.wechatMapper = wechatMapper;
        this.lockService = lockService;
    }
    public void registerUser(RegisterDTO dto, String userType) {
@@ -65,39 +69,49 @@
    }
    public void registerCustomerUser(RegisterCustomerDTO dto, String userType, String openId, String sessionKey, String unionId) {
        if(checkUserExist(dto.getTel(),userType)){
            throw new ValidationException("该手机号码已经注册");
        String key = dto.getTel() + "_" + userType;
        boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key);
        if(!lock){
            return;
        }
        User user = new User();
        user.setId(UUIDGenerator.getUUID());
        user.setLoginName(dto.getTel());
        user.setTel(dto.getTel());
        user.setNickName(dto.getTel());
        user.setPassword(passwordEncoder.encode(dto.getPassword()));
        user.setType(userType);
        user.setStatus(Constants.STATUS_ACTIVE);
        user.setIsSys(Constants.N);
        user.create();
        userMapper.insert(user);
        if(StringUtils.isNotBlank(openId)){
            UserWechat wechat = wechatMapper.selectOne(new LambdaQueryWrapper<UserWechat>()
                    .eq(UserWechat::getUserId,user.getId())
                    .eq(UserWechat::getOpenId,openId));
            if(wechat != null){
                throw new ValidationException("该微信用户已经注册");
        try {
            if(checkUserExist(dto.getTel(),userType)){
                throw new ValidationException("该手机号码已经注册");
            }
            wechat = new UserWechat();
            wechat.setUserId(user.getId());
            wechat.setId(UUIDGenerator.getUUID());
            wechat.setOpenId(openId);
            wechat.setUnionId(unionId);
            wechat.setSessionKey(sessionKey);
            wechat.create(user.getId());
            wechatMapper.insert(wechat);
            User user = new User();
            user.setId(UUIDGenerator.getUUID());
            user.setLoginName(dto.getTel());
            user.setTel(dto.getTel());
            user.setNickName(dto.getTel());
            user.setPassword(passwordEncoder.encode(dto.getPassword()));
            user.setType(userType);
            user.setStatus(Constants.STATUS_ACTIVE);
            user.setIsSys(Constants.N);
            user.create();
            userMapper.insert(user);
            if(StringUtils.isNotBlank(openId)){
                UserWechat wechat = wechatMapper.selectOne(new LambdaQueryWrapper<UserWechat>()
                        .eq(UserWechat::getUserId,user.getId())
                        .eq(UserWechat::getOpenId,openId));
                if(wechat != null){
                    throw new ValidationException("该微信用户已经注册");
                }
                wechat = new UserWechat();
                wechat.setUserId(user.getId());
                wechat.setId(UUIDGenerator.getUUID());
                wechat.setOpenId(openId);
                wechat.setUnionId(unionId);
                wechat.setSessionKey(sessionKey);
                wechat.create(user.getId());
                wechatMapper.insert(wechat);
            }
            dto.getDto().setUserId(user.getId());
            customerService.addOrUpdateCustomer(dto.getDto());
        }finally {
            lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key);
        }
        dto.getDto().setUserId(user.getId());
        customerService.addOrUpdateCustomer(dto.getDto());
    }