workDesktop.go 11 KB

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