package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.exception.UserException;
import com.example.dao.mapper.UserMapper;
import com.example.dao.mapper.UserViewMapper;
import com.example.dao.model.UserDO;
import com.example.dao.model.UserView;
import com.example.service.UserService;
import com.example.util.JwtUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import static com.example.constant.MailConstant.CODE_KEY_PREFIX;
/**
* @author durance
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
@Resource
UserMapper userMapper;
@Resource
UserViewMapper userViewMapper;
@Resource
RedisTemplate<String, Integer> redisTemplate;
@Override
public Boolean registerUser(UserDO user, Integer idencode) {
// 判断验证码是否相同
String key = CODE_KEY_PREFIX + user.getEmail();
Integer authcode = redisTemplate.opsForValue().get(key);
if (authcode == null) {
throw new UserException("验证码已过期,请重新发送");
}
if (!authcode.equals(idencode)) {
throw new UserException("验证码错误");
}
if (user.getUsername() == null || user.getPassword() == null) {
throw new UserException("账号和密码不能为空");
}
LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserDO::getPhone, user.getPhone()).or().eq(UserDO::getUsername, user.getUsername());
UserDO check = userMapper.selectOne(wrapper);
// 不为null说明已经创建改用户
if (check != null) {
return false;
}
Timestamp time = new Timestamp(System.currentTimeMillis());
user.setCreateTime(time);
String hashpw = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
user.setPassword(hashpw);
userMapper.insert(user);
return true;
}
@Override
public Map<String, Object> loginUser(UserDO user) {
LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserDO::getUsername, user.getUsername());
UserDO check = userMapper.selectOne(wrapper);
// 核对密码是否正确
boolean checkpw = BCrypt.checkpw(user.getPassword(), check.getPassword());
if (!checkpw) {
return null;
}
// 签发jwt,设置用户id
Map<String, Object> res = new HashMap<>(2);
String token = JwtUtil.createJwt("userId", check.getId());
res.put("token", token);
UserView userView = userViewMapper.selectById(check.getId());
res.put("userInfo", userView);
return res;
}
@Override
public Map<String, Object> loginUserByCode(String email, Integer idencode) {
// 查询用户信息
LambdaQueryWrapper<UserView> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserView::getEmail, email);
UserView userView = userViewMapper.selectOne(wrapper);
if (userView == null) {
throw new UserException("不存在该邮箱账号");
}
// 查询验证码是否正确
String key = CODE_KEY_PREFIX + email;
Integer checkCode = redisTemplate.opsForValue().get(key);
if (checkCode == null || !checkCode.equals(idencode)) {
throw new UserException("验证码错误");
}
// 签发jwt
String token = JwtUtil.createJwt("userId", userView.getId());
// 封装响应体
Map<String, Object> res = new HashMap<>(2);
res.put("token", token);
res.put("userInfo", userView);
redisTemplate.delete(key);
return res;
}
@Override
public UserView updateUserInfo(UserDO user) {
if(user == null || user.getId() == null){
throw new UserException("未检测到用户id");
}
if(user.getAvatar() != null || user.getPassword() != null){
throw new UserException("不能直接修改头像、密码");
}
query().getBaseMapper().updateById(user);
return userViewMapper.selectById(user.getId());
}
@Override
public Boolean deleteUser(Integer userId) {
int num = query().getBaseMapper().deleteById(userId);
if(num != 1){
throw new UserException("删除异常");
}
return true;
}
}