package entity import ( MC "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "database/sql" "github.com/zeromicro/go-zero/core/logx" "sort" "strconv" "strings" "userCenter/rpc/pb" ) // WorkDesktopMenu 工作桌面菜单信息 type WorkDesktopMenu struct { MenuTree []*JYMenu UserId string NewUserId string EntId int64 EntUserId int64 AppId string TimeOut int BigMemberOff bool } // JYMenu 工作桌面菜单内容 type JYMenu struct { Id int Name string //菜单名称 Match string //滤镜过滤正则 OrderId int //菜单排序id ParentId int //父级id PowerIds string //权限id CheckCode int //是否必须验证权限才显示菜单 Url string //菜单跳转链接 Icon string //菜单图标 AdditionalInfo Additional //附加弹窗信息 AppType string //菜单模式 OpenType string //打开方式 Status int //是否是可用服务 PermissionCode string //功能代码 CapitalCode string //留资代码 CapitalInfo Additional //留资弹窗 Children []*JYMenu } type Additional struct { Title string `json:"title"` Content string `json:"content"` ConfirmUrl string `json:"confirmUrl"` ConfirmText string `json:"confirmText"` IsShowCancel bool `json:"isShowCancel"` AppType string `json:"appType,omitempty"` OpenType string `json:"openType,omitempty"` } // WorkMenuTree 菜单-格式化-tree func (m *WorkDesktopMenu) WorkMenuTree(parentId int) (jyMenu []*JYMenu) { jyMenu = make([]*JYMenu, 0) for _, mv := range m.MenuTree { if mv.ParentId == parentId { mv.Children = m.WorkMenuTree(mv.Id) sort.SliceStable(mv.Children, func(i, j int) bool { return mv.Children[i].OrderId < mv.Children[j].OrderId }) jyMenu = append(jyMenu, mv) } } sort.SliceStable(jyMenu, func(i, j int) bool { return jyMenu[i].OrderId < jyMenu[j].OrderId }) //bytes, _ := json.MarshalIndent(jyMenu, "", " ") //fmt.Printf("%s\n", bytes) //log.Println("-------") return } // WorkMenuFormat 菜单 格式化 func (m *WorkDesktopMenu) WorkMenuFormat() ([]*pb.MenuList, error) { logx.Info("树的长度:", len(m.MenuTree)) menuList := make([]*pb.MenuList, 0) for _, mv := range m.MenuTree { //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示) usable := m.VerifyPermissions(mv.PowerIds) //checkCode==1 验证权限结果 无权限不显示 if !usable && mv.CheckCode == 1 { continue } //GetResources(m.AppId, m.NewUserId, m.EntId, m.EntUserId) oneLevel := &pb.MenuList{ Name: mv.Name, Icon: mv.Icon, Url: mv.Url, Id: encrypt.SE.EncodeString(strconv.Itoa(mv.Id)), //功能id加密 Usable: usable, //MC.If(mv.PowerIds != "", usable && mv.CheckCode == 1, true).(bool), AppType: mv.AppType, OpenType: mv.OpenType, Child: []*pb.SecondLevelMenu{}, TipInfo: &pb.TipInfo{ Title: mv.AdditionalInfo.Title, Content: mv.AdditionalInfo.Content, ConfirmUrl: mv.AdditionalInfo.ConfirmUrl, ConfirmText: mv.AdditionalInfo.ConfirmText, IsShowCancel: mv.AdditionalInfo.IsShowCancel, AppType: mv.AdditionalInfo.AppType, OpenType: mv.AdditionalInfo.OpenType, }, Match: MC.If(mv.Match != "", strings.Split(mv.Match, ","), []string{}).([]string), } if len(mv.Children) > 0 { for _, sv := range mv.Children { //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示) usable := m.VerifyPermissions(sv.PowerIds) //checkCode==1 验证权限结果 无权限不显示 if !usable && sv.CheckCode == 1 { continue } secondLevel := &pb.SecondLevelMenu{ Name: sv.Name, Icon: sv.Icon, Url: sv.Url, Id: encrypt.SE.EncodeString(strconv.Itoa(sv.Id)), //功能id加密 Usable: usable, //MC.If(sv.PowerIds != "", usable && sv.CheckCode == 1, true).(bool), Child: []*pb.ThreeLevelMenu{}, AppType: sv.AppType, OpenType: sv.OpenType, TipInfo: &pb.TipInfo{ Title: sv.AdditionalInfo.Title, Content: sv.AdditionalInfo.Content, ConfirmUrl: sv.AdditionalInfo.ConfirmUrl, ConfirmText: sv.AdditionalInfo.ConfirmText, IsShowCancel: sv.AdditionalInfo.IsShowCancel, AppType: sv.AdditionalInfo.AppType, OpenType: sv.AdditionalInfo.OpenType, }, Match: MC.If(sv.Match != "", strings.Split(sv.Match, ","), []string{}).([]string), } if len(sv.Children) > 0 { for _, tv := range sv.Children { //附件下载包、采购单位画像记录、企业画像记录 如果是大会员+超级订阅 排除此三项菜单 //灵活性降低,不应该这么搞 if (tv.Name == "附件下载包" && m.VerifyPermissions("3")) || (tv.Name == "采购单位画像记录" && m.VerifyPermissions("5")) || (tv.Name == "企业画像记录" && m.VerifyPermissions("4,12,13,19,20,21,23")) { continue } //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示) usable := m.VerifyPermissions(tv.PowerIds) //checkCode==1 验证权限结果 无权限不显示 if !usable && tv.CheckCode == 1 { continue } var ( title, content, confirmUrl, confirmText = tv.AdditionalInfo.Title, tv.AdditionalInfo.Content, tv.AdditionalInfo.ConfirmUrl, tv.AdditionalInfo.ConfirmText ) //三级菜单------ if len(tv.Children) == 0 && (tv.CapitalCode != "" || tv.PermissionCode != "") { //用户是否需要留资 //资源中台获取用户权限--没有权限:title等置空 //ResourceLib.PowerHandle() title, content, confirmUrl, confirmText, usable = CheckCapitalResources(tv.CapitalCode, m.NewUserId, m.AppId, m.EntId, m.EntUserId, tv.CapitalInfo, tv.PermissionCode) } threeLevel := &pb.ThreeLevelMenu{ Name: tv.Name, Icon: tv.Icon, Url: tv.Url, Id: encrypt.SE.EncodeString(strconv.Itoa(tv.Id)), //功能id加密 Usable: usable, //MC.If(tv.PowerIds != "", usable && tv.CheckCode == 1, true).(bool), AppType: tv.AppType, OpenType: tv.OpenType, TipInfo: &pb.TipInfo{ Title: title, Content: content, ConfirmUrl: confirmUrl, ConfirmText: confirmText, IsShowCancel: tv.AdditionalInfo.IsShowCancel, AppType: tv.AdditionalInfo.AppType, OpenType: tv.AdditionalInfo.OpenType, }, Match: MC.If(tv.Match != "", strings.Split(tv.Match, ","), []string{}).([]string), } if len(tv.Children) > 0 { L: for _, fv := range tv.Children { if fv.Name == "" { continue } if fv.CapitalCode != "" || fv.PermissionCode != "" { //用户是否需要留资 //资源中台获取用户权限--没有权限:title等置空 //ResourceLib.PowerHandle() title, content, confirmUrl, confirmText, usable = CheckCapitalResources(fv.CapitalCode, m.NewUserId, m.AppId, m.EntId, m.EntUserId, fv.CapitalInfo, fv.PermissionCode) } threeLevel.Url = MC.If(fv.Url != "", fv.Url, tv.Url).(string) threeLevel.Icon = MC.If(fv.Icon != "", fv.Icon, tv.Icon).(string) threeLevel.AppType = fv.AppType threeLevel.OpenType = fv.OpenType threeLevel.Usable = usable if fv.AdditionalInfo.Title != "" { threeLevel.TipInfo = &pb.TipInfo{ Title: title, Content: content, ConfirmUrl: confirmUrl, ConfirmText: confirmText, IsShowCancel: fv.AdditionalInfo.IsShowCancel, AppType: fv.AdditionalInfo.AppType, OpenType: fv.AdditionalInfo.OpenType, } } threeLevel.Match = MC.If(fv.Match != "", strings.Split(fv.Match, ","), []string{}).([]string) //四级菜单必须有顺序性,大会员》商机管理》超级订阅》免费用户 name := MC.If(strings.Contains(fv.Name, "-"), strings.Split(fv.Name, "-")[0], "免费").(string) if len(UserRolePowers[name]) > 0 && m.VerifyPermissions(strings.Join(UserRolePowers[name], ",")) { break L } } } if threeLevel != nil { secondLevel.Child = append(secondLevel.Child, threeLevel) } } } oneLevel.Child = append(oneLevel.Child, secondLevel) } } menuList = append(menuList, oneLevel) } //bytes, _ := json.MarshalIndent(menuList, "", " ") //fmt.Printf("%s\n", bytes) return menuList, nil } // VerifyPermissions 是否有权限可用此服务 func (m *WorkDesktopMenu) VerifyPermissions(powerIds string) (b bool) { if powerIds == "" { return true } if len(strings.Split(powerIds, ",")) > 0 { userPower := AutoUserPowerInfo(m.UserId, m.NewUserId, m.AppId, m.TimeOut, m.BigMemberOff, m.EntId) func(powerIds string) { for _, pv := range strings.Split(powerIds, ",") { if userPower[pv] > 0 { b = true break } } }(powerIds) } return } //常用功能 const ( WorkCommonly = "work_commonly" WorkMenu = "work_menu" ) // CommonlyUpdate 更新常用功能 func CommonlyUpdate(in *pb.WorkDesktopComprehensiveReq) (B bool, M string) { //事务 1:查; 2: 存;3: 删; var ( ids []string mk int ) for _, mid := range strings.Split(in.MenuIds, ",") { if mid == "" { continue } mk++ //常用功能数量限制 if in.CommonlySize > 0 && mk >= int(in.CommonlySize) { break } //id 解密 ids = append(ids, encrypt.SE.DecodeString(mid)) } //更新此用户设置的常用功能 if B = BaseMysql.ExecTx("常用功能批量更新", func(tx *sql.Tx) bool { //查询此用户常用功能是否已存在记录 var id = 0 existingData := BaseMysql.SelectBySqlByTx(tx, `SELECT id FROM `+WorkCommonly+` WHERE base_userid = ? AND appid = ? AND field = ? AND platform = ? ORDER BY id DESC `, in.NewUserId, in.AppId, in.ActionMode, in.Platform) if existingData != nil && len(*existingData) > 0 { id = MC.IntAll((*existingData)[0]["id"]) } switch { case id > 0: //更新 if BaseMysql.UpdateOrDeleteBySqlByTx(tx, `UPDATE `+WorkCommonly+` SET value = ? WHERE id = ?`, strings.Join(ids, ","), id) < 0 { logx.Info("常用功能-更新数据失败") return false } default: //插入 if BaseMysql.InsertBySqlByTx(tx, `INSERT INTO `+WorkCommonly+` (appid,base_userid,platform,field,value) VALUES (?,?,?,?,?)`, in.AppId, in.NewUserId, in.Platform, in.ActionMode, strings.Join(ids, ",")) < 0 { logx.Info("常用功能-插入数据失败") return false } } return true }); !B { M = "常用功能更新数据失败" } return } // CommonlyFormat 常用功能 格式化 func (m *WorkDesktopMenu) CommonlyFormat(childMenus map[int][]*JYMenu) ([]*pb.ThreeLevelMenu, []string, bool) { menuList := make([]*pb.ThreeLevelMenu, 0) subLevel := map[int]bool{} delBool := false //更新因超级订阅用户,又购买大会员得用户 导致附件下载包||采购单位画像记录||企业画像记录 不应该展示 var saveIds []string for _, mv := range m.MenuTree { //过滤子级 ++ 三级下架菜单:mv.Status == 1 if subLevel[mv.ParentId] || mv.Status == 1 { continue } //附件下载包、采购单位画像记录、企业画像记录 如果是大会员+超级订阅 排除此三项菜单 //灵活性降低,不应该这么搞 if (mv.Name == "附件下载包" && m.VerifyPermissions("3")) || (mv.Name == "采购单位画像记录" && m.VerifyPermissions("5")) || (mv.Name == "企业画像记录" && m.VerifyPermissions("4,12,13,19,20,21,23")) { delBool = true continue } saveIds = append(saveIds, encrypt.SE.EncodeString(strconv.Itoa(mv.Id))) subLevel[mv.Id] = true //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示) usable := m.VerifyPermissions(mv.PowerIds) oneLevel := &pb.ThreeLevelMenu{ Name: mv.Name, Icon: mv.Icon, Url: mv.Url, Id: encrypt.SE.EncodeString(strconv.Itoa(mv.Id)), //功能id加密 Usable: usable, //MC.If(mv.PowerIds != "", usable && mv.CheckCode == 1, true).(bool), AppType: mv.AppType, OpenType: mv.OpenType, TipInfo: &pb.TipInfo{ Title: mv.AdditionalInfo.Title, Content: mv.AdditionalInfo.Content, ConfirmUrl: mv.AdditionalInfo.ConfirmUrl, ConfirmText: mv.AdditionalInfo.ConfirmText, IsShowCancel: mv.AdditionalInfo.IsShowCancel, AppType: mv.AppType, OpenType: mv.OpenType, }, Match: MC.If(mv.Match != "", strings.Split(mv.Match, ","), []string{}).([]string), } //处理子级 if childMenus[mv.Id] != nil && len(childMenus[mv.Id]) > 0 { childMenu := childMenus[mv.Id] sort.Slice(childMenu, func(i, j int) bool { return childMenu[i].OrderId > childMenu[j].OrderId }) L: for _, cv := range childMenu { if cv.Name == "" || cv.ParentId != mv.Id { continue } oneLevel.Url = MC.If(cv.Url != "", cv.Url, mv.Url).(string) oneLevel.Icon = MC.If(cv.Icon != "", cv.Icon, mv.Icon).(string) oneLevel.AppType = cv.AppType oneLevel.OpenType = cv.OpenType if cv.AdditionalInfo.Title != "" { oneLevel.TipInfo = &pb.TipInfo{ Title: cv.AdditionalInfo.Title, Content: cv.AdditionalInfo.Content, ConfirmUrl: cv.AdditionalInfo.ConfirmUrl, ConfirmText: cv.AdditionalInfo.ConfirmText, IsShowCancel: cv.AdditionalInfo.IsShowCancel, AppType: cv.AppType, OpenType: cv.OpenType, } } oneLevel.Match = MC.If(cv.Match != "", strings.Split(cv.Match, ","), []string{}).([]string) name := MC.If(strings.Contains(cv.Name, "-"), strings.Split(cv.Name, "-")[0], "免费").(string) if len(UserRolePowers[name]) > 0 && m.VerifyPermissions(strings.Join(UserRolePowers[name], ",")) { break L } } } menuList = append(menuList, oneLevel) } return menuList, saveIds, delBool }