修改登录API逻辑

This commit is contained in:
guo
2024-07-09 22:32:12 +08:00
parent f1dc42fce5
commit e3f27cf975
7 changed files with 75 additions and 16 deletions
@@ -1,9 +1,8 @@
package com.guo.learningprogresstracker.service;
import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.exception.AppException;
public interface LoginService {
String login(String username, String password) throws AppException;
String login(String userId) throws AppException;
}
@@ -2,6 +2,7 @@ package com.guo.learningprogresstracker.service;
import com.guo.learningprogresstracker.domain.entity.UserEntity;
import com.baomidou.mybatisplus.extension.service.IService;
import com.guo.learningprogresstracker.exception.AppException;
/**
* @author guo
@@ -10,4 +11,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface UserService extends IService<UserEntity> {
/**
* 验证账号密码是否匹配
* @param username
* @param password
* @return
*/
String authenticate(String username, String password) throws AppException;
}
@@ -1,9 +1,6 @@
package com.guo.learningprogresstracker.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.domain.entity.UserEntity;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.service.LoginService;
import com.guo.learningprogresstracker.service.UserService;
@@ -14,12 +11,10 @@ import org.springframework.stereotype.Service;
public class LoginServiceImpl implements LoginService {
@Autowired
private UserService userService;
@Override
public String login(String username, String password) throws AppException {
UserEntity userEntity = userService.getOneOpt(Wrappers.<UserEntity>lambdaQuery()
.eq(UserEntity::getUserName, username)
.eq(UserEntity::getUserPassword, password)).orElseThrow(() -> new AppException("账号或密码错误!"));
StpUtil.login(userEntity.getId());
public String login(String userId) throws AppException {
StpUtil.login(userId);
//todo Token只在此处使用了,属于后续优化点
return StpUtil.getTokenInfo().getTokenValue();
}
@@ -1,9 +1,12 @@
package com.guo.learningprogresstracker.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guo.learningprogresstracker.domain.entity.UserEntity;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.service.UserService;
import com.guo.learningprogresstracker.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
@@ -12,9 +15,18 @@ import org.springframework.stereotype.Service;
* @createDate 2024-06-09 15:28:48
*/
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity>
implements UserService{
@Override
public String authenticate(String username, String password) throws AppException {
UserEntity userEntity = this.getOneOpt(Wrappers.<UserEntity>lambdaQuery()
.eq(UserEntity::getUserName, username)
.eq(UserEntity::getUserPassword, password)).orElseThrow(() -> new AppException("账号或密码错误!"));
return userEntity.getId();
}
}