system_permissions.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package backend
  2. import (
  3. "context"
  4. "kpt-tmr-group/model"
  5. "kpt-tmr-group/pkg/logger/zaplog"
  6. "kpt-tmr-group/pkg/xerr"
  7. operationPb "kpt-tmr-group/proto/go/backend/operation"
  8. "net/http"
  9. "sort"
  10. "strings"
  11. "sync"
  12. "time"
  13. "go.uber.org/zap"
  14. )
  15. type SystemAllPermissionsList struct {
  16. PastureList []*model.SystemGroupPasturePermissions
  17. MenuList []*model.SystemMenuPermissions
  18. MobileList []*model.SystemMobilePermissions
  19. }
  20. func (s *StoreEntry) AllPermissionsListToRolePermissions(req *SystemAllPermissionsList) *operationPb.RolePermissionsList {
  21. res := &operationPb.RolePermissionsList{
  22. Code: http.StatusOK,
  23. Msg: "ok",
  24. Data: &operationPb.RolePermissionsData{
  25. MobileList: make([]uint32, 0),
  26. MenuList: make([]uint32, 0),
  27. PastureList: make([]uint32, 0),
  28. },
  29. }
  30. wg := sync.WaitGroup{}
  31. wg.Add(3)
  32. go func() {
  33. defer wg.Done()
  34. for _, v := range req.MobileList {
  35. res.Data.MobileList = append(res.Data.MobileList, uint32(v.MobileId))
  36. }
  37. }()
  38. go func() {
  39. defer wg.Done()
  40. for _, v := range req.MenuList {
  41. res.Data.MenuList = append(res.Data.MenuList, uint32(v.MenuId))
  42. }
  43. }()
  44. go func() {
  45. defer wg.Done()
  46. for _, v := range req.PastureList {
  47. res.Data.PastureList = append(res.Data.PastureList, uint32(v.PastureId))
  48. }
  49. }()
  50. wg.Wait()
  51. return res
  52. }
  53. // SystemUserMenuPermissionsUnDuplicate 角色权限去重
  54. func (s *SystemAllPermissionsList) SystemUserMenuPermissionsUnDuplicate() {
  55. newMenuList := make([]*model.SystemMenuPermissions, 0)
  56. seen := make(map[int64]bool)
  57. for _, menu := range s.MenuList {
  58. if _, ok := seen[menu.MenuId]; !ok {
  59. newMenuList = append(newMenuList, menu)
  60. }
  61. }
  62. s.MenuList = newMenuList
  63. }
  64. func (s *StoreEntry) SystemPermissionsFormatPb(pastureList []*model.GroupPasture, mobileList []*model.SystemMobile, menuList []*model.SystemMenu) *operationPb.SystemUserMenuPermissions {
  65. systemUserMenuPermissions := &operationPb.SystemUserMenuPermissions{
  66. Code: http.StatusOK,
  67. Msg: "ok",
  68. Data: &operationPb.SystemUserMenuData{
  69. PastureList: make([]*operationPb.AddPastureRequest, 0),
  70. MenuList: make([]*operationPb.AddMenuRequest, 0),
  71. MobileList: make([]*operationPb.AddMobileRequest, 0),
  72. MenuButtonsPath: make([]*operationPb.MenuButtonsPath, 0),
  73. },
  74. }
  75. wg := sync.WaitGroup{}
  76. wg.Add(3)
  77. go func() {
  78. for _, v := range pastureList {
  79. systemUserMenuPermissions.Data.PastureList = append(systemUserMenuPermissions.Data.PastureList,
  80. &operationPb.AddPastureRequest{
  81. Id: int32(v.Id),
  82. Name: v.Name,
  83. },
  84. )
  85. }
  86. wg.Done()
  87. }()
  88. // TODO 后面优化成递归算法
  89. go func() {
  90. level := make(map[int32][]*operationPb.AddMenuRequest, 0)
  91. for _, menu := range menuList {
  92. if _, ok := level[menu.Level]; !ok {
  93. level[menu.Level] = make([]*operationPb.AddMenuRequest, 0)
  94. }
  95. level[menu.Level] = append(level[menu.Level], &operationPb.AddMenuRequest{
  96. Id: int32(menu.Id),
  97. Name: menu.Name,
  98. ParentId: int32(menu.ParentId),
  99. MenuType: menu.MenuType,
  100. Title: menu.Title,
  101. Path: menu.Path,
  102. IsShow: menu.IsShow,
  103. Component: menu.Component,
  104. Icon: menu.Icon,
  105. Sort: menu.Sort,
  106. Redirect: menu.Redirect,
  107. CreatedAt: int32(menu.CreatedAt),
  108. CreatedAtFormat: time.Unix(menu.CreatedAt, 0).Format(model.LayoutTime),
  109. Level: menu.Level,
  110. Affix: true,
  111. KeepAlive: true,
  112. Children: make([]*operationPb.AddMenuRequest, 0),
  113. })
  114. if menu.Level == model.Level3 {
  115. systemUserMenuPermissions.Data.MenuButtonsPath = append(systemUserMenuPermissions.Data.MenuButtonsPath, &operationPb.MenuButtonsPath{
  116. Path: menu.Path,
  117. MenuId: int32(menu.Id),
  118. })
  119. }
  120. }
  121. level2IsShow := make([]int32, 0)
  122. for _, leve2Data := range level[model.Level2] {
  123. for _, leve3Data := range level[model.Level3] {
  124. if leve3Data.ParentId == leve2Data.Id && strings.Contains(leve3Data.Path, ":page") {
  125. level2IsShow = append(level2IsShow, leve2Data.Id)
  126. }
  127. if leve3Data.ParentId == leve2Data.Id {
  128. if leve2Data.Children == nil {
  129. leve2Data.Children = make([]*operationPb.AddMenuRequest, 0)
  130. }
  131. leve2Data.Children = append(leve2Data.Children, leve3Data)
  132. }
  133. }
  134. }
  135. sort.SliceStable(level[model.Level2], func(i, j int) bool {
  136. return level[model.Level2][i].Sort > level[model.Level2][j].Sort
  137. })
  138. for _, leve2Data := range level[model.Level2] {
  139. var info bool
  140. for _, v := range level2IsShow {
  141. if v == leve2Data.Id {
  142. info = true
  143. }
  144. }
  145. if !info {
  146. continue
  147. }
  148. for _, leve1Data := range level[model.Level1] {
  149. if leve2Data.ParentId == leve1Data.Id {
  150. if leve1Data.Children == nil {
  151. leve1Data.Children = make([]*operationPb.AddMenuRequest, 0)
  152. }
  153. leve1Data.Children = append(leve1Data.Children, leve2Data)
  154. }
  155. }
  156. }
  157. sort.SliceStable(level[model.Level1], func(i, j int) bool {
  158. return level[model.Level1][i].Sort > level[model.Level1][j].Sort
  159. })
  160. systemUserMenuPermissions.Data.MenuList = level[model.Level1]
  161. wg.Done()
  162. }()
  163. go func() {
  164. for _, v := range mobileList {
  165. systemUserMenuPermissions.Data.MobileList = append(systemUserMenuPermissions.Data.MobileList,
  166. &operationPb.AddMobileRequest{
  167. Id: uint32(v.Id),
  168. Name: v.Name,
  169. },
  170. )
  171. }
  172. wg.Done()
  173. }()
  174. wg.Wait()
  175. return systemUserMenuPermissions
  176. }
  177. // GetSystemAllPermissionsList 获取用户相关权限
  178. func (s *StoreEntry) GetSystemAllPermissionsList(ctx context.Context, roleId int64) *SystemAllPermissionsList {
  179. systemAllPermissionsList := &SystemAllPermissionsList{
  180. PastureList: make([]*model.SystemGroupPasturePermissions, 0),
  181. MenuList: make([]*model.SystemMenuPermissions, 0),
  182. MobileList: make([]*model.SystemMobilePermissions, 0),
  183. }
  184. wg := sync.WaitGroup{}
  185. wg.Add(3)
  186. go func() {
  187. pastureList := make([]*model.SystemGroupPasturePermissions, 0)
  188. if err := s.DB.Where("role_id = ? and is_show = ?", roleId, operationPb.IsShow_OK).Find(&pastureList).Error; err != nil {
  189. zaplog.Error("SystemPasturePermissions", zap.Any("Err", err))
  190. }
  191. systemAllPermissionsList.PastureList = pastureList
  192. wg.Done()
  193. }()
  194. go func() {
  195. menuList := make([]*model.SystemMenuPermissions, 0)
  196. if err := s.DB.Where("role_id = ? and is_show = ?", roleId, operationPb.IsShow_OK).Find(&menuList).Error; err != nil {
  197. zaplog.Error("SystemMenuPermissions", zap.Any("Err", err))
  198. }
  199. systemAllPermissionsList.MenuList = menuList
  200. wg.Done()
  201. }()
  202. // 获取供应商数据
  203. go func() {
  204. mobileList := make([]*model.SystemMobilePermissions, 0)
  205. if err := s.DB.Where("role_id = ? and is_show = ?", roleId, operationPb.IsShow_OK).Find(&mobileList).Error; err != nil {
  206. zaplog.Error("SystemMobilePermissions", zap.Any("Err", err))
  207. }
  208. systemAllPermissionsList.MobileList = mobileList
  209. wg.Done()
  210. }()
  211. wg.Wait()
  212. return systemAllPermissionsList
  213. }
  214. // GetPastureList 获取******
  215. func (s *StoreEntry) GetPastureList(ctx context.Context, req []*model.SystemGroupPasturePermissions) ([]*model.GroupPasture, error) {
  216. ids := make([]int64, 0)
  217. for _, v := range req {
  218. ids = append(ids, v.PastureId)
  219. }
  220. groupPastureList := make([]*model.GroupPasture, 0)
  221. if len(ids) > 0 {
  222. if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&groupPastureList, ids).Error; err != nil {
  223. return nil, xerr.WithStack(err)
  224. }
  225. }
  226. return groupPastureList, nil
  227. }
  228. // GetMenuList 获取******
  229. func (s *StoreEntry) GetMenuList(ctx context.Context, req []*model.SystemMenuPermissions) ([]*model.SystemMenu, error) {
  230. ids := make([]int64, 0)
  231. for _, v := range req {
  232. ids = append(ids, v.MenuId)
  233. }
  234. menuList := make([]*model.SystemMenu, 0)
  235. if len(ids) > 0 {
  236. if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&menuList, ids).Error; err != nil {
  237. return nil, xerr.WithStack(err)
  238. }
  239. }
  240. return menuList, nil
  241. }
  242. // GetMobileList 获取******
  243. func (s *StoreEntry) GetMobileList(ctx context.Context, req []*model.SystemMobilePermissions) ([]*model.SystemMobile, error) {
  244. ids := make([]int64, 0)
  245. for _, v := range req {
  246. ids = append(ids, v.MobileId)
  247. }
  248. mobileList := make([]*model.SystemMobile, 0)
  249. if len(ids) > 0 {
  250. if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).Find(&mobileList, ids).Error; err != nil {
  251. return nil, xerr.WithStack(err)
  252. }
  253. }
  254. return mobileList, nil
  255. }