workDesktop.go 14 KB

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