提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.framework.web.service; |
懒 |
2 |
|
|
3 |
import org.springframework.beans.factory.annotation.Autowired; |
|
4 |
import org.springframework.stereotype.Component; |
|
5 |
import com.jcdm.common.constant.CacheConstants; |
|
6 |
import com.jcdm.common.constant.Constants; |
|
7 |
import com.jcdm.common.constant.UserConstants; |
|
8 |
import com.jcdm.common.core.domain.entity.SysUser; |
|
9 |
import com.jcdm.common.core.domain.model.RegisterBody; |
|
10 |
import com.jcdm.common.core.redis.RedisCache; |
|
11 |
import com.jcdm.common.exception.user.CaptchaException; |
|
12 |
import com.jcdm.common.exception.user.CaptchaExpireException; |
|
13 |
import com.jcdm.common.utils.MessageUtils; |
|
14 |
import com.jcdm.common.utils.SecurityUtils; |
|
15 |
import com.jcdm.common.utils.StringUtils; |
|
16 |
import com.jcdm.framework.manager.AsyncManager; |
|
17 |
import com.jcdm.framework.manager.factory.AsyncFactory; |
|
18 |
import com.jcdm.system.service.ISysConfigService; |
|
19 |
import com.jcdm.system.service.ISysUserService; |
|
20 |
|
|
21 |
/** |
|
22 |
* 注册校验方法 |
|
23 |
* |
|
24 |
* @author jc |
|
25 |
*/ |
|
26 |
@Component |
|
27 |
public class SysRegisterService |
|
28 |
{ |
|
29 |
@Autowired |
|
30 |
private ISysUserService userService; |
|
31 |
|
|
32 |
@Autowired |
|
33 |
private ISysConfigService configService; |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private RedisCache redisCache; |
|
37 |
|
|
38 |
/** |
|
39 |
* 注册 |
|
40 |
*/ |
|
41 |
public String register(RegisterBody registerBody) |
|
42 |
{ |
|
43 |
String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword(); |
|
44 |
SysUser sysUser = new SysUser(); |
|
45 |
sysUser.setUserName(username); |
|
46 |
|
|
47 |
// 验证码开关 |
|
48 |
boolean captchaEnabled = configService.selectCaptchaEnabled(); |
|
49 |
if (captchaEnabled) |
|
50 |
{ |
|
51 |
validateCaptcha(username, registerBody.getCode(), registerBody.getUuid()); |
|
52 |
} |
|
53 |
|
|
54 |
if (StringUtils.isEmpty(username)) |
|
55 |
{ |
|
56 |
msg = "用户名不能为空"; |
|
57 |
} |
|
58 |
else if (StringUtils.isEmpty(password)) |
|
59 |
{ |
|
60 |
msg = "用户密码不能为空"; |
|
61 |
} |
|
62 |
else if (username.length() < UserConstants.USERNAME_MIN_LENGTH |
|
63 |
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) |
|
64 |
{ |
|
65 |
msg = "账户长度必须在2到20个字符之间"; |
|
66 |
} |
|
67 |
else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH |
|
68 |
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) |
|
69 |
{ |
|
70 |
msg = "密码长度必须在5到20个字符之间"; |
|
71 |
} |
|
72 |
else if (!userService.checkUserNameUnique(sysUser)) |
|
73 |
{ |
|
74 |
msg = "保存用户'" + username + "'失败,注册账号已存在"; |
|
75 |
} |
|
76 |
else |
|
77 |
{ |
|
78 |
sysUser.setNickName(username); |
|
79 |
sysUser.setPassword(SecurityUtils.encryptPassword(password)); |
|
80 |
boolean regFlag = userService.registerUser(sysUser); |
|
81 |
if (!regFlag) |
|
82 |
{ |
|
83 |
msg = "注册失败,请联系系统管理人员"; |
|
84 |
} |
|
85 |
else |
|
86 |
{ |
|
87 |
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success"))); |
|
88 |
} |
|
89 |
} |
|
90 |
return msg; |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* 校验验证码 |
|
95 |
* |
|
96 |
* @param username 用户名 |
|
97 |
* @param code 验证码 |
|
98 |
* @param uuid 唯一标识 |
|
99 |
* @return 结果 |
|
100 |
*/ |
|
101 |
public void validateCaptcha(String username, String code, String uuid) |
|
102 |
{ |
|
103 |
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, ""); |
|
104 |
String captcha = redisCache.getCacheObject(verifyKey); |
|
105 |
redisCache.deleteObject(verifyKey); |
|
106 |
if (captcha == null) |
|
107 |
{ |
|
108 |
throw new CaptchaExpireException(); |
|
109 |
} |
|
110 |
if (!code.equalsIgnoreCase(captcha)) |
|
111 |
{ |
|
112 |
throw new CaptchaException(); |
|
113 |
} |
|
114 |
} |
|
115 |
} |