Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 25s
feat(admin): 新增工会管理功能 feat(activity): 添加活动管理相关服务 feat(user): 实现用户道具卡和积分管理 feat(guild): 新增工会成员管理功能 fix: 修复数据库连接配置 fix: 修正jwtoken导入路径 fix: 解决端口冲突问题 style: 统一代码格式和注释风格 style: 更新项目常量命名 docs: 添加项目框架和开发规范文档 docs: 更新接口文档注释 chore: 移除无用代码和文件 chore: 更新Makefile和配置文件 chore: 清理日志文件 test: 添加道具卡测试脚本
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"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"
|
|
// @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
|
|
}
|
|
uidStr := ctx.Param("user_id")
|
|
userID, _ := strconv.ParseInt(uidStr, 10, 64)
|
|
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)
|
|
}
|
|
}
|