From dc885d07f9f62c60ac90dbd0c44c5fdbb0b00486 Mon Sep 17 00:00:00 2001
From: 陶杰 <1378534974@qq.com>
Date: 星期二, 31 十二月 2024 10:30:36 +0800
Subject: [PATCH] 1.微信登录-用户不存在新注册用户,昵称没有解决

---
 src/main/java/com/mzl/flower/service/system/UserService.java |  115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 110 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/system/UserService.java b/src/main/java/com/mzl/flower/service/system/UserService.java
index fca2da7..5bdb3f8 100644
--- a/src/main/java/com/mzl/flower/service/system/UserService.java
+++ b/src/main/java/com/mzl/flower/service/system/UserService.java
@@ -7,31 +7,41 @@
 import com.mzl.flower.config.security.SecurityUtils;
 import com.mzl.flower.constant.Constants;
 import com.mzl.flower.dto.request.BindWechatDTO;
+import com.mzl.flower.dto.request.CreateWechatUserDTO;
+import com.mzl.flower.dto.request.customer.UpdateCustomerDTO;
 import com.mzl.flower.dto.request.system.*;
 import com.mzl.flower.dto.response.current.CurrentUserDTO;
-import com.mzl.flower.dto.response.system.EmployeeDTO;
-import com.mzl.flower.dto.response.system.MenuTreeDTO;
-import com.mzl.flower.dto.response.system.UserDetailsDTO;
-import com.mzl.flower.dto.response.system.UserListDTO;
+import com.mzl.flower.dto.response.system.*;
+import com.mzl.flower.entity.point.CustomerPoint;
 import com.mzl.flower.entity.system.*;
 import com.mzl.flower.enums.TrueOrFalseEnum;
+import com.mzl.flower.mapper.point.CustomerPointMapper;
 import com.mzl.flower.mapper.system.*;
 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.service.point.CustomerPointService;
+import com.mzl.flower.service.supplier.StationService;
 import com.mzl.flower.service.supplier.SupplierService;
 import com.mzl.flower.utils.TreeBuilderUtil;
 import com.mzl.flower.utils.UUIDGenerator;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
 
 import javax.validation.constraints.NotBlank;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 @Service
 @Transactional
@@ -74,6 +84,14 @@
     @Autowired
     private EmployeeMapper employeeMapper;
 
+    @Autowired
+    private CustomerPointMapper customerPointMapper;
+    @Autowired
+    private StationService stationService;
+
+    @Autowired
+    RedissonClient redissonClient;
+
     public User findUserByOpenId(String openId, String sessionKey, String unionId
             , String imgUrl, String nickname,String userType){
         List<String> userTypes = new ArrayList<>();
@@ -82,7 +100,8 @@
         if(user == null){
             throw new ValidationException("请注册账号后至\"个人中心-绑定微信\"后再使用微信登录");
         }
-        user.setNickName(nickname);
+//        user.setNickName(nickname);
+        user.setNickName(user.getLoginName());
         user.setPicture(imgUrl);
         user.update(user.getId());
         userMapper.updateById(user);
@@ -188,10 +207,21 @@
 
         if(Constants.USER_TYPE.customer.name().equals(user.getType())){
             result.setCustomerDTO(customerService.getCurrentCustomer());
+            //查询积分
+            CustomerPoint customerPoint = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
+                    .eq(CustomerPoint::getUserId, userId));
+            if(customerPoint == null){
+                result.setCurrentPoint(0);
+            }else{
+                Integer currentPoint =customerPoint.getTotalPoint()-customerPoint.getUsedPoint()-customerPoint.getExpiredPoint()-customerPoint.getDeductionPoint();
+                result.setCurrentPoint(currentPoint>=0?currentPoint:0);
+            }
         }else if(Constants.USER_TYPE.supplier.name().equals(user.getType())){
             result.setSupplierDTO(supplierService.getCurrentSupplier());
         }else if(Constants.USER_TYPE.partner.name().equals(user.getType())){
             result.setPartnerDTO(partnerService.getCurrentPartner());
+        }else if (Constants.USER_TYPE.admin.name().equals(user.getType())){
+            result.setMainWarehouse(stationService.getMainWarehouse(user.getId()));
         }
         result.setBindWechat(wechatMapper.selectCount(new LambdaQueryWrapper<UserWechat>()
                 .eq(UserWechat::getUserId, userId)) > 0);
@@ -462,4 +492,79 @@
                 .eq(User::getVipGrade,getVipGrade);
         return userMapper.selectList(queryWrapper);
     }
+
+    /**
+     * 查看微信是否已经存在用户
+     * @param openId
+     * @param sessionKey
+     * @param unionId
+     * @param userType
+     * @return
+     */
+    public User getExistUserByOpenId(String openId, String sessionKey, String unionId, String userType) {
+        List<String> userTypes = new ArrayList<>();
+        userTypes.add(userType);
+        User user = userMapper.getByOpenID(openId,userTypes);
+        return user;
+    }
+
+    public void registWxUser(CreateWechatUserDTO dto) {
+        String RETISTER_KEY = "%s_%s";
+        RLock lock = redissonClient.getLock(String.format(RETISTER_KEY,dto.getPurePhoneNumber(),dto.getUserType()));
+        try {
+            // 获取锁,最多等待 10 秒,锁自动释放时间 30 秒
+            if (lock.tryLock(10, 30, TimeUnit.SECONDS)) {
+                try{
+                // 其次查找当前loginName是prephonenume的,userType是customer的用户存在不存在
+                if (StringUtils.isEmpty(dto.getPurePhoneNumber()) && StringUtils.isEmpty(dto.getPhoneNumber())) {
+                    throw new ValidationException("微信获取的电话号码为空!无法注册");
+                }
+                // 1:查看用户存在不存在
+                List<String> userTypes = new ArrayList<>();
+                userTypes.add(dto.getUserType());
+                User user = findByLoginName(dto.getPurePhoneNumber(), userTypes);
+                if (ObjectUtils.isEmpty(user)) {
+                    // 是新用户,需要注册
+                    user = new User();
+                    user.setId(UUIDGenerator.getUUID());
+                    user.setLoginName(dto.getPurePhoneNumber());
+                    user.setTel(dto.getPurePhoneNumber());
+                    user.setNickName(dto.getPurePhoneNumber());
+                    user.setPassword(passwordEncoder.encode("12345678"));
+                    user.setType(dto.getUserType());
+                    user.setStatus(Constants.STATUS_ACTIVE);
+                    user.setIsSys(Constants.N);
+                    user.create();
+                    userMapper.insert(user);
+
+                    // 注册商户信息
+                    UpdateCustomerDTO updateCustomerDTO = new UpdateCustomerDTO();
+                    updateCustomerDTO.setUserId(user.getId());
+                    if (StringUtils.isNotBlank(dto.getPurePhoneNumber())) {
+                        updateCustomerDTO.setName("花满芫-" + dto.getPurePhoneNumber().substring(dto.getPurePhoneNumber().length() - 4));
+
+                    }
+                    customerService.addOrUpdateCustomer(updateCustomerDTO);
+                }
+
+                BindWechatDTO wechatDTO = new BindWechatDTO();
+                wechatDTO.setRealName(dto.getPurePhoneNumber());
+                bindWechat(user.getId(), dto.getOpenId(), dto.getUnionId(), dto.getSessionKey(), wechatDTO);
+                }finally {
+                    lock.unlock();
+                }
+            }
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public Page<UserListInfoDTO> searchUserInfos(SearchUserDTO dto, Page page) {
+        List<UserListInfoDTO> result = userMapper.searchUserInfo(page, dto);
+
+        page.setRecords(result);
+        return page;
+    }
+
+
 }

--
Gitblit v1.9.3