返回数据封装

版权声明:本文为博主原创文章,欢迎注明出处转载 https://blog.csdn.net/shepherd_dirk/article/details/85341094

ApiResult.java

package com.dirk.doorlock.viewmodel;

/**
 * @Author: Dirk
 * @Description: api
 * @Date: Created in 21:49 2018/4/13
 */

public class ApiResult {


    public ApiResult() {
        this.code = 1;
        this.timestamp = System.currentTimeMillis();
    }

    public ApiResult(Object data) {
        this.code = 1;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 返回API的状态码
     */
    private int code;

    private long timestamp;

    /**
     * 返回的数据
     */
    private Object data;

    public static ApiResult ok() {
        return new ApiResult();
    }

    public static ApiResult ok(Object data) {
        return new ApiResult(data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

ApiError.java

package com.dirk.doorlock.viewmodel;

/**
 * @Author: Dirk
 * @Description: api
 * @Date: Created in 13:10 2018/4/14
 */

public class ApiError extends ApiResult {

    /**
     * 未登陆
     */
    public static int UN_LOGIN = -1;

    /**
     * 不存在的用户名
     */
    public static int NOLoginId = -2;

    /**
     * 密码错误
     */
    public static int ErrorPassword = -3;

    /**
     * 上传失败
     */
    public static int FailUpload = -4;


    private String message;

    public ApiError(String message, int status) {
        super();
        setCode(status);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 未登陆
     *
     * @return ApiError
     */
    public static ApiError unLogin() {
        return new ApiError("未登陆", UN_LOGIN);
    }

    /**
     * 不存在的用户名
     *
     * @return ApiError
     */
    public static ApiError loginIdNotFound() {
        return new ApiError("不存在的用户名", NOLoginId);
    }


    /**
     * 密码错误
     *
     * @return ApiError
     */
    public static ApiError errorPassword() {
        return new ApiError("密码错误", ErrorPassword);
    }

    public static ApiError failUpload(){
        return new ApiError("上传失败",FailUpload);
    }
}

猜你喜欢

转载自blog.csdn.net/shepherd_dirk/article/details/85341094