master
wubin 5 years ago
parent b79ba681b3
commit 822010f70f

@ -123,18 +123,20 @@ func SetMenuSort(in *BaseMenuProto.SetMenuSortArg) (bool, error) {
return false, errors.New("传入调整方向有误向上1向下2传入值不正确")
}
//查找当前menu的sort_id
list := SqlKit.QueryByIds([]string{in.MenuId}, "t_base_role_menu")
list := SqlKit.QueryByIds([]string{in.MenuId}, "t_base_menu")
if len(list) == 0 {
return false, errors.New("没有找到对应的menu_id!")
}
parentId := list[0]["parent_id"].(string)
sortId := list[0]["sort_id"].(int32)
sortId := int32(list[0]["sort_id"].(float64))
sql := `select menu_id,sort_id from t_base_menu where parent_id=? and sort_id#? order sort_id limit 1`
sql := `select menu_id,sort_id from t_base_menu where parent_id=? and sort_id#? order by sort_id $ limit 1`
if in.Direction == 1 {
sql = strings.Replace(sql, "#", "<", -1)
sql = strings.Replace(sql, "$", "desc", -1)
} else {
sql = strings.Replace(sql, "#", ">", -1)
sql = strings.Replace(sql, "$", "asc", -1)
}
list2, err := db.SQL(sql, parentId, sortId).Query().List()
if err != nil {
@ -146,7 +148,7 @@ func SetMenuSort(in *BaseMenuProto.SetMenuSortArg) (bool, error) {
}
//目标替换对象
menuId2 := list2[0]["menu_id"].(string)
sortId2 := list2[0]["sort_id"].(int32)
sortId2 := list2[0]["sort_id"].(int64)
//更新
//1、清除Redis缓存

@ -16,6 +16,7 @@ func Routers(r *gin.RouterGroup) {
rr.POST("/AddMenuInfo", AddMenuInfo)
rr.POST("/DeleteMenuInfo", DeleteMenuInfo)
rr.POST("/UpdateMenuInfo", UpdateMenuInfo)
rr.POST("/SetMenuSortInfo", SetMenuSortInfo)
return
}
@ -26,7 +27,7 @@ func Routers(r *gin.RouterGroup) {
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param appId query string true "系统ID"
// @Param identityId query string true "身份ID"
// @Param identityId query int true "身份ID"
// @Success 200 {object} Model.Res
// @Router /base/menu/PageMenuInfo [get]
// @X-EmptyLimit ["appId","identityId"]
@ -51,8 +52,7 @@ func PageMenuInfo(c *gin.Context) {
c.JSON(http.StatusOK, Model.Res{
Success: r.Success,
Message: r.Message,
List: CommonUtil.ConvertStringArrayToArray(r.List),
Count: r.Count,
List: CommonUtil.ConvertJsonStringToMapArray("[" + r.List + "]"),
})
}
@ -83,7 +83,7 @@ func GetMenuInfo(c *gin.Context) {
c.JSON(http.StatusOK, Model.Res{
Success: r.Success,
Message: r.Message,
List: CommonUtil.ConvertStringArrayToArray(r.List),
List: CommonUtil.ConvertJsonStringToMapArray(r.List),
Count: r.Count,
})
}
@ -95,6 +95,7 @@ func GetMenuInfo(c *gin.Context) {
// @Produce json
// @Param appId formData string true "系统ID"
// @Param parentId formData string true "父节点ID"
// @Param identityId formData int true "身份ID"
// @Param menuName formData string true "菜单名称"
// @Param menuCode formData string true "菜单编码"
// @Param menuUrl formData string true "菜单的URL"
@ -102,7 +103,8 @@ func GetMenuInfo(c *gin.Context) {
// @Success 200 {object} Model.Res
// @Router /base/menu/AddMenuInfo [post]
// @X-EmptyLimit ["appId","parentId","menuName","menuCode","menuUrl","menuIcon"]
// @X-LengthLimit [{"appId":"36,36"},{"parentId":"36,36"},{"menuName":"2,50"},{"menuCode":"2,20"},{"menuUrl":"1,500"}]
// @X-IntRangeLimit [{"identityId":"1,4"}]
// @X-LengthLimit [{"appId":"36,36"},{"parentId":"36,36"},{"menuName":"2,50"},{"menuCode":"1,6"},{"menuUrl":"1,500"}]
// @X-RoleLimit ["1"]
// @X-Sort [3]
func AddMenuInfo(c *gin.Context) {
@ -110,6 +112,8 @@ func AddMenuInfo(c *gin.Context) {
appId := c.PostForm("appId")
//父节点ID
parentId := c.PostForm("parentId")
//身份ID
identityId := CommonUtil.ConvertStringToInt32(c.PostForm("identityId"))
//菜单名称
menuName := c.PostForm("menuName")
//菜单编码
@ -119,7 +123,7 @@ func AddMenuInfo(c *gin.Context) {
//菜单图标
menuIcon := c.PostForm("menuIcon")
r, err := BaseMenuService.AddBaseMenuInfo(appId,parentId,menuName, menuCode, menuUrl, menuIcon)
r, err := BaseMenuService.AddBaseMenuInfo(appId, parentId, identityId, menuName, menuCode, menuUrl, menuIcon)
if err != nil {
c.JSON(http.StatusOK, Model.Res{
Success: false,
@ -180,7 +184,7 @@ func DeleteMenuInfo(c *gin.Context) {
// @X-Sort [5]
func UpdateMenuInfo(c *gin.Context) {
//菜单ID
menuId := c.PostForm("menuName")
menuId := c.PostForm("menuId")
//菜单名称
menuName := c.PostForm("menuName")
//菜单编码

@ -2,11 +2,14 @@ package BaseMenuService
import (
"dsBaseWeb/Business/BaseMenu/BaseMenuProto"
"dsBaseWeb/Utils/CommonUtil"
"encoding/json"
"strings"
)
func PageBaseMenuInfo(appId string, identityId int32) (*BaseMenuProto.Reply, error) {
Reply, err := PageBaseMenu(BaseMenuProto.QueryArg{AppId: appId, IdentityId: identityId})
Reply.List = convertPositionTree(Reply.List)
return Reply, err
}
@ -15,8 +18,8 @@ func GetBaseMenuInfo(memuId string) (*BaseMenuProto.Reply, error) {
return Reply, err
}
func AddBaseMenuInfo(appId string, parentId string, menuName string, menuCode string, menuUrl string, menuIcon string) (*BaseMenuProto.Reply, error) {
Reply, err := AddBaseMenu(BaseMenuProto.ModelArg{AppId: appId, ParentId: parentId, MenuName: menuName, MenuCode: menuCode, MenuUrl: menuUrl, MenuIcon: menuIcon})
func AddBaseMenuInfo(appId string, parentId string, identityId int32, menuName string, menuCode string, menuUrl string, menuIcon string) (*BaseMenuProto.Reply, error) {
Reply, err := AddBaseMenu(BaseMenuProto.ModelArg{AppId: appId, ParentId: parentId, IdentityId: identityId, MenuName: menuName, MenuCode: menuCode, MenuUrl: menuUrl, MenuIcon: menuIcon})
return Reply, err
}
@ -35,3 +38,82 @@ func SetMenuSortInfo(menuId string, direction int32) (*BaseMenuProto.Reply, erro
Reply, err := SetMenuSort(BaseMenuProto.SetMenuSortArg{MenuId: menuId, Direction: direction})
return Reply, err
}
func convertPositionTree(listStr string) string {
list := CommonUtil.ConvertJsonStringToMapArray(listStr)
ListOrgNode := make([]*OrgNode, 0)
for i := range list {
record := list[i]
//添加数据
a := OrgNode{
MenuId: record["menu_id"].(string),
MenuName: record["menu_name"].(string),
ParentId: record["parent_id"].(string),
MenuCode: record["menu_code"].(string),
MenuIcon: record["menu_icon"].(string),
AppId: record["app_id"].(string),
SortId: record["sort_id"].(float64),
}
ListOrgNode = append(ListOrgNode, &a)
}
//结构体的SortId根据数据库获取时使用order by方法获取有序的子元素集合这里就不在代码中进行排序了。
//从根节点开始进行构建,递归调用生成tree
startOrgNode := ListOrgNode[0]
makeTree(ListOrgNode, startOrgNode)
//根据结构体生成json数据
jsonByte, _ := json.Marshal(startOrgNode)
return string(jsonByte)
}
/**
Struct
2020-06-03
*/
type OrgNode struct {
MenuId string `json:"menu_id"`
MenuName string `json:"menu_name"`
ParentId string `json:"parent_id"`
MenuCode string `json:"menu_code"`
MenuIcon string `json:"menu_icon"`
AppId string `json:"app_id"`
SortId float64 `json:"sort_id"`
Children []*OrgNode `json:"children,omitempty"`
}
/**
:
2020-06-03
*/
func makeTree(allNode []*OrgNode, node *OrgNode) {
children, _ := haveChild(allNode, node) //是不是有子节点
if children != nil {
node.Children = append(node.Children, children[0:]...) //添加子节点
for _, v := range children { //查询子节点的子节点,并添加到子节点
_, has := haveChild(allNode, v)
if has {
makeTree(allNode, v) //递归添加节点
}
}
}
}
/**
2020-06-03
*/
func haveChild(AllNode []*OrgNode, node *OrgNode) (children []*OrgNode, yes bool) {
for _, v := range AllNode {
if v.ParentId == node.MenuId {
children = append(children, v)
}
}
if children != nil {
yes = true
}
return
}

Loading…
Cancel
Save