admin_user.go 3.9 KB

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