|
@@ -0,0 +1,109 @@
|
|
|
+package com.example.demo.user.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.example.demo.common.utils.Md5Util;
|
|
|
+import com.example.demo.common.utils.TokenUtils;
|
|
|
+import com.example.demo.user.domain.User;
|
|
|
+import com.example.demo.user.dto.LoginDto;
|
|
|
+import com.example.demo.user.dto.UserDto;
|
|
|
+import com.example.demo.user.service.UserService;
|
|
|
+import com.example.demo.user.dao.UserMapper;
|
|
|
+import com.example.demo.user.vo.ResultVo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.redisson.api.RLock;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+* @author 徐乐
|
|
|
+* @description 针对表【user(用户基础信息表)】的数据库操作Service实现
|
|
|
+* @createDate 2025-06-04 17:31:35
|
|
|
+*/
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
+ implements UserService{
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserMapper userMapper;
|
|
|
+ @Autowired
|
|
|
+ private RedissonClient redissonClient;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String,String> redisTemplate;
|
|
|
+ @Override
|
|
|
+ public ResultVo register(UserDto dto) {
|
|
|
+ QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
|
|
|
+ userQueryWrapper.eq("phone", dto.getPhone());
|
|
|
+ log.info("加锁");
|
|
|
+ RLock lock = redissonClient.getLock(dto.getPhone());
|
|
|
+ try {
|
|
|
+ log.info("获取锁");
|
|
|
+ boolean lockSuccess = lock.tryLock(3, TimeUnit.SECONDS);
|
|
|
+ if (!lockSuccess) {
|
|
|
+ throw new RuntimeException("操作频繁,请稍后再试");
|
|
|
+ }
|
|
|
+ User user = userMapper.selectOne(userQueryWrapper);
|
|
|
+ if (user != null) {
|
|
|
+ return ResultVo.error("用户已存在");
|
|
|
+ }
|
|
|
+ log.info("注册");
|
|
|
+ User u = new User();
|
|
|
+ u.setId(IdUtil.getSnowflake().nextId());
|
|
|
+ u.setUsername(dto.getUsername());
|
|
|
+ //生成盐值
|
|
|
+ String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
+ u.setSalt(uuid);
|
|
|
+ //获取密码
|
|
|
+ String password = dto.getPassword();
|
|
|
+ //加密
|
|
|
+ String md5Password = Md5Util.MD5(uuid + password);
|
|
|
+ u.setPassword(md5Password);
|
|
|
+ u.setEmail(dto.getEmail());
|
|
|
+ u.setPhone(dto.getPhone());
|
|
|
+ u.setAvatar(dto.getAvatar());
|
|
|
+ userMapper.insert(u);
|
|
|
+ return ResultVo.success("注册成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException("注册过程中出现异常,请稍后再试");
|
|
|
+ } finally {
|
|
|
+ if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
+ log.info("释放锁");
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResultVo login(LoginDto dto) {
|
|
|
+ QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
|
|
|
+ userQueryWrapper.eq("phone",dto.getPhone());
|
|
|
+ User user = userMapper.selectOne(userQueryWrapper);
|
|
|
+ if (user==null){
|
|
|
+ return ResultVo.error("用户不存在");
|
|
|
+ }
|
|
|
+ //获取盐值
|
|
|
+ String salt = user.getSalt();
|
|
|
+ //获取密码
|
|
|
+ String password = dto.getPassword();
|
|
|
+ //加密
|
|
|
+ String encryptedPassword = Md5Util.MD5(salt + password);
|
|
|
+ if (encryptedPassword.equals(user.getPassword())){
|
|
|
+ String token = TokenUtils.createJwtToken(user.getId().toString());
|
|
|
+ redisTemplate.opsForValue().set("userToken:",token);
|
|
|
+ return ResultVo.success("登录成功");
|
|
|
+ }
|
|
|
+ return ResultVo.error("用户名或密码有误");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|