不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-12-10 19:56:35  修改时间:2024-04-16 18:11:36  分类:Java框架/系统  编辑

【版本一】【推荐】

package com.wenjianbao.codepub.framework.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wenjianbao.codepub.framework.exception.ErrorEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 业务结果类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    private static final String CODE_OK = "200";
    private static final String CODE_ERROR = "500";
    private static final String MSG_OK = "操作成功";
    private static final String MSG_ERROR = "操作失败";

    private String code;
    private String msg;
    private T data;

    @JsonIgnore
    public boolean isOk() {
        return this.code.equals(CODE_OK);
    }

    @JsonIgnore
    public boolean isError() {
        return !isOk();
    }

    /**
     * 输出JSON字符串
     */
    @JsonIgnore
    public String outJson() {
        Map<String, Object> jsonMap = new LinkedHashMap<>();
        jsonMap.put("code", this.code);
        jsonMap.put("msg", this.msg);
        jsonMap.put("data", this.data);

        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(jsonMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return "{\"code\":\"" + this.getCode() + "\",\"msg\":\"" + this.getMsg() + "\", \"data\": \"\"}";
    }

    public static <T> Result<T> ok() {
        return ok(CODE_OK, Result.MSG_OK, null);
    }

    public static <T> Result<T> ok(String msg) {
        return ok(CODE_OK, msg, null);
    }

    public static <T> Result<T> ok(T data) {
        return ok(CODE_OK, Result.MSG_OK, data);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return ok(CODE_OK, msg, data);
    }

    public static <T> Result<T> ok(String code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

    public static <T> Result<Map<String, T>> okMap(String key, T data) {
        return okMap(CODE_OK, Result.MSG_OK, key, data);
    }

    public static <T> Result<Map<String, T>> okMap(String msg, String key, T data) {
        return okMap(CODE_OK, msg, key, data);
    }

    public static <T> Result<Map<String, T>> okMap(String code, String msg, String key, T data) {
        HashMap<String, T> map = new HashMap<>();
        map.put(key, data);
        return ok(code, msg, map);
    }

    public static <T> Result<T> error() {
        return error(CODE_ERROR, Result.MSG_ERROR, null);
    }

    public static <T> Result<T> error(String msg) {
        return error(CODE_ERROR, msg, null);
    }

    public static <T> Result<T> error(T data) {
        return error(CODE_ERROR, Result.MSG_ERROR, data);
    }

    public static <T> Result<T> error(String msg, T data) {
        return error(CODE_ERROR, msg, data);
    }

    public static <T> Result<T> error(ErrorEnum errorEnum) {
        return error(errorEnum.getErrorCode(), errorEnum.getErrorMsg(), null);
    }

    public static <T> Result<T> error(String code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

}

【版本二】链式版

package com.wanma.helper;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 业务操作结果类
 */
public class Result<T> {
    public final static String CODE_SUCCESS = "0";         // 成功状态码
    public final static String CODE_FAIL = "000001";       //(默认的)失败状态码(前3位是模块名,后3位是消息码)
    public final static String MSG_SUCCESS = "操作成功";   //(默认的)成功提示语
    public final static String MSG_FAIL = "操作失败";      //(默认的)失败提示语
    public final static String WIN_SELF = "window";
    public final static String WIN_PARENT = "parent";
    public final static String WIN_TOP = "top";

    private String code = Result.CODE_FAIL;         // 状态码,"0":成功,非"0":失败
    private String msg = Result.MSG_FAIL;           // 消息语
    private T data = null;                          // 数据
    private String url = "";                // 跳转URL
    private int time = 1;                   // 跳转时间(单位:秒)
    private String js = "";                 // 执行的JS脚步
    private boolean reload = false;        // 是否刷新页面
    private String window = "window";       // 刷新的窗体,可选项值:window|parent|top
    private boolean dialog = false;        // 是否显示对话框

    /**
     * 实例化一个对象
     */
    public static <T> Result<T> instance() {
        return new Result<T>();
    }

    public String getCode() {
        return code;
    }

    public Result<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public String getUrl() {
        return url;
    }

    public Result<T> setUrl(String url) {
        this.url = url;
        return this;
    }

    public int getTime() {
        return time;
    }

    public Result<T> setTime(int time) {
        this.time = time;
        return this;
    }

    public String getJs() {
        return js;
    }

    public Result<T> setJs(String js) {
        this.js = js;
        return this;
    }

    public boolean isReload() {
        return reload;
    }

    public Result<T> setReload(boolean reload) {
        this.reload = reload;
        return this;
    }

    public String getWindow() {
        return window;
    }

    public Result<T> setWindow(String window) {
        this.window = window;
        return this;
    }

    public boolean isDialog() {
        return dialog;
    }

    public Result<T> setDialog(boolean dialog) {
        this.dialog = dialog;
        return this;
    }

    public Result<T> setDialog() {
        this.dialog = true;
        return this;
    }

    public Result<T> success() {
        code = Result.CODE_SUCCESS;

        if (msg.equals("") || msg.equals(Result.MSG_FAIL)) {
            msg = Result.MSG_SUCCESS;
        }

        return this;
    }

    public Result<T> success(String msg) {
        code = Result.CODE_SUCCESS;
        this.msg = msg;
        return this;
    }

    public Result<T> success(T data) {
        code = Result.CODE_SUCCESS;
        this.data = data;

        if (msg.equals("") || msg.equals(Result.MSG_FAIL)) {
            msg = Result.MSG_SUCCESS;
        }

        return this;
    }

    public Result<T> success(String msg, T data) {
        code = Result.CODE_SUCCESS;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public Result<T> fail() {
        code = Result.CODE_FAIL;

        if (msg.equals("") || msg.equals(Result.MSG_OK )) {
            msg = Result.MSG_FAIL;
        }

        return this;
    }

    public Result<T> fail(String msg) {
        code = Result.CODE_FAIL;
        this.msg = msg;
        return this;
    }

    public Result<T> fail(T data) {
        code = Result.CODE_FAIL;
        this.data = data;

        if (msg.equals("") || msg.equals(Result.MSG_OK )) {
            msg = Result.MSG_FAIL;
        }

        return this;
    }

    public Result<T> fail(String msg, T data) {
        code = Result.CODE_FAIL;
        this.msg = msg;
        this.data = data;
        return this;
    }

    /**
     * 是否为 成功
     */
    public boolean isSuccess() {
        return code.equals(Result.CODE_SUCCESS);
    }

    /**
     * 是否为 失败
     */
    public boolean isFail() {
        return !isSuccess();
    }

    @Override
    public String toString() {
        return "{\"code\":\"" + getCode() + "\",\"msg\":\"" + getMsg() + "\", \"data\": \"\"}";
    }

    public Map<String, Object> toMap() {
        Map<String, Object> ret = new LinkedHashMap<>();
        ret.put("code", code);
        ret.put("msg", msg);
        ret.put("data", data);
        ret.put("url", url);
        ret.put("time", time);
        ret.put("js", js);
        ret.put("reload", reload);
        return ret;
    }

    /**
     * 输出JSON字符串
     */
    public String outJson() {
        Map<String, Object> jsonMap = new LinkedHashMap<>();
        jsonMap.put("code", code);
        jsonMap.put("msg", msg);
        jsonMap.put("data", data);
        
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(jsonMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        
        return "{\"code\":\"" + getCode() + "\",\"msg\":\"" + getMsg() + "\", \"data\": \"\"}";
    }

    /**
     * 输出Ajax字符串
     */
    public String outAjax() {
        StringBuilder content = new StringBuilder();

        if (!isDialog() && !getMsg().equals("")) {
            content.append(getMsg());
        }

        String js = "";
        if (isReload()) {
            js = "<script type=\"text/javascript\" reload=\"1\">\n" +
                "    setTimeout(function(){\n" +
                "        #window#.location.reload();\n" +
                "    }, #time# * 1000);\n" +
                "</script>";
            js = js.replace("#window#", getWindow())
                .replace("#time#", String.valueOf(getTime()));
            content.append(js);
        }

        if (!isReload() && StrUtil.isNotEmpty(getUrl())) {
            js = "<script type=\"text/javascript\" reload=\"1\">\n" +
                "    setTimeout(function(){\n" +
                "        #window#.location.href= \"#url#\";\n" +
                "    }, #time# * 1000);\n" +
                "</script>";
            js = js.replace("#window#", getWindow())
                .replace("#time#", String.valueOf(getTime()))
                .replace("#url#", getUrl());
            content.append(js);
        }

        if (isDialog() && isSuccess()) {
            if (getTime() < 2) {
                setTime(2);
            }

            js = "<script type=\"text/javascript\" reload=\"1\">\n" +
                "    dialog_success('#msg#', #time#)\n" +
                "</script>";
            js = js.replace("#msg#", getMsg())
                .replace("#time#", String.valueOf(getTime()));
            content.append(js);
        }

        if (isDialog() && isFail()) {
            if (getTime() < 3) {
                setTime(3);
            }

            js = "<script type=\"text/javascript\" reload=\"1\">\n" +
                "    dialog_fail('#msg#', #time#})\n" +
                "</script>";
            js = js.replace("#msg#", getMsg())
                .replace("#time#", String.valueOf(getTime()));
            content.append(js);
        }

        if (StrUtil.isNotEmpty(getJs())) {
            js = "<script type=\"text/javascript\" reload=\"1\">\n" +
                "    #js#\n" +
                "</script>";
            js = js.replace("#js#", getJs());
            content.append(js);
        }

        return ResponseHelper.outXml(content.toString());
    }
}