hejianhao
2026-08-20 ad13f20070707e26b2f6f5ea7660c8a535de0344
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.tcm.training.admin.live.service;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tcm.training.admin.live.dto.ProjectIntroductionPasswordRequest;
import com.tcm.training.admin.live.entity.ProjectIntroductionPassword;
import com.tcm.training.admin.live.mapper.ProjectIntroductionPasswordMapper;
import com.tcm.training.admin.live.vo.ProjectIntroductionPasswordVO;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
public class ProjectIntroductionPasswordService {
 
    private final ProjectIntroductionPasswordMapper passwordMapper;
    private final BCryptPasswordEncoder passwordEncoder;
 
    public ProjectIntroductionPasswordService(ProjectIntroductionPasswordMapper passwordMapper,
                                               BCryptPasswordEncoder passwordEncoder) {
        this.passwordMapper = passwordMapper;
        this.passwordEncoder = passwordEncoder;
    }
 
    /**
     * 获取项目介绍密码配置
     */
    public ProjectIntroductionPasswordVO getPasswordConfig() {
        ProjectIntroductionPassword config = passwordMapper.selectOne(
                new LambdaQueryWrapper<ProjectIntroductionPassword>()
                        .orderByAsc(ProjectIntroductionPassword::getId)
                        .last("limit 1")
        );
 
        ProjectIntroductionPasswordVO vo = new ProjectIntroductionPasswordVO();
        if (config != null) {
            vo.setId(config.getId());
            vo.setPasswordEnabled(config.getPasswordEnabled());
            // 密码不返回明文,前端只需要知道是否设置了密码
            vo.setPassword(StrUtil.isNotBlank(config.getPassword()) ? "******" : null);
        } else {
            vo.setPasswordEnabled(0);
            vo.setPassword(null);
        }
        return vo;
    }
 
    /**
     * 保存项目介绍密码配置
     */
    @Transactional
    public void savePasswordConfig(ProjectIntroductionPasswordRequest req) {
        // 验证密码格式
        if (req.getPasswordEnabled() == 1) {
            if (StrUtil.isBlank(req.getPassword())) {
                throw new IllegalArgumentException("启用密码访问时,密码不能为空");
            }
            if (req.getPassword().length() != 6) {
                throw new IllegalArgumentException("密码必须是6个字符");
            }
        }
 
        // 查询是否已存在配置
        ProjectIntroductionPassword config = passwordMapper.selectOne(
                new LambdaQueryWrapper<ProjectIntroductionPassword>()
                        .orderByAsc(ProjectIntroductionPassword::getId)
                        .last("limit 1")
        );
 
        if (config == null) {
            // 新增
            config = new ProjectIntroductionPassword();
            config.setPasswordEnabled(req.getPasswordEnabled());
            if (req.getPasswordEnabled() == 1 && StrUtil.isNotBlank(req.getPassword())) {
                config.setPassword(passwordEncoder.encode(req.getPassword()));
            } else {
                config.setPassword(null);
            }
            passwordMapper.insert(config);
        } else {
            // 更新 - 使用自定义SQL支持设置为null
            String encodedPassword = null;
            if (req.getPasswordEnabled() == 1 && StrUtil.isNotBlank(req.getPassword())) {
                encodedPassword = passwordEncoder.encode(req.getPassword());
            }
            passwordMapper.updatePasswordConfig(config.getId(), req.getPasswordEnabled(), encodedPassword);
        }
    }
 
    /**
     * 验证项目介绍密码
     */
    public boolean verifyPassword(String inputPassword) {
        if (StrUtil.isBlank(inputPassword)) {
            return false;
        }
 
        ProjectIntroductionPassword config = passwordMapper.selectOne(
                new LambdaQueryWrapper<ProjectIntroductionPassword>()
                        .eq(ProjectIntroductionPassword::getPasswordEnabled, 1)
                        .isNotNull(ProjectIntroductionPassword::getPassword)
                        .orderByAsc(ProjectIntroductionPassword::getId)
                        .last("limit 1")
        );
 
        return config != null
                && config.getPassword() != null
                && !config.getPassword().isEmpty()
                && passwordEncoder.matches(inputPassword, config.getPassword());
    }
 
    /**
     * 检查项目介绍是否启用密码访问
     */
    public boolean isPasswordEnabled() {
        ProjectIntroductionPassword config = passwordMapper.selectOne(
                new LambdaQueryWrapper<ProjectIntroductionPassword>()
                        .eq(ProjectIntroductionPassword::getPasswordEnabled, 1)
                        .orderByAsc(ProjectIntroductionPassword::getId)
                        .last("limit 1")
        );
        return config != null;
    }
}