关于枚举类Enum的使用

check-handler-deal-return,程序的处理;

使用枚举类Enum可以更加便捷的完成检验和处理;

没有使用枚举类之前,使用的是常量定义,在commonConstants中定义了很多想要使用的常量,

然后在代码处理过程中,就是用了多个if-else if-else if-else ,代码繁琐,但是,使用了枚举类后,根据一个属性,

直接获取其他相应的属性值,省略了所有的if else判断,并且维护方便,将一类数据放在了一起!

package com.yifenqi.constant;


import org.springframework.util.StringUtils;


import lombok.AllArgsConstructor;
import lombok.Getter;


@AllArgsConstructor
@Getter
public enum AuthTypeEnum {
    /**
     * 授权-认证类型
     */
    AUTHTYPE_1("1", "身份认证", 0, "", "", ""),


    AUTHTYPE_2("2", "银行认证", 0, "", "", ""),


    AUTHTYPE_3("3", "邮箱验证", 0, "", "", ""),


    AUTHTYPE_4("4", "手机号验证", 0, "", "", ""),


    AUTHTYPE_5("5", "地址验证", 0, "", "", ""),


    AUTHTYPE_6("6", "邮箱认证(发送信息到邮箱,邮箱点击进行认证)", 0, "", "", ""),


    AUTHTYPE_7("7", "芝麻认证", 0, "", "", ""),


    AUTHTYPE_8("8", ApiProduct.SELECT_AUTHORIZATION_RESULT_YYS.getMsg(), 30,
            ApiProduct.SELECT_AUTHORIZATION_RESULT_YYS.getCode(), "https://open.shujumohe.com/box/yys",
            "https://api.shujumohe.com/octopus/task.unify.query/v4"),


    AUTHTYPE_9("9", ApiProduct.SELECT_AUTHORIZATION_RESULT_SHE_BAO.getMsg(), 30,
            ApiProduct.SELECT_AUTHORIZATION_RESULT_SHE_BAO.getCode(), "https://open.shujumohe.com/box/she_bao",
            "https://api.shujumohe.com/octopus/task.unify.query/v3"),


    AUTHTYPE_10("10", ApiProduct.SELECT_AUTHORIZATION_RESULT_GJJ.getMsg(), 30,
            ApiProduct.SELECT_AUTHORIZATION_RESULT_GJJ.getCode(), "https://open.shujumohe.com/box/gjj",
            "https://api.shujumohe.com/octopus/task.unify.query/v3"),


    AUTHTYPE_11("11", ApiProduct.SELECT_AUTHORIZATION_RESULT_DS.getMsg(), 30,
            ApiProduct.SELECT_AUTHORIZATION_RESULT_DS.getCode(), "https://open.shujumohe.com/box/tb",
            "https://api.shujumohe.com/octopus/task.unify.query/v3");


    // 授权类型
    private String code;


    // 授权信息
    private String msg;


    // 授权时间
    private int    expireDay;


    // 数据魔盒对此返回的任务编号含有关键字
    private String taskId;


    // 数据魔盒开始授权host
    private String host;


    // 查询授权结果host
    private String selectResultHost;


    public static AuthTypeEnum getEnumByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return null;
        }
        for (AuthTypeEnum item : AuthTypeEnum.values()) {
            if (item.getCode().equals(code)) {
                return item;
            }
        }
        return null;
    }


    public static AuthTypeEnum genEnumByTaskId(String taskId) {
        if (StringUtils.isEmpty(taskId)) {
            return null;
        }
        for (AuthTypeEnum item : AuthTypeEnum.values()) {
            if (!StringUtils.isEmpty(item.getTaskId())) {
                if (taskId.indexOf(item.getTaskId()) != -1) {
                    return item;
                }
            }
        }
        return null;
    }
}


在service层想要使用其中的数据时,

// 根据授权类型authType取出对应的授权信息

        AuthTypeEnum authTypeEnum = AuthTypeEnum.getEnumByCode(authType);

或者是:

// 根据taskId取出对应的授权信息

        AuthTypeEnum authTypeEnum = AuthTypeEnum.genEnumByTaskId(taskId);


每个不同种类的授权信息,都放在了一个Enum对象中,方便配置!

最后将处理好的数据进行封装,放入一个Result<T>中,统一格式,方便在前段处理.




猜你喜欢

转载自blog.csdn.net/weixin_41564440/article/details/80812024