| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package v1
- import (
- "github.com/runningwater/gohub/app/models/user"
- "github.com/runningwater/gohub/app/requests"
- "github.com/runningwater/gohub/pkg/auth"
- "github.com/runningwater/gohub/pkg/config"
- "github.com/runningwater/gohub/pkg/file"
- "github.com/runningwater/gohub/pkg/response"
- "github.com/gin-gonic/gin"
- )
- type UsersController struct {
- BaseApiController
- }
- // CurrentUser 当前登录用户信息
- func (ctrl *UsersController) CurrentUser(c *gin.Context) {
- users := auth.CurrentUser(c)
- response.Data(c, users)
- }
- // Index 所有用户
- func (ctrl *UsersController) Index(c *gin.Context) {
- // 输入参数校验
- request := requests.PaginationRequest{}
- if ok := requests.Validate(c, &request, requests.Pagination); !ok {
- return
- }
- data, pager := user.Paginate(c, 2)
- response.JSON(c, gin.H{
- "data": data,
- "pager": pager,
- })
- }
- // UpdateProfile 更新用户信息
- func (ctrl *UsersController) UpdateProfile(c *gin.Context) {
- request := requests.UserUpdateProfileRequest{}
- if ok := requests.Validate(c, &request, requests.UserUpdateProfile); !ok {
- return
- }
- currentUser := auth.CurrentUser(c)
- currentUser.Name = request.Name
- currentUser.City = request.City
- currentUser.Introduction = request.Introduction
- affected := currentUser.Save()
- if affected > 0 {
- response.Data(c, currentUser)
- } else {
- response.Abort500(c, "更新失败,请稍后尝试")
- }
- }
- // UpdateEmail 更新用户邮箱
- func (ctrl *UsersController) UpdateEmail(c *gin.Context) {
- request := requests.UserUpdateEmailRequest{}
- if ok := requests.Validate(c, &request, requests.UserUpdateEmail); !ok {
- return
- }
- currentUser := auth.CurrentUser(c)
- currentUser.Email = request.Email
- affected := currentUser.Save()
- if affected > 0 {
- response.Success(c)
- return
- }
- response.Abort500(c, "更新失败,请稍后尝试")
- }
- // UpdateAvatar 更新用户头像
- func (ctrl *UsersController) UpdateAvatar(c *gin.Context) {
- request := requests.UserUpdateAvatarRequest{}
- if ok := requests.Validate(c, &request, requests.UserUpdateAvatar); !ok {
- return
- }
- avatar, err := file.SaveUploadAvatar(c, request.Avatar)
- if err != nil {
- response.Abort500(c, "上传头像失败,请稍后尝试")
- return
- }
- currentUser := auth.CurrentUser(c)
- currentUser.Avatar = config.GetString("app.url") + "/" + avatar
- _ = currentUser.Save()
- response.Data(c, currentUser)
- }
|