workDesktop.go 9.3 KB

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