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());
|
}
|
}
|