workDesktop.go 8.9 KB

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