Blame view

src/main/java/com/huaheng/framework/web/domain/AjaxResult.java 3.16 KB
tangying authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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);
    }

    /**
     * 返回成功消息
     */
mahuandong authored
88
89
90
91
92
93
94
95
    public static AjaxResult success()
    {
        return new AjaxResult<>().setCode(RetCode.SUCCESS).setMsg("成功");
    }

    /**
     * 返回成功消息
     */
tangying authored
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    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);
    }

    /**
huhai authored
118
119
120
121
122
123
124
125
     * 返回失败消息
     */
    public static <T> AjaxResult<T> error(String message,T data)
    {
        return new AjaxResult<T>().setCode(RetCode.FAIL).setMsg(message).setData(data);
    }

    /**
mahuandong authored
126
127
128
129
130
131
132
133
     * 返回失败消息
     */
    public static <T> AjaxResult<T> error()
    {
        return new AjaxResult<T>().setCode(RetCode.FAIL).setMsg("失败");
    }

    /**
tangying authored
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
     * 返回消息
     */
    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);
    }

}