后台搭建 统一消息返回

pull/1/MERGE
Chengliang 3 years ago
parent 0e8cf02581
commit b6c920dc64

15
.gitignore vendored

@ -0,0 +1,15 @@
/weaver-hrm-organization.iml
/out/
/.idea/
HELP.md
target/
### IntelliJ IDEA ###
.idea
/src/test
/src/META-INF
/log

@ -0,0 +1,14 @@
package com.api.organization.web;
import javax.ws.rs.Path;
/**
* @Author weaver_cl
* @Description: TODO
* @Date 2022/4/26
* @Version V1.0
**/
@Path("/bs/hrmorganization/demo")
public class DemoController extends com.engine.organization.web.DemoController {
}

@ -0,0 +1,24 @@
package com.engine.organization.entity.po;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author weaver_cl
* @Description: TODO
* @Date 2022/4/26
* @Version V1.0
**/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DemoUser {
private String username;
private Integer age;
}

@ -0,0 +1,18 @@
package com.engine.organization.exception;
/**
* @Author weaver_cl
* @Description: TODO
* @Date 2022/4/26
* @Version V1.0
**/
public class OrganizationRunTimeException extends RuntimeException{
public OrganizationRunTimeException(String message) {
super(message);
}
public OrganizationRunTimeException(Throwable cause) {
super(cause);
}
}

@ -0,0 +1,162 @@
package com.engine.organization.util;
import com.engine.organization.exception.OrganizationRunTimeException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.util.Collection;
import java.util.Map;
/**
* @Author weaver_cl
* @Description: TODO
* @Date 2022/4/26
* @Version V1.0
**/
public abstract class OrganizationAssert {
/**
* null
*
* @param object
* @param message
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new OrganizationRunTimeException(message);
}
}
/**
* null
*
* @param message
* @param objects
*/
public static void notNull(String message, Object... objects) {
for (Object obj : objects) {
if (obj == null) {
throw new OrganizationRunTimeException(message);
}
}
}
/**
* null
*
* @param object
* @param message
*/
public static void isNull(Object object, String message) {
if (object != null) {
throw new OrganizationRunTimeException(message);
}
}
/**
*
*
* @param collection
* @param message
*/
public static void isEmpty(Collection<?> collection, String message) {
if (!CollectionUtils.isEmpty(collection)) {
throw new OrganizationRunTimeException(message);
}
}
/**
*
*
* @param collection
* @param message
*/
public static void notEmpty(Collection<?> collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new OrganizationRunTimeException(message);
}
}
/**
*
*
* @param arr
* @param message
*/
public static void notEmpty(Object[] arr, String message) {
if (ObjectUtils.isEmpty(arr)) {
throw new OrganizationRunTimeException(message);
}
}
/**
* map
*
* @param map
* @param message
*/
public static void notEmpty(Map<?, ?> map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new OrganizationRunTimeException(message);
}
}
/**
*
*
* @param arr
* @param message
*/
public static void notNullElement(Object[] arr, String message) {
if (arr != null) {
for (Object obj : arr) {
if (obj == null) {
throw new OrganizationRunTimeException(message);
}
}
}
}
/**
* boolean
*
* @param expression
* @param message
*/
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new OrganizationRunTimeException(message);
}
}
public static void isFalse(boolean expression, String message) {
if (expression) {
throw new OrganizationRunTimeException(message);
}
}
public static void isBlank(CharSequence cs, String message) {
int strLen;
if (cs != null && (strLen = cs.length()) != 0) {
for (int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(cs.charAt(i))) {
throw new OrganizationRunTimeException(message);
}
}
}
}
public static void notBlank(CharSequence cs, String message) {
int strLen;
if (cs != null && (strLen = cs.length()) != 0) {
for (int i = 0; i < strLen; ++i) {
if (Character.isWhitespace(cs.charAt(i))) {
throw new OrganizationRunTimeException(message);
}
}
}
if (cs == null || cs.length() == 0) {
throw new OrganizationRunTimeException(message);
}
}
}

@ -0,0 +1,38 @@
package com.engine.organization.util.response;
public enum ResultCode implements IErrorCode {
SUCCESS(200,"操作成功"),
FAILED(500,"操作失败"),
VALIDATE_FAILED(404,"参数检验失败"),
UNAUTHORIZED(401,"暂未登陆或相关token已经过期"),
FORBIDDEN(403,"没有相关权限"),
BODY_NOT_MATCH(400,"请求的数据格式不符!"),
INTERNAL_SERVER_ERROR(500, "服务器内部错误!"),
SERVER_BUSY(503,"服务器正忙,请稍后再试!");
private long code;
private String msg;
ResultCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public long getCode() {
return code;
}
@Override
public String getMessage() {
return msg;
}
}
interface IErrorCode {
long getCode();
String getMessage();
}

@ -0,0 +1,180 @@
package com.engine.organization.util.response;
/**
* @Author weaver_cl
* @Description: TODO
* @Date 2022/4/26
* @Version V1.0
**/
import java.io.Serializable;
public class ReturnResult <T> implements Serializable {
private static final long serialVersionUID = 1L;
private long code;
private T data;
private String msg;
public ReturnResult() {
this.code= ResultCode.SUCCESS.getCode();
this.msg= ResultCode.SUCCESS.getMessage();
}
/**
* @param code
* @param msg
* @param data
*/
public ReturnResult(long code, String msg, T data) {
this.code = code;
this.data = data;
this.msg = msg;
}
/**
* code data
* @param data
*/
public ReturnResult(T data) {
this.code = ResultCode.SUCCESS.getCode();
this.data = data;
}
/**
* code msg
* @param code
* @param msg
*/
public ReturnResult(long code, String msg) {
this.code = code;
this.msg = msg;
}
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ReturnResult Msg(String msg){
this.setMsg(msg);
return this;
}
public ReturnResult Code(long code){
this.setCode(code);
return this;
}
public ReturnResult Data(T data){
this.setData(data);
return this;
}
/**
*
*/
public static <T> ReturnResult<T> successed() {
return new ReturnResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
}
/**
*
*
* @param data
* @return
*/
public static <T> ReturnResult<T> successed(T data) {
return new ReturnResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
}
/**
*
*
* @param errorCode
* @param <T>
* @return
*/
public static <T> ReturnResult<T> failed(IErrorCode errorCode) {
return new ReturnResult<>(errorCode.getCode(), errorCode.getMessage(), null);
}
/**
*
*
* @param msg
* @return
*/
public static <T> ReturnResult<T> failed(String msg) {
return failed(ResultCode.FAILED);
}
/**
*
*
* @param <T>
* @return
*/
public static <T> ReturnResult<T> validateFailed() {
return failed(ResultCode.VALIDATE_FAILED);
}
/**
*
*
* @param msg
* @param <T>
* @return
*/
public static <T> ReturnResult<T> validateFailed(String msg) {
return new ReturnResult<>(ResultCode.VALIDATE_FAILED.getCode(), msg);
}
/**
* token..
*
* @param data
* @param <T>
* @return
*/
public static <T> ReturnResult<T> unauthorized(T data) {
return new ReturnResult<>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
}
/**
*
*
* @param data
* @param <T>
* @return
*/
public static <T> ReturnResult<T> forbiden(T data) {
return new ReturnResult<>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
}
}

@ -0,0 +1,39 @@
package com.engine.organization.web;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.DemoWrapper;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
/**
* @Author weaver_cl
* @Description: TODO
* @Date 2022/4/26
* @Version V1.0
**/
public class DemoController {
public DemoWrapper getDemoWrapper(User user){
return ServiceUtil.getService(DemoWrapper.class,user);
}
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public ReturnResult testDemo(@Context HttpServletRequest request, @Context HttpServletResponse response,
@QueryParam("name") String name) {
User user = HrmUserVarify.getUser(request, response);
return getDemoWrapper(user).testDemo(name);
}
}

@ -0,0 +1,23 @@
package com.engine.organization.wrapper;
import com.engine.core.impl.Service;
import com.engine.organization.entity.po.DemoUser;
import com.engine.organization.util.OrganizationAssert;
import com.engine.organization.util.response.ReturnResult;
/**
* @Author weaver_cl
* @Description: TODO
* @Date 2022/4/26
* @Version V1.0
**/
public class DemoWrapper extends Service {
public ReturnResult testDemo(String name) {
OrganizationAssert.notNull(name,"参数不能为空");
DemoUser demoUser = DemoUser.builder().username("张三").age(12).build();
return ReturnResult.successed(demoUser);
}
}
Loading…
Cancel
Save