package app import ( "net/http" "bindbox-game/configs" "bindbox-game/internal/code" "bindbox-game/internal/pkg/core" "bindbox-game/internal/pkg/miniprogram" "bindbox-game/internal/pkg/validation" "bindbox-game/internal/pkg/wechat" ) type bindPhoneRequest struct { Code string `json:"code"` } type bindPhoneResponse struct { Success bool `json:"success"` Mobile string `json:"mobile"` } // BindPhone 绑定手机号 // @Summary 绑定手机号 // @Description 使用微信手机号 code 换取手机号并绑定到指定用户 // @Tags APP端.用户 // @Accept json // @Produce json // @Param user_id path integer true "用户ID" // @Security LoginVerifyToken // @Param RequestBody body bindPhoneRequest true "请求参数" // @Success 200 {object} bindPhoneResponse // @Failure 400 {object} code.Failure // @Router /api/app/users/{user_id}/phone/bind [post] func (h *handler) BindPhone() core.HandlerFunc { return func(ctx core.Context) { req := new(bindPhoneRequest) rsp := new(bindPhoneResponse) if err := ctx.ShouldBindJSON(req); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err))) return } userID := int64(ctx.SessionUserInfo().Id) if userID <= 0 || req.Code == "" { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "缺少必要参数")) return } cfg := configs.Get() var tokenRes struct { AccessToken string `json:"access_token"` } if err := miniprogram.GetAccessToken(cfg.Wechat.AppID, cfg.Wechat.AppSecret, &tokenRes); err != nil || tokenRes.AccessToken == "" { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "获取微信access_token失败")) return } pn, err := wechat.GetPhoneNumber(ctx, tokenRes.AccessToken, req.Code) if err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error())) return } mobile := pn.PhoneInfo.PurePhoneNumber if mobile == "" { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "手机号为空")) return } if _, err := h.writeDB.Users.WithContext(ctx.RequestContext()).Where(h.writeDB.Users.ID.Eq(userID)).Updates(map[string]any{"mobile": mobile}); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error())) return } rsp.Success = true rsp.Mobile = mobile ctx.Payload(rsp) } }