|
@@ -2,14 +2,17 @@ package com.sf.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.sf.dto.req.UserLoginReqDto;
|
|
|
import com.sf.dto.req.UserRegisterReqDto;
|
|
|
import com.sf.dto.resp.ImgVerifyCodeRespDto;
|
|
|
+import com.sf.dto.resp.UserLoginRespDto;
|
|
|
import com.sf.dto.resp.UserRegisterRespDto;
|
|
|
import com.sf.entity.UserInfo;
|
|
|
import com.sf.mapper.UserInfoMapper;
|
|
|
import com.sf.service.IUserInfoService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.sf.util.CaptchaUtils;
|
|
|
+import com.sf.util.JwtUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.util.Pair;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -35,6 +38,9 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
|
|
|
@Autowired
|
|
|
private UserInfoMapper userInfoMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private JwtUtils jwtUtils;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public ImgVerifyCodeRespDto getImgVerifyCode() {
|
|
@@ -84,7 +90,7 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
|
|
|
.nickName(reqDto.getUsername())
|
|
|
.salt("0")
|
|
|
.accountBalance(0L)
|
|
|
- .status((byte)0)
|
|
|
+ .status((byte) 0)
|
|
|
.createTime(LocalDateTime.now()) // 把当前时间存储进去
|
|
|
.updateTime(LocalDateTime.now())
|
|
|
.build();
|
|
@@ -93,7 +99,42 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> i
|
|
|
respDto.setType(0);
|
|
|
// 在存入数据库后 获取生成的id
|
|
|
respDto.setUid(userInfo.getId());
|
|
|
- respDto.setToken("token_" + userInfo.getId());
|
|
|
+// respDto.setToken("token_" + userInfo.getId());
|
|
|
+ respDto.setToken(jwtUtils.generateToken(userInfo.getId(),"front"));
|
|
|
return respDto;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserLoginRespDto login(UserLoginReqDto userLoginReqDto) {
|
|
|
+ // 判断用户名和密码是否正确
|
|
|
+ // select * from user_info where username='' and password = ''
|
|
|
+ // 可以一起查询 只返回用户名或密码错误
|
|
|
+ // 也可以分开处理 返回用户名错误 还是密码错误
|
|
|
+ // select * from user_info where username=''
|
|
|
+
|
|
|
+ LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(UserInfo::getUsername, userLoginReqDto.getUsername());
|
|
|
+ // 如果有一条 使用selectOne
|
|
|
+ // 如果有多条 使用selectList
|
|
|
+ // 如果只想查询个数 使用selectCount
|
|
|
+ UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
|
|
|
+ if (userInfo == null) {
|
|
|
+ // 用户名不存在
|
|
|
+ return UserLoginRespDto.builder().type(1).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ String pwd = DigestUtils.md5DigestAsHex(userLoginReqDto.getPassword().getBytes(StandardCharsets.UTF_8));
|
|
|
+ if (!userInfo.getPassword().equals(pwd)) {
|
|
|
+ // 密码错误
|
|
|
+ return UserLoginRespDto.builder().type(2).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 登录成功
|
|
|
+// String token = "token_" + userInfo.getId();
|
|
|
+ String token = jwtUtils.generateToken(userInfo.getId(),"front");
|
|
|
+ return UserLoginRespDto.builder().type(0)
|
|
|
+ .uid(userInfo.getId())
|
|
|
+ .nickName(userInfo.getNickName())
|
|
|
+ .token(token).build();
|
|
|
+ }
|
|
|
}
|