• MVC+ajax权限管理


     不喜欢说废话,直接了当:

    1、控制器

     /// <summary>
            /// 获取列表
            /// </summary>
            /// <returns></returns>
            public ActionResult GetRoleList()
            {
                return View();
            }
    
            public string GetListF(int id)
            {
                List<CM_Menu> list= _menuService.GetListF(id);
                string html = "";
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        html += "<li><button onclick=GetMenu(" + item.Id + ") class='button'>" + item.Name + "</button>";
                    }
                }
                return html;
            }
            /// <summary>
            /// 子菜单加载(权限)
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public string GetListSon(int id)
            {
                //权限判断
                CM_Info_Role roleModel = Session["InfoRole"] as CM_Info_Role;
                string role = roleModel.Role;
                List<CM_Menu> list= _menuService.GetListSon(id);
                string html = "";
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count;i++ )
                    {
                        int temp = Convert.ToInt32(list[i].ShowName.Replace("role", "").Trim());
                        if (role.Substring(temp, 1) == "1")
                        {
                            html += "<li><a href='" + list[i].URL + "' target='_blank' >" + list[i].Name + "</a></li>";
                        }
                        else
                        {
                            html += "";
                        }
                    }
                }
                return html;
            }

    2、ajax

    function bodyonload() //加载的是父ID为0的,即最主要的菜单
    {
        var data = 0;
        $.ajax({
            type: 'post',
            async: false,
            data: { id: data },
            url: '/Menu/GetListF',
            dataType: 'html',
            success: function (json, textStatus) {
                $("#MenuListF").append(json);
            },
            complete: function (XMLHttpRequest, textStatus) {
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.alert("失败提示", textStatus);
            }
        });
        //GetMenu(0);//初始化加载0的
    }
    
    function GetMenu(fid)//获取
    {
        $.ajax({
            type: 'post',
            async: false,
            data: { id: fid },
            url: '/Menu/GetListSon',
            dataType: 'html',
            success: function (json, textStatus) {
                //if (!$("#MenuListSon").has("li").length) {
                //    $("#MenuListSon").append(json);
                //}
                //else {
                    $("#MenuListSon > *").remove();
                    $("#MenuListSon").append(json);
                //}
            },
            complete: function (XMLHttpRequest, textStatus) {
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.alert("失败提示", textStatus);
            }
        });
    }

    3、页面

     <div id="head_wrap" class="container_12">
                <div id="logo" class="grid_4">
                    <h1><span>logo</span></h1>
                </div>
                <div id="controlpanel" class="grid_8">
                    <ul>
                        <li><p><strong>你好,用户名</strong></p></li>
                    </ul>
                </div>
                <div id="navigation" class=" grid_12">
                    <ul id="MenuListF">
                    </ul>
                </div>
            </div><!-- end headwarp  -->

      在大神面前来说,这就是个菜,但是加载这个菜单的时候,可能因为新手的缘故,对自己解决问题还是挺满意的。

      解释一下:

      控制器里面其实就是输出html字符串,ajax里面的就是加载一下这个字符串。页面调用显示

      

    int temp = Convert.ToInt32(list[i].ShowName.Replace("role", "").Trim());
                        if (role.Substring(temp, 1) == "1")
                        {
                            html += "<li><a href='" + list[i].URL + "' target='_blank' >" + list[i].Name + "</a></li>";
                        }
                        else
                        {
                            html += "";
                        }

    这里其实就是我的权限判断了,我觉得这样做的话后台代码也没有那么容易暴露,因为这个权限是基于 User、Menu、Role做的,我觉得这个思路就是这个样子

  • 相关阅读:
    变态的IE
    视频豪横时代,应用如何快速构建视频点播能力?
    阿里云峰会 | 阿里云CDN六大边缘安全能力,全力助推政企数字化转型
    从 2018 年 Nacos 开源说起
    完美日记:实现高弹性高稳定电商架构
    Dubbo 迈出云原生重要一步 应用级服务发现解析
    如何提升微服务的幸福感
    怀里橘猫柴犬,掌上代码江湖——对话阿里云 MVP郭旭东
    云原生时代消息中间件的演进路线
    solr中特殊字符的处理
  • 原文地址:https://www.cnblogs.com/JeffController/p/4615618.html
Copyright © 2020-2023  润新知