package io.renren.common.utils;
import org.apache.http.HttpStatus;
import org.springframework.ui.ModelMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 返回数据
*/
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public R() {
put("code", 0);
put("msg", "success");
}
public static R error() {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "网络异常,请重试");
}
public static R error(String msg) {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
}
public static R error(int code, String msg) {
return init(code, msg);
}
public static R ok() {
return new R();
}
public static R ok(String msg) {
R r = new R();
r.put("msg", msg);
return r;
}
public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
return r;
}
public static boolean isOk(R check) {
int code = Integer.parseInt(check.get("code").toString());
return code == 0;
}
public static R ok(ModelMap map) {
R r = new R();
r.putAll(map);
return r;
}
@Override
public R put(String key, Object value) {
super.put(key, value);
return this;
}
public R data(Object value) {
this.put("data", value);
return this;
}
public R count(Object value) {
this.put("count", value);
return this;
}
public static R noLogin() {
return init(401, "token无效");
}
public static R success() {
return ok("操作成功");
}
public static R fail() {
return error("操作失败");
}
private static R init(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
}
06-02
378
11-15
1090