mirror of
https://github.com/RemainderTime/spring-boot-base-demo.git
synced 2026-02-20 14:00:54 +08:00
全局统一返回对象实现
This commit is contained in:
22
src/main/java/cn/xf/basedemo/common/enums/SystemStatus.java
Normal file
22
src/main/java/cn/xf/basedemo/common/enums/SystemStatus.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package cn.xf.basedemo.common.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum SystemStatus {
|
||||
|
||||
SUSSES(200, "请求成功"),
|
||||
UNAVAILABILITY(401, "token无效"),
|
||||
ERROR(500, "系统异常")
|
||||
;
|
||||
|
||||
|
||||
private Integer code;
|
||||
|
||||
private String errorMessage;
|
||||
|
||||
SystemStatus(Integer code, String errorMessage) {
|
||||
this.code = code;
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,6 @@ public class LoginUser {
|
||||
private String account;
|
||||
|
||||
private String phone;
|
||||
|
||||
private String token;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.xf.basedemo.common.model;
|
||||
|
||||
import cn.xf.basedemo.common.enums.SystemStatus;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@@ -18,9 +19,47 @@ public class RetObj<T> {
|
||||
|
||||
private T data;
|
||||
|
||||
public RetObj(Integer code, String message, T data) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public RetObj(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public RetObj(SystemStatus status) {
|
||||
this.code = status.getCode();
|
||||
this.message = status.getErrorMessage();
|
||||
}
|
||||
|
||||
public RetObj(SystemStatus status, T data) {
|
||||
this.code = status.getCode();
|
||||
this.message = status.getErrorMessage();
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public RetObj(Integer code, String errorMsg) {
|
||||
this.code = code;
|
||||
this.message = errorMsg;
|
||||
}
|
||||
|
||||
public static <T> RetObj<T> success() {
|
||||
return new RetObj();
|
||||
return new RetObj(SystemStatus.SUSSES);
|
||||
}
|
||||
|
||||
public static <T> RetObj<T> success(T data) {
|
||||
return new RetObj(SystemStatus.SUSSES, data);
|
||||
}
|
||||
|
||||
|
||||
public static <T> RetObj<T> error(SystemStatus status) {
|
||||
return new RetObj(status);
|
||||
}
|
||||
|
||||
public static <T> RetObj<T> error(String errorMsg) {
|
||||
return new RetObj(SystemStatus.ERROR.getCode(), errorMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.xf.basedemo.controller;
|
||||
|
||||
import cn.xf.basedemo.common.model.RetObj;
|
||||
import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||
import cn.xf.basedemo.service.UserService;
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
@@ -28,7 +29,7 @@ public class UserController {
|
||||
@ApiOperation(value = "用户登录", notes = "用户登录")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@PostMapping("/login")
|
||||
public String login(@RequestBody LoginInfoRes res){
|
||||
public RetObj login(@RequestBody LoginInfoRes res){
|
||||
|
||||
return userService.login(res);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.xf.basedemo.service;
|
||||
|
||||
import cn.xf.basedemo.common.model.RetObj;
|
||||
import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||
|
||||
/**
|
||||
@@ -11,5 +12,5 @@ import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||
**/
|
||||
public interface UserService {
|
||||
|
||||
String login(LoginInfoRes res);
|
||||
RetObj login(LoginInfoRes res);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.xf.basedemo.service.impl;
|
||||
|
||||
import cn.xf.basedemo.common.model.LoginInfo;
|
||||
import cn.xf.basedemo.common.model.LoginUser;
|
||||
import cn.xf.basedemo.common.model.RetObj;
|
||||
import cn.xf.basedemo.common.utils.JwtTokenUtils;
|
||||
import cn.xf.basedemo.common.utils.RSAUtils;
|
||||
import cn.xf.basedemo.config.GlobalConfig;
|
||||
@@ -11,6 +12,7 @@ import cn.xf.basedemo.model.res.LoginInfoRes;
|
||||
import cn.xf.basedemo.service.UserService;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -45,7 +47,7 @@ public class UserServiceImpl implements UserService {
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Override
|
||||
public String login(LoginInfoRes res) {
|
||||
public RetObj login(LoginInfoRes res) {
|
||||
|
||||
if (Objects.isNull(res) || StringUtils.isEmpty(res.getEncryptedData())) {
|
||||
return null;
|
||||
@@ -56,9 +58,14 @@ public class UserServiceImpl implements UserService {
|
||||
} catch (Exception e) {
|
||||
log.error("解密失败------", e);
|
||||
}
|
||||
LoginInfo loginInfo = objectMapper.convertValue(loginJson, LoginInfo.class);
|
||||
LoginInfo loginInfo = null;
|
||||
try {
|
||||
loginInfo = objectMapper.readValue(loginJson, LoginInfo.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (StringUtils.isNotBlank(loginInfo.check())) {
|
||||
return loginInfo.check();
|
||||
return RetObj.error(loginInfo.check());
|
||||
}
|
||||
//校验登录账号密码
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
@@ -66,7 +73,7 @@ public class UserServiceImpl implements UserService {
|
||||
queryWrapper.eq("password", loginInfo.getPwd());
|
||||
User user = userMapper.selectOne(queryWrapper);
|
||||
if (Objects.isNull(user)) {
|
||||
return "账号或密码错误";
|
||||
return RetObj.error("账号或密码错误");
|
||||
}
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setId(user.getId());
|
||||
@@ -75,10 +82,11 @@ public class UserServiceImpl implements UserService {
|
||||
loginUser.setPhone(user.getPhone());
|
||||
|
||||
String token = JwtTokenUtils.createToken(user.getId());
|
||||
loginUser.setToken(token);
|
||||
|
||||
redisTemplate.opsForValue().set("token:" + token, JSONObject.toJSONString(loginUser), 3600, TimeUnit.SECONDS);
|
||||
redisTemplate.opsForValue().set("user_login_token:" + user.getId(), token, 3600, TimeUnit.SECONDS);
|
||||
|
||||
return "登录成功";
|
||||
return RetObj.success(loginUser);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user