增加用户登录接口

master
pan 5 years ago
parent 8d56591cf5
commit bda3bfe16e
  1. 3
      src/main/java/com/share/help/Constants.java
  2. 12
      src/main/java/com/share/help/controller/UserController.java
  3. 7
      src/main/java/com/share/help/form/UserLoginForm.java
  4. 3
      src/main/java/com/share/help/res/JSONResponse.java
  5. 45
      src/main/java/com/share/help/res/account/LoginRes.java
  6. 3
      src/main/java/com/share/help/res/account/RegisterRes.java
  7. 15
      src/main/java/com/share/help/service/UserService.java

@ -11,5 +11,6 @@ public class Constants {
//用户注册接口 //用户注册接口
public static final String USER_INTERFACE_REGISTER="/register"; public static final String USER_INTERFACE_REGISTER="/register";
//用户登陆接口
public static final String USER_INTERFACE_LOGIN = "/login";
} }

@ -1,14 +1,17 @@
package com.share.help.controller; package com.share.help.controller;
import com.share.help.Constants; import com.share.help.Constants;
import com.share.help.form.UserLoginForm;
import com.share.help.form.UserRegisterForm; import com.share.help.form.UserRegisterForm;
import com.share.help.res.JSONResponse; import com.share.help.res.JSONResponse;
import com.share.help.res.Result; import com.share.help.res.Result;
import com.share.help.res.account.LoginRes;
import com.share.help.res.account.RegisterRes; import com.share.help.res.account.RegisterRes;
import com.share.help.service.UserService; import com.share.help.service.UserService;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; 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.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -41,4 +44,13 @@ public class UserController {
} }
} }
@PostMapping(Constants.USER_INTERFACE_LOGIN)
public JSONResponse<LoginRes.Res,LoginRes> login(UserLoginForm userLoginForm){
if(StringUtils.isNoneEmpty(userLoginForm.getUserId(),userLoginForm.getPassword())){
return userService.login(userLoginForm);
}else{
return new JSONResponse<>(LoginRes.Res.form_error,Result.FAIL);
}
}
} }

@ -0,0 +1,7 @@
package com.share.help.form;
import com.share.help.entity.UserEntity;
public class UserLoginForm extends UserEntity {
}

@ -41,8 +41,9 @@ public class JSONResponse<E extends Enum<E>,L> {
return body; return body;
} }
public void setBody(L body) { public JSONResponse<E,L> setBody(L body) {
this.body = body; this.body = body;
return this;
} }
public JSONResponse(Enum<E> customResult, Result result) { public JSONResponse(Enum<E> customResult, Result result) {

@ -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
}
}

@ -1,5 +1,8 @@
package com.share.help.res.account; package com.share.help.res.account;
/**
* 注册结果
*/
public enum RegisterRes { public enum RegisterRes {
//注册成功 //注册成功
ok, ok,

@ -2,9 +2,11 @@ package com.share.help.service;
import com.share.help.dao.UserMapper; import com.share.help.dao.UserMapper;
import com.share.help.entity.UserEntity; import com.share.help.entity.UserEntity;
import com.share.help.form.UserLoginForm;
import com.share.help.form.UserRegisterForm; import com.share.help.form.UserRegisterForm;
import com.share.help.res.JSONResponse; import com.share.help.res.JSONResponse;
import com.share.help.res.Result; import com.share.help.res.Result;
import com.share.help.res.account.LoginRes;
import com.share.help.res.account.RegisterRes; import com.share.help.res.account.RegisterRes;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -26,6 +28,7 @@ public class UserService extends ImageService{
* @return 返回注册结果 * @return 返回注册结果
*/ */
public JSONResponse<RegisterRes,Void> register(UserRegisterForm userRegisterForm){ public JSONResponse<RegisterRes,Void> register(UserRegisterForm userRegisterForm){
// 用户id已存在,注册失败
if(userMapper.hasUser(userRegisterForm.getUserId())){ if(userMapper.hasUser(userRegisterForm.getUserId())){
return new JSONResponse<>(RegisterRes.user_repeat,Result.FAIL); return new JSONResponse<>(RegisterRes.user_repeat,Result.FAIL);
} }
@ -53,4 +56,16 @@ public class UserService extends ImageService{
userEntity.setChatStatus(false); userEntity.setChatStatus(false);
return userMapper.insert(userEntity)?new JSONResponse<>(RegisterRes.ok, Result.OK):new JSONResponse<>(RegisterRes.fail, Result.FAIL); return userMapper.insert(userEntity)?new JSONResponse<>(RegisterRes.ok, Result.OK):new JSONResponse<>(RegisterRes.fail, Result.FAIL);
} }
/**
*
* @param userLoginForm 登陆表单
* @return 返回登录结果
*/
public JSONResponse<LoginRes.Res,LoginRes> 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,LoginRes>(LoginRes.Res.ok,Result.OK).setBody(new LoginRes(userEntity));
}
} }

Loading…
Cancel
Save