Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 31s
refactor(api/user): 重构用户相关接口使用token验证替代user_id路径参数 docs: 更新API文档规范,明确私有接口需携带token及返回字段要求 fix(service/user): 避免写入未使用字段的零值导致MySQL校验错误 style: 统一格式化部分代码缩进和导入顺序 chore: 更新DS_Store等IDE配置文件
77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
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)
|
|
}
|
|
}
|