Enhance API and error handling for video services
- Updated .gitignore to exclude cache files. - Refactored main.go to streamline route registration and improve code organization. - Added detailed comments in auth_handler.go, video_handler.go, and service files for better clarity on request handling and service logic. - Improved error messages in video_handler.go to provide clearer feedback to users in Chinese. - Introduced context handling in service methods to manage request timeouts effectively.
This commit is contained in:
@@ -11,6 +11,10 @@ import (
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
// handler 层通常只做“协议转换”:
|
||||
// - 把 HTTP 请求(JSON/header 等)解析成结构体
|
||||
// - 调用 service 完成业务逻辑
|
||||
// - 把结果/错误转换成统一的 JSON 响应
|
||||
authService *service.AuthService
|
||||
}
|
||||
|
||||
@@ -21,21 +25,30 @@ func NewAuthHandler(authService *service.AuthService) *AuthHandler {
|
||||
}
|
||||
|
||||
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"`
|
||||
Gender *int `json:"gender"`
|
||||
Phone string `json:"phone"`
|
||||
// 使用 *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,
|
||||
|
||||
@@ -28,13 +28,13 @@ type removeWatermarkRequest struct {
|
||||
func (h *VideoHandler) RemoveWatermark(c *gin.Context) {
|
||||
var req removeWatermarkRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "invalid request payload"))
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
user, ok := getCurrentUser(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
||||
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -42,21 +42,21 @@ func (h *VideoHandler) RemoveWatermark(c *gin.Context) {
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, service.ErrURLNotFound):
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "content must contain a valid url"))
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请检查分享链接是否正确"))
|
||||
return
|
||||
case errors.Is(err, service.ErrDailyQuotaExceeded):
|
||||
c.JSON(http.StatusForbidden, model.Error(http.StatusForbidden, err.Error()))
|
||||
c.JSON(http.StatusForbidden, model.Error(http.StatusForbidden, "今日免费次数已用完,观看广告后今天可无限制使用"))
|
||||
return
|
||||
case errors.Is(err, service.ErrShortVideoAPIKey):
|
||||
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "short video api key missing"))
|
||||
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "服务暂不可用,请联系管理员"))
|
||||
return
|
||||
default:
|
||||
var thirdPartyErr *service.ThirdPartyError
|
||||
if errors.As(err, &thirdPartyErr) {
|
||||
c.JSON(http.StatusBadGateway, model.Error(http.StatusBadGateway, thirdPartyErr.Error()))
|
||||
c.JSON(http.StatusBadGateway, model.Error(http.StatusBadGateway, "解析服务异常,请稍后重试"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "remove watermark failed"))
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "去水印失败,请稍后重试"))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -71,12 +71,12 @@ func (h *VideoHandler) RemoveWatermark(c *gin.Context) {
|
||||
func (h *VideoHandler) UnlockQuota(c *gin.Context) {
|
||||
user, ok := getCurrentUser(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
||||
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.videoService.UnlockForToday(c.Request.Context(), user); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "unlock failed"))
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "解锁失败,请稍后重试"))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user