Files
wx_service/internal/common/auth/handler/auth_handler.go
T
nepiedg cd7ae5ac56 Integrate Qiniu upload service and update configuration
- Added Qiniu configuration options to .env.example and config.go for file uploads.
- Refactored main.go to include new Qiniu service and upload handler.
- Updated route registration to accommodate the new upload handler.
- Enhanced documentation to include references for Qiniu upload functionality.
- Removed legacy authentication handler and services to streamline the codebase.
2025-12-31 03:18:03 +00:00

107 lines
3.5 KiB
Go

package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"wx_service/internal/common/auth/service"
"wx_service/internal/model"
)
type AuthHandler struct {
// handler 层通常只做“协议转换”:
// - 把 HTTP 请求(JSON/header 等)解析成结构体
// - 调用 service 完成业务逻辑
// - 把结果/错误转换成统一的 JSON 响应
authService *service.AuthService
}
func NewAuthHandler(authService *service.AuthService) *AuthHandler {
return &AuthHandler{
authService: authService,
}
}
type weChatLoginRequest struct {
// binding:"required" 是 Gin 的校验标签:字段缺失或为空会导致 ShouldBindJSON 返回错误
MiniProgramID uint `json:"mini_program_id" binding:"required"`
Code string `json:"code" binding:"required"`
NickName string `json:"nickname"`
AvatarURL string `json:"avatar_url"`
// 使用 *int 可以区分:
// - nil:前端没传 gender
// - 非 nil:前端传了具体值(即使是 0)
Gender *int `json:"gender"`
Phone string `json:"phone"`
}
func (h *AuthHandler) LoginWithWeChat(c *gin.Context) {
// gin.Context 是每个请求的上下文对象:
// - c.Request.Context() 是标准库 context,用于把超时/取消信号传递到 DB/HTTP 调用
// - c.JSON(...) 用于写 JSON 响应
var req weChatLoginRequest
// ShouldBindJSON 会从请求体 JSON 反序列化到结构体,并根据 binding 标签做基础校验
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "invalid request payload"))
return
}
// 业务逻辑下沉到 service:这样 handler 更薄、更容易测试
result, err := h.authService.LoginWithCode(c.Request.Context(), service.LoginRequest{
MiniProgramID: req.MiniProgramID,
Code: req.Code,
NickName: req.NickName,
AvatarURL: req.AvatarURL,
Gender: req.Gender,
Phone: req.Phone,
})
if err != nil {
switch {
case errors.Is(err, service.ErrCodeRequired):
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "code is required"))
case errors.Is(err, service.ErrMiniProgramRequired):
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "mini_program_id is required"))
case errors.Is(err, service.ErrMiniProgramNotFound):
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "mini program not found"))
default:
var apiErr *service.WeChatError
if errors.As(err, &apiErr) {
c.JSON(http.StatusBadGateway, model.Error(http.StatusBadGateway, apiErr.Error()))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "login failed"))
}
return
}
userPayload := gin.H{
"id": result.User.ID,
"mini_program_id": result.User.MiniProgramID,
"open_id": result.User.OpenID,
"nickname": result.User.NickName,
"avatar_url": result.User.AvatarURL,
"gender": result.User.Gender,
"phone": result.User.Phone,
}
if result.User.UnionID != "" {
userPayload["union_id"] = result.User.UnionID
}
miniProgramPayload := gin.H{
"id": result.MiniProgram.ID,
"name": result.MiniProgram.Name,
"app_id": result.MiniProgram.AppID,
}
if result.MiniProgram.Description != "" {
miniProgramPayload["description"] = result.MiniProgram.Description
}
c.JSON(http.StatusOK, model.Success(gin.H{
"user": userPayload,
"session_key": result.SessionKey,
"mini_program": miniProgramPayload,
}))
}