统一响应

public class BaseApiService<T> {
    public BaseResponse<T> setResultError(Integer code, String msg) {
        return this.setResult(code, msg, (Object)null);
    }

    public BaseResponse<T> setResultError(String msg) {
        return this.setResult(Constants.HTTP_RES_CODE_500, msg, (Object)null);
    }

    public BaseResponse<T> setResultSuccess(T data) {
        return this.setResult(Constants.HTTP_RES_CODE_200, "success", data);
    }

    public BaseResponse<T> setResultSuccess() {
        return this.setResult(Constants.HTTP_RES_CODE_200, "success", (Object)null);
    }

    public BaseResponse<T> setResult(Integer code, String msg, T data) {
        return new BaseResponse(code, msg, data);
    }

    public BaseResponse<T> setResultDb(Integer code, String msg, T data) {
        return new BaseResponse(code, msg, data);
    }

    public BaseApiService() {
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BaseApiService)) {
            return false;
        } else {
            BaseApiService<?> other = (BaseApiService)o;
            return other.canEqual(this);
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BaseApiService;
    }

    public int hashCode() {
        int result = true;
        return 1;
    }

    public String toString() {
        return "BaseApiService()";
    }
}
public class BaseResponse<T> {
    private Integer code;
    private String msg;
    private T data;

    public BaseResponse() {
    }

    public BaseResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BaseResponse)) {
            return false;
        } else {
            BaseResponse<?> other = (BaseResponse)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BaseResponse;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $code = this.getCode();
        int result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        return result;
    }

    public String toString() {
        return "BaseResponse(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }
}
@RestController
public class MemberServiceImpl extends BaseApiService implements MemberService {
    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private UserMapper userMapper;

    @Value("${token.prefix}")
    private String loginTokenPrefix;

    @Autowired
    private AsyncLoginLogManage asyncLoginLogManage;

    @Override
    public BaseResponse<JSONObject> register(UserReqRegisterDto userReqRegisterDto) {
        // 参数验证
        String mobile = userReqRegisterDto.getMobile();
        if (StringUtils.isEmpty(mobile)) {
            return setResultError("mobile参数不能为空");
        }
        String passWord = userReqRegisterDto.getPassword();
        if (StringUtils.isEmpty(passWord)) {
            return setResultError("passWord参数不能为空");
        }
        // 先检查该手机号码是否存在
        Object userDbDo = userMapper.existMobile(mobile);
        if (userDbDo != null) {
            return setResultError("该手机号码已经存在");
        }
        // dto转换成do
        // 插入数据库中
        UserDo userDo = dtoToDo(userReqRegisterDto, UserDo.class);
        String newPassWord = MD5Util.MD5(passWord);
        userDo.setPassWord(newPassWord);
        //盐 密码+手机号码、提前定义盐值常量
        int register = userMapper.register(userDo);
//        return register > 0 ? setResultSuccess("注册成功") :
//                setResultError("注册失败");
        return setResult(register, "注册成功", null);
    }


    @Override
    public BaseResponse<JSONObject> login(UserLoginDto userLoginDto) {
       // 参数验证
        String mobile = userLoginDto.getMobile();
        if (StringUtils.isEmpty(mobile)) {
            return setResultError("mobile参数不能为空");
        }
        String passWord = userLoginDto.getPassword();
        if (StringUtils.isEmpty(userLoginDto.getPassword())) {
            return setResultError("passWord参数不能为空");
        }
        // 查询我们的数据库
        String newPassWord = MD5Util.MD5(passWord);
        userLoginDto.setPassword(newPassWord);
        UserDo loginUserDo = userMapper.login(userLoginDto);
        if (loginUserDo == null) {
            return setResultError("手机号码或者密码不正确!");
        }
        //查看是否是登入状态

        //获取userId
        Long userId = loginUserDo.getUserId();
        String userToken = tokenUtils.createToken(loginTokenPrefix, userId + "");

        JSONObject resultJSON = new JSONObject();
        resultJSON.put("userToken", userToken);

        // 写入日志
        log.info(Thread.currentThread().getName() + " 处理流程1");
        asyncLoginLogManage.loginLog(userId, "192.168.212.110", new Date(), userToken
                , "PC", "windows 谷歌浏览器");
        log.info(Thread.currentThread().getName() + " 处理流程3");
        return setResultSuccess(resultJSON);
    }

    @Override
    public BaseResponse<UserRespDto> getUserInfo(UserInfoDto userInfoDto) {
        return null;
    }

}

 

posted on 2020-05-16 20:08  JAVA-ROAD  阅读(435)  评论(0)    收藏  举报

导航