forked from github/cool-admin-java
feat: add email and username login for app user
This commit is contained in:
@@ -179,4 +179,32 @@ public class AppUserLoginController {
|
||||
CoolPreconditions.checkEmpty(password);
|
||||
return R.ok(userLoginService.password(phone, password));
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮箱密码登录
|
||||
*/
|
||||
@TokenIgnore
|
||||
@Operation(summary = "邮箱密码登录")
|
||||
@PostMapping("/email")
|
||||
public R email(@RequestBody LoginParam param) {
|
||||
String email = param.getEmail();
|
||||
String password = param.getPassword();
|
||||
CoolPreconditions.checkEmpty(email);
|
||||
CoolPreconditions.checkEmpty(password);
|
||||
return R.ok(userLoginService.emailLogin(email, password));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户名密码登录
|
||||
*/
|
||||
@TokenIgnore
|
||||
@Operation(summary = "用户名密码登录")
|
||||
@PostMapping("/username")
|
||||
public R username(@RequestBody LoginParam param) {
|
||||
String username = param.getUsername();
|
||||
String password = param.getPassword();
|
||||
CoolPreconditions.checkEmpty(username);
|
||||
CoolPreconditions.checkEmpty(password);
|
||||
return R.ok(userLoginService.usernameLogin(username, password));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,4 +31,10 @@ public class LoginParam {
|
||||
|
||||
/*******密码登录*******/
|
||||
private String password;
|
||||
|
||||
/*******邮箱登录*******/
|
||||
private String email;
|
||||
|
||||
/*******用户名登录*******/
|
||||
private String username;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ public class UserInfoEntity extends BaseEntity<UserInfoEntity> {
|
||||
@ColumnDefine(comment = "手机号")
|
||||
private String phone;
|
||||
|
||||
@UniIndex
|
||||
@ColumnDefine(comment = "邮箱")
|
||||
private String email;
|
||||
|
||||
@ColumnDefine(comment = "性别 0-未知 1-男 2-女", defaultValue = "0")
|
||||
private Integer gender;
|
||||
|
||||
|
||||
@@ -54,4 +54,14 @@ public interface UserLoginService {
|
||||
* 密码登录
|
||||
*/
|
||||
Object password(String phone, String password);
|
||||
|
||||
/**
|
||||
* 邮箱密码登录
|
||||
*/
|
||||
Object emailLogin(String email, String password);
|
||||
|
||||
/**
|
||||
* 用户名密码登录
|
||||
*/
|
||||
Object usernameLogin(String username, String password);
|
||||
}
|
||||
|
||||
@@ -137,6 +137,34 @@ public class UserLoginServiceImpl implements UserLoginService {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object emailLogin(String email, String password) {
|
||||
CoolPreconditions.checkEmpty(email, "邮箱不能为空");
|
||||
CoolPreconditions.checkEmpty(password, "密码不能为空");
|
||||
UserInfoEntity userInfoEntity = userInfoService.getOne(
|
||||
QueryWrapper.create().eq(UserInfoEntity::getEmail, email));
|
||||
CoolPreconditions.checkEmpty(userInfoEntity, "邮箱或密码错误");
|
||||
if (userInfoEntity.getPassword().equals(MD5.create().digestHex(password))) {
|
||||
return generateToken(userInfoEntity, null);
|
||||
}
|
||||
CoolPreconditions.alwaysThrow("邮箱或密码错误");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object usernameLogin(String username, String password) {
|
||||
CoolPreconditions.checkEmpty(username, "用户名不能为空");
|
||||
CoolPreconditions.checkEmpty(password, "密码不能为空");
|
||||
UserInfoEntity userInfoEntity = userInfoService.getOne(
|
||||
QueryWrapper.create().eq(UserInfoEntity::getNickName, username));
|
||||
CoolPreconditions.checkEmpty(userInfoEntity, "用户名或密码错误");
|
||||
if (userInfoEntity.getPassword().equals(MD5.create().digestHex(password))) {
|
||||
return generateToken(userInfoEntity, null);
|
||||
}
|
||||
CoolPreconditions.alwaysThrow("用户名或密码错误");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 前置已校验用户的手机号,
|
||||
* 根据手机号找到用户生成token
|
||||
|
||||
Reference in New Issue
Block a user