users_controller.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package v1
  2. import (
  3. "github.com/runningwater/gohub/app/models/user"
  4. "github.com/runningwater/gohub/app/requests"
  5. "github.com/runningwater/gohub/pkg/auth"
  6. "github.com/runningwater/gohub/pkg/response"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type UsersController struct {
  10. BaseApiController
  11. }
  12. // CurrentUser 当前登录用户信息
  13. func (ctrl *UsersController) CurrentUser(c *gin.Context) {
  14. users := auth.CurrentUser(c)
  15. response.Data(c, users)
  16. }
  17. // Index 所有用户
  18. func (ctrl *UsersController) Index(c *gin.Context) {
  19. // 输入参数校验
  20. request := requests.PaginationRequest{}
  21. if ok := requests.Validate(c, &request, requests.Pagination); !ok {
  22. return
  23. }
  24. data, pager := user.Paginate(c, 2)
  25. response.JSON(c, gin.H{
  26. "data": data,
  27. "pager": pager,
  28. })
  29. }
  30. // UpdateProfile 更新用户信息
  31. func (ctrl *UsersController) UpdateProfile(c *gin.Context) {
  32. request := requests.UserUpdateProfileRequest{}
  33. if ok := requests.Validate(c, &request, requests.UserUpdateProfile); !ok {
  34. return
  35. }
  36. currentUser := auth.CurrentUser(c)
  37. currentUser.Name = request.Name
  38. currentUser.City = request.City
  39. currentUser.Introduction = request.Introduction
  40. affected := currentUser.Save()
  41. if affected > 0 {
  42. response.Data(c, currentUser)
  43. } else {
  44. response.Abort500(c, "更新失败,请稍后尝试")
  45. }
  46. }