AjaxResult.java 3.16 KB
package com.huaheng.framework.web.domain;

import com.alibaba.fastjson.JSON;
import java.io.Serializable;

/**
 * 操作消息提醒
 *
 * @author huaheng
 */
public class AjaxResult<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    public int code;

    private String msg;

    private T data;

    public Boolean hasErr(){
        if(code == RetCode.SUCCESS.getValue()){
            return false;
        }else {
            return true;
        }
    }

    public AjaxResult<T> setCode(RetCode retCode) {
        this.code = retCode.getValue();
        return this;
    }

    public int getCode() {
        return code;
    }

    public AjaxResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

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

    public T getData() {
        return data;
    }

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

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    /**
     * 响应返回结果
     *
     * @param succeed 是否成功
     * @return 操作结果
     */
    public static AjaxResult toAjax(boolean succeed)
    {
        return succeed ? success("成功") : error("失败");
    }

    /**
     * 返回成功消息
     */
    public static AjaxResult success(String message)
    {
        return new AjaxResult<>().setCode(RetCode.SUCCESS).setMsg(message);
    }

    /**
     * 返回成功消息
     */
    public static AjaxResult success()
    {
        return new AjaxResult<>().setCode(RetCode.SUCCESS).setMsg("成功");
    }

    /**
     * 返回成功消息
     */
    public static <T> AjaxResult<T> success(T data)
    {
        return new AjaxResult<T>().setCode(RetCode.SUCCESS).setMsg("成功").setData(data);
    }

    /**
     * 返回成功消息
     */
    public static <T> AjaxResult<T> success(String message, T data)
    {
        return new AjaxResult<T>().setCode(RetCode.SUCCESS).setMsg(message).setData(data);
    }

    /**
     * 返回失败消息
     */
    public static AjaxResult error(String message)
    {
        return new AjaxResult<>().setCode(RetCode.FAIL).setMsg(message);
    }

    /**
     * 返回失败消息
     */
    public static <T> AjaxResult<T> error(String message,T data)
    {
        return new AjaxResult<T>().setCode(RetCode.FAIL).setMsg(message).setData(data);
    }

    /**
     * 返回失败消息
     */
    public static <T> AjaxResult<T> error()
    {
        return new AjaxResult<T>().setCode(RetCode.FAIL).setMsg("失败");
    }

    /**
     * 返回消息
     */
    public static <T> AjaxResult<T> setResult(RetCode retCode, String message, T data)
    {
        return new AjaxResult<T>().setCode(retCode).setMsg(message).setData(data);
    }

    /**
     * 返回响应码和消息
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public  static <T> AjaxResult<T> setResultCS(int code, String message)
    {
        return new AjaxResult<T>().setCode(code).setMsg(message);
    }

}