From 911867d5a589d010c698261a6b04b0f59cb6150b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Mon, 17 Jan 2022 17:09:27 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=20=E5=AF=B9=E6=8E=A5=E5=A4=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BD=93=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/domain/entity/SysUser.java | 6 +++ .../common/core/domain/model/LoginUser.java | 6 ++- .../core/domain/model/RegisterBody.java | 36 ++++++++----- .../java/com/ruoyi/common/enums/UserType.java | 7 ++- .../com/ruoyi/common/helper/LoginHelper.java | 8 ++- .../ruoyi/system/service/SysLoginService.java | 51 ++++++++++++------- .../system/service/SysRegisterService.java | 8 ++- .../resources/mapper/system/SysUserMapper.xml | 2 + ruoyi-ui/src/views/register.vue | 4 +- script/sql/ry_20210908.sql | 6 +-- script/sql/test.sql | 6 +-- 11 files changed, 93 insertions(+), 47 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysUser.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysUser.java index 09b82e99..bc3170d3 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysUser.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/entity/SysUser.java @@ -64,6 +64,12 @@ public class SysUser extends BaseEntity { @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符") private String nickName; + /** + * 用户类型(sys_user系统用户) + */ + @ApiModelProperty(value = "用户类型") + private String userType; + /** * 用户邮箱 */ diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java index 215eb57c..c93bd1cf 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java @@ -1,5 +1,6 @@ package com.ruoyi.common.core.domain.model; +import com.ruoyi.common.helper.LoginHelper; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; @@ -85,8 +86,11 @@ public class LoginUser implements Serializable { */ private String username; + /** + * 获取登录id + */ public String getLoginId() { - return userType + userId; + return userType + LoginHelper.JOIN_CODE + userId; } } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java index ce6c385a..b034dc1d 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/RegisterBody.java @@ -1,13 +1,23 @@ -package com.ruoyi.common.core.domain.model; - -import io.swagger.annotations.ApiModel; - -/** - * 用户注册对象 - * - * @author Lion Li - */ -@ApiModel("用户注册对象") -public class RegisterBody extends LoginBody { - -} +package com.ruoyi.common.core.domain.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +/** + * 用户注册对象 + * + * @author Lion Li + */ +@Data +@EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) +@ApiModel("用户注册对象") +public class RegisterBody extends LoginBody { + + @ApiModelProperty(value = "用户类型") + private String userType; + +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/enums/UserType.java b/ruoyi-common/src/main/java/com/ruoyi/common/enums/UserType.java index a5f12f02..ec6510d9 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/enums/UserType.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/enums/UserType.java @@ -1,13 +1,12 @@ package com.ruoyi.common.enums; -import com.ruoyi.common.exception.UtilException; import com.ruoyi.common.utils.StringUtils; import lombok.AllArgsConstructor; import lombok.Getter; /** * 设备类型 - * 针对两套 用户体系 + * 针对多套 用户体系 * * @author Lion Li */ @@ -18,12 +17,12 @@ public enum UserType { /** * pc端 */ - SYS_USER("sys_user:"), + SYS_USER("sys_user"), /** * app端 */ - APP_USER("app_user:"); + APP_USER("app_user"); private final String userType; diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/helper/LoginHelper.java b/ruoyi-common/src/main/java/com/ruoyi/common/helper/LoginHelper.java index ddd50188..66b5854a 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/helper/LoginHelper.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/helper/LoginHelper.java @@ -19,7 +19,9 @@ import lombok.NoArgsConstructor; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class LoginHelper { - private static final String LOGIN_USER_KEY = "loginUser"; + public static final String JOIN_CODE = ":"; + public static final String LOGIN_USER_KEY = "loginUser"; + private static final ThreadLocal LOGIN_CACHE = new ThreadLocal<>(); /** @@ -80,7 +82,9 @@ public class LoginHelper { String userId = null; for (UserType value : UserType.values()) { if (StringUtils.contains(loginId, value.getUserType())) { - userId = StringUtils.replace(loginId, value.getUserType(), StringUtils.EMPTY); + String[] strs = StringUtils.split(loginId, JOIN_CODE); + // 用户id在总是在最后 + userId = strs[strs.length - 1]; } } if (StringUtils.isBlank(userId)) { diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java index 1c7babf5..cd2b0190 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java @@ -8,7 +8,6 @@ import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.core.service.LogininforService; import com.ruoyi.common.enums.DeviceType; import com.ruoyi.common.enums.UserStatus; -import com.ruoyi.common.enums.UserType; import com.ruoyi.common.exception.user.CaptchaException; import com.ruoyi.common.exception.user.CaptchaExpireException; import com.ruoyi.common.exception.user.UserException; @@ -61,17 +60,8 @@ public class SysLoginService { throw new UserException("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME); } - SysUser user = userService.selectUserByUserName(username); - if (StringUtils.isNull(user)) { - log.info("登录用户:{} 不存在.", username); - throw new UserException("user.not.exists", username); - } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) { - log.info("登录用户:{} 已被删除.", username); - throw new UserException("user.password.delete", username); - } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) { - log.info("登录用户:{} 已被停用.", username); - throw new UserException("user.blocked", username); - } + SysUser user = loadUserByUsername(username); + if (!SecurityUtils.matchesPassword(password, user.getPassword())) { // 是否第一次 errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1; @@ -93,13 +83,7 @@ public class SysLoginService { asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request); recordLoginInfo(user.getUserId(), username); - LoginUser loginUser = new LoginUser(); - loginUser.setUserId(user.getUserId()); - loginUser.setDeptId(user.getDeptId()); - loginUser.setUsername(user.getUserName()); - loginUser.setUserType(UserType.SYS_USER.getUserType()); - loginUser.setMenuPermission(permissionService.getMenuPermission(user)); - loginUser.setRolePermission(permissionService.getRolePermission(user)); + LoginUser loginUser = buildLoginUser(user); // 生成token LoginHelper.loginByDevice(loginUser, DeviceType.PC); @@ -127,6 +111,35 @@ public class SysLoginService { } } + private SysUser loadUserByUsername(String username) { + SysUser user = userService.selectUserByUserName(username); + if (StringUtils.isNull(user)) { + log.info("登录用户:{} 不存在.", username); + throw new UserException("user.not.exists", username); + } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) { + log.info("登录用户:{} 已被删除.", username); + throw new UserException("user.password.delete", username); + } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) { + log.info("登录用户:{} 已被停用.", username); + throw new UserException("user.blocked", username); + } + return user; + } + + /** + * 构建登录用户 + */ + private LoginUser buildLoginUser(SysUser user) { + LoginUser loginUser = new LoginUser(); + loginUser.setUserId(user.getUserId()); + loginUser.setDeptId(user.getDeptId()); + loginUser.setUsername(user.getUserName()); + loginUser.setUserType(user.getUserType()); + loginUser.setMenuPermission(permissionService.getMenuPermission(user)); + loginUser.setRolePermission(permissionService.getRolePermission(user)); + return loginUser; + } + /** * 记录登录信息 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java index 415a72bc..8c5cfff3 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java @@ -5,6 +5,7 @@ import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.RegisterBody; import com.ruoyi.common.core.service.LogininforService; +import com.ruoyi.common.enums.UserType; import com.ruoyi.common.exception.user.CaptchaException; import com.ruoyi.common.exception.user.CaptchaExpireException; import com.ruoyi.common.utils.MessageUtils; @@ -32,7 +33,11 @@ public class SysRegisterService { * 注册 */ public String register(RegisterBody registerBody) { - String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword(); + String msg = ""; + String username = registerBody.getUsername(); + String password = registerBody.getPassword(); + // 校验用户类型是否存在 + String userType = UserType.getUserType(registerBody.getUserType()).getUserType(); boolean captchaOnOff = configService.selectCaptchaOnOff(); // 验证码开关 @@ -57,6 +62,7 @@ public class SysRegisterService { sysUser.setUserName(username); sysUser.setNickName(username); sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword())); + sysUser.setUserType(userType); boolean regFlag = userService.registerUser(sysUser); if (!regFlag) { msg = "注册失败,请联系系统管理人员"; diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml index 8c2437e9..7f12db7f 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml @@ -9,6 +9,7 @@ + @@ -50,6 +51,7 @@ u.dept_id, u.user_name, u.nick_name, + u.user_type, u.email, u.avatar, u.phonenumber, diff --git a/ruoyi-ui/src/views/register.vue b/ruoyi-ui/src/views/register.vue index 4fc9b9cf..c9f1efbb 100644 --- a/ruoyi-ui/src/views/register.vue +++ b/ruoyi-ui/src/views/register.vue @@ -124,7 +124,9 @@ export default { this.$refs.registerForm.validate(valid => { if (valid) { this.loading = true; - register(this.registerForm).then(res => { + let registerForm = this.registerForm; + registerForm.userType = "sys_user" + register(registerForm).then(res => { const username = this.registerForm.username; this.$alert("恭喜你,您的账号 " + username + " 注册成功!", '系统提示', { dangerouslyUseHTMLString: true, diff --git a/script/sql/ry_20210908.sql b/script/sql/ry_20210908.sql index e7012e71..fd78de96 100644 --- a/script/sql/ry_20210908.sql +++ b/script/sql/ry_20210908.sql @@ -44,7 +44,7 @@ create table sys_user ( dept_id bigint(20) default null comment '部门ID', user_name varchar(30) not null comment '用户账号', nick_name varchar(30) not null comment '用户昵称', - user_type varchar(2) default '00' comment '用户类型(00系统用户)', + user_type varchar(10) default 'sys_user' comment '用户类型(sys_user系统用户)', email varchar(50) default '' comment '用户邮箱', phonenumber varchar(11) default '' comment '手机号码', sex char(1) default '0' comment '用户性别(0男 1女 2未知)', @@ -65,8 +65,8 @@ create table sys_user ( -- ---------------------------- -- 初始化-用户信息表数据 -- ---------------------------- -insert into sys_user values(1, 103, 'admin', '若依', '00', 'ry@163.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), '', null, '管理员'); -insert into sys_user values(2, 105, 'ry', '若依', '00', 'ry@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), '', null, '测试员'); +insert into sys_user values(1, 103, 'admin', '疯狂的狮子Li', 'sys_user', 'crazyLionLi@163.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), '', null, '管理员'); +insert into sys_user values(2, 105, 'lionli', '疯狂的狮子Li', 'sys_user', 'crazyLionLi@163.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), '', null, '测试员'); -- ---------------------------- diff --git a/script/sql/test.sql b/script/sql/test.sql index 601360a7..41ed7eeb 100644 --- a/script/sql/test.sql +++ b/script/sql/test.sql @@ -33,6 +33,9 @@ CREATE TABLE test_tree PRIMARY KEY (id) USING BTREE ) ENGINE = InnoDB COMMENT = '测试树表'; +INSERT INTO sys_user(user_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time, remark) VALUES (3, 108, 'test', '本部门及以下 密码666', 'sys_user', '', '', '0', '', '$2a$10$M6tZRpUZbWKq11O/z6YISePQc./Jhru8E18mmVJTr9aV8whzfjacC', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), 'test', sysdate(), NULL); +INSERT INTO sys_user(user_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time, remark) VALUES (4, 102, 'test1', '仅本人 密码666', 'sys_user', '', '', '0', '', '$2a$10$yBSXp5Ba1m402cxXTPSy4eXUO8CXCGvXfquNVP/XMWwZ8nf9GaoMy', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), 'test1', sysdate(), NULL); + INSERT INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) VALUES (5, '测试菜单', 0, 5, 'demo', NULL, 1, 0, 'M', '0', '0', NULL, 'star', 'admin', '2021-05-30 00:34:26', NULL, NULL, ''); INSERT INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) VALUES (1500, '测试单表', 5, 1, 'demo', 'demo/demo/index', 1, 0, 'C', '0', '0', 'demo:demo:list', '#', 'admin', '2021-05-30 00:39:23', '', NULL, '测试单表菜单'); @@ -136,9 +139,6 @@ INSERT INTO sys_role_menu(role_id, menu_id) VALUES (4, 1509); INSERT INTO sys_role_menu(role_id, menu_id) VALUES (4, 1510); INSERT INTO sys_role_menu(role_id, menu_id) VALUES (4, 1511); -INSERT INTO sys_user(user_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time, remark) VALUES (3, 108, 'test', '本部门及以下 密码666', '00', '', '', '0', '', '$2a$10$M6tZRpUZbWKq11O/z6YISePQc./Jhru8E18mmVJTr9aV8whzfjacC', '0', '0', '127.0.0.1', '2021-05-30 02:00:37', 'admin', '2021-04-22 09:50:41', 'test', '2021-05-30 02:00:37', NULL); -INSERT INTO sys_user(user_id, dept_id, user_name, nick_name, user_type, email, phonenumber, sex, avatar, password, status, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time, remark) VALUES (4, 102, 'test1', '仅本人 密码666', '00', '', '', '0', '', '$2a$10$yBSXp5Ba1m402cxXTPSy4eXUO8CXCGvXfquNVP/XMWwZ8nf9GaoMy', '0', '0', '127.0.0.1', '2021-05-30 01:48:03', 'admin', '2021-05-30 01:16:02', 'test1', '2021-05-30 01:48:03', NULL); - INSERT INTO sys_user_role(user_id, role_id) VALUES (3, 3); INSERT INTO sys_user_role(user_id, role_id) VALUES (4, 4);