liu
2026-10-27 17ce55a3b038ed61d332bb54916de4fa55ba19c6
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
@@ -512,11 +512,8 @@
        if (CollUtil.isEmpty(importUsers)) {
            throw exception(USER_IMPORT_LIST_IS_EMPTY);
        }
        // 1.2 初始化密码不能为空
        // 1.2 系统参数中的初始密码,作为 Excel 未填写密码时的兜底
        String initPassword = configApi.getConfigValueByKey(USER_INIT_PASSWORD_KEY);
        if (StrUtil.isEmpty(initPassword)) {
            throw exception(USER_IMPORT_INIT_PASSWORD);
        }
        // 2. 遍历,逐个创建 or 更新
        UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>())
@@ -524,11 +521,20 @@
        AtomicInteger index = new AtomicInteger(1);
        importUsers.forEach(importUser -> {
            int currentIndex = index.getAndIncrement();
            String key = StrUtil.blankToDefault(importUser.getUsername(), "第 " + currentIndex + " 行");
            // 2.1.0 解析该行初始密码:Excel 填写了就用填写的,否则读取系统参数
            String effectivePassword = StrUtil.isNotBlank(importUser.getPassword())
                    ? importUser.getPassword() : initPassword;
            if (StrUtil.isBlank(effectivePassword)) {
                respVO.getFailureUsernames().put(key,
                        "未填写初始密码,且系统参数 system.user.init-password 未配置。"
                                + "请在 Excel 中填写该行初始密码,或先在参数设置中配置 system.user.init-password");
                return;
            }
            // 2.1.1 校验字段是否符合要求
            try {
                ValidationUtils.validate(BeanUtils.toBean(importUser, UserSaveReqVO.class).setPassword(initPassword));
                ValidationUtils.validate(BeanUtils.toBean(importUser, UserSaveReqVO.class).setPassword(effectivePassword));
            } catch (ConstraintViolationException ex) {
                String key = StrUtil.blankToDefault(importUser.getUsername(), "第 " + currentIndex + " 行");
                respVO.getFailureUsernames().put(key, ex.getMessage());
                return;
            }
@@ -545,7 +551,7 @@
            AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername());
            if (existUser == null) {
                userMapper.insert(BeanUtils.toBean(importUser, AdminUserDO.class)
                        .setPassword(encodePassword(initPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组
                        .setPassword(encodePassword(effectivePassword)).setPostIds(new HashSet<>())); // 设置初始密码及空岗位编号数组
                respVO.getCreateUsernames().add(importUser.getUsername());
                return;
            }
@@ -556,6 +562,9 @@
            }
            AdminUserDO updateUser = BeanUtils.toBean(importUser, AdminUserDO.class);
            updateUser.setId(existUser.getId());
            // 密码单独处理:Excel 填写了才更新;未填写则置空,保持原密码不变(避免把明文写进库)
            updateUser.setPassword(StrUtil.isNotBlank(importUser.getPassword())
                    ? encodePassword(importUser.getPassword()) : null);
            userMapper.updateById(updateUser);
            respVO.getUpdateUsernames().add(importUser.getUsername());
        });