|
@@ -1,15 +1,17 @@
|
|
|
package com.zhentao.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.zhentao.enums.ApiServerException;
|
|
|
import com.zhentao.exception.AsynException;
|
|
|
+import com.zhentao.tool.SnowflakeIdGenerator;
|
|
|
import com.zhentao.tool.TokenUtils;
|
|
|
import com.zhentao.user.domain.UserLogin;
|
|
|
import com.zhentao.user.dto.*;
|
|
|
-import com.zhentao.user.service.UserLoginService;
|
|
|
import com.zhentao.user.mapper.UserLoginMapper;
|
|
|
+import com.zhentao.user.service.EmailService;
|
|
|
+import com.zhentao.user.service.UserLoginService;
|
|
|
+import com.zhentao.user.service.UserRepository;
|
|
|
import com.zhentao.vo.Result;
|
|
|
import org.redisson.api.RLock;
|
|
|
import org.redisson.api.RedissonClient;
|
|
@@ -17,7 +19,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.DigestUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.UUID;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
@@ -35,82 +39,25 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
|
private RedissonClient redissonClient;
|
|
|
@Autowired
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private UserRepository userRepository;
|
|
|
+ @Autowired
|
|
|
+ private EmailService emailService;
|
|
|
|
|
|
//注册
|
|
|
@Override
|
|
|
public Result register(UserRegister userRegister) {
|
|
|
- //打印用户注册的信息
|
|
|
- System.err.println(userRegister);
|
|
|
- //使用redisson客户端获取分布式锁,确保并发情况下用户注册的安全性
|
|
|
- RLock lock = redissonClient.getLock(userRegister.getPhone() + userRegister.getPassword());
|
|
|
- try {
|
|
|
- boolean b = lock.tryLock(10, 20, TimeUnit.SECONDS);
|
|
|
-
|
|
|
- if(b){
|
|
|
- //用来判断验证码是否正确
|
|
|
- String s = stringRedisTemplate.opsForValue().get(userRegister.getPhone());
|
|
|
- System.err.println("redis取出来的验证码"+s);
|
|
|
-
|
|
|
- //验证码不匹配就抛出异常
|
|
|
- if(!s.equals(userRegister.getCode())){
|
|
|
- throw new AsynException(ApiServerException.NOTE_ERROR);
|
|
|
- }
|
|
|
- //根据手机号查询信息
|
|
|
- QueryWrapper<UserLogin> queryWrapper=new QueryWrapper<>();
|
|
|
- queryWrapper.eq("user_mobile",userRegister.getPhone());
|
|
|
- UserLogin one = this.getOne(queryWrapper);
|
|
|
-
|
|
|
- if(one==null){
|
|
|
- //新用户注册的流程
|
|
|
- UserLogin userLogin=new UserLogin();
|
|
|
- userLogin.setUserMobile(userRegister.getPhone());
|
|
|
- userLogin.setUserUsername(userRegister.getUsername());
|
|
|
- //随机字符串
|
|
|
- String uuid = String.valueOf(UUID.randomUUID());
|
|
|
- userLogin.setSalt(uuid);
|
|
|
-
|
|
|
- //md5加密
|
|
|
- String s1 = DigestUtils.md5DigestAsHex((uuid + userRegister.getPassword()).getBytes());
|
|
|
- userLogin.setUserPassword(s1);
|
|
|
-
|
|
|
- //生成唯一Id
|
|
|
- long l = IdUtil.getSnowflake(1, 1).nextId();
|
|
|
- userLogin.setId(l);
|
|
|
- //进行注册
|
|
|
- System.err.println(userLogin);
|
|
|
- boolean save = this.save(userLogin);
|
|
|
- if(save){
|
|
|
- return Result.OK(save,"注册成功");
|
|
|
- }else{
|
|
|
- return Result.ERR(save,"注册失败");
|
|
|
- }
|
|
|
- }else{
|
|
|
- //老用户更新信息流程
|
|
|
- one.setUserUsername(userRegister.getUsername());
|
|
|
- //随机字符串
|
|
|
- String uuid = String.valueOf(UUID.randomUUID());
|
|
|
- one.setSalt(uuid);
|
|
|
- //md5加密
|
|
|
- String s1 = DigestUtils.md5DigestAsHex((uuid + userRegister.getPassword()).getBytes());
|
|
|
- one.setUserPassword(s1);
|
|
|
-
|
|
|
- //进行更新
|
|
|
- boolean b1 = this.updateById(one);
|
|
|
- if(b1){
|
|
|
- return Result.OK(b1,"注册成功");
|
|
|
- }else{
|
|
|
- return Result.ERR(b1,"注册失败");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }catch (InterruptedException e){
|
|
|
- Thread.currentThread().interrupt();
|
|
|
- }finally {
|
|
|
- //释放锁
|
|
|
- lock.unlock();
|
|
|
+ UserLogin userLogin = new UserLogin();
|
|
|
+ QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("email", userRegister.getEmail());
|
|
|
+ UserLogin user = userLoginMapper.selectOne(queryWrapper);
|
|
|
+ if (user == null) {
|
|
|
+ userLogin.setId(SnowflakeIdGenerator.getSnowId());
|
|
|
+ emailService.sendVerificationEmail(userRegister.getEmail());
|
|
|
+ return Result.OK(null,"邮箱发送成功,请前往邮箱注册");
|
|
|
+ } else {
|
|
|
+ throw new AsynException(ApiServerException.EMAIL_EXIST);
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -122,7 +69,6 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
|
System.err.println("手机号:"+noteDto.getPhone());
|
|
|
System.err.println("验证码:"+randomSixDigit);
|
|
|
stringRedisTemplate.opsForValue().set(noteDto.getPhone(),randomSixDigit+"");
|
|
|
-
|
|
|
return Result.OK(randomSixDigit,"发送成功");
|
|
|
}
|
|
|
//登录
|
|
@@ -209,9 +155,6 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
|
}
|
|
|
// 生成JWT令牌
|
|
|
String jwtToken = TokenUtils.generateToken(one.getId()+"");
|
|
|
- stringRedisTemplate.opsForValue().set(one.getId().toString(),jwtToken);
|
|
|
- System.err.println(jwtToken);
|
|
|
- System.err.println(stringRedisTemplate.opsForValue().get(one.getId().toString()));
|
|
|
// 返回登录成功结果和JWT令牌
|
|
|
return Result.OK("登录成功",jwtToken);
|
|
|
}else {
|
|
@@ -264,6 +207,33 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Result setUsernameAndPassword(EmailDto emailDto) {
|
|
|
+ if(emailDto.getUsername()!=null&&emailDto.getPassword()!=null){
|
|
|
+ if(StringUtils.isEmpty(emailDto.getEmail())){
|
|
|
+ return Result.ERR(null,"请求超时,请重新发送邮箱");
|
|
|
+ }
|
|
|
+ QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("user_username",emailDto.getUsername());
|
|
|
+ UserLogin userLogin = userLoginMapper.selectOne(queryWrapper);
|
|
|
+ if(userLogin!=null){
|
|
|
+ return Result.ERR(null,"用户已存在");
|
|
|
+ }
|
|
|
+ String salt = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ UserLogin user=new UserLogin();
|
|
|
+ user.setId(SnowflakeIdGenerator.getSnowId());
|
|
|
+ user.setSalt(salt);
|
|
|
+ user.setUserUsername(emailDto.getUsername());
|
|
|
+ String s = DigestUtils.md5DigestAsHex((emailDto.getPassword()+salt).getBytes(StandardCharsets.UTF_8));
|
|
|
+ user.setUserPassword(s);
|
|
|
+ user.setEmail(emailDto.getEmail());
|
|
|
+ userLoginMapper.insert(user);
|
|
|
+ return Result.OK(null,"操作成功");
|
|
|
+ }
|
|
|
+ return Result.ERR(null,"用户名或密码不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|