workDesktop.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. TimeOut int
  17. BigMemberOff bool
  18. }
  19. // JYMenu 工作桌面菜单内容
  20. type JYMenu struct {
  21. Id int
  22. Name string
  23. OrderId int
  24. ParentId int
  25. PowerIds string
  26. CheckCode int
  27. Url string
  28. Icon string
  29. AdditionalInfo Additional
  30. AppType string
  31. OpenType string
  32. Children []*JYMenu
  33. }
  34. type Additional struct {
  35. Title string `json:"title"`
  36. Content string `json:"content"`
  37. ConfirmUrl string `json:"confirmUrl"`
  38. ConfirmText string `json:"confirmText"`
  39. IsShowCancel bool `json:"isShowCancel"`
  40. }
  41. // WorkMenuTree 菜单-格式化-tree
  42. func (m *WorkDesktopMenu) WorkMenuTree(parentId int) (jyMenu []*JYMenu) {
  43. jyMenu = make([]*JYMenu, 0)
  44. for _, mv := range m.MenuTree {
  45. if mv.ParentId == parentId {
  46. mv.Children = m.WorkMenuTree(mv.Id)
  47. sort.SliceStable(mv.Children, func(i, j int) bool {
  48. return mv.Children[i].OrderId < mv.Children[j].OrderId
  49. })
  50. jyMenu = append(jyMenu, mv)
  51. }
  52. }
  53. sort.SliceStable(jyMenu, func(i, j int) bool {
  54. return jyMenu[i].OrderId < jyMenu[j].OrderId
  55. })
  56. //bytes, _ := json.MarshalIndent(jyMenu, "", " ")
  57. //fmt.Printf("%s\n", bytes)
  58. //log.Println("-------")
  59. return
  60. }
  61. var verification = map[string]string{
  62. "商机管理": "100,101,102,110,111,112",
  63. "大会员": "1",
  64. "超级订阅": "200,201",
  65. }
  66. // WorkMenuFormat 菜单 格式化
  67. func (m *WorkDesktopMenu) WorkMenuFormat() ([]*pb.MenuList, error) {
  68. logx.Info("树的长度:", len(m.MenuTree))
  69. menuList := make([]*pb.MenuList, 0)
  70. for _, mv := range m.MenuTree {
  71. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  72. usable := m.VerifyPermissions(mv.PowerIds)
  73. //checkCode==1 验证权限结果 无权限不显示
  74. if !usable && mv.CheckCode == 1 {
  75. continue
  76. }
  77. oneLevel := &pb.MenuList{
  78. Name: mv.Name,
  79. Icon: mv.Icon,
  80. Url: mv.Url,
  81. Id: encrypt.SE.EncodeString(strconv.Itoa(mv.Id)), //功能id加密
  82. Usable: MC.If(mv.PowerIds != "", usable && mv.CheckCode == 1, true).(bool),
  83. Child: []*pb.SecondLevelMenu{},
  84. }
  85. if len(mv.Children) > 0 {
  86. for _, sv := range mv.Children {
  87. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  88. usable := m.VerifyPermissions(mv.PowerIds)
  89. //checkCode==1 验证权限结果 无权限不显示
  90. if !usable && mv.CheckCode == 1 {
  91. continue
  92. }
  93. secondLevel := &pb.SecondLevelMenu{
  94. Name: sv.Name,
  95. Icon: sv.Icon,
  96. Url: sv.Url,
  97. Id: encrypt.SE.EncodeString(strconv.Itoa(sv.Id)), //功能id加密
  98. Usable: MC.If(mv.PowerIds != "", usable && mv.CheckCode == 1, true).(bool),
  99. Child: []*pb.ThreeLevelMenu{},
  100. }
  101. if len(sv.Children) > 0 {
  102. for _, tv := range sv.Children {
  103. //0:默认展示;1:需验证权限,有权限展示;2:需验证权限,无权限也可展示(可用服务无权限不展示)
  104. usable := m.VerifyPermissions(mv.PowerIds)
  105. //checkCode==1 验证权限结果 无权限不显示
  106. if !usable && mv.CheckCode == 1 {
  107. continue
  108. }
  109. threeLevel := &pb.ThreeLevelMenu{
  110. Name: tv.Name,
  111. Icon: tv.Icon,
  112. Url: tv.Url,
  113. Id: encrypt.SE.EncodeString(strconv.Itoa(tv.Id)), //功能id加密
  114. Usable: MC.If(mv.PowerIds != "", usable && mv.CheckCode == 1, true).(bool),
  115. AppType: tv.AppType,
  116. OpenType: tv.OpenType,
  117. TipInfo: &pb.TipInfo{
  118. Title: tv.AdditionalInfo.Title,
  119. Content: tv.AdditionalInfo.Content,
  120. ConfirmUrl: tv.AdditionalInfo.ConfirmUrl,
  121. ConfirmText: tv.AdditionalInfo.ConfirmText,
  122. IsShowCancel: tv.AdditionalInfo.IsShowCancel,
  123. },
  124. }
  125. if len(tv.Children) > 0 {
  126. for _, fv := range tv.Children {
  127. if fv.Name == "" {
  128. continue
  129. }
  130. threeLevel.Url = MC.If(fv.Url != "", fv.Url, tv.Url).(string)
  131. threeLevel.Icon = MC.If(fv.Icon != "", fv.Icon, tv.Icon).(string)
  132. if fv.AdditionalInfo.Title != "" {
  133. threeLevel.TipInfo = &pb.TipInfo{
  134. Title: fv.AdditionalInfo.Title,
  135. Content: fv.AdditionalInfo.Content,
  136. ConfirmUrl: fv.AdditionalInfo.ConfirmUrl,
  137. ConfirmText: fv.AdditionalInfo.ConfirmText,
  138. IsShowCancel: fv.AdditionalInfo.IsShowCancel,
  139. }
  140. }
  141. //订阅是商机管理优先 其他功能是大会员优先(这样是有问题的 有些功能超级订阅用户可用 大会员用户权不可用)
  142. //解决上面的问题:菜单只保留三级菜单;四级菜单信息不入库;判断三级菜单权限
  143. switch {
  144. case strings.Contains(tv.Name, "订阅"):
  145. switch {
  146. case strings.HasPrefix(fv.Name, "商机管理") && m.VerifyPermissions(verification["商机管理"]):
  147. break
  148. case strings.HasPrefix(fv.Name, "大会员") && m.VerifyPermissions(verification["大会员"]):
  149. break
  150. case strings.HasPrefix(fv.Name, "超级订阅") && m.VerifyPermissions(verification["超级订阅"]):
  151. break
  152. }
  153. default:
  154. switch {
  155. case strings.HasPrefix(fv.Name, "大会员") && m.VerifyPermissions(verification["大会员"]):
  156. break
  157. case strings.HasPrefix(fv.Name, "商机管理") && m.VerifyPermissions(verification["商机管理"]):
  158. break
  159. case strings.HasPrefix(fv.Name, "超级订阅") && m.VerifyPermissions(verification["超级订阅"]):
  160. break
  161. }
  162. }
  163. }
  164. }
  165. if threeLevel != nil {
  166. secondLevel.Child = append(secondLevel.Child, threeLevel)
  167. }
  168. }
  169. }
  170. oneLevel.Child = append(oneLevel.Child, secondLevel)
  171. }
  172. }
  173. menuList = append(menuList, oneLevel)
  174. }
  175. //bytes, _ := json.MarshalIndent(menuList, "", " ")
  176. //fmt.Printf("%s\n", bytes)
  177. return menuList, nil
  178. }
  179. // VerifyPermissions 是否有权限可用此服务
  180. func (m *WorkDesktopMenu) VerifyPermissions(powerIds string) (b bool) {
  181. if powerIds == "" {
  182. return true
  183. }
  184. if len(strings.Split(powerIds, ",")) > 0 {
  185. PowerLock.Lock()
  186. userPower := UserPowerMapMap[m.UserId]
  187. PowerLock.Unlock()
  188. if userPower == nil {
  189. userPower = AutoUserPowerInfo(m.UserId, m.TimeOut, m.BigMemberOff)
  190. }
  191. func(powerIds string) {
  192. UserPowerLock[m.UserId].Lock()
  193. defer UserPowerLock[m.UserId].Unlock()
  194. for _, pv := range strings.Split(powerIds, ",") {
  195. if userPower[pv] > 0 {
  196. b = true
  197. break
  198. }
  199. }
  200. }(powerIds)
  201. }
  202. return
  203. }
  204. //常用功能
  205. const (
  206. WorkCommonly = "work_commonly"
  207. WorkMenu = "work_menu"
  208. )
  209. var (
  210. InsertCollKey = []string{"appid", "userid", "platform", "field", "value", "ischeck"}
  211. )
  212. //更新常用功能
  213. func CommonlyUpdate(in *pb.WorkDesktopComprehensiveReq, size int) (B bool, M string) {
  214. //事务 1:查; 2: 存;3: 删;
  215. var ids []string
  216. for mk, mid := range strings.Split(in.MenuIds, ",") {
  217. //常用功能数量限制
  218. if size > 0 && mk >= size {
  219. break
  220. }
  221. //id 解密
  222. ids = append(ids, encrypt.SE.DecodeString(mid))
  223. }
  224. //更新此用户设置的常用功能
  225. if B = BaseMysql.ExecTx("常用功能批量更新", func(tx *sql.Tx) bool {
  226. //查询此用户常用功能是否已存在记录
  227. var id = 0
  228. existingData := BaseMysql.SelectBySqlByTx(tx, `SELECT id FROM `+WorkCommonly+` WHERE userid = ? AND appid = ? AND field = ? AND platform = ? ORDER BY id DESC `, in.UserId, in.AppId, in.ActionMode, in.Platform)
  229. if existingData != nil && len(*existingData) > 0 {
  230. id = MC.IntAll((*existingData)[0]["id"])
  231. }
  232. switch {
  233. case id > 0: //更新
  234. if BaseMysql.UpdateOrDeleteBySqlByTx(tx, `UPDATE `+WorkCommonly+` SET value = ? WHERE id = ?`, strings.Join(ids, ","), id) < 0 {
  235. logx.Info("常用功能-更新数据失败")
  236. return false
  237. }
  238. default: //插入
  239. if BaseMysql.InsertBySqlByTx(tx, `INSERT INTO `+WorkCommonly+` (appid,userid,platform,field,value) VALUES (?,?,?,?,?)`, in.AppId, in.UserId, in.Platform, in.ActionMode, strings.Join(ids, ",")) < 0 {
  240. logx.Info("常用功能-插入数据失败")
  241. return false
  242. }
  243. }
  244. return true
  245. }); !B {
  246. M = "常用功能更新数据失败"
  247. }
  248. return
  249. }