workDesktop.go 13 KB

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