admin_user.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package api
  2. import (
  3. "encoding/base64"
  4. "entrance-grpc/iam"
  5. "gadmin/config"
  6. "gadmin/internal/admin/forms"
  7. "gadmin/internal/admin/service"
  8. "gadmin/internal/gorm/query"
  9. "gadmin/utility/serializer"
  10. "gadmin/utility/token"
  11. "github.com/gin-gonic/gin"
  12. "github.com/sirupsen/logrus"
  13. )
  14. func UserRegister(c *gin.Context) {
  15. var req forms.UserRegisterReq
  16. if err := c.ShouldBind(&req); err == nil {
  17. res := service.User.Register(req)
  18. c.JSON(200, res)
  19. } else {
  20. c.JSON(200, ErrorResponse(err))
  21. }
  22. }
  23. //func UserLogin(c *gin.Context) {
  24. // ip := c.ClientIP()
  25. // var req forms.UserLoginReq
  26. // if err := c.ShouldBind(&req); err == nil {
  27. // res := service.User.Login(req, ip)
  28. // c.JSON(200, res)
  29. // } else {
  30. // c.JSON(200, ErrorResponse(err))
  31. // }
  32. //}
  33. func UserMe(c *gin.Context) {
  34. var (
  35. user = token.CurrentUser(c)
  36. )
  37. permissions := make([]forms.UserLoginPermissions, 0)
  38. permissions = append(permissions, forms.UserLoginPermissions{
  39. Label: "控制台",
  40. Value: "value",
  41. })
  42. //获取用户角色权限
  43. //roleInfo, err := service.AdminRole.GetRole(c, forms.AdminRoleReq{ID: user.RoleId})
  44. //if err != nil || roleInfo == nil {
  45. // c.JSON(200, ErrorResponse(err))
  46. // return
  47. //}
  48. if user.RoleID == 1 {
  49. for k, _ := range config.AuthMenuMap {
  50. permissions = append(permissions, forms.UserLoginPermissions{
  51. Label: config.AuthNameMap[k],
  52. Value: config.AuthMenuMap[k],
  53. })
  54. }
  55. } else {
  56. rpdb := query.Use(config.DB).AdminRolePermission
  57. rolePermission := make([]int, 0)
  58. err := rpdb.Pluck(rpdb.PermissionID, &rolePermission)
  59. //err = json.Unmarshal([]byte(roleInfo.Permissions), &rolePermission)
  60. if err != nil {
  61. c.JSON(200, ErrorResponse(err))
  62. return
  63. }
  64. for _, v := range rolePermission {
  65. if _, ok := config.AuthMenuMap[v]; ok {
  66. permissions = append(permissions, forms.UserLoginPermissions{
  67. Label: config.AuthNameMap[v],
  68. Value: config.AuthMenuMap[v],
  69. })
  70. }
  71. }
  72. }
  73. //将权限赋值给permission
  74. info := forms.UserMeReq{
  75. ID: user.ID,
  76. UserName: user.UserName,
  77. RoleId: user.RoleID,
  78. Avatar: user.Avatar,
  79. Nickname: user.NickName,
  80. Permissions: permissions,
  81. IsSuper: config.IsSuperRole(user.RoleID),
  82. }
  83. c.JSON(200, serializer.Suc(info, "获取成功"))
  84. }
  85. func UserLogout(c *gin.Context) {
  86. encodeToken := token.GetAuthorization(c)
  87. if encodeToken == "" {
  88. c.JSON(200, serializer.CheckLogin())
  89. return
  90. }
  91. bytesT, err := base64.URLEncoding.DecodeString(encodeToken)
  92. if err != nil {
  93. logrus.Warningf("middleware base64.URLEncoding.DecodeString:%+v", err.Error())
  94. c.JSON(200, serializer.CheckLogin())
  95. return
  96. }
  97. t := string(bytesT)
  98. resp, err := config.GetIamClient().DeleteToken(c, &iam.DeleteTokenReq{
  99. Token: t,
  100. })
  101. if err != nil {
  102. logrus.Warningf("middleware config.GetIamClient().DeleteToken:%+v", err.Error())
  103. c.JSON(200, serializer.CheckLogin())
  104. return
  105. }
  106. if resp.Code != 0 {
  107. logrus.Warningf("middleware config.GetIamClient().DeleteToken code:%+v,msg:%+v", resp.Code, resp.Msg)
  108. c.JSON(200, serializer.CheckLogin())
  109. return
  110. }
  111. c.JSON(200, serializer.Suc(nil, "退出成功"))
  112. }
  113. //func AdminUserList(c *gin.Context) {
  114. // var req forms.AdminUserListReq
  115. // if err := c.ShouldBind(&req); err != nil {
  116. // c.JSON(200, ErrorResponse(err))
  117. // return
  118. // }
  119. // if err := forms.ParseParams(&req); err != nil {
  120. // c.JSON(200, ErrorResponse(err))
  121. // return
  122. // }
  123. //
  124. // c.JSON(200, service.User.List(c, req))
  125. //}
  126. //func AdminUserEdit(c *gin.Context) {
  127. // var req forms.AdminUserEditReq
  128. // if err := c.ShouldBind(&req); err != nil {
  129. // c.JSON(200, ErrorResponse(err))
  130. // return
  131. // }
  132. // if err := forms.ParseParams(&req); err != nil {
  133. // c.JSON(200, ErrorResponse(err))
  134. // return
  135. // }
  136. //
  137. // c.JSON(200, service.User.Edit(c, req))
  138. //}
  139. //func ResetPassword(c *gin.Context) {
  140. // var req forms.AdminUserResetPasswordReq
  141. // if err := c.ShouldBind(&req); err != nil {
  142. // c.JSON(200, ErrorResponse(err))
  143. // return
  144. // }
  145. // if err := forms.ParseParams(&req); err != nil {
  146. // c.JSON(200, ErrorResponse(err))
  147. // return
  148. // }
  149. //
  150. // c.JSON(200, service.User.ResetPassword(c, req))
  151. //}
  152. //
  153. //func UpdatePassword(c *gin.Context) {
  154. // var req forms.AdminUserUpdatePasswordReq
  155. // if err := c.ShouldBind(&req); err != nil {
  156. // c.JSON(200, ErrorResponse(err))
  157. // return
  158. // }
  159. // if err := forms.ParseParams(&req); err != nil {
  160. // c.JSON(200, ErrorResponse(err))
  161. // return
  162. // }
  163. //
  164. // c.JSON(200, service.User.UpdatePassword(c, req))
  165. //}
  166. //
  167. //func RolePermission(c *gin.Context) {
  168. // is, err := service.User.GetUserRolePermission(c)
  169. // if err != nil {
  170. // c.JSON(200, ErrorResponse(err))
  171. // return
  172. // }
  173. // c.JSON(200, serializer.Suc(is, "获取成功"))
  174. //}