123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package entity
- import (
- MC "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/encrypt"
- "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
- "database/sql"
- "github.com/zeromicro/go-zero/core/logx"
- "sort"
- "strconv"
- "strings"
- )
- // WorkDesktopMenu 工作桌面菜单信息
- type WorkDesktopMenu struct {
- MenuTree []*JYMenu
- UserId string
- NewUserId string
- EntId int64
- EntUserId int64
- AppId string
- }
- // 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,optional"` //是否显示取消按钮
- AppType string `json:"appType,optional,omitempty"` //跳转方式
- OpenType string `json:"openType,optional,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
- })
- 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, isShowCancel, appType, openType, feasibility = tv.AdditionalInfo.Title, tv.AdditionalInfo.Content, tv.AdditionalInfo.ConfirmUrl, tv.AdditionalInfo.ConfirmText, tv.AdditionalInfo.IsShowCancel, tv.AdditionalInfo.AppType, tv.AdditionalInfo.OpenType, usable
- )
- //三级菜单------
- if len(tv.Children) == 0 && (tv.CapitalCode != "" || tv.PermissionCode != "") {
- //用户是否需要留资
- //资源中台获取用户权限--没有权限:title等置空
- //ResourceLib.PowerHandle()
- title, content, confirmUrl, confirmText, appType, openType, isShowCancel, feasibility = CheckCapitalResources(tv, m.NewUserId, m.AppId, m.EntId, m.EntUserId, usable)
- }
- threeLevel := &pb.ThreeLevelMenu{
- Name: tv.Name,
- Icon: tv.Icon,
- Url: tv.Url,
- Id: encrypt.SE.EncodeString(strconv.Itoa(tv.Id)), //功能id加密
- Usable: feasibility, //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: isShowCancel,
- AppType: appType,
- OpenType: 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, appType, openType, isShowCancel, feasibility = CheckCapitalResources(fv, m.NewUserId, m.AppId, m.EntId, m.EntUserId, usable)
- }
- 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 = feasibility
- if fv.AdditionalInfo.Title != "" {
- threeLevel.TipInfo = &pb.TipInfo{
- Title: title,
- Content: content,
- ConfirmUrl: confirmUrl,
- ConfirmText: confirmText,
- IsShowCancel: isShowCancel,
- AppType: appType,
- OpenType: 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 !feasibility && tv.CheckCode == 1 {
- continue
- }
- if threeLevel != nil {
- secondLevel.Child = append(secondLevel.Child, threeLevel)
- }
- }
- }
- //二级菜单 有三级子菜单;才会显示
- if len(secondLevel.Child) == 0 && sv.CheckCode == 1 {
- continue
- }
- oneLevel.Child = append(oneLevel.Child, secondLevel)
- }
- }
- //一级菜单 有二级子菜单;才会显示
- if len(oneLevel.Child) == 0 && mv.CheckCode == 1 {
- continue
- }
- menuList = append(menuList, oneLevel)
- }
- //清用户内存信息
- func(baseUserid string) {
- OverallLock.Lock()
- defer OverallLock.Unlock()
- userInfo := UserInfoMap[baseUserid]
- if userInfo != nil {
- userInfo.Lock.Lock()
- defer userInfo.Lock.Unlock()
- userInfo.Permissions = map[string]int{}
- userInfo.Capitals = map[string]int{}
- }
- }(m.NewUserId)
- //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.AppId, m.EntId) //m.TimeOut, m.BigMemberOff,
- 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 ConfigJson.CommonlySize > 0 && mk >= int(ConfigJson.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
- }
|