From 34fb245ed04c6598f02647f76812facbc82c7435 Mon Sep 17 00:00:00 2001 From: dxfeng Date: Thu, 29 Aug 2024 11:18:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E7=8E=AF=E5=A2=83=E6=90=AD?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/CustomizeRunTimeException.java | 22 ++ src/com/engine/mzg/util/ExceptionUtil.java | 20 ++ src/com/engine/mzg/util/ResponseResult.java | 192 ++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 src/com/engine/mzg/exception/CustomizeRunTimeException.java create mode 100644 src/com/engine/mzg/util/ExceptionUtil.java create mode 100644 src/com/engine/mzg/util/ResponseResult.java diff --git a/src/com/engine/mzg/exception/CustomizeRunTimeException.java b/src/com/engine/mzg/exception/CustomizeRunTimeException.java new file mode 100644 index 0000000..d90b798 --- /dev/null +++ b/src/com/engine/mzg/exception/CustomizeRunTimeException.java @@ -0,0 +1,22 @@ +package com.engine.mzg.exception; + +/** + * @Author weaver_cl + * @Description: + * @Date 2023/2/21 + * @Version V1.0 + **/ +public class CustomizeRunTimeException extends RuntimeException{ + + public CustomizeRunTimeException(String message) { + super(message); + } + + public CustomizeRunTimeException(Throwable cause) { + super(cause); + } + + public CustomizeRunTimeException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/com/engine/mzg/util/ExceptionUtil.java b/src/com/engine/mzg/util/ExceptionUtil.java new file mode 100644 index 0000000..6b42f1d --- /dev/null +++ b/src/com/engine/mzg/util/ExceptionUtil.java @@ -0,0 +1,20 @@ +package com.engine.mzg.util; + +/** + * @Author weaver_cl + * @Description: + * @Date 2023/2/21 + * @Version V1.0 + **/ +public class ExceptionUtil { + public static String getRealMessage(Throwable e) { + while (e != null) { + Throwable cause = e.getCause(); + if (cause == null) { + return e.getMessage(); + } + e = cause; + } + return ""; + } +} diff --git a/src/com/engine/mzg/util/ResponseResult.java b/src/com/engine/mzg/util/ResponseResult.java new file mode 100644 index 0000000..305f7db --- /dev/null +++ b/src/com/engine/mzg/util/ResponseResult.java @@ -0,0 +1,192 @@ +package com.engine.mzg.util; + + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.engine.core.exception.ECException; +import com.engine.mzg.exception.CustomizeRunTimeException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import weaver.general.BaseBean; +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; + +/** + * 请求执行器 + *

Copyright: Copyright (c) 2022

+ *

Company: 泛微软件

+ * + * @author qiantao + * @version 1.0 + **/ +@Slf4j +public class ResponseResult { + + private static final long serialVersionUID = 1L; + + private final User user; + + private final BaseBean baseBean = new BaseBean(); + + private final Boolean isLog = "true".equals(baseBean.getPropValue("hrmSalary", "log")); + + public ResponseResult(User user) { + this.user = user; + } + + /** + * 统一返回方法(自定义返回格式) + */ + public String customRun(Function f, T t) { + try { + if (isLog) { + log.info("run api , param {}", t); + } + return getJsonString(f.apply(t)); + } catch (CustomizeRunTimeException e) { + log.error("api run fail", e); + return Error(e.getMessage()); + } catch (ECException e) { + log.error("api run fail", e); + Throwable cause = e.getCause(); + return Error(cause.getMessage()); + } catch (Exception e) { + log.error("api run fail", e); + return Error("系统异常!"); + } + } + + /** + * 统一返回方法 + */ + public String run(Function f, T t) { + try { + if (isLog) { + log.info("run api , param {}", t); + } + return Ok(f.apply(t)); + } catch (CustomizeRunTimeException e) { + log.error("api run fail", e); + return Error(e.getMessage()); + } catch (ECException e) { + log.error("api run fail", e); + Throwable cause = e.getCause(); + return Error(cause.getMessage()); + } catch (Exception e) { + log.error("api run fail", e); + return Error("系统异常!"); + } + } + + /** + * 统一返回方法(有参无返回) + */ + public String run(Consumer f, T t) { + try { + if (isLog) { + log.info("run api , param {}", t); + } + f.accept(t); + return Ok(); + } catch (CustomizeRunTimeException e) { + log.error("api run fail", e); + return Error(e.getMessage()); + } catch (ECException e) { + log.error("api run fail", e); + return Error(ExceptionUtil.getRealMessage(e)); + } catch (Exception e) { + log.error("api run fail", e); + return Error("系统异常!", e); + } + } + + + /** + * 统一返回方法(无参有返回) + */ + public String run(Supplier f) { + try { + if (isLog) { + log.info("run api"); + } + return Ok(f.get()); + } catch (CustomizeRunTimeException e) { + log.error("api run fail", e); + return Error(e.getMessage()); + } catch (ECException e) { + log.error("api run fail", e); + Throwable cause = e.getCause(); + return Error(cause.getMessage()); + } catch (Exception e) { + log.error("api run fail", e); + return Error("系统异常!", e); + } + } + + + private static String getJsonString(Object apidatas) { + ObjectMapper mapper = new ObjectMapper(); + try { + return mapper.writeValueAsString(apidatas); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return ""; + } + + + /** + * 成功返回 + */ + private String Ok() { + Map apidatas = new HashMap<>(); + apidatas.put("api_status", true); + return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect); + } + + + /** + * 成功返回 + */ + private String Ok(R r) { + Map apidatas = new HashMap<>(); + apidatas.put("api_status", true); + apidatas.put("data", r); + String success = JSON.toJSONString(apidatas); + if (isLog) { + log.info("run salary api success return {}", success); + } + return success; + } + + + /** + * 失败返回 + */ + private static String Error(String message) { + Map apidatas = new HashMap<>(); + apidatas.put("api_status", false); + apidatas.put("errormsg", message); + return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect); + } + + + /** + * 系统异常失败返回 + */ + private static String Error(String message, Exception e) { + Map apidatas = new HashMap<>(); + apidatas.put("api_status", false); + apidatas.put("errormsg", message); + apidatas.put("error", e.getMessage()); + return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect); + } + +}