2024-05-13 10:07:53 +08:00
|
|
|
package com.whdc.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
2024-05-13 14:55:42 +08:00
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
import com.whdc.exception.MyException;
|
2024-05-15 10:10:20 +08:00
|
|
|
import com.whdc.mapper.AddressBookMapper;
|
2024-05-13 10:07:53 +08:00
|
|
|
import com.whdc.mapper.UserMapper;
|
2024-05-13 14:55:42 +08:00
|
|
|
import com.whdc.model.dto.LoginDto;
|
2024-05-16 20:14:43 +08:00
|
|
|
import com.whdc.model.dto.UserDto;
|
2024-05-15 10:10:20 +08:00
|
|
|
import com.whdc.model.entity.AddressBook;
|
2024-05-13 10:07:53 +08:00
|
|
|
import com.whdc.model.entity.User;
|
2024-05-13 14:55:42 +08:00
|
|
|
import com.whdc.model.vo.LoginVo;
|
2024-05-13 10:07:53 +08:00
|
|
|
import com.whdc.service.IUserService;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
2024-05-16 20:14:43 +08:00
|
|
|
import com.whdc.utils.ResultJson;
|
2024-05-16 20:56:52 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-05-13 14:55:42 +08:00
|
|
|
import org.springframework.beans.BeanUtils;
|
2024-05-15 10:10:20 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2024-05-13 10:07:53 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
2024-05-13 14:55:42 +08:00
|
|
|
import org.springframework.util.DigestUtils;
|
2024-05-13 10:07:53 +08:00
|
|
|
|
2024-05-13 14:55:42 +08:00
|
|
|
import java.util.Date;
|
2024-05-13 10:07:53 +08:00
|
|
|
import java.util.List;
|
2024-05-16 20:14:43 +08:00
|
|
|
import java.util.Objects;
|
2024-05-13 14:55:42 +08:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
2024-05-13 10:07:53 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* 服务实现类
|
|
|
|
|
* </p>
|
|
|
|
|
* @author xusan
|
|
|
|
|
* @date 2024-05-11
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
2024-05-16 20:56:52 +08:00
|
|
|
@Slf4j
|
2024-05-13 10:07:53 +08:00
|
|
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
|
|
|
|
|
|
2024-05-14 15:09:10 +08:00
|
|
|
public static final String DEFAULT_PASSWORD = "Fxkh@123456";
|
2024-05-13 10:07:53 +08:00
|
|
|
|
2024-05-15 10:10:20 +08:00
|
|
|
@Autowired
|
|
|
|
|
private AddressBookMapper addressBookMapper;
|
|
|
|
|
|
2024-05-13 10:07:53 +08:00
|
|
|
@Override
|
|
|
|
|
public IPage<User> page(User dto) {
|
|
|
|
|
return baseMapper.page(new Page<>(), dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<User> find(User dto) {
|
|
|
|
|
return baseMapper.find(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-13 14:55:42 +08:00
|
|
|
@Override
|
2024-05-13 16:44:01 +08:00
|
|
|
public LoginVo login(LoginDto dto) {
|
2024-05-13 14:55:42 +08:00
|
|
|
|
2024-05-13 16:44:01 +08:00
|
|
|
User sysUser = findByLoginName(dto.getLoginName());
|
2024-05-13 14:55:42 +08:00
|
|
|
|
|
|
|
|
if (sysUser == null) {
|
|
|
|
|
throw new MyException("用户名或密码错误");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 16:44:01 +08:00
|
|
|
if (getPassword(sysUser.getSalt() ,dto.getPassword()).equals(sysUser.getPassword())) {
|
2024-05-13 14:55:42 +08:00
|
|
|
LoginVo out = new LoginVo();
|
|
|
|
|
BeanUtils.copyProperties(sysUser, out);
|
2024-05-15 10:10:20 +08:00
|
|
|
|
|
|
|
|
AddressBook book = addressBookMapper.selectById(sysUser.getAbId());
|
2024-05-16 20:14:43 +08:00
|
|
|
if (Objects.nonNull(book)){
|
|
|
|
|
out.setName(book.getName());
|
|
|
|
|
}
|
2024-05-13 14:55:42 +08:00
|
|
|
return out;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new MyException("用户名或密码错误");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean register(User entity) {
|
|
|
|
|
// 生成盐值
|
|
|
|
|
String salt = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
|
// 生成密码
|
|
|
|
|
entity.setSalt(salt)
|
|
|
|
|
.setPassword(getPassword(salt, DEFAULT_PASSWORD));
|
|
|
|
|
return this.save(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getRole(Integer id) {
|
|
|
|
|
|
|
|
|
|
User byId = this.getById(id);
|
|
|
|
|
if (byId == null){
|
|
|
|
|
throw new MyException("该用户不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return byId.getRole();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2024-05-16 11:57:51 +08:00
|
|
|
public boolean register(String phone, Integer abId, String role) {
|
2024-05-13 14:55:42 +08:00
|
|
|
User user = new User();
|
|
|
|
|
user.setAbId(abId)
|
2024-05-16 11:57:51 +08:00
|
|
|
.setRole(role)
|
2024-05-13 14:55:42 +08:00
|
|
|
.setUsername(phone);
|
|
|
|
|
return register(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean updateLastDate(Integer id) {
|
|
|
|
|
return this.lambdaUpdate()
|
|
|
|
|
.set(User::getLastLoginDate,new Date())
|
|
|
|
|
.eq(User::getId,id)
|
|
|
|
|
.update();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 20:14:43 +08:00
|
|
|
@Override
|
|
|
|
|
public ResultJson updatePwd(UserDto dto) {
|
|
|
|
|
|
|
|
|
|
|
2024-05-16 20:27:41 +08:00
|
|
|
User sysUser = this.getById(dto.getId());
|
|
|
|
|
if (Objects.isNull(sysUser)) {
|
2024-05-16 20:14:43 +08:00
|
|
|
|
|
|
|
|
return ResultJson.error("当前数据不存在");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (getPassword(sysUser.getSalt() ,dto.getOldPassword()).equals(sysUser.getPassword())) {
|
|
|
|
|
// 更新密码
|
2024-05-16 20:27:41 +08:00
|
|
|
sysUser.setPassword(getPassword(sysUser.getSalt() ,dto.getNewPassword()));
|
2024-05-16 20:56:52 +08:00
|
|
|
log.info("修改密码" + sysUser.getUsername() + "_" + dto.getNewPassword());
|
2024-05-16 20:27:41 +08:00
|
|
|
return ResultJson.ok(sysUser.updateById());
|
2024-05-16 20:14:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultJson.error("密码错误");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 14:55:42 +08:00
|
|
|
private User findByLoginName(String loginName) {
|
|
|
|
|
if (loginName == null) {
|
|
|
|
|
throw new MyException("登录名不能为空");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.lambdaQuery().eq(User::getUsername, loginName).one();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 20:56:52 +08:00
|
|
|
public static String getPassword(String salt,String password) {
|
2024-05-13 14:55:42 +08:00
|
|
|
return DigestUtils.md5DigestAsHex((salt + password).getBytes());
|
|
|
|
|
}
|
2024-05-14 11:00:45 +08:00
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String salt = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
|
System.out.println(salt);
|
|
|
|
|
System.out.println(DigestUtils.md5DigestAsHex((salt + DEFAULT_PASSWORD).getBytes()));
|
|
|
|
|
}
|
2024-05-13 10:07:53 +08:00
|
|
|
}
|