rank.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package http
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "go-common/app/admin/main/manager/conf"
  6. "go-common/app/admin/main/manager/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/xstr"
  11. )
  12. func rankGroups(c *bm.Context) {
  13. form := c.Request.Form
  14. pn, _ := strconv.Atoi(form.Get("pn"))
  15. if pn < 1 {
  16. pn = 1
  17. }
  18. ps, _ := strconv.Atoi(form.Get("ps"))
  19. if ps < 1 || ps > conf.Conf.Cfg.RankGroupMaxPs {
  20. ps = conf.Conf.Cfg.RankGroupMaxPs
  21. }
  22. groups, total, err := mngSvc.RankGroups(c, pn, ps)
  23. if err != nil {
  24. c.JSON(nil, err)
  25. return
  26. }
  27. page := map[string]int{
  28. "page": pn,
  29. "pagesize": ps,
  30. "total": total,
  31. }
  32. c.JSONMap(map[string]interface{}{
  33. "pager": page,
  34. "data": groups,
  35. }, err)
  36. }
  37. func rankGroup(c *bm.Context) {
  38. form := c.Request.Form
  39. gid, _ := strconv.ParseInt(form.Get("id"), 10, 64)
  40. if gid <= 0 {
  41. c.JSON(nil, ecode.RequestErr)
  42. log.Error("id unnarmal (%d)", gid)
  43. return
  44. }
  45. c.JSON(mngSvc.RankGroup(c, gid))
  46. }
  47. func addRankGroup(c *bm.Context) {
  48. form := c.Request.Form
  49. name := form.Get("name")
  50. if name == "" {
  51. c.JSON(nil, ecode.RequestErr)
  52. log.Error("name is empty")
  53. return
  54. }
  55. desc := form.Get("desc")
  56. auths, _ := xstr.SplitInts(form.Get("auths"))
  57. g := &model.RankGroup{Name: name, Desc: desc}
  58. c.JSON(mngSvc.AddRankGroup(c, g, auths))
  59. }
  60. func updateRankGroup(c *bm.Context) {
  61. form := c.Request.Form
  62. gid, _ := strconv.ParseInt(form.Get("id"), 10, 64)
  63. if gid <= 0 {
  64. c.JSON(nil, ecode.RequestErr)
  65. log.Error("id unnarmal (%d)", gid)
  66. return
  67. }
  68. name := form.Get("name")
  69. if name == "" {
  70. c.JSON(nil, ecode.RequestErr)
  71. log.Error("name is empty")
  72. return
  73. }
  74. desc := form.Get("desc")
  75. auths, _ := xstr.SplitInts(form.Get("auths"))
  76. g := &model.RankGroup{ID: gid, Name: name, Desc: desc}
  77. c.JSON(nil, mngSvc.UpdateRankGroup(c, g, auths))
  78. }
  79. func delRankGroup(c *bm.Context) {
  80. form := c.Request.Form
  81. gid, _ := strconv.ParseInt(form.Get("id"), 10, 64)
  82. if gid <= 0 {
  83. c.JSON(nil, ecode.RequestErr)
  84. log.Error("id unnarmal (%d)", gid)
  85. return
  86. }
  87. c.JSON(nil, mngSvc.DelRankGroup(c, gid))
  88. }
  89. func addRankUser(c *bm.Context) {
  90. form := c.Request.Form
  91. uid, _ := strconv.ParseInt(form.Get("uid"), 10, 64)
  92. if uid <= 0 {
  93. c.JSON(nil, ecode.RequestErr)
  94. log.Error("uid unnarmal (%d)", uid)
  95. return
  96. }
  97. c.JSON(nil, mngSvc.AddRankUser(c, uid))
  98. }
  99. func rankUsers(c *bm.Context) {
  100. form := c.Request.Form
  101. pn, _ := strconv.Atoi(form.Get("pn"))
  102. if pn < 1 {
  103. pn = 1
  104. }
  105. ps, _ := strconv.Atoi(form.Get("ps"))
  106. if ps < 1 || ps > conf.Conf.Cfg.RankGroupMaxPs {
  107. ps = conf.Conf.Cfg.RankGroupMaxPs
  108. }
  109. un := form.Get("username")
  110. users, total, err := mngSvc.RankUsers(c, pn, ps, un)
  111. if err != nil {
  112. c.JSON(nil, err)
  113. return
  114. }
  115. page := map[string]int{
  116. "page": pn,
  117. "pagesize": ps,
  118. "total": total,
  119. }
  120. c.JSONMap(map[string]interface{}{
  121. "pager": page,
  122. "data": users,
  123. }, err)
  124. }
  125. func saveRankUser(c *bm.Context) {
  126. form := c.Request.Form
  127. uid, _ := strconv.ParseInt(form.Get("uid"), 10, 64)
  128. if uid <= 0 {
  129. c.JSON(nil, ecode.RequestErr)
  130. log.Error("uid unnormal (%d)", uid)
  131. return
  132. }
  133. ranks := form.Get("ranks")
  134. if ranks == "" {
  135. c.JSON(nil, ecode.RequestErr)
  136. log.Error("ranks is empty")
  137. return
  138. }
  139. rs := make(map[string]int)
  140. if err := json.Unmarshal([]byte(ranks), &rs); err != nil {
  141. c.JSON(nil, ecode.RequestErr)
  142. log.Error("ranks unnormal (%s) error(%v)", ranks, err)
  143. return
  144. }
  145. rMap := make(map[int64]int)
  146. for g, r := range rs {
  147. gid, _ := strconv.ParseInt(g, 10, 64)
  148. rMap[gid] = r
  149. }
  150. c.JSON(nil, mngSvc.SaveRankUser(c, uid, rMap))
  151. }
  152. func delRankUser(c *bm.Context) {
  153. form := c.Request.Form
  154. uid, _ := strconv.ParseInt(form.Get("uid"), 10, 64)
  155. if uid <= 0 {
  156. c.JSON(nil, ecode.RequestErr)
  157. log.Error("uid unnormal (%d)", uid)
  158. return
  159. }
  160. c.JSON(nil, mngSvc.DelRankUser(c, uid))
  161. }