Day37项目saas-export项目-角色授权页面显示

角色授权页面

在这里插入图片描述

RoleController

打开授权页面显示角色名称

 //location.href="${path}/system/role/toRoleModule.do?roleId="+id;
    @RequestMapping(path="/toRoleModule",method ={
    
     RequestMethod.GET, RequestMethod.POST})
    public String toRoleModule(String roleId){
    
    //接收页面提交的roleId
        //当前授权页面需要显示 角色名称
        Role role = iRoleService.findById(roleId);

        //数据转发到页面
        request.setAttribute("role",role);
        return "system/role/role-module";
    }

role-module.jsp

页面发请求到后台获取数据

<script type="text/javascript">


        //当前的配置信息
        var setting = {
    
    
            check: {
    
    
                enable: true
            },
            data: {
    
    
                simpleData: {
    
    
                    enable: true
                }
            }
        };
        //当前的数据
        /*var zNodes =[
            { id:1, pId:0, name:"Sass管理", open:true},
            { id:11, pId:1, name:"企业管理", open:true,checked:true},
            { id:111, pId:1, name:"模块管理"}
        ];*/

        $(document).ready(function(){
    
    
            var fn =function(data){
    
    
                //菜单的初始化
                $.fn.zTree.init($("#treeDemo"), setting, data);
                //参1 显示的标签
                //参2 设置的参数 比如支持复选 check enable = true
                //参3 数据
            }
            $.get('${path}/system/role/getZtreeData.do?roleId=${role.roleId}',fn,'json')


        });

    </script>

RoleController

查询数据,以及角色对应的权限查出,发送到页面。

 //$.get('${path}/role/getZtreeData.do?roleId=${role.roleId}',fn,'json')

    @RequestMapping(path="/getZtreeData",method ={
    
     RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody Object getZtreeData(String roleId) {
    
    //接收页面提交的roleId
        //所有的权限查询出来
        List<Module> all = iModuleService.findAllModules();
        //转换成 List<Map<String,Object>>  { id:1, pId:0, name:"Sass管理", open:true},
        //根据 roleId查 该角色的权限
        List<Module> myList = iModuleService.findModuleByRoleId(roleId);
        List<Map<String,Object>> list = new ArrayList<>();
        //返回给页面
        for(Module m:all){
    
    
            //生成一个集合 Map<String,Object> 表示一节点
            Map<String,Object> node = new HashMap<String,Object>();
            node.put("id",m.getModuleId());
            node.put("pId",m.getParentId());
            node.put("name",m.getName());
            node.put("open",true);
            if(isInMyList(m,myList)){
    
    
                node.put("checked",true);//为了在菜单页面上打上勾。有打勾就表示有这个权限,否则就是没有
            }
            //添加到集合中
            list.add(node);
        }
        return list;//@ResponseBody将list转成json
    }

在这里插入图片描述

勾选有权限的模块

RoleController

  • 在循环每个模块时,增加了 判断。如果属于该角色的权限生成checked=true 值 ,
  • 该值可以控制选项被勾选,用户可以通勾选判断是否有权限
 //$.get('${path}/role/getZtreeData.do?roleId=${role.roleId}',fn,'json')

   @RequestMapping(path="/getZtreeData",method ={
    
     RequestMethod.GET, RequestMethod.POST})
   public @ResponseBody Object getZtreeData(String roleId) {
    
    //接收页面提交的roleId
       //所有的权限查询出来
       List<Module> all = iModuleService.findAllModules();
       //转换成 List<Map<String,Object>>  { id:1, pId:0, name:"Sass管理", open:true},
       //根据 roleId查 该角色的权限
       List<Module> myList = iModuleService.findModuleByRoleId(roleId);
       List<Map<String,Object>> list = new ArrayList<>();
       //返回给页面
       for(Module m:all){
    
    
           //生成一个集合 Map<String,Object> 表示一节点
           Map<String,Object> node = new HashMap<String,Object>();
           node.put("id",m.getModuleId());
           node.put("pId",m.getParentId());
           node.put("name",m.getName());
           node.put("open",true);
           if(isInMyList(m,myList)){
    
    
               node.put("checked",true);//为了在菜单页面上打上勾。有打勾就表示有这个权限,否则就是没有
           }
           //添加到集合中
           list.add(node);
       }
       return list;//@ResponseBody将list转成json
   }

   //需要判断m是否在myList里面,如果在表示该角色有这个权限,否则没有
   private boolean isInMyList(Module m, List<Module> myList) {
    
    
       for(Module my:myList){
    
    
           if(m.getModuleId().equals(my.getModuleId())){
    
    
               return true;
           }
       }//end for 循环结束
       return false;
   }

IModuleService,ModuleServiceImpl

List<Module> findAllModules();
List<Module> findModuleByRoleId(String roleId);
    @Override
    public List<Module> findAllModules() {
    
    
        return iModuleDao.findAll();
    }
    
    @Override
    public List<Module> findModuleByRoleId(String roleId) {
    
    
        return iModuleDao.findByRoleId(roleId);
    }

IModuleDao,IModuleDao.xml

List<Module> findByRoleId(String roleId);
List<Module> findAll();
 <select id="findByRoleId" parameterType="string" resultMap="moduleMap" >
        select m.*
        from pe_role_module rm
        inner join ss_module m
        on rm.module_id = m.module_id
        where rm.role_id=#{roleId}
</select>
  <select id="findAll" resultMap="moduleMap">
        select * from ss_module order by order_no asc
    </select>
 <!-- 创建实体类与表字段的映射关系 -->
    <resultMap id="moduleMap" type="module">
        <id column="module_id"     property="moduleId"    />
        <result column="parent_id"     property="parentId"    />
        <result column="parent_name"   property="parentName"  />
        <result column="name"          property="name"        />
        <result column="layer_num"     property="layerNum"    />
        <result column="is_leaf"       property="isLeaf"      />
        <result column="ico"           property="ico"         />
        <result column="cpermission"   property="cpermission" />
        <result column="curl"          property="curl"        />
        <result column="ctype"         property="ctype"       />
        <result column="state"         property="state"       />
        <result column="belong"        property="belong"      />
        <result column="cwhich"        property="cwhich"      />
        <result column="quote_num"     property="quoteNum"    />
        <result column="remark"        property="remark"      />
        <result column="order_no"      property="orderNo"     />
    </resultMap>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40711092/article/details/109542703