BaseRes.fromJson constructor

BaseRes.fromJson(
  1. dynamic json
)

Implementation

factory BaseRes.fromJson(json) {
  //特殊处理上传图片返回数据格式
  int code = -1;
  try {
    if (json["code"] != null || json["Header"] != null) {
      try {
        var flag = json["Header"]["Flag"];
        if (flag) {
          code = 200;
        } else {
          code = -1;
        }
      } catch (e) {
        code = -1;
      }
    } else {
      return BaseRes(code: -1, message: "服务器开小差了~", data: null);
    }
    return BaseRes(
        code: json["code"] ?? code,
        message: json["message"],
        data: json["data"] ?? json["Tbody"]);
  } catch (e) {
    if (json != null) {
      var imageJson = jsonDecode(json.toString());
      if (imageJson["url"] != null) {
        return BaseRes(code: 200, message: "上传文件成功。", data: imageJson);
      }
    }
    return BaseRes(code: -1, message: "数据格式异常", data: json);
  }
}