55import info .xiaomo .core .model .UserModel ;
66import info .xiaomo .core .service .UserService ;
77import info .xiaomo .core .untils .MD5 ;
8+ import org .hibernate .service .spi .ServiceException ;
89import org .springframework .beans .factory .annotation .Autowired ;
910import org .springframework .web .bind .annotation .*;
1011
12+ import java .text .ParseException ;
13+ import java .util .Date ;
1114import java .util .HashMap ;
1215
1316/**
@@ -32,9 +35,9 @@ public class UserController extends BaseController {
3235 private UserService service ;
3336
3437 @ RequestMapping (value = "login" , method = RequestMethod .POST )
35- public HashMap <String , Object > login (@ RequestParam String userName , @ RequestParam String password ) {
38+ public HashMap <String , Object > login (@ RequestParam String email , @ RequestParam String password ) {
3639 HashMap <String , Object > result = new HashMap <>();
37- UserModel userModel = service .findUserByUserName ( userName );
40+ UserModel userModel = service .findUserByEmail ( email );
3841 if (userModel == null ) {
3942 result .put (code , notFound );
4043 } else {
@@ -51,7 +54,6 @@ public HashMap<String, Object> login(@RequestParam String userName, @RequestPara
5154
5255 @ RequestMapping (value = "register" , method = RequestMethod .POST )
5356 public HashMap <String , Object > register (
54- @ RequestParam String userName ,
5557 @ RequestParam String nickName ,
5658 @ RequestParam String password ,
5759 @ RequestParam String email ,
@@ -60,15 +62,16 @@ public HashMap<String, Object> register(
6062 @ RequestParam String address
6163 ) {
6264 HashMap <String , Object > result = new HashMap <>();
63- UserModel userModel = service .findUserByUserName ( userName );
65+ UserModel userModel = service .findUserByEmail ( email );
6466 if (userModel != null ) {
6567 result .put (code , error );
6668 } else {
6769 userModel = new UserModel ();
68- userModel .setUserName (userName );
6970 userModel .setNickName (nickName );
7071 userModel .setEmail (email );
7172 userModel .setGender (gender );
73+ userModel .setValidateStatus (0 );//默认未验证
74+ userModel .setValidateCode (MD5 .encode (email ));
7275 userModel .setPhone (phone );
7376 userModel .setAddress (address );
7477 userModel .setPassword (MD5 .encode (password ));
@@ -91,7 +94,6 @@ public UserModel findUserById(@PathVariable("id") Long id) {
9194
9295 @ RequestMapping (value = "update" , method = RequestMethod .POST )
9396 public HashMap <String , Object > update (
94- @ RequestParam String userName ,
9597 @ RequestParam String nickName ,
9698 @ RequestParam String password ,
9799 @ RequestParam String email ,
@@ -100,12 +102,11 @@ public HashMap<String, Object> update(
100102 @ RequestParam String address
101103 ) throws UserNotFoundException {
102104 HashMap <String , Object > result = new HashMap <>();
103- UserModel userModel = service .findUserByUserName ( userName );
105+ UserModel userModel = service .findUserByEmail ( email );
104106 if (userModel != null ) {
105107 result .put (code , error );
106108 } else {
107109 userModel = new UserModel ();
108- userModel .setUserName (userName );
109110 userModel .setNickName (nickName );
110111 userModel .setEmail (email );
111112 userModel .setGender (gender );
@@ -123,4 +124,45 @@ public HashMap<String, Object> update(
123124 return result ;
124125 }
125126
127+ /**
128+ * 处理激活
129+ *
130+ * @throws ParseException
131+ */
132+ @ RequestMapping (value = "validateEmail" , method = RequestMethod .GET )
133+ public void validateEmail (@ RequestParam String email , @ RequestParam String validateCode ) throws ServiceException , ParseException , UserNotFoundException {
134+ //数据访问层,通过email获取用户信息
135+ UserModel user = service .findUserByEmail (email );
136+ //验证用户是否存在
137+ if (user != null ) {
138+ //验证用户激活状态
139+ if (user .getValidateStatus () == 0 ) {
140+ ///没激活
141+ Date currentTime = new Date ();//获取当前时间
142+ //验证链接是否过期
143+ currentTime .before (user .getCreateTime ());
144+ if (currentTime .before (user .getUpdateTime ())) {
145+ //验证激活码是否正确
146+ if (validateCode .equals (user .getValidateCode ())) {
147+ //激活成功, //并更新用户的激活状态,为已激活
148+ System .out .println ("==sq===" + user .getValidateStatus ());
149+ user .setValidateStatus (1 );//把状态改为激活
150+ System .out .println ("==sh===" + user .getValidateStatus ());
151+ service .updateUser (user );
152+ } else {
153+ throw new ServiceException ("激活码不正确" );
154+ }
155+ } else {
156+ throw new ServiceException ("激活码已过期!" );
157+ }
158+ } else {
159+ throw new ServiceException ("邮箱已激活,请登录!" );
160+ }
161+ } else {
162+ throw new ServiceException ("该邮箱未注册(邮箱地址不存在)!" );
163+ }
164+
165+ }
166+
167+
126168}
0 commit comments