bindbox-game/internal/api/user/login_app.go
邹方成 1ab39d2f5a
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 25s
refactor: 重构项目结构并重命名模块
feat(admin): 新增工会管理功能
feat(activity): 添加活动管理相关服务
feat(user): 实现用户道具卡和积分管理
feat(guild): 新增工会成员管理功能

fix: 修复数据库连接配置
fix: 修正jwtoken导入路径
fix: 解决端口冲突问题

style: 统一代码格式和注释风格
style: 更新项目常量命名

docs: 添加项目框架和开发规范文档
docs: 更新接口文档注释

chore: 移除无用代码和文件
chore: 更新Makefile和配置文件
chore: 清理日志文件

test: 添加道具卡测试脚本
2025-11-14 21:10:00 +08:00

73 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"net/http"
"time"
"bindbox-game/configs"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/jwtoken"
"bindbox-game/internal/pkg/validation"
"bindbox-game/internal/pkg/wechat"
"bindbox-game/internal/proposal"
usersvc "bindbox-game/internal/service/user"
)
type weixinLoginRequest struct {
Code string `json:"code"`
InviteCode string `json:"invite_code"`
}
type weixinLoginResponse struct {
UserID int64 `json:"user_id"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
InviteCode string `json:"invite_code"`
Token string `json:"token"`
}
// WeixinLogin 微信登录
// @Summary 微信登录
// @Description 微信静默登录(需传递 code可选 invite_code
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param RequestBody body weixinLoginRequest true "请求参数"
// @Success 200 {object} weixinLoginResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/weixin/login [post]
func (h *handler) WeixinLogin() core.HandlerFunc {
return func(ctx core.Context) {
req := new(weixinLoginRequest)
rsp := new(weixinLoginResponse)
if err := ctx.ShouldBindJSON(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
return
}
cfg := configs.Get()
wxcfg := &wechat.WechatConfig{AppID: cfg.Wechat.AppID, AppSecret: cfg.Wechat.AppSecret}
c2s, err := wechat.Code2Session(ctx, wxcfg, req.Code)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10006, err.Error()))
return
}
in := usersvc.LoginWeixinInput{OpenID: c2s.OpenID, UnionID: c2s.UnionID, InviteCode: req.InviteCode}
u, err := h.user.LoginWeixin(ctx.RequestContext(), in)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10006, err.Error()))
return
}
rsp.UserID = u.ID
rsp.Nickname = u.Nickname
rsp.Avatar = u.Avatar
rsp.InviteCode = u.InviteCode
sessionUserInfo := proposal.SessionUserInfo{Id: int32(u.ID), UserName: u.Nickname, NickName: u.Nickname, IsSuper: 0, Platform: "APP"}
tokenString, tErr := jwtoken.New(configs.Get().JWT.PatientSecret).Sign(sessionUserInfo, 30*24*time.Hour)
if tErr == nil {
rsp.Token = tokenString
}
ctx.Payload(rsp)
}
}