feat: 支持邮箱登录

在个人中心-安全设置中绑定邮箱后,才支持邮箱登录
This commit is contained in:
2023-10-24 21:39:38 +08:00
parent d9af44f9fa
commit 17b169eb0e
14 changed files with 239 additions and 44 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.auth.model.request;
import java.io.Serializable;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;
import top.charles7c.cnadmin.common.constant.RegexConsts;
/**
* 邮箱登录信息
*
* @author Charles7c
* @since 2023/10/23 20:15
*/
@Data
@Schema(description = "邮箱登录信息")
public class EmailLoginRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 邮箱
*/
@Schema(description = "邮箱", example = "123456789@qq.com")
@NotBlank(message = "邮箱不能为空")
@Pattern(regexp = RegexConsts.EMAIL, message = "邮箱格式错误")
private String email;
/**
* 验证码
*/
@Schema(description = "验证码", example = "888888")
@NotBlank(message = "验证码不能为空")
@Length(max = 6, message = "验证码非法")
private String captcha;
}

View File

@@ -31,7 +31,7 @@ import me.zhyd.oauth.model.AuthUser;
public interface LoginService {
/**
* 用户登录
* 账号登录
*
* @param username
* 用户名
@@ -39,7 +39,16 @@ public interface LoginService {
* 密码
* @return 令牌
*/
String login(String username, String password);
String accountLogin(String username, String password);
/**
* 邮箱登录
*
* @param email
* 邮箱
* @return 令牌
*/
String emailLogin(String email);
/**
* 三方账号登录

View File

@@ -76,11 +76,19 @@ public class LoginServiceImpl implements LoginService {
private final UserSocialService userSocialService;
@Override
public String login(String username, String password) {
public String accountLogin(String username, String password) {
UserDO user = userService.getByUsername(username);
CheckUtils.throwIfNull(user, "用户名或密码错误");
CheckUtils.throwIfNull(user, "用户名或密码不正确");
Long userId = user.getId();
CheckUtils.throwIfNotEqual(SecureUtils.md5Salt(password, userId.toString()), user.getPassword(), "用户名或密码错误");
CheckUtils.throwIfNotEqual(SecureUtils.md5Salt(password, userId.toString()), user.getPassword(), "用户名或密码不正确");
this.checkUserStatus(user);
return this.login(user);
}
@Override
public String emailLogin(String email) {
UserDO user = userService.getByEmail(email);
CheckUtils.throwIfNull(user, "此邮箱未绑定本系统账号");
this.checkUserStatus(user);
return this.login(user);
}
@@ -187,6 +195,6 @@ public class LoginServiceImpl implements LoginService {
private void checkUserStatus(UserDO user) {
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, user.getStatus(), "此账号已被禁用,如有疑问,请联系管理员");
DeptDetailVO deptDetailVO = deptService.get(user.getDeptId());
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, deptDetailVO.getStatus(), "此账号部门已被禁用,如有疑问,请联系管理员");
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, deptDetailVO.getStatus(), "此账号所属部门已被禁用,如有疑问,请联系管理员");
}
}

View File

@@ -40,6 +40,16 @@ public interface UserMapper extends DataPermissionMapper<UserDO> {
@Select("SELECT * FROM `sys_user` WHERE `username` = #{username}")
UserDO selectByUsername(@Param("username") String username);
/**
* 根据邮箱查询
*
* @param email
* 邮箱
* @return 用户信息
*/
@Select("SELECT * FROM `sys_user` WHERE `email` = #{email}")
UserDO selectByEmail(@Param("email") String email);
/**
* 根据 ID 查询昵称
*

View File

@@ -118,6 +118,15 @@ public interface UserService extends BaseService<UserVO, UserDetailVO, UserQuery
*/
UserDO getByUsername(String username);
/**
* 根据邮箱查询
*
* @param email
* 邮箱
* @return 用户信息
*/
UserDO getByEmail(String email);
/**
* 根据部门 ID 列表查询
*

View File

@@ -244,6 +244,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO,
return baseMapper.selectByUsername(username);
}
@Override
public UserDO getByEmail(String email) {
return baseMapper.selectByEmail(email);
}
@Override
public Long countByDeptIds(List<Long> deptIds) {
return baseMapper.lambdaQuery().in(UserDO::getDeptId, deptIds).count();