workDesktop.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package entity
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  6. "database/sql"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. )
  12. // WorkDesktopMenu 工作桌面菜单信息
  13. type WorkDesktopMenu struct {
  14. MenuTree []*JYMenu
  15. UserId string
  16. NewUserId string
  17. EntId int64
  18. EntUserId int64
  19. AppId string
  20. TimeOut int
  21. BigMemberOff bool
  22. }
  23. // JYMenu 工作桌面菜单内容
  24. type JYMenu struct {
  25. Id int
  26. Name string //菜单名称
  27. Match string //滤镜过滤正则
  28. OrderId int //菜单排序id
  29. ParentId int //父级id
  30. PowerIds string //权限id
  31. CheckCode int //是否必须验证权限才显示菜单
  32. Url string //菜单跳转链接
  33. Icon string //菜单图标
  34. AdditionalInfo Additional //附加弹窗信息
  35. AppType string //菜单模式
  36. OpenType string //打开方式
  37. Status int //是否是可用服务
  38. PermissionCode string //功能代码
  39. CapitalCode string //留资代码
  40. CapitalInfo Additional //留资弹窗
  41. Children []*JYMenu
  42. }
  43. type Additional struct {
  44. Title string `json:"title"`
  45. Content string `json:"content"`
  46. ConfirmUrl string `json:"confirmUrl"`
  47. ConfirmText string `json:"confirmText"`
  48. IsShowCancel bool `json:"isShowCancel"`
  49. AppType string `json:"appType,omitempty"`
  50. OpenType string `json:"openType,omitempty"`
  51. }
  52. // WorkMenuTree 菜单-格式化-tree
  53. func (m *WorkDesktopMenu) WorkMenuTree(parentId int) (jyMenu []*JYMenu) {
  54. jyMenu = make([]*JYMenu, 0)
  55. for _, mv := range m.MenuTree {
  56. if mv.ParentId == parentId {
  57. mv.Children = m.WorkMenuTree(mv.Id)
  58. sort.SliceStable(mv.Children, func(i, j int) bool {
  59. return mv.Children[i].OrderId < mv.Children[j].OrderId
  60. })
  61. jyMenu = append(jyMenu, mv)
  62. }
  63. }
  64. sort.SliceStable(jyMenu, func(i, j int) bool {
  65. return jyMenu[i].OrderId < jyMenu[j].OrderId
  66. })
  67. return
  68. }
  69. // WorkMenuFormat 菜单 格式化
  70. func (m *WorkDesktopMenu) WorkMenuFormat() ([]*pb.MenuList, error) {
  71. logx.Info("树的长度:", len(m.MenuTree))
  72. menuList := make([]*pb.MenuList, 0)
  73. for _, mv := range m.MenuTree {
  74. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  75. usable := m.VerifyPermissions(mv.PowerIds)
  76. //checkCode==1 验证权限结果 无权限不显示
  77. if !usable && mv.CheckCode == 1 {
  78. continue
  79. }
  80. //GetResources(m.AppId, m.NewUserId, m.EntId, m.EntUserId)
  81. oneLevel := &pb.MenuList{
  82. Name: mv.Name,
  83. Icon: mv.Icon,
  84. Url: mv.Url,
  85. Id: encrypt.SE.EncodeString(strconv.Itoa(mv.Id)), //功能id加密
  86. Usable: usable, //MC.If(mv.PowerIds != "", usable && mv.CheckCode == 1, true).(bool),
  87. AppType: mv.AppType,
  88. OpenType: mv.OpenType,
  89. Child: []*pb.SecondLevelMenu{},
  90. TipInfo: &pb.TipInfo{
  91. Title: mv.AdditionalInfo.Title,
  92. Content: mv.AdditionalInfo.Content,
  93. ConfirmUrl: mv.AdditionalInfo.ConfirmUrl,
  94. ConfirmText: mv.AdditionalInfo.ConfirmText,
  95. IsShowCancel: mv.AdditionalInfo.IsShowCancel,
  96. AppType: mv.AdditionalInfo.AppType,
  97. OpenType: mv.AdditionalInfo.OpenType,
  98. },
  99. Match: MC.If(mv.Match != "", strings.Split(mv.Match, ","), []string{}).([]string),
  100. }
  101. if len(mv.Children) > 0 {
  102. for _, sv := range mv.Children {
  103. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  104. usable := m.VerifyPermissions(sv.PowerIds)
  105. //checkCode==1 验证权限结果 无权限不显示
  106. if !usable && sv.CheckCode == 1 {
  107. continue
  108. }
  109. secondLevel := &pb.SecondLevelMenu{
  110. Name: sv.Name,
  111. Icon: sv.Icon,
  112. Url: sv.Url,
  113. Id: encrypt.SE.EncodeString(strconv.Itoa(sv.Id)), //功能id加密
  114. Usable: usable, //MC.If(sv.PowerIds != "", usable && sv.CheckCode == 1, true).(bool),
  115. Child: []*pb.ThreeLevelMenu{},
  116. AppType: sv.AppType,
  117. OpenType: sv.OpenType,
  118. TipInfo: &pb.TipInfo{
  119. Title: sv.AdditionalInfo.Title,
  120. Content: sv.AdditionalInfo.Content,
  121. ConfirmUrl: sv.AdditionalInfo.ConfirmUrl,
  122. ConfirmText: sv.AdditionalInfo.ConfirmText,
  123. IsShowCancel: sv.AdditionalInfo.IsShowCancel,
  124. AppType: sv.AdditionalInfo.AppType,
  125. OpenType: sv.AdditionalInfo.OpenType,
  126. },
  127. Match: MC.If(sv.Match != "", strings.Split(sv.Match, ","), []string{}).([]string),
  128. }
  129. if len(sv.Children) > 0 {
  130. for _, tv := range sv.Children {
  131. //附件下载包、采购单位画像记录、企业画像记录 如果是大会员+超级订阅 排除此三项菜单
  132. //灵活性降低,不应该这么搞
  133. if (tv.Name == "附件下载包" && m.VerifyPermissions("3")) || (tv.Name == "采购单位画像记录" && m.VerifyPermissions("5")) || (tv.Name == "企业画像记录" && m.VerifyPermissions("4,12,13,19,20,21,23")) {
  134. continue
  135. }
  136. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  137. usable := m.VerifyPermissions(tv.PowerIds)
  138. //checkCode==1 验证权限结果 无权限不显示
  139. if !usable && tv.CheckCode == 1 {
  140. continue
  141. }
  142. var (
  143. 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
  144. )
  145. //三级菜单------
  146. if len(tv.Children) == 0 && (tv.CapitalCode != "" || tv.PermissionCode != "") {
  147. //用户是否需要留资
  148. //资源中台获取用户权限--没有权限:title等置空
  149. //ResourceLib.PowerHandle()
  150. title, content, confirmUrl, confirmText, appType, openType, isShowCancel, feasibility = CheckCapitalResources(tv, m.NewUserId, m.AppId, m.EntId, m.EntUserId, usable)
  151. }
  152. threeLevel := &pb.ThreeLevelMenu{
  153. Name: tv.Name,
  154. Icon: tv.Icon,
  155. Url: tv.Url,
  156. Id: encrypt.SE.EncodeString(strconv.Itoa(tv.Id)), //功能id加密
  157. Usable: feasibility, //MC.If(tv.PowerIds != "", usable && tv.CheckCode == 1, true).(bool),
  158. AppType: tv.AppType,
  159. OpenType: tv.OpenType,
  160. TipInfo: &pb.TipInfo{
  161. Title: title,
  162. Content: content,
  163. ConfirmUrl: confirmUrl,
  164. ConfirmText: confirmText,
  165. IsShowCancel: isShowCancel,
  166. AppType: appType,
  167. OpenType: openType,
  168. },
  169. Match: MC.If(tv.Match != "", strings.Split(tv.Match, ","), []string{}).([]string),
  170. }
  171. if len(tv.Children) > 0 {
  172. L:
  173. for _, fv := range tv.Children {
  174. if fv.Name == "" {
  175. continue
  176. }
  177. if fv.CapitalCode != "" || fv.PermissionCode != "" {
  178. //用户是否需要留资
  179. //资源中台获取用户权限--没有权限:title等置空
  180. //ResourceLib.PowerHandle()
  181. title, content, confirmUrl, confirmText, appType, openType, isShowCancel, feasibility = CheckCapitalResources(fv, m.NewUserId, m.AppId, m.EntId, m.EntUserId, usable)
  182. }
  183. threeLevel.Url = MC.If(fv.Url != "", fv.Url, tv.Url).(string)
  184. threeLevel.Icon = MC.If(fv.Icon != "", fv.Icon, tv.Icon).(string)
  185. threeLevel.AppType = fv.AppType
  186. threeLevel.OpenType = fv.OpenType
  187. threeLevel.Usable = feasibility
  188. if fv.AdditionalInfo.Title != "" {
  189. threeLevel.TipInfo = &pb.TipInfo{
  190. Title: title,
  191. Content: content,
  192. ConfirmUrl: confirmUrl,
  193. ConfirmText: confirmText,
  194. IsShowCancel: isShowCancel,
  195. AppType: appType,
  196. OpenType: openType,
  197. }
  198. }
  199. threeLevel.Match = MC.If(fv.Match != "", strings.Split(fv.Match, ","), []string{}).([]string)
  200. //四级菜单必须有顺序性,大会员》商机管理》超级订阅》免费用户
  201. name := MC.If(strings.Contains(fv.Name, "-"), strings.Split(fv.Name, "-")[0], "免费").(string)
  202. if len(UserRolePowers[name]) > 0 && m.VerifyPermissions(strings.Join(UserRolePowers[name], ",")) {
  203. break L
  204. }
  205. }
  206. }
  207. if strings.Contains(tv.Name, "知识库管理") || strings.Contains(tv.Name, "客服工作台") {
  208. logx.Info(m.EntUserId, "------entuserid---", feasibility, "---", tv.CheckCode, "---", tv.PermissionCode)
  209. }
  210. //无资源中台权限 菜单不显示
  211. if !feasibility && tv.CheckCode == 1 {
  212. continue
  213. }
  214. if threeLevel != nil {
  215. secondLevel.Child = append(secondLevel.Child, threeLevel)
  216. }
  217. }
  218. }
  219. //二级菜单 有三级子菜单;才会显示
  220. if len(secondLevel.Child) == 0 && sv.CheckCode == 1 {
  221. continue
  222. }
  223. oneLevel.Child = append(oneLevel.Child, secondLevel)
  224. }
  225. }
  226. //一级菜单 有二级子菜单;才会显示
  227. if len(oneLevel.Child) == 0 && mv.CheckCode == 1 {
  228. continue
  229. }
  230. menuList = append(menuList, oneLevel)
  231. }
  232. //清用户内存信息
  233. func(baseUserid string) {
  234. OverallLock.Lock()
  235. defer OverallLock.Unlock()
  236. userInfo := UserInfoMap[baseUserid]
  237. if userInfo != nil {
  238. userInfo.Lock.Lock()
  239. defer userInfo.Lock.Unlock()
  240. userInfo.Permissions = map[string]int{}
  241. userInfo.Capitals = map[string]int{}
  242. }
  243. }(m.NewUserId)
  244. //bytes, _ := json.MarshalIndent(menuList, "", " ")
  245. //fmt.Printf("%s\n", bytes)
  246. return menuList, nil
  247. }
  248. // VerifyPermissions 是否有权限可用此服务
  249. func (m *WorkDesktopMenu) VerifyPermissions(powerIds string) (b bool) {
  250. if powerIds == "" {
  251. return true
  252. }
  253. if len(strings.Split(powerIds, ",")) > 0 {
  254. userPower := AutoUserPowerInfo(m.UserId, m.AppId, m.TimeOut, m.BigMemberOff, m.EntId)
  255. func(powerIds string) {
  256. for _, pv := range strings.Split(powerIds, ",") {
  257. if userPower[pv] > 0 {
  258. b = true
  259. break
  260. }
  261. }
  262. }(powerIds)
  263. }
  264. return
  265. }
  266. //常用功能
  267. const (
  268. WorkCommonly = "work_commonly"
  269. WorkMenu = "work_menu"
  270. )
  271. // CommonlyUpdate 更新常用功能
  272. func CommonlyUpdate(in *pb.WorkDesktopComprehensiveReq) (B bool, M string) {
  273. //事务 1:查; 2: 存;3: 删;
  274. var (
  275. ids []string
  276. mk int
  277. )
  278. for _, mid := range strings.Split(in.MenuIds, ",") {
  279. if mid == "" {
  280. continue
  281. }
  282. mk++
  283. //常用功能数量限制
  284. if in.CommonlySize > 0 && mk >= int(in.CommonlySize) {
  285. break
  286. }
  287. //id 解密
  288. ids = append(ids, encrypt.SE.DecodeString(mid))
  289. }
  290. //更新此用户设置的常用功能
  291. if B = BaseMysql.ExecTx("常用功能批量更新", func(tx *sql.Tx) bool {
  292. //查询此用户常用功能是否已存在记录
  293. var id = 0
  294. 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)
  295. if existingData != nil && len(*existingData) > 0 {
  296. id = MC.IntAll((*existingData)[0]["id"])
  297. }
  298. switch {
  299. case id > 0: //更新
  300. if BaseMysql.UpdateOrDeleteBySqlByTx(tx, `UPDATE `+WorkCommonly+` SET value = ? WHERE id = ?`, strings.Join(ids, ","), id) < 0 {
  301. logx.Info("常用功能-更新数据失败")
  302. return false
  303. }
  304. default: //插入
  305. 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 {
  306. logx.Info("常用功能-插入数据失败")
  307. return false
  308. }
  309. }
  310. return true
  311. }); !B {
  312. M = "常用功能更新数据失败"
  313. }
  314. return
  315. }
  316. // CommonlyFormat 常用功能 格式化
  317. func (m *WorkDesktopMenu) CommonlyFormat(childMenus map[int][]*JYMenu) ([]*pb.ThreeLevelMenu, []string, bool) {
  318. menuList := make([]*pb.ThreeLevelMenu, 0)
  319. subLevel := map[int]bool{}
  320. delBool := false //更新因超级订阅用户,又购买大会员得用户 导致附件下载包||采购单位画像记录||企业画像记录 不应该展示
  321. var saveIds []string
  322. for _, mv := range m.MenuTree {
  323. //过滤子级 ++ 三级下架菜单:mv.Status == 1
  324. if subLevel[mv.ParentId] || mv.Status == 1 {
  325. continue
  326. }
  327. //附件下载包、采购单位画像记录、企业画像记录 如果是大会员+超级订阅 排除此三项菜单
  328. //灵活性降低,不应该这么搞
  329. if (mv.Name == "附件下载包" && m.VerifyPermissions("3")) || (mv.Name == "采购单位画像记录" && m.VerifyPermissions("5")) || (mv.Name == "企业画像记录" && m.VerifyPermissions("4,12,13,19,20,21,23")) {
  330. delBool = true
  331. continue
  332. }
  333. saveIds = append(saveIds, encrypt.SE.EncodeString(strconv.Itoa(mv.Id)))
  334. subLevel[mv.Id] = true
  335. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  336. usable := m.VerifyPermissions(mv.PowerIds)
  337. oneLevel := &pb.ThreeLevelMenu{
  338. Name: mv.Name,
  339. Icon: mv.Icon,
  340. Url: mv.Url,
  341. Id: encrypt.SE.EncodeString(strconv.Itoa(mv.Id)), //功能id加密
  342. Usable: usable, //MC.If(mv.PowerIds != "", usable && mv.CheckCode == 1, true).(bool),
  343. AppType: mv.AppType,
  344. OpenType: mv.OpenType,
  345. TipInfo: &pb.TipInfo{
  346. Title: mv.AdditionalInfo.Title,
  347. Content: mv.AdditionalInfo.Content,
  348. ConfirmUrl: mv.AdditionalInfo.ConfirmUrl,
  349. ConfirmText: mv.AdditionalInfo.ConfirmText,
  350. IsShowCancel: mv.AdditionalInfo.IsShowCancel,
  351. AppType: mv.AppType,
  352. OpenType: mv.OpenType,
  353. },
  354. Match: MC.If(mv.Match != "", strings.Split(mv.Match, ","), []string{}).([]string),
  355. }
  356. //处理子级
  357. if childMenus[mv.Id] != nil && len(childMenus[mv.Id]) > 0 {
  358. childMenu := childMenus[mv.Id]
  359. sort.Slice(childMenu, func(i, j int) bool {
  360. return childMenu[i].OrderId > childMenu[j].OrderId
  361. })
  362. L:
  363. for _, cv := range childMenu {
  364. if cv.Name == "" || cv.ParentId != mv.Id {
  365. continue
  366. }
  367. oneLevel.Url = MC.If(cv.Url != "", cv.Url, mv.Url).(string)
  368. oneLevel.Icon = MC.If(cv.Icon != "", cv.Icon, mv.Icon).(string)
  369. oneLevel.AppType = cv.AppType
  370. oneLevel.OpenType = cv.OpenType
  371. if cv.AdditionalInfo.Title != "" {
  372. oneLevel.TipInfo = &pb.TipInfo{
  373. Title: cv.AdditionalInfo.Title,
  374. Content: cv.AdditionalInfo.Content,
  375. ConfirmUrl: cv.AdditionalInfo.ConfirmUrl,
  376. ConfirmText: cv.AdditionalInfo.ConfirmText,
  377. IsShowCancel: cv.AdditionalInfo.IsShowCancel,
  378. AppType: cv.AppType,
  379. OpenType: cv.OpenType,
  380. }
  381. }
  382. oneLevel.Match = MC.If(cv.Match != "", strings.Split(cv.Match, ","), []string{}).([]string)
  383. name := MC.If(strings.Contains(cv.Name, "-"), strings.Split(cv.Name, "-")[0], "免费").(string)
  384. if len(UserRolePowers[name]) > 0 && m.VerifyPermissions(strings.Join(UserRolePowers[name], ",")) {
  385. break L
  386. }
  387. }
  388. }
  389. menuList = append(menuList, oneLevel)
  390. }
  391. return menuList, saveIds, delBool
  392. }