linwe
2024-06-18 cb734129b410c02275c0f2fc5e820d6ccc0e17c0
src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java
@@ -39,6 +39,7 @@
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.BladeTenantProperties;
import org.springblade.core.tool.constant.BladeConstant;
@@ -65,10 +66,15 @@
import org.springblade.modules.system.vo.UserDetailVO;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static org.springblade.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD;
@@ -88,6 +94,10 @@
   private final IDeptService deptService;
   private final BladeTenantProperties tenantProperties;
   private final IPoliceAffairsGridService policeAffairsGridService;
   @Autowired
   private BladeRedis redisTemplate;
   @Override
   @Transactional(rollbackFor = Exception.class)
@@ -214,6 +224,42 @@
      boolean b = userDeptService.saveBatch(userDeptList);
      return b;
   }
   /**
    * 尝试获取锁
    * @param timeout 超时时间(毫秒)
    * @return 是否成功获取锁
    */
   public boolean lock(long timeout,String lockKey) {
      long endTime = System.currentTimeMillis() + timeout;
      ValueOperations<String, Object> ops = redisTemplate.getValueOps();
      while (System.currentTimeMillis() < endTime) {
         // 使用setIfAbsent命令尝试设置值,仅当key不存在时设置,类似于SETNX
         Boolean result = ops.setIfAbsent(lockKey, String.valueOf(System.currentTimeMillis() + 5000), 5, TimeUnit.SECONDS);
         if (result != null && result) {
            return true;
         }
         try {
            // 短暂休眠,防止CPU过度占用
            Thread.sleep(10);
         } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
         }
      }
      return false;
   }
   /**
    * 释放锁
    */
   public void unlock(String lockKey) {
      // 使用lua脚本保证操作的原子性,避免删除非本客户端创建的锁
      String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
      redisTemplate.getRedisTemplate().execute((RedisCallback<Object>) (connection) -> connection.eval(script.getBytes(), ReturnType.INTEGER, 1, lockKey.getBytes(), String.valueOf(redisTemplate.getValueOps().get(lockKey)).getBytes()));
   }
   @Override
   public IPage<User> selectUserPage(IPage<User> page, User user, String deptId, String tenantId) {
@@ -554,7 +600,7 @@
      }
      user.setPassword(DigestUtil.encrypt(user.getPassword()));
      user.setRoleId("1759487358708310017");
      user.setRoleId("1717429059648606209");
      boolean oauthTemp = save(user);
      return (oauthTemp);
   }