项目结构改造

This commit is contained in:
macro
2019-04-20 10:18:46 +08:00
parent cdd332a667
commit b5800856fb
34 changed files with 500 additions and 634 deletions

View File

@@ -0,0 +1,73 @@
package com.macro.mall.common.api;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import java.util.List;
/**
* 分页数据封装类
*/
public class CommonPage<T> {
private Integer pageNum;
private Integer pageSize;
private Long totalPage;
private List<T> list;
/**
* 将PageHelper分页后的list转为分页信息
*/
public static <T> CommonPage<T> restPage(List<T> list) {
CommonPage<T> result = new CommonPage<T>();
PageInfo<T> pageInfo = new PageInfo<T>(list);
result.setTotalPage(pageInfo.getTotal() / pageInfo.getPageSize());
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setList(pageInfo.getList());
return result;
}
/**
* 将SpringData分页后的list转为分页信息
*/
public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
CommonPage<T> result = new CommonPage<T>();
result.setTotalPage((long) pageInfo.getTotalPages());
result.setPageNum(pageInfo.getNumber());
result.setPageSize(pageInfo.getSize());
result.setList(pageInfo.getContent());
return result;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Long getTotalPage() {
return totalPage;
}
public void setTotalPage(Long totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}

View File

@@ -0,0 +1,118 @@
package com.macro.mall.common.api;
/**
* 通用返回对象
*/
public class CommonResult<T> {
private long code;
private String message;
private T data;
/**
* 普通成功返回
*
* @param data 获取的数据
*/
public static <T> CommonResult<T> success(T data) {
CommonResult<T> result = new CommonResult<T>();
result.setCode(ResultCode.SUCCESS.getCode());
result.setMessage(ResultCode.SUCCESS.getMsg());
result.setData(data);
return result;
}
/**
* 普通成功返回
*
* @param data 获取的数据
*/
public static <T> CommonResult<T> success(T data,String message) {
CommonResult<T> result = new CommonResult<T>();
result.setCode(ResultCode.SUCCESS.getCode());
result.setMessage(message);
result.setData(data);
return result;
}
/**
* 通过错误码对象构造返回结果
*/
public static <T> CommonResult<T> failed(IErrorCode errorCode) {
CommonResult<T> result = new CommonResult<T>();
result.setCode(errorCode.getCode());
result.setMessage(errorCode.getMsg());
return result;
}
/**
* 普通失败提示信息
*/
public static <T> CommonResult<T> failed(String message) {
CommonResult<T> result = new CommonResult<T>();
result.setCode(ResultCode.FAILED.getCode());
result.setMessage(message);
return result;
}
/**
* 普通操作失败
*/
public static <T> CommonResult<T> failed() {
return failed(ResultCode.FAILED);
}
/**
* 参数验证失败使用
*/
public static <T> CommonResult<T> validateFailed() {
return failed(ResultCode.VALIDATE_FAILED);
}
/**
* 参数验证失败使用
*/
public static <T> CommonResult<T> validateFailed(String message) {
CommonResult<T> result = new CommonResult<T>();
result.setCode(ResultCode.FAILED.getCode());
result.setMessage(message);
return result;
}
/**
* 用户没有登录
*/
public static <T> CommonResult<T> unauthorized() {
return failed(ResultCode.UNAUTHORIZED);
}
/**
* 用户没有相应权限
*/
public static <T> CommonResult<T> forbidden() {
return failed(ResultCode.UNAUTHORIZED);
}
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}

View File

@@ -0,0 +1,11 @@
package com.macro.mall.common.api;
/**
* 封装API的错误码
* Created by macro on 2019/4/19.
*/
public interface IErrorCode {
long getCode();
String getMsg();
}

View File

@@ -0,0 +1,28 @@
package com.macro.mall.common.api;
/**
* 枚举了一些常用API操作码
* Created by macro on 2019/4/19.
*/
public enum ResultCode implements IErrorCode {
SUCCESS(200, "操作成功"),
FAILED(500, "操作失败"),
VALIDATE_FAILED(404, "参数检验失败"),
UNAUTHORIZED(401, "暂未登录或token已经过期"),
FORBIDDEN(403, "没有相关权限");
private long code;
private String msg;
private ResultCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
public long getCode() {
return code;
}
public String getMsg() {
return msg;
}
}