You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.6 KiB
Java
135 lines
3.6 KiB
Java
package com.engine.hrmelog.util;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import weaver.hrm.User;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Function;
|
|
import java.util.function.Supplier;
|
|
|
|
/**
|
|
* 请求执行器
|
|
* <p>Copyright: Copyright (c) 2022</p>
|
|
* <p>Company: 泛微软件</p>
|
|
*
|
|
* @author qiantao
|
|
* @version 1.0
|
|
**/
|
|
@Slf4j
|
|
public class ResponseResult<T, R> {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private final User user;
|
|
|
|
public ResponseResult(User user) {
|
|
this.user = user;
|
|
}
|
|
|
|
|
|
/**
|
|
* 统一返回方法
|
|
*/
|
|
public String run(Function<T, R> f, T t) {
|
|
try {
|
|
return Ok(f.apply(t));
|
|
} catch (RuntimeException e) {
|
|
log.error("hrmelog api run fail , param {}", t, e);
|
|
return Error(e.getMessage());
|
|
} catch (Exception e) {
|
|
log.error("hrmelog api run fail , param {}", t, e);
|
|
return Error("系统异常!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 统一返回方法(有参无返回)
|
|
*/
|
|
public String run(Consumer<T> f, T t) {
|
|
try {
|
|
f.accept(t);
|
|
return Ok();
|
|
} catch (RuntimeException e) {
|
|
log.error("hrmelog api run fail , param {}", t, e);
|
|
return Error(e.getMessage());
|
|
} catch (Exception e) {
|
|
log.error("hrmelog api run fail , param {}", t, e);
|
|
return Error("系统异常!", e);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 统一返回方法(无参有返回)
|
|
*/
|
|
public String run(Supplier<R> f) {
|
|
try {
|
|
return Ok(f.get());
|
|
} catch (RuntimeException e) {
|
|
log.error("hrmelog api run fail", e);
|
|
return Error(e.getMessage());
|
|
} catch (Exception e) {
|
|
log.error("hrmelog api run fail", e);
|
|
return Error("系统异常!", e);
|
|
}
|
|
}
|
|
|
|
|
|
private static String getJsonString(Object apidatas) throws JsonProcessingException {
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
return mapper.writeValueAsString(apidatas);
|
|
}
|
|
|
|
/**
|
|
* 成功返回
|
|
*/
|
|
private String Ok() {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("status", true);
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
}
|
|
|
|
|
|
/**
|
|
* 成功返回
|
|
*/
|
|
private String Ok(R r) throws JsonProcessingException {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("status", true);
|
|
apidatas.put("data", r);
|
|
return JSON.toJSONString(apidatas);
|
|
}
|
|
|
|
|
|
/**
|
|
* 失败返回
|
|
*/
|
|
private static String Error(String message) {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("status", false);
|
|
apidatas.put("errormsg", message);
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
}
|
|
|
|
|
|
/**
|
|
* 系统异常失败返回
|
|
*/
|
|
private static String Error(String message, Exception e) {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("status", false);
|
|
apidatas.put("errormsg", message);
|
|
apidatas.put("error", e.getMessage());
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
}
|
|
|
|
}
|