117 lines
3.4 KiB
Go
117 lines
3.4 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
"bindbox-game/internal/pkg/validation"
|
|
)
|
|
|
|
// GetUserProfile 获取当前用户信息
|
|
// @Summary 获取用户信息
|
|
// @Description 获取当前登录用户的详细信息
|
|
// @Tags APP端.用户
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security LoginVerifyToken
|
|
// @Success 200 {object} userItem
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/app/users/profile [get]
|
|
func (h *handler) GetUserProfile() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
userID := int64(ctx.SessionUserInfo().Id)
|
|
user, err := h.user.GetProfile(ctx.RequestContext(), userID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error()))
|
|
return
|
|
}
|
|
|
|
// Mask sensitive data if needed, but for "My Profile" usually we return full non-critical info.
|
|
// Returning masked phone for display.
|
|
phone := user.Mobile
|
|
if len(phone) >= 11 {
|
|
phone = phone[:3] + "****" + phone[7:]
|
|
}
|
|
|
|
balance, _ := h.user.GetPointsBalance(ctx.RequestContext(), userID)
|
|
|
|
res := userItem{
|
|
ID: user.ID,
|
|
Nickname: user.Nickname,
|
|
Avatar: user.Avatar,
|
|
InviteCode: user.InviteCode,
|
|
InviterID: user.InviterID,
|
|
Mobile: phone,
|
|
Balance: balance,
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
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"`
|
|
Mobile string `json:"mobile"`
|
|
Balance int64 `json:"balance"` // Points
|
|
}
|
|
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
|
|
}
|
|
// For update response, we might not have all fields populated from Update result if it returns a partial object or just the updated fields?
|
|
// But Service UpdateProfile returns the Full User object.
|
|
// So we can populate everything.
|
|
maskedPhone := item.Mobile
|
|
if len(maskedPhone) >= 11 {
|
|
maskedPhone = maskedPhone[:3] + "****" + maskedPhone[7:]
|
|
}
|
|
|
|
balance, _ := h.user.GetPointsBalance(ctx.RequestContext(), userID)
|
|
|
|
rsp.User = userItem{
|
|
ID: item.ID,
|
|
Nickname: item.Nickname,
|
|
Avatar: item.Avatar,
|
|
InviteCode: item.InviteCode,
|
|
InviterID: item.InviterID,
|
|
Mobile: maskedPhone,
|
|
Balance: balance,
|
|
}
|
|
ctx.Payload(rsp)
|
|
}
|
|
}
|