From d8585d0ee7a80ca0847e1b9f61396422fee24397 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期二, 27 十二月 2022 09:46:36 +0800
Subject: [PATCH] update 优化 删除 vue3 模板无用参数
---
ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java | 104 ++++++++++++++++++++++++++++++----------------------
1 files changed, 60 insertions(+), 44 deletions(-)
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 045c680..bf9d00d 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
@@ -1,71 +1,67 @@
package com.ruoyi.system.service;
+import cn.dev33.satoken.secure.BCrypt;
+import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
+import com.ruoyi.common.core.domain.event.LogininforEvent;
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.*;
-import org.springframework.beans.factory.annotation.Autowired;
+import com.ruoyi.common.exception.user.UserException;
+import com.ruoyi.common.utils.MessageUtils;
+import com.ruoyi.common.utils.ServletUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.redis.RedisUtils;
+import com.ruoyi.common.utils.spring.SpringUtils;
+import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
+
+import javax.servlet.http.HttpServletRequest;
/**
* 娉ㄥ唽鏍¢獙鏂规硶
*
- * @author ruoyi
+ * @author Lion Li
*/
+@RequiredArgsConstructor
@Service
public class SysRegisterService {
- @Autowired
- private ISysUserService userService;
-
- @Autowired
- private ISysConfigService configService;
-
- @Autowired
- private LogininforService asyncService;
+ private final ISysUserService userService;
+ private final ISysConfigService configService;
/**
* 娉ㄥ唽
*/
- public String register(RegisterBody registerBody) {
- String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword();
+ public void register(RegisterBody registerBody) {
+ HttpServletRequest request = ServletUtils.getRequest();
+ String username = registerBody.getUsername();
+ String password = registerBody.getPassword();
+ // 鏍¢獙鐢ㄦ埛绫诲瀷鏄惁瀛樺湪
+ String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
- boolean captchaOnOff = configService.selectCaptchaOnOff();
+ boolean captchaEnabled = configService.selectCaptchaEnabled();
// 楠岃瘉鐮佸紑鍏�
- if (captchaOnOff) {
- validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
+ if (captchaEnabled) {
+ validateCaptcha(username, registerBody.getCode(), registerBody.getUuid(), request);
}
+ SysUser sysUser = new SysUser();
+ sysUser.setUserName(username);
+ sysUser.setNickName(username);
+ sysUser.setPassword(BCrypt.hashpw(password));
+ sysUser.setUserType(userType);
- if (StringUtils.isEmpty(username)) {
- msg = "鐢ㄦ埛鍚嶄笉鑳戒负绌�";
- } else if (StringUtils.isEmpty(password)) {
- msg = "鐢ㄦ埛瀵嗙爜涓嶈兘涓虹┖";
- } else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
- || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
- msg = "璐︽埛闀垮害蹇呴』鍦�2鍒�20涓瓧绗︿箣闂�";
- } else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
- || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
- msg = "瀵嗙爜闀垮害蹇呴』鍦�5鍒�20涓瓧绗︿箣闂�";
- } else if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username))) {
- msg = "淇濆瓨鐢ㄦ埛'" + username + "'澶辫触锛屾敞鍐岃处鍙峰凡瀛樺湪";
- } else {
- SysUser sysUser = new SysUser();
- sysUser.setUserName(username);
- sysUser.setNickName(username);
- sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword()));
- boolean regFlag = userService.registerUser(sysUser);
- if (!regFlag) {
- msg = "娉ㄥ唽澶辫触,璇疯仈绯荤郴缁熺鐞嗕汉鍛�";
- } else {
- asyncService.recordLogininfor(username, Constants.REGISTER,
- MessageUtils.message("user.register.success"), ServletUtils.getRequest());
- }
+ if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(sysUser))) {
+ throw new UserException("user.register.save.error", username);
}
- return msg;
+ boolean regFlag = userService.registerUser(sysUser);
+ if (!regFlag) {
+ throw new UserException("user.register.error");
+ }
+ recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success"));
}
/**
@@ -76,15 +72,35 @@
* @param uuid 鍞竴鏍囪瘑
* @return 缁撴灉
*/
- public void validateCaptcha(String username, String code, String uuid) {
- String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
+ public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
+ String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
String captcha = RedisUtils.getCacheObject(verifyKey);
RedisUtils.deleteObject(verifyKey);
if (captcha == null) {
+ recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire"));
throw new CaptchaExpireException();
}
if (!code.equalsIgnoreCase(captcha)) {
+ recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error"));
throw new CaptchaException();
}
}
+
+ /**
+ * 璁板綍鐧诲綍淇℃伅
+ *
+ * @param username 鐢ㄦ埛鍚�
+ * @param status 鐘舵��
+ * @param message 娑堟伅鍐呭
+ * @return
+ */
+ private void recordLogininfor(String username, String status, String message) {
+ LogininforEvent logininforEvent = new LogininforEvent();
+ logininforEvent.setUsername(username);
+ logininforEvent.setStatus(status);
+ logininforEvent.setMessage(message);
+ logininforEvent.setRequest(ServletUtils.getRequest());
+ SpringUtils.context().publishEvent(logininforEvent);
+ }
+
}
--
Gitblit v1.9.3