JAVA——后端Vue动态路由配置类JavaBean封装

Vue文档

https://router.vuejs.org/zh/ 

源代码 

package cn.edu.zstu.shihua.xihu.dto;

import cn.edu.zstu.shihua.xihu.entity.Menu;
import cn.edu.zstu.shihua.xihu.entity.Meta;
import cn.edu.zstu.shihua.xihu.model.Resource;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;


/**
 * @author ShenTuZhiGang
 * @version 1.0.0
 * @date 2020-07-26 15:13
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "Vue路由配置")
public class VueRouteConfig {
    /*
        RouteConfig {
          path: string
          name?: string
          component?: Component
          components?: Dictionary
          redirect?: RedirectOption
          alias?: string | string[]
          children?: RouteConfig[]
          meta?: any
          beforeEnter?: NavigationGuard
          props?: boolean | Object | RoutePropsFunction
          caseSensitive?: boolean
          pathToRegexpOptions?: PathToRegexpOptions
        }
    */
    @ApiModelProperty(name = "路径",value = "路径")
    private String path;
    @ApiModelProperty(name = "组件",value = "组件")
    private String component;
    @ApiModelProperty(name = "名称",value = "名称")
    private String name;
    @ApiModelProperty(name = "重定向",value = "重定向")
    private String redirect;
    @ApiModelProperty(name = "别名",value = "别名")
    private String alias;
    @ApiModelProperty(name = "元",value = "元")
    private Meta meta;
    @ApiModelProperty(name = "子菜单",value = "子菜单")
    private List children;
}
package cn.edu.zstu.shihua.xihu.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author ShenTuZhiGang
 * @version 1.0.0
 * @date 2020-07-26 14:14
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "元")
public class Meta {
    @ApiModelProperty(name = "标题",value = "标题")
    private String title;
    @ApiModelProperty(name = "链接",value = "链接")
    private String link;
    @ApiModelProperty(name = "图标class",value = "图标class")
    private String iconClass;
    @ApiModelProperty(name = "保持",value = "保持")
    private Boolean keepAlive;
    @ApiModelProperty(name = "是否需要授权",value = "是否需要授权")
    private Boolean requireAuth;
}

 

构建参考:

 Resource为数据库Model

    public static VueRouteConfig build(List resources){
        Map map = new HashMap<>();
        Map> listMap = new HashMap<>();
        for (Resource resource : resources){
            map.put(resource.getId(),resource);
            if(listMap.get(resource.getParentId())==null){
                listMap.put(resource.getParentId(),new ArrayList<>());
            }
            listMap.get(resource.getParentId()).add(resource.getId());
        }

        return findChildren(map,listMap,map.get(1));

    }

    private static VueRouteConfig findChildren(Map map,
                                     Map> listMap,
                                     Resource resource) {
        VueRouteConfig vueRouteConfig = new VueRouteConfig();
        vueRouteConfig.setName(resource.getName());
        vueRouteConfig.setComponent(resource.getComponent());
        Meta meta = new Meta();
        meta.setIconClass(resource.getIconClass());
        meta.setKeepAlive(resource.getKeepAlive());
        meta.setRequireAuth(resource.getRequireAuth());
        vueRouteConfig.setMeta(meta);
        vueRouteConfig.setPath(resource.getPath());
        List vueRouteConfigs = new ArrayList<>();
        List integers = listMap.get(resource.getId());
        if(integers==null){
            return vueRouteConfig;
        }
        for(Integer id:integers){
            vueRouteConfigs.add(findChildren(map,listMap,map.get(id)));
        }
        vueRouteConfig.setChildren(vueRouteConfigs);
        return vueRouteConfig;
    }

参考文章

https://blog.csdn.net/frankcheng5143/article/details/52958486

https://blog.csdn.net/frankcheng5143/article/details/72723958

https://www.cnblogs.com/fenghua12/p/5281902.html

你可能感兴趣的:(#,JAVA,JAVA,Vue,动态路由)