add 完成 国际化 功能

2.X
疯狂的狮子li 3 years ago
parent 2ccf189139
commit dc6f7cd691

@ -56,7 +56,7 @@
| 工具类框架 | Hutool、Lombok | [Hutool文档](https://www.hutool.cn/docs/) | 减少代码冗余 增加安全性 | | 工具类框架 | Hutool、Lombok | [Hutool文档](https://www.hutool.cn/docs/) | 减少代码冗余 增加安全性 |
| 代码生成器 | 适配MP、Knife4j规范化代码 | [Hutool文档](https://www.hutool.cn/docs/) | 一键生成前后端代码 | | 代码生成器 | 适配MP、Knife4j规范化代码 | [Hutool文档](https://www.hutool.cn/docs/) | 一键生成前后端代码 |
| 部署方式 | Docker | [Docker文档](https://docs.docker.com/) | 容器编排 一键部署业务集群 | | 部署方式 | Docker | [Docker文档](https://docs.docker.com/) | 容器编排 一键部署业务集群 |
| 国际化(未完成) | SpringMessage | [SpringMVC文档](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc) | Spring标准国际化方案 | | 国际化 | SpringMessage | [SpringMVC文档](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc) | Spring标准国际化方案 |
## 软件架构图 ## 软件架构图

@ -55,6 +55,10 @@ spring:
pathmatch: pathmatch:
# 适配 boot 2.6 路由与 springfox 兼容 # 适配 boot 2.6 路由与 springfox 兼容
matching-strategy: ANT_PATH_MATCHER matching-strategy: ANT_PATH_MATCHER
# 资源信息
messages:
# 国际化资源文件路径
basename: i18n/messages
servlet: servlet:
multipart: multipart:
# 整个请求大小限制 # 整个请求大小限制

@ -26,11 +26,4 @@ public interface RemoteUserService {
*/ */
Boolean registerUserInfo(SysUser sysUser); Boolean registerUserInfo(SysUser sysUser);
/**
*
*
* @param username
* @return
*/
String checkUserNameUnique(String username);
} }

@ -22,16 +22,16 @@ public class LoginBody {
/** /**
* *
*/ */
@NotBlank(message = "用户名不能为空") @NotBlank(message = "{user.username.not.blank}")
@Length(min = UserConstants.USERNAME_MIN_LENGTH, max = UserConstants.USERNAME_MAX_LENGTH, message = "账户长度必须在2到20个字符之间") @Length(min = UserConstants.USERNAME_MIN_LENGTH, max = UserConstants.USERNAME_MAX_LENGTH, message = "{user.username.length.valid}")
@ApiModelProperty(value = "用户名") @ApiModelProperty(value = "用户名")
private String username; private String username;
/** /**
* *
*/ */
@NotBlank(message = "密码不能为空") @NotBlank(message = "{user.password.not.blank}")
@Length(min = UserConstants.PASSWORD_MIN_LENGTH, max = UserConstants.PASSWORD_MAX_LENGTH, message = "密码长度必须在5到20个字符之间") @Length(min = UserConstants.PASSWORD_MIN_LENGTH, max = UserConstants.PASSWORD_MAX_LENGTH, message = "{user.password.length.valid}")
@ApiModelProperty(value = "用户密码") @ApiModelProperty(value = "用户密码")
private String password; private String password;

@ -5,9 +5,10 @@ import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.auth.form.RegisterBody; import com.ruoyi.auth.form.RegisterBody;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.enums.UserType; import com.ruoyi.common.core.enums.UserType;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.exception.user.UserException;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.core.utils.ServletUtils; import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.utils.RedisUtils; import com.ruoyi.common.redis.utils.RedisUtils;
@ -44,8 +45,8 @@ public class SysLoginService {
userInfo = remoteUserService.getUserInfo(username); userInfo = remoteUserService.getUserInfo(username);
if (ObjectUtil.isNull(userInfo)) { if (ObjectUtil.isNull(userInfo)) {
recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在"); recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists", username));
throw new ServiceException("登录用户:" + username + " 不存在"); throw new UserException("user.not.exists", username);
} }
} catch (Exception e) { } catch (Exception e) {
recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()); recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage());
@ -56,9 +57,8 @@ public class SysLoginService {
Integer errorNumber = RedisUtils.getCacheObject(CacheConstants.LOGIN_ERROR + username); Integer errorNumber = RedisUtils.getCacheObject(CacheConstants.LOGIN_ERROR + username);
// 锁定时间内登录 则踢出 // 锁定时间内登录 则踢出
if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(CacheConstants.LOGIN_ERROR_NUMBER)) { if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(CacheConstants.LOGIN_ERROR_NUMBER)) {
String msg = "密码错误次数过多,帐户锁定" + CacheConstants.LOGIN_ERROR_LIMIT_TIME + "分钟"; recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME));
recordLogininfor(username, Constants.LOGIN_FAIL, msg); throw new UserException("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME);
throw new ServiceException(msg, null);
} }
if (!BCrypt.checkpw(password, userInfo.getPassword())) { if (!BCrypt.checkpw(password, userInfo.getPassword())) {
@ -66,26 +66,24 @@ public class SysLoginService {
errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1; errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
// 达到规定错误次数 则锁定登录 // 达到规定错误次数 则锁定登录
if (errorNumber.equals(CacheConstants.LOGIN_ERROR_NUMBER)) { if (errorNumber.equals(CacheConstants.LOGIN_ERROR_NUMBER)) {
String msg = "密码错误次数过多,帐户锁定" + CacheConstants.LOGIN_ERROR_LIMIT_TIME + "分钟";
RedisUtils.setCacheObject(CacheConstants.LOGIN_ERROR + username, errorNumber, CacheConstants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES); RedisUtils.setCacheObject(CacheConstants.LOGIN_ERROR + username, errorNumber, CacheConstants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES);
recordLogininfor(username, Constants.LOGIN_FAIL, msg); recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME));
throw new ServiceException(msg, null); throw new UserException("user.password.retry.limit.exceed", CacheConstants.LOGIN_ERROR_LIMIT_TIME);
} else { } else {
// 未达到规定错误次数 则递增 // 未达到规定错误次数 则递增
String msg = "密码输入错误" + errorNumber + "次";
RedisUtils.setCacheObject(CacheConstants.LOGIN_ERROR + username, errorNumber); RedisUtils.setCacheObject(CacheConstants.LOGIN_ERROR + username, errorNumber);
recordLogininfor(username, Constants.LOGIN_FAIL, msg); recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", errorNumber));
throw new ServiceException(msg, null); throw new UserException("user.password.retry.limit.count", errorNumber);
} }
} }
// 登录成功 清空错误次数 // 登录成功 清空错误次数
RedisUtils.deleteObject(CacheConstants.LOGIN_ERROR + username); RedisUtils.deleteObject(CacheConstants.LOGIN_ERROR + username);
recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功"); recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
return userInfo; return userInfo;
} }
public void logout(String loginName) { public void logout(String loginName) {
recordLogininfor(loginName, Constants.LOGOUT, "退出成功"); recordLogininfor(loginName, Constants.LOGOUT, MessageUtils.message("user.logout.success"));
} }
/** /**
@ -97,9 +95,6 @@ public class SysLoginService {
// 校验用户类型是否存在 // 校验用户类型是否存在
String userType = UserType.getUserType(registerBody.getUserType()).getUserType(); String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
if (UserConstants.NOT_UNIQUE.equals(remoteUserService.checkUserNameUnique(username))) {
throw new ServiceException("保存用户 " + username + " 失败,注册账号已存在");
}
// 注册用户信息 // 注册用户信息
SysUser sysUser = new SysUser(); SysUser sysUser = new SysUser();
sysUser.setUserName(username); sysUser.setUserName(username);
@ -108,9 +103,9 @@ public class SysLoginService {
sysUser.setUserType(userType); sysUser.setUserType(userType);
boolean regFlag = remoteUserService.registerUserInfo(sysUser); boolean regFlag = remoteUserService.registerUserInfo(sysUser);
if (!regFlag) { if (!regFlag) {
throw new ServiceException("注册失败,请联系系统管理人员"); throw new UserException("user.register.error");
} }
recordLogininfor(username, Constants.REGISTER, "注册成功"); recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success"));
} }
/** /**

@ -1,14 +1,18 @@
package com.ruoyi.common.core.exception; package com.ruoyi.common.core.exception;
/** /**
* *
* *
* @author ruoyi * @author Lion Li
*/ */
public class CaptchaException extends RuntimeException { public class CaptchaException extends RuntimeException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public CaptchaException(String msg) { public CaptchaException() {
super(msg); super("user.jcaptcha.error");
} }
}
public CaptchaException(String msg) {
super(msg);
}
}

@ -42,6 +42,7 @@ public final class ServiceException extends RuntimeException {
return detailMessage; return detailMessage;
} }
@Override
public String getMessage() { public String getMessage() {
return message; return message;
} }
@ -59,4 +60,4 @@ public final class ServiceException extends RuntimeException {
this.detailMessage = detailMessage; this.detailMessage = detailMessage;
return this; return this;
} }
} }

@ -1,5 +1,7 @@
package com.ruoyi.common.core.exception.base; package com.ruoyi.common.core.exception.base;
import com.ruoyi.common.core.utils.MessageUtils;
import com.ruoyi.common.core.utils.StringUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@ -49,4 +51,16 @@ public class BaseException extends RuntimeException {
this(null, null, null, defaultMessage); this(null, null, null, defaultMessage);
} }
@Override
public String getMessage() {
String message = null;
if (!StringUtils.isEmpty(code)) {
message = MessageUtils.message(code, args);
}
if (message == null) {
message = defaultMessage;
}
return message;
}
} }

@ -5,12 +5,12 @@ import com.ruoyi.common.core.exception.base.BaseException;
/** /**
* *
* *
* @author ruoyi * @author Lion Li
*/ */
public class UserException extends BaseException { public class UserException extends BaseException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public UserException(String code, Object[] args) { public UserException(String code, Object... args) {
super("user", code, args, null); super("user", code, args, null);
} }
} }

@ -0,0 +1,28 @@
package com.ruoyi.common.core.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* i18n
*
* @author Lion Li
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MessageUtils {
private static final MessageSource MESSAGE_SOURCE = SpringUtils.getBean(MessageSource.class);
/**
* spring messageSource
*
* @param code
* @param args
* @return
*/
public static String message(String code, Object... args) {
return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale());
}
}

@ -0,0 +1,21 @@
package com.ruoyi.common.web.config;
import com.ruoyi.common.web.core.I18nLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
/**
*
*
* @author Lion Li
*/
@Configuration
public class I18nConfig {
@Bean
public LocaleResolver localeResolver() {
return new I18nLocaleResolver();
}
}

@ -0,0 +1,31 @@
package com.ruoyi.common.web.core;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
*
*
* @author Lion Li
*/
public class I18nLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String language = httpServletRequest.getHeader("content-language");
Locale locale = Locale.getDefault();
if (language != null && language.length() > 0) {
String[] split = language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}

@ -1 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration= org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ruoyi.common.web.config.I18nConfig

@ -0,0 +1,39 @@
#错误消息
not.null=* 必须填写
user.jcaptcha.not.blank=验证码不能为空
user.jcaptcha.error=验证码错误
user.jcaptcha.expire=验证码已失效
user.not.exists=对不起, 您的账号:{0} 不存在.
user.password.not.match=用户不存在/密码错误
user.password.retry.limit.count=密码输入错误{0}次
user.password.retry.limit.exceed=密码错误次数过多,帐户锁定{0}分钟
user.password.delete=对不起,您的账号:{0} 已被删除
user.blocked=对不起,您的账号:{0} 已禁用,请联系管理员
role.blocked=角色已封禁,请联系管理员
user.logout.success=退出成功
length.not.valid=长度必须在{min}到{max}个字符之间
user.username.not.blank=用户名不能为空
user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成且必须以非数字开头
user.username.length.valid=账户长度必须在{min}到{max}个字符之间
user.password.not.blank=用户密码不能为空
user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间
user.password.not.valid=* 5-50个字符
user.email.not.valid=邮箱格式错误
user.mobile.phone.number.not.valid=手机号格式错误
user.login.success=登录成功
user.register.success=注册成功
user.register.save.error=保存用户 {0} 失败,注册账号已存在
user.register.error=注册失败,请联系系统管理人员
user.notfound=请重新登录
user.forcelogout=管理员强制退出,请重新登录
user.unknown.error=未知错误,请重新登录
##文件上传消息
upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB
upload.filename.exceed.length=上传的文件名最长{0}个字符
##权限
no.permission=您没有数据的权限,请联系管理员添加权限 [{0}]
no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}]
no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}]
no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}]
no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]

@ -0,0 +1,39 @@
#错误消息
not.null=* Required fill in
user.jcaptcha.not.blank=Captcha cannot be blank
user.jcaptcha.error=Captcha error
user.jcaptcha.expire=Captcha invalid
user.not.exists=Sorry, your account: {0} does not exist
user.password.not.match=User does not exist/Password error
user.password.retry.limit.count=Password input error {0} times
user.password.retry.limit.exceed=Too many password errors, account locked for {0} minutes
user.password.delete=Sorry, your account{0} has been deleted
user.blocked=Sorry, your account: {0} has been disabled. Please contact the administrator
role.blocked=Role disabledplease contact administrators
user.logout.success=Exit successful
length.not.valid=The length must be between {min} and {max} characters
user.username.not.blank=Username cannot be blank
user.username.not.valid=* 2 to 20 chinese characters, letters, numbers or underscores, and must start with a non number
user.username.length.valid=Account length must be between {min} and {max} characters
user.password.not.blank=Password cannot be empty
user.password.length.valid=Password length must be between {min} and {max} characters
user.password.not.valid=* 5-50 characters
user.email.not.valid=Mailbox format error
user.mobile.phone.number.not.valid=Phone number format error
user.login.success=Login successful
user.register.success=Register successful
user.register.save.error=Failed to save user {0}, The registered account already exists
user.register.error=Register failed, please contact system administrator
user.notfound=Please login again
user.forcelogout=The administrator is forced to exitplease login again
user.unknown.error=Unknown error, please login again
##文件上传消息
upload.exceed.maxSize=The uploaded file size exceeds the limit file size<br/>the maximum allowed file size is{0}MB
upload.filename.exceed.length=The maximum length of uploaded file name is {0} characters
##权限
no.permission=You do not have permission to the dataplease contact your administrator to add permissions [{0}]
no.create.permission=You do not have permission to create dataplease contact your administrator to add permissions [{0}]
no.update.permission=You do not have permission to modify dataplease contact your administrator to add permissions [{0}]
no.delete.permission=You do not have permission to delete dataplease contact your administrator to add permissions [{0}]
no.export.permission=You do not have permission to export dataplease contact your administrator to add permissions [{0}]
no.view.permission=You do not have permission to view dataplease contact your administrator to add permissions [{0}]

@ -0,0 +1,39 @@
#错误消息
not.null=* 必须填写
user.jcaptcha.not.blank=验证码不能为空
user.jcaptcha.error=验证码错误
user.jcaptcha.expire=验证码已失效
user.not.exists=对不起, 您的账号:{0} 不存在.
user.password.not.match=用户不存在/密码错误
user.password.retry.limit.count=密码输入错误{0}次
user.password.retry.limit.exceed=密码错误次数过多,帐户锁定{0}分钟
user.password.delete=对不起,您的账号:{0} 已被删除
user.blocked=对不起,您的账号:{0} 已禁用,请联系管理员
role.blocked=角色已封禁,请联系管理员
user.logout.success=退出成功
length.not.valid=长度必须在{min}到{max}个字符之间
user.username.not.blank=用户名不能为空
user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成且必须以非数字开头
user.username.length.valid=账户长度必须在{min}到{max}个字符之间
user.password.not.blank=用户密码不能为空
user.password.length.valid=用户密码长度必须在{min}到{max}个字符之间
user.password.not.valid=* 5-50个字符
user.email.not.valid=邮箱格式错误
user.mobile.phone.number.not.valid=手机号格式错误
user.login.success=登录成功
user.register.success=注册成功
user.register.save.error=保存用户 {0} 失败,注册账号已存在
user.register.error=注册失败,请联系系统管理人员
user.notfound=请重新登录
user.forcelogout=管理员强制退出,请重新登录
user.unknown.error=未知错误,请重新登录
##文件上传消息
upload.exceed.maxSize=上传的文件大小超出限制的文件大小!<br/>允许的文件最大大小是:{0}MB
upload.filename.exceed.length=上传的文件名最长{0}个字符
##权限
no.permission=您没有数据的权限,请联系管理员添加权限 [{0}]
no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}]
no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}]
no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
no.export.permission=您没有导出数据的权限,请联系管理员添加权限 [{0}]
no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]

@ -7,6 +7,7 @@ import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.CaptchaException; import com.ruoyi.common.core.exception.CaptchaException;
import com.ruoyi.common.core.exception.user.CaptchaExpireException;
import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.reflect.ReflectUtils; import com.ruoyi.common.core.utils.reflect.ReflectUtils;
@ -85,17 +86,17 @@ public class ValidateCodeServiceImpl implements ValidateCodeService {
@Override @Override
public void checkCaptcha(String code, String uuid) throws CaptchaException { public void checkCaptcha(String code, String uuid) throws CaptchaException {
if (StringUtils.isEmpty(code)) { if (StringUtils.isEmpty(code)) {
throw new CaptchaException("验证码不能为空"); throw new CaptchaException("user.jcaptcha.not.blank");
} }
if (StringUtils.isEmpty(uuid)) { if (StringUtils.isEmpty(uuid)) {
throw new CaptchaException("验证码已失效"); throw new CaptchaExpireException();
} }
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
String captcha = RedisUtils.getCacheObject(verifyKey); String captcha = RedisUtils.getCacheObject(verifyKey);
RedisUtils.deleteObject(verifyKey); RedisUtils.deleteObject(verifyKey);
if (!code.equalsIgnoreCase(captcha)) { if (!code.equalsIgnoreCase(captcha)) {
throw new CaptchaException("验证码错误"); throw new CaptchaException();
} }
} }
} }

@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.constant.UserConstants; import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.enums.UserStatus; import com.ruoyi.common.core.enums.UserStatus;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.exception.user.UserException;
import com.ruoyi.system.api.RemoteUserService; import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser; import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser; import com.ruoyi.system.api.model.LoginUser;
@ -37,13 +38,13 @@ public class RemoteUserServiceImpl implements RemoteUserService {
public LoginUser getUserInfo(String username) { public LoginUser getUserInfo(String username) {
SysUser sysUser = userService.selectUserByUserName(username); SysUser sysUser = userService.selectUserByUserName(username);
if (ObjectUtil.isNull(sysUser)) { if (ObjectUtil.isNull(sysUser)) {
throw new ServiceException("用户名或密码错误"); throw new UserException("user.not.exists", username);
} }
if (UserStatus.DELETED.getCode().equals(sysUser.getDelFlag())) { if (UserStatus.DELETED.getCode().equals(sysUser.getDelFlag())) {
throw new ServiceException("对不起,您的账号:" + username + " 已被删除"); throw new UserException("user.password.delete", username);
} }
if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) { if (UserStatus.DISABLE.getCode().equals(sysUser.getStatus())) {
throw new ServiceException("对不起,您的账号:" + username + " 已停用"); throw new UserException("user.blocked", username);
} }
// 角色集合 // 角色集合
Set<String> rolePermission = permissionService.getRolePermission(sysUser.getUserId()); Set<String> rolePermission = permissionService.getRolePermission(sysUser.getUserId());
@ -70,13 +71,9 @@ public class RemoteUserServiceImpl implements RemoteUserService {
throw new ServiceException("当前系统没有开启注册功能"); throw new ServiceException("当前系统没有开启注册功能");
} }
if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) { if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) {
throw new ServiceException("保存用户'" + username + "'失败,注册账号已存在"); throw new UserException("user.register.save.error", username);
} }
return userService.registerUser(sysUser); return userService.registerUser(sysUser);
} }
@Override
public String checkUserNameUnique(String username) {
return userService.checkUserNameUnique(username);
}
} }

@ -1,161 +1,163 @@
import axios from 'axios' import axios from 'axios'
import { Notification, MessageBox, Message, Loading } from 'element-ui' import { Notification, MessageBox, Message, Loading } from 'element-ui'
import store from '@/store' import store from '@/store'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode' import errorCode from '@/utils/errorCode'
import { tansParams, blobValidate } from "@/utils/ruoyi"; import { tansParams, blobValidate } from "@/utils/ruoyi";
import cache from '@/plugins/cache' import cache from '@/plugins/cache'
import { saveAs } from 'file-saver' import { saveAs } from 'file-saver'
let downloadLoadingInstance; let downloadLoadingInstance;
// 是否显示重新登录 // 是否显示重新登录
let isReloginShow; let isReloginShow;
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例 // 对应国际化资源文件后缀
const service = axios.create({ axios.defaults.headers['Content-Language'] = 'zh_CN'
// axios中请求配置有baseURL选项表示请求URL公共部分 // 创建axios实例
baseURL: process.env.VUE_APP_BASE_API, const service = axios.create({
// 超时 // axios中请求配置有baseURL选项表示请求URL公共部分
timeout: 10000 baseURL: process.env.VUE_APP_BASE_API,
}) // 超时
timeout: 10000
// request拦截器 })
service.interceptors.request.use(config => {
// 是否需要设置 token // request拦截器
const isToken = (config.headers || {}).isToken === false service.interceptors.request.use(config => {
// 是否需要防止数据重复提交 // 是否需要设置 token
const isRepeatSubmit = (config.headers || {}).repeatSubmit === false const isToken = (config.headers || {}).isToken === false
if (getToken() && !isToken) { // 是否需要防止数据重复提交
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 const isRepeatSubmit = (config.headers || {}).repeatSubmit === false
} if (getToken() && !isToken) {
// get请求映射params参数 config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
if (config.method === 'get' && config.params) { }
let url = config.url + '?' + tansParams(config.params); // get请求映射params参数
url = url.slice(0, -1); if (config.method === 'get' && config.params) {
config.params = {}; let url = config.url + '?' + tansParams(config.params);
config.url = url; url = url.slice(0, -1);
} config.params = {};
if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) { config.url = url;
const requestObj = { }
url: config.url, if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data, const requestObj = {
time: new Date().getTime() url: config.url,
} data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
const sessionObj = cache.session.getJSON('sessionObj') time: new Date().getTime()
if (sessionObj === undefined || sessionObj === null || sessionObj === '') { }
cache.session.setJSON('sessionObj', requestObj) const sessionObj = cache.session.getJSON('sessionObj')
} else { if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
const s_url = sessionObj.url; // 请求地址 cache.session.setJSON('sessionObj', requestObj)
const s_data = sessionObj.data; // 请求数据 } else {
const s_time = sessionObj.time; // 请求时间 const s_url = sessionObj.url; // 请求地址
const interval = 1000; // 间隔时间(ms),小于此时间视为重复提交 const s_data = sessionObj.data; // 请求数据
if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) { const s_time = sessionObj.time; // 请求时间
const message = '数据正在处理,请勿重复提交'; const interval = 1000; // 间隔时间(ms),小于此时间视为重复提交
console.warn(`[${s_url}]: ` + message) if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
return Promise.reject(new Error(message)) const message = '数据正在处理,请勿重复提交';
} else { console.warn(`[${s_url}]: ` + message)
cache.session.setJSON('sessionObj', requestObj) return Promise.reject(new Error(message))
} } else {
} cache.session.setJSON('sessionObj', requestObj)
} }
return config }
}, error => { }
console.log(error) return config
Promise.reject(error) }, error => {
}) console.log(error)
Promise.reject(error)
// 响应拦截器 })
service.interceptors.response.use(res => {
// 未设置状态码则默认成功状态 // 响应拦截器
const code = res.data.code || 200; service.interceptors.response.use(res => {
// 获取错误信息 // 未设置状态码则默认成功状态
const msg = errorCode[code] || res.data.msg || errorCode['default'] const code = res.data.code || 200;
// 二进制数据则直接返回 // 获取错误信息
if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){ const msg = errorCode[code] || res.data.msg || errorCode['default']
return res.data // 二进制数据则直接返回
} if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){
if (code === 401) { return res.data
if (!isReloginShow) { }
isReloginShow = true; if (code === 401) {
MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { if (!isReloginShow) {
confirmButtonText: '重新登录', isReloginShow = true;
cancelButtonText: '取消', MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
type: 'warning' confirmButtonText: '重新登录',
} cancelButtonText: '取消',
).then(() => { type: 'warning'
isReloginShow = false; }
store.dispatch('LogOut').then(() => { ).then(() => {
// 如果是登录页面不需要重新加载 isReloginShow = false;
if (window.location.hash.indexOf("#/login") != 0) { store.dispatch('LogOut').then(() => {
location.href = '/index'; // 如果是登录页面不需要重新加载
} if (window.location.hash.indexOf("#/login") != 0) {
}) location.href = '/index';
}).catch(() => { }
isReloginShow = false; })
}); }).catch(() => {
} isReloginShow = false;
return Promise.reject('无效的会话,或者会话已过期,请重新登录。') });
} else if (code === 500) { }
Message({ return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
message: msg, } else if (code === 500) {
type: 'error' Message({
}) message: msg,
return Promise.reject(new Error(msg)) type: 'error'
} else if (code !== 200) { })
Notification.error({ return Promise.reject(new Error(msg))
title: msg } else if (code !== 200) {
}) Notification.error({
return Promise.reject('error') title: msg
} else { })
return res.data return Promise.reject('error')
} } else {
}, return res.data
error => { }
console.log('err' + error) },
let { message } = error; error => {
if (message == "Network Error") { console.log('err' + error)
message = "后端接口连接异常"; let { message } = error;
} if (message == "Network Error") {
else if (message.includes("timeout")) { message = "后端接口连接异常";
message = "系统接口请求超时"; }
} else if (message.includes("timeout")) {
else if (message.includes("Request failed with status code")) { message = "系统接口请求超时";
message = "系统接口" + message.substr(message.length - 3) + "异常"; }
} else if (message.includes("Request failed with status code")) {
Message({ message = "系统接口" + message.substr(message.length - 3) + "异常";
message: message, }
type: 'error', Message({
duration: 5 * 1000 message: message,
}) type: 'error',
return Promise.reject(error) duration: 5 * 1000
} })
) return Promise.reject(error)
}
// 通用下载方法 )
export function download(url, params, filename) {
downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", }) // 通用下载方法
return service.post(url, params, { export function download(url, params, filename) {
transformRequest: [(params) => { return tansParams(params) }], downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, return service.post(url, params, {
responseType: 'blob' transformRequest: [(params) => { return tansParams(params) }],
}).then(async (data) => { headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
const isLogin = await blobValidate(data); responseType: 'blob'
if (isLogin) { }).then(async (data) => {
const blob = new Blob([data]) const isLogin = await blobValidate(data);
saveAs(blob, filename) if (isLogin) {
} else { const blob = new Blob([data])
const resText = await data.text(); saveAs(blob, filename)
const rspObj = JSON.parse(resText); } else {
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'] const resText = await data.text();
Message.error(errMsg); const rspObj = JSON.parse(resText);
} const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
downloadLoadingInstance.close(); Message.error(errMsg);
}).catch((r) => { }
console.error(r) downloadLoadingInstance.close();
Message.error('下载文件出现错误,请联系管理员!') }).catch((r) => {
downloadLoadingInstance.close(); console.error(r)
}) Message.error('下载文件出现错误,请联系管理员!')
} downloadLoadingInstance.close();
})
export default service }
export default service

@ -1,209 +1,210 @@
<template> <template>
<div class="register"> <div class="register">
<el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form"> <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
<h3 class="title">若依后台管理系统</h3> <h3 class="title">若依后台管理系统</h3>
<el-form-item prop="username"> <el-form-item prop="username">
<el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="账号"> <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="账号">
<svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item prop="password"> <el-form-item prop="password">
<el-input <el-input
v-model="registerForm.password" v-model="registerForm.password"
type="password" type="password"
auto-complete="off" auto-complete="off"
placeholder="密码" placeholder="密码"
@keyup.enter.native="handleRegister" @keyup.enter.native="handleRegister"
> >
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item prop="confirmPassword"> <el-form-item prop="confirmPassword">
<el-input <el-input
v-model="registerForm.confirmPassword" v-model="registerForm.confirmPassword"
type="password" type="password"
auto-complete="off" auto-complete="off"
placeholder="确认密码" placeholder="确认密码"
@keyup.enter.native="handleRegister" @keyup.enter.native="handleRegister"
> >
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item prop="code" v-if="captchaOnOff"> <el-form-item prop="code" v-if="captchaOnOff">
<el-input <el-input
v-model="registerForm.code" v-model="registerForm.code"
auto-complete="off" auto-complete="off"
placeholder="验证码" placeholder="验证码"
style="width: 63%" style="width: 63%"
@keyup.enter.native="handleRegister" @keyup.enter.native="handleRegister"
> >
<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
</el-input> </el-input>
<div class="register-code"> <div class="register-code">
<img :src="codeUrl" @click="getCode" class="register-code-img"/> <img :src="codeUrl" @click="getCode" class="register-code-img"/>
</div> </div>
</el-form-item> </el-form-item>
<el-form-item style="width:100%;"> <el-form-item style="width:100%;">
<el-button <el-button
:loading="loading" :loading="loading"
size="medium" size="medium"
type="primary" type="primary"
style="width:100%;" style="width:100%;"
@click.native.prevent="handleRegister" @click.native.prevent="handleRegister"
> >
<span v-if="!loading"> </span> <span v-if="!loading"> </span>
<span v-else> ...</span> <span v-else> ...</span>
</el-button> </el-button>
<div style="float: right;"> <div style="float: right;">
<router-link class="link-type" :to="'/login'">使用已有账户登录</router-link> <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
</div> </div>
</el-form-item> </el-form-item>
</el-form> </el-form>
<!-- 底部 --> <!-- 底部 -->
<div class="el-register-footer"> <div class="el-register-footer">
<span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span> <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import { getCodeImg, register } from "@/api/login"; import { getCodeImg, register } from "@/api/login";
export default { export default {
name: "Register", name: "Register",
data() { data() {
const equalToPassword = (rule, value, callback) => { const equalToPassword = (rule, value, callback) => {
if (this.registerForm.password !== value) { if (this.registerForm.password !== value) {
callback(new Error("两次输入的密码不一致")); callback(new Error("两次输入的密码不一致"));
} else { } else {
callback(); callback();
} }
}; };
return { return {
codeUrl: "", codeUrl: "",
registerForm: { registerForm: {
username: "", username: "",
password: "", password: "",
confirmPassword: "", confirmPassword: "",
code: "", code: "",
uuid: "" uuid: "",
}, user_type: "sys_user"
registerRules: { },
username: [ registerRules: {
{ required: true, trigger: "blur", message: "请输入您的账号" }, username: [
{ min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' } { required: true, trigger: "blur", message: "请输入您的账号" },
], { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
password: [ ],
{ required: true, trigger: "blur", message: "请输入您的密码" }, password: [
{ min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' } { required: true, trigger: "blur", message: "请输入您的密码" },
], { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
confirmPassword: [ ],
{ required: true, trigger: "blur", message: "请再次输入您的密码" }, confirmPassword: [
{ required: true, validator: equalToPassword, trigger: "blur" } { required: true, trigger: "blur", message: "请再次输入您的密码" },
], { required: true, validator: equalToPassword, trigger: "blur" }
code: [{ required: true, trigger: "change", message: "请输入验证码" }] ],
}, code: [{ required: true, trigger: "change", message: "请输入验证码" }]
loading: false, },
captchaOnOff: true loading: false,
}; captchaOnOff: true
}, };
created() { },
this.getCode(); created() {
}, this.getCode();
methods: { },
getCode() { methods: {
getCodeImg().then(res => { getCode() {
this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff; getCodeImg().then(res => {
if (this.captchaOnOff) { this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
this.codeUrl = "data:image/gif;base64," + res.img; if (this.captchaOnOff) {
this.registerForm.uuid = res.uuid; this.codeUrl = "data:image/gif;base64," + res.img;
} this.registerForm.uuid = res.uuid;
}); }
}, });
handleRegister() { },
this.$refs.registerForm.validate(valid => { handleRegister() {
if (valid) { this.$refs.registerForm.validate(valid => {
this.loading = true; if (valid) {
register(this.registerForm).then(res => { this.loading = true;
const username = this.registerForm.username; register(this.registerForm).then(res => {
this.$alert("<font color='red'>恭喜你,您的账号 " + username + " 注册成功!</font>", '系统提示', { const username = this.registerForm.username;
dangerouslyUseHTMLString: true, this.$alert("<font color='red'>恭喜你,您的账号 " + username + " 注册成功!</font>", '系统提示', {
type: 'success' dangerouslyUseHTMLString: true,
}).then(() => { type: 'success'
this.$router.push("/login"); }).then(() => {
}).catch(() => {}); this.$router.push("/login");
}).catch(() => { }).catch(() => {});
this.loading = false; }).catch(() => {
if (this.captchaOnOff) { this.loading = false;
this.getCode(); if (this.captchaOnOff) {
} this.getCode();
}) }
} })
}); }
} });
} }
}; }
</script> };
</script>
<style rel="stylesheet/scss" lang="scss">
.register { <style rel="stylesheet/scss" lang="scss">
display: flex; .register {
justify-content: center; display: flex;
align-items: center; justify-content: center;
height: 100%; align-items: center;
background-image: url("../assets/images/login-background.jpg"); height: 100%;
background-size: cover; background-image: url("../assets/images/login-background.jpg");
} background-size: cover;
.title { }
margin: 0px auto 30px auto; .title {
text-align: center; margin: 0px auto 30px auto;
color: #707070; text-align: center;
} color: #707070;
}
.register-form {
border-radius: 6px; .register-form {
background: #ffffff; border-radius: 6px;
width: 400px; background: #ffffff;
padding: 25px 25px 5px 25px; width: 400px;
.el-input { padding: 25px 25px 5px 25px;
height: 38px; .el-input {
input { height: 38px;
height: 38px; input {
} height: 38px;
} }
.input-icon { }
height: 39px; .input-icon {
width: 14px; height: 39px;
margin-left: 2px; width: 14px;
} margin-left: 2px;
} }
.register-tip { }
font-size: 13px; .register-tip {
text-align: center; font-size: 13px;
color: #bfbfbf; text-align: center;
} color: #bfbfbf;
.register-code { }
width: 33%; .register-code {
height: 38px; width: 33%;
float: right; height: 38px;
img { float: right;
cursor: pointer; img {
vertical-align: middle; cursor: pointer;
} vertical-align: middle;
} }
.el-register-footer { }
height: 40px; .el-register-footer {
line-height: 40px; height: 40px;
position: fixed; line-height: 40px;
bottom: 0; position: fixed;
width: 100%; bottom: 0;
text-align: center; width: 100%;
color: #fff; text-align: center;
font-family: Arial; color: #fff;
font-size: 12px; font-family: Arial;
letter-spacing: 1px; font-size: 12px;
} letter-spacing: 1px;
.register-code-img { }
height: 38px; .register-code-img {
} height: 38px;
</style> }
</style>

Loading…
Cancel
Save