diff --git a/src/main/java/com/share/help/Constants.java b/src/main/java/com/share/help/Constants.java index f312671..1826363 100644 --- a/src/main/java/com/share/help/Constants.java +++ b/src/main/java/com/share/help/Constants.java @@ -11,5 +11,6 @@ public class Constants { //用户注册接口 public static final String USER_INTERFACE_REGISTER="/register"; - + //用户登陆接口 + public static final String USER_INTERFACE_LOGIN = "/login"; } diff --git a/src/main/java/com/share/help/controller/UserController.java b/src/main/java/com/share/help/controller/UserController.java index 2464331..c9f6e3a 100644 --- a/src/main/java/com/share/help/controller/UserController.java +++ b/src/main/java/com/share/help/controller/UserController.java @@ -1,14 +1,17 @@ package com.share.help.controller; import com.share.help.Constants; +import com.share.help.form.UserLoginForm; import com.share.help.form.UserRegisterForm; import com.share.help.res.JSONResponse; import com.share.help.res.Result; +import com.share.help.res.account.LoginRes; import com.share.help.res.account.RegisterRes; import com.share.help.service.UserService; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -41,4 +44,13 @@ public class UserController { } } + + @PostMapping(Constants.USER_INTERFACE_LOGIN) + public JSONResponse login(UserLoginForm userLoginForm){ + if(StringUtils.isNoneEmpty(userLoginForm.getUserId(),userLoginForm.getPassword())){ + return userService.login(userLoginForm); + }else{ + return new JSONResponse<>(LoginRes.Res.form_error,Result.FAIL); + } + } } diff --git a/src/main/java/com/share/help/form/UserLoginForm.java b/src/main/java/com/share/help/form/UserLoginForm.java new file mode 100644 index 0000000..e1a5807 --- /dev/null +++ b/src/main/java/com/share/help/form/UserLoginForm.java @@ -0,0 +1,7 @@ +package com.share.help.form; + +import com.share.help.entity.UserEntity; + +public class UserLoginForm extends UserEntity { + +} diff --git a/src/main/java/com/share/help/res/JSONResponse.java b/src/main/java/com/share/help/res/JSONResponse.java index fe37d7f..8e12755 100644 --- a/src/main/java/com/share/help/res/JSONResponse.java +++ b/src/main/java/com/share/help/res/JSONResponse.java @@ -41,8 +41,9 @@ public class JSONResponse,L> { return body; } - public void setBody(L body) { + public JSONResponse setBody(L body) { this.body = body; + return this; } public JSONResponse(Enum customResult, Result result) { diff --git a/src/main/java/com/share/help/res/account/LoginRes.java b/src/main/java/com/share/help/res/account/LoginRes.java new file mode 100644 index 0000000..dc2d237 --- /dev/null +++ b/src/main/java/com/share/help/res/account/LoginRes.java @@ -0,0 +1,45 @@ +package com.share.help.res.account; + +import com.share.help.entity.UserEntity; + +/** + * 登陆结果 + */ +public class LoginRes { + + public LoginRes(UserEntity userEntity) { + this.userId=userEntity.getUserId(); + this.userType=userEntity.getUserType(); + } + + //用户id + private String userId; + + //用户身份 + private String userType; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserType() { + return userType; + } + + public void setUserType(String userType) { + this.userType = userType; + } + + public enum Res { + // 登陆成功 + ok, + // 账号或密码错误 + valid_error, + // 表单不合法,请联系管理员 + form_error + } +} diff --git a/src/main/java/com/share/help/res/account/RegisterRes.java b/src/main/java/com/share/help/res/account/RegisterRes.java index e12077f..8195919 100644 --- a/src/main/java/com/share/help/res/account/RegisterRes.java +++ b/src/main/java/com/share/help/res/account/RegisterRes.java @@ -1,5 +1,8 @@ package com.share.help.res.account; +/** + * 注册结果 + */ public enum RegisterRes { //注册成功 ok, diff --git a/src/main/java/com/share/help/service/UserService.java b/src/main/java/com/share/help/service/UserService.java index aa0c7ab..cce15bc 100644 --- a/src/main/java/com/share/help/service/UserService.java +++ b/src/main/java/com/share/help/service/UserService.java @@ -2,9 +2,11 @@ package com.share.help.service; import com.share.help.dao.UserMapper; import com.share.help.entity.UserEntity; +import com.share.help.form.UserLoginForm; import com.share.help.form.UserRegisterForm; import com.share.help.res.JSONResponse; import com.share.help.res.Result; +import com.share.help.res.account.LoginRes; import com.share.help.res.account.RegisterRes; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -26,6 +28,7 @@ public class UserService extends ImageService{ * @return 返回注册结果 */ public JSONResponse register(UserRegisterForm userRegisterForm){ +// 用户id已存在,注册失败 if(userMapper.hasUser(userRegisterForm.getUserId())){ return new JSONResponse<>(RegisterRes.user_repeat,Result.FAIL); } @@ -53,4 +56,16 @@ public class UserService extends ImageService{ userEntity.setChatStatus(false); return userMapper.insert(userEntity)?new JSONResponse<>(RegisterRes.ok, Result.OK):new JSONResponse<>(RegisterRes.fail, Result.FAIL); } + + /** + * + * @param userLoginForm 登陆表单 + * @return 返回登录结果 + */ + public JSONResponse login(UserLoginForm userLoginForm){ + UserEntity userEntity=userMapper.findOneWithPwd(userLoginForm.getUserId(),DigestUtils.md5DigestAsHex(userLoginForm.getPassword().getBytes())); + + return userEntity==null?new JSONResponse<>(LoginRes.Res.valid_error,Result.FAIL) + :new JSONResponse(LoginRes.Res.ok,Result.OK).setBody(new LoginRes(userEntity)); + } }