随心的博客

好记性不如个烂笔头,随心记录!

返回顶部

管理中心,栏目管理,及权限设置

管理后台的栏目管理,及权限设置。

栏目列表效果:

功能:添加栏目,修改栏目,搜索栏目,删除栏目

 

添加栏目效果:

 

分类:是左侧菜单里面的大分类。无URL,仅为名称,及分类

菜单:是分类下的具有具体功能的 链接

操作:是某个菜单下的,例如添加,删除,修改 等动作,用户权限判断

 

 

控制器:admins/controllers/system.go

 

//菜单列表
func Menus(c *gin.Context)  {
    //搜索
    menu_name := c.Query("menu_name")
    if menu_name != "" {
        //获取系统设置
        treeList := modes.SearchMenuList(menu_name)
        //显示登录模板
        c.HTML(http.StatusOK,"system/menu.html",gin.H{
            "treeList":treeList,
            "sum":len(treeList),
            "menu_name":menu_name,
        })
    }else{
        //获取系统设置
        treeList,sum := modes.MenuList()
        //显示登录模板
        c.HTML(http.StatusOK,"system/menu.html",gin.H{
            "treeList":treeList,
            "sum":sum,
            "menu_name":"",
        })
    }


}
//新增/编辑菜单
func MenusEdit(c *gin.Context)  {

    //获取分类
    treeList,_ := modes.MenuList()
    menu := modes.SysMenus{}
    //获取分类
    id,_ := strconv.Atoi(c.Query("id"))
    fmt.Println(id)
    if id != 0  {
        menu = modes.MenuItem(id)
    }
    //显示登录模板
    c.HTML(http.StatusOK,"system/menu_edit.html",gin.H{
        "treeList": treeList,
        "menu":menu,
    })
}

//保存菜单
func MenusSave(c *gin.Context)  {

    var menu  modes.SysMenus
    err := c.ShouldBind(&menu)
    if err != nil {
        c.JSON(http.StatusOK, gin.H{ "code": 400, "msg":  err.Error(),})
        return
    }
    res,msg := modes.SaveMenu(&menu)
    if res != true {
        c.JSON(http.StatusOK, gin.H{ "code": 400, "msg":  msg,})
        return
    }

    c.JSON(http.StatusOK, gin.H{ "code": 0, "msg":  "添加成功",})
}

//删除菜单
func MenusDel(c *gin.Context)  {
    //获取系统设置
    id := c.PostForm("id")

    ids,_ :=strconv.Atoi(id)
    res,msg := modes.DelMenu(ids)
    if res != true {
        c.JSON(http.StatusOK, gin.H{ "code": 400, "msg":  msg,})
        return
    }
    c.JSON(http.StatusOK, gin.H{ "code": 0, "msg":  msg,})
}

 

模型:modes/adminsMenusModel.go

type SysMenus struct {
    Id int `form:"id"`
    MenuName string `form:"menu_name"`
    MenuType int `form:"menu_type"`
    MenuOrder int `form:"menu_order"`
    MenuPid int `form:"menu_pid"`
    MenuUrl string `form:"menu_url"`
    MenuStatus int `form:"menu_status"`
    AddDatetime string `form:"add_datetime"`
}
type TreeList  struct {
    Id int `form:"id"`
    MenuName string `form:"menu_name"`
    MenuType int `form:"menu_type"`
    MenuOrder int `form:"menu_order"`
    MenuPid int `form:"menu_pid"`
    MenuUrl string `form:"menu_url"`
    MenuStatus int `form:"menu_status"`
    AddDatetime string `form:"add_datetime"`
    Children []*TreeList `form:"children"`
}


// 搜索
func SearchMenuList(menu_name string) []*TreeList {

    var menu []SysMenus
    //获取第一级菜单
    DB.Model(SysMenus{}).Where("menu_name like ?", "%"+menu_name+"%").Find(&menu)
    treeList := []*TreeList{}

    for _, v := range menu {
        node := &TreeList{
            Id:          v.Id,
            MenuName:    v.MenuName,
            MenuType:    v.MenuType,
            MenuOrder:   v.MenuOrder,
            MenuPid:     v.MenuPid,
            MenuUrl:     v.MenuUrl,
            MenuStatus:  v.MenuStatus,
            AddDatetime: v.AddDatetime,
        }
        node.Children = nil
        treeList = append(treeList, node)
    }

    return treeList
}

// menu单条
func MenuItem(id int) SysMenus {
    var menu SysMenus

    DB.Model(SysMenus{}).Where("id=?", id).Find(&menu)

    return menu
}

// menu单条
func CurrentMenu(c *gin.Context) SysMenus {
    var menu SysMenus

    var url string
    if strings.Contains(c.Request.RequestURI, "?") {
        spurl := strings.Split(c.Request.RequestURI, "?")
        url = spurl[0]
    } else {
        url = c.Request.RequestURI
    }

    DB.Model(SysMenus{}).Where("menu_url=?", url).Find(&menu)

    return menu
}

// menu列表
func MenuList() ([]*TreeList, int) {
    return getMenu(0)
}

// 获取菜单
func getMenu(pid int) ([]*TreeList, int) {
    var menu []SysMenus
    var count = 0
    //获取第一级菜单
    DB.Model(SysMenus{}).Where("menu_pid=?", pid).Order("menu_order DESC ").Find(&menu)

    treeList := []*TreeList{}

    for _, v := range menu {
        count++
        child, num := getMenu(v.Id)
        node := &TreeList{
            Id:          v.Id,
            MenuName:    v.MenuName,
            MenuType:    v.MenuType,
            MenuOrder:   v.MenuOrder,
            MenuPid:     v.MenuPid,
            MenuUrl:     v.MenuUrl,
            MenuStatus:  v.MenuStatus,
            AddDatetime: v.AddDatetime,
        }
        node.Children = child
        count += num
        treeList = append(treeList, node)
    }
    return treeList, count
}

// 保存设置
func SaveMenu(menu *SysMenus) (bool, string) {

    if menu.Id == 0 {
        menu.AddDatetime = time.Now().Format(common.TimeTem)
        res := DB.Model(SysMenus{}).Create(menu)
        if res.Error != nil {
            return false, res.Error.Error()
        }
        return true, "保存成功"
    } else {
        res := DB.Model(SysMenus{}).Where("id = ?", menu.Id).Updates(menu)
        if res.Error != nil {
            return false, res.Error.Error()
        }
        return true, "保存成功"
    }
}

// 保存设置
func DelMenu(id int) (bool, string) {
    var count int64
    DB.Model(SysMenus{}).Where("menu_pid = ?", id).Count(&count)
    if count > 0 {
        return false, "菜单下有子菜单,删除失败"
    }

    res := DB.Delete(SysMenus{}, id)
    if res.Error != nil {
        return false, res.Error.Error()
    }
    return true, "删除成功"
}

 

 

菜单列表视图 views/admins/system/menu.html 

  <div class="cl pd-5 bg-1 bk-gray mt-20"> <span class="l">
            {{ $admin_uid := GetAdminId}}
            <a class="btn btn-primary radius" {{if has_powa $admin_uid "/admin/system_menus_edit"}} style="display:none" {{end}} onclick="system_category_add('添加栏目','/admin/system_menus_edit')" href="javascript:;"><i class="Hui-iconfont">&#xe600;</i> 添加栏目</a></span> <span class="r">共有数据:<strong>{{.sum}}</strong> 条</span> </div>
        <div class="mt-20">
            <table class="table table-border table-bordered table-hover table-bg">
                <thead>
                <tr class="text-c">
                    <th width="25"><input type="checkbox" name="" value=""></th>
                    <th width="80">ID</th>
                    <th width="80">排序</th>
                    <th width="100">栏目类型</th>
                    <th>栏目名称</th>
                    <th width="250">栏目url</th>
                    <th width="100">状态</th>
                    <th width="150">添加时间</th>
                    <th width="100">操作</th>
                </tr>
                </thead>
                <tbody>

                {{ range $k,$v1 := .treeList}}
                <tr class="text-c">
                    <td><input type="checkbox" name="" value=""></td>
                    <td>{{$v1.Id}}</td>
                    <td>{{$v1.MenuOrder}}</td>
                    <td class="text-l">
                        {{if eq $v1.MenuType  1}} 一级分类
                        {{ else if eq $v1.MenuType 2}} &nbsp;&nbsp;&nbsp;&nbsp;--|菜单
                        {{ else if eq $v1.MenuType 3}} &nbsp;&nbsp;&nbsp;&nbsp;----|操作
                        {{ end }}
                    </td>
                    <td class="text-l">
                        {{if eq $v1.MenuType 1}}
                        {{ else if eq $v1.MenuType 2}} &nbsp;&nbsp;&nbsp;&nbsp;--|
                        {{ else if eq $v1.MenuType 3}} &nbsp;&nbsp;&nbsp;&nbsp;----|
                        {{ end }}
                        {{$v1.MenuName}}
                    </td>
                    <td>{{$v1.MenuUrl}}</td>
                    <td>
                        {{if eq $v1.MenuStatus  1}} 启用
                        {{else}} 停用
                        {{end}}
                    </td>
                    <td>{{$v1.AddDatetime}}</td>
                    <td class="f-14"><a title="编辑" href="javascript:;" onclick="system_category_edit('栏目编辑','/admin/system_menus_edit',{{$v1.Id}},'700','480')" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> <a title="删除" href="javascript:;" onclick="article_category_del(this,{{$v1.Id}})" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a></td>
                </tr>
                    {{ range $k2,$v2 := $v1.Children}}
                    <tr class="text-c">
                        <td><input type="checkbox" name="" value=""></td>
                        <td>{{$v2.Id}}</td>
                        <td>{{$v2.MenuOrder}}</td>
                        <td class="text-l">
                            {{if eq $v2.MenuType  1}} 一级分类
                            {{ else if eq $v2.MenuType 2}} &nbsp;&nbsp;&nbsp;&nbsp;--|菜单
                            {{ else if eq $v2.MenuType 3}} &nbsp;&nbsp;&nbsp;&nbsp;----|操作
                            {{ end }}
                        </td>
                        <td class="text-l">
                            {{if eq $v2.MenuType 1}}
                            {{ else if eq $v2.MenuType 2}} &nbsp;&nbsp;&nbsp;&nbsp;--|
                            {{ else if eq $v2.MenuType 3}} &nbsp;&nbsp;&nbsp;&nbsp;----|
                            {{ end }}
                            {{$v2.MenuName}}
                        </td>
                        <td>{{$v2.MenuUrl}}</td>
                        <td>
                            {{if eq $v2.MenuStatus  1}} 启用
                            {{else}} 停用
                            {{end}}
                        </td>
                        <td>{{$v2.AddDatetime}}</td>
                        <td class="f-14"><a title="编辑" href="javascript:;" onclick="system_category_edit('栏目编辑','/admin/system_menus_edit',{{$v2.Id}},'700','480')" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> <a title="删除" href="javascript:;" onclick="article_category_del(this,{{$v2.Id}})" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a></td>
                    </tr>
                        {{ range $k3,$v3 := $v2.Children}}
                        <tr class="text-c">
                            <td><input type="checkbox" name="" value=""></td>
                            <td>{{$v3.Id}}</td>
                            <td>{{$v3.MenuOrder}}</td>
                            <td class="text-l">
                                {{if eq $v3.MenuType  1}} 一级分类
                                {{ else if eq $v3.MenuType 2}} &nbsp;&nbsp;&nbsp;&nbsp;--|菜单
                                {{ else if eq $v3.MenuType 3}} &nbsp;&nbsp;&nbsp;&nbsp;----|操作
                                {{ end }}
                            </td>
                            <td class="text-l">
                                {{if eq $v3.MenuType 1}}
                                {{ else if eq $v3.MenuType 2}} &nbsp;&nbsp;&nbsp;&nbsp;--|
                                {{ else if eq $v3.MenuType 3}} &nbsp;&nbsp;&nbsp;&nbsp;----|
                                {{ end }}
                                {{$v3.MenuName}}
                            </td>
                            <td>{{$v3.MenuUrl}}</td>
                            <td>{{if eq $v3.MenuStatus  1}} 启用
                                {{else}} 停用
                                {{end}}
                            </td>
                            <td>{{$v3.AddDatetime}}</td>
                            <td class="f-14">
                                <a title="编辑" href="javascript:;" {{if has_powa $admin_uid "/admin/system_menus_edit"}} style="display:none" {{end}} onclick="system_category_edit('栏目编辑','/admin/system_menus_edit',{{$v3.Id}},'700','480')" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>
                                <a title="删除" href="javascript:;" {{if has_powa $admin_uid "/admin/system_menus_del"}} style="display:none" {{end}} onclick="article_category_del(this,{{$v3.Id}})" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a></td>
                        </tr>
                        {{ end }}
                    {{ end }}
                {{end}}
                </tbody>
            </table>
        </div>

<script type="text/javascript">
    $('.table-sort').dataTable({
        "aaSorting": [[ 1, "desc" ]],//默认第几个排序
        "bStateSave": true,//状态保存
        "aoColumnDefs": [
            //{"bVisible": false, "aTargets": [ 3 ]} //控制列的隐藏显示
            {"orderable":false,"aTargets":[0,4]}// 制定列不参与排序
        ]
    });
    /*系统-栏目-添加*/
    function system_category_add(title,url,w,h){
        layer_show(title,url,w,h);
    }
    /*系统-栏目-编辑*/
    function system_category_edit(title,url,id,w,h){
        layer_show(title,url+"?id="+id,w,h);
    }
    /*系统-栏目-删除*/
    function article_category_del(obj,id){
        layer.confirm('确认要删除吗?',function(index){
            $.post("/admin/system_menus_del",{id:id},function(res){
                if (res.code==0){
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!',{icon:1,time:1000});
                }else{
                    layer.msg(res.msg);
                }
            },'json')

        });
    }
</script>

 

 

添加和编辑视图:views/admins/system/menu_edit.html 

<article class="cl pd-20">
    <form action="/admin/system_menus_save" method="post" class="form form-horizontal" id="form-member-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>栏目/菜单名称:</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{{.menu.MenuName}}" placeholder="" id="menu_name" name="menu_name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>栏目/菜单类型:</label>
            <div class="formControls col-xs-8 col-sm-9 skin-minimal">
                <div class="radio-box">
                    <input name="menu_type" type="radio" id="menu_type-1" value="1" {{if eq .menu.MenuType 1 }}checked{{end}} {{if not .menu.MenuType}}checked{{end}}>
                    <label for="menu_type-1">分类</label>
                </div>
                <div class="radio-box">
                    <input type="radio" id="menu_type-2" name="menu_type" value="2" {{if eq .menu.MenuType 2 }}checked{{end}}>
                    <label for="menu_type-2">菜单</label>
                </div>
                <div class="radio-box">
                    <input type="radio" id="menu_type-3" name="menu_type" value="3" {{if eq .menu.MenuType 3 }}checked{{end}}>
                    <label for="menu_type-3">操作</label>
                </div>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">上级菜单:</label>
            <div class="formControls col-xs-8 col-sm-9"> <span class="select-box">
                <select class="select" size="1" name="menu_pid">
                    <option value="" >顶级分类</option>
                    {{ range $k,$v1 := .treeList}}
                    <option value="{{$v1.Id}}" {{if eq $.menu.MenuPid $v1.Id }}selected{{end}}>&nbsp;&nbsp;&nbsp;&nbsp;-| {{$v1.MenuName}}</option>
                        {{ range $k2,$v2 := $v1.Children}}
                        <option value="{{$v2.Id}}" {{if eq $.menu.MenuPid $v2.Id }}selected{{end}}>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --| {{$v2.MenuName}}</option>
                        {{end}}
                    {{end }}
                </select>
                </span> </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">菜单/操作URL:</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{{.menu.MenuUrl}}" placeholder="顶级分类可以为空" id="menu_url" name="menu_url">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">排序:</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" placeholder="数字越大越靠前" value="{{.menu.MenuOrder}}" name="menu_order" id="menu_order">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">栏目状态:</label>
            <div class="formControls col-xs-8 col-sm-9 skin-minimal">
                <div class="radio-box">
                    <input name="menu_status" type="radio" id="menu_status-1" value="1" {{if eq .menu.MenuStatus 1 }}checked{{end}} {{if not .menu.MenuStatus}}checked{{end}}>
                    <label for="menu_status-1">正常</label>
                </div>
                <div class="radio-box">
                    <input type="radio" id="menu_status-2" name="menu_status" value="2" {{if eq .menu.MenuStatus 2 }}checked{{end}}>
                    <label for="menu_type-2">停用</label>
                </div>
            </div>
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input type="hidden" name="id" id="id" value="{{.menu.Id}}" />
                <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>

<script type="text/javascript">
    $(function(){
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });

        $("#form-member-add").validate({
            rules:{
                menu_name:{
                    required:true,
                    minlength:2,
                    maxlength:16
                },


            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                $(form).ajaxSubmit(function (data){
                    if(data.code == 0){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.window.location.reload();
                        parent.layer.close(index);
                    }else{
                        layer.alert(data.msg)
                    }
                });
            }
        });
    });
</script>

完结

 

posted @ 2023-04-20 22:36  yangphp  阅读(63)  评论(0编辑  收藏  举报