bindbox-game/internal/api/user/profile_app.go
邹方成 42e7cb5f12
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 31s
feat(interceptor): 添加APP端token验证接口并实现用户私有数据鉴权
refactor(api/user): 重构用户相关接口使用token验证替代user_id路径参数

docs: 更新API文档规范,明确私有接口需携带token及返回字段要求

fix(service/user): 避免写入未使用字段的零值导致MySQL校验错误

style: 统一格式化部分代码缩进和导入顺序

chore: 更新DS_Store等IDE配置文件
2025-11-15 00:49:53 +08:00

56 lines
1.7 KiB
Go

package app
import (
"net/http"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
)
type modifyUserRequest struct {
Nickname *string `json:"nickname"`
Avatar *string `json:"avatar"`
}
type userItem struct {
ID int64 `json:"id"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
InviteCode string `json:"invite_code"`
InviterID int64 `json:"inviter_id"`
}
type modifyUserResponse struct {
User userItem `json:"user"`
}
// ModifyUser 修改用户信息
// @Summary 修改用户信息
// @Description 修改用户昵称与头像
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param user_id path integer true "用户ID"
// @Security LoginVerifyToken
// @Param RequestBody body modifyUserRequest true "请求参数"
// @Success 200 {object} modifyUserResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/{user_id} [put]
func (h *handler) ModifyUser() core.HandlerFunc {
return func(ctx core.Context) {
req := new(modifyUserRequest)
rsp := new(modifyUserResponse)
if err := ctx.ShouldBindJSON(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
userID := int64(ctx.SessionUserInfo().Id)
item, err := h.user.UpdateProfile(ctx.RequestContext(), userID, req.Nickname, req.Avatar)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10001, err.Error()))
return
}
rsp.User = userItem{ID: item.ID, Nickname: item.Nickname, Avatar: item.Avatar, InviteCode: item.InviteCode, InviterID: item.InviterID}
ctx.Payload(rsp)
}
}