Files
wx_service/internal/marketing/handler/download_handler.go
T

117 lines
3.2 KiB
Go

package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"wx_service/config"
qiniuservice "wx_service/internal/common/qiniu/service"
"wx_service/internal/marketing/service"
"wx_service/internal/middleware"
"wx_service/internal/model"
)
type DownloadHandler struct {
svc *service.DownloadService
}
func NewDownloadHandler(svc *service.DownloadService) *DownloadHandler {
return &DownloadHandler{svc: svc}
}
func (h *DownloadHandler) Create(c *gin.Context) {
user := middleware.MustCurrentUser(c)
var req service.CreateDownloadRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
return
}
dl, err := h.svc.Create(user.ID, req)
if err != nil {
if service.IsNotFoundError(err) {
c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "模板不存在"))
return
}
if service.IsBadRequestError(err) {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, err.Error()))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误"))
return
}
c.JSON(http.StatusOK, model.Success(dl))
}
type adCallbackRequest struct {
DownloadID uint `json:"download_id" binding:"required"`
}
func (h *DownloadHandler) AdCallback(c *gin.Context) {
var req adCallbackRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
return
}
if err := h.svc.MarkAdCompleted(req.DownloadID); err != nil {
if service.IsNotFoundError(err) {
c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "记录不存在"))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误"))
return
}
c.JSON(http.StatusOK, model.Success(nil))
}
func (h *DownloadHandler) ListByUser(c *gin.Context) {
user := middleware.MustCurrentUser(c)
page := parseIntQuery(c, "page", 1)
pageSize := parseIntQuery(c, "page_size", 20)
resp, err := h.svc.ListByUser(user.ID, page, pageSize)
if err != nil {
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误"))
return
}
c.JSON(http.StatusOK, model.Success(resp))
}
func (h *DownloadHandler) AdminStats(c *gin.Context) {
stats, err := h.svc.GetStats()
if err != nil {
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误"))
return
}
c.JSON(http.StatusOK, model.Success(stats))
}
type adminQiniuTokenRequest struct {
Filename string `json:"filename"`
}
func (h *DownloadHandler) AdminQiniuToken(c *gin.Context) {
var req adminQiniuTokenRequest
_ = c.ShouldBindJSON(&req)
qiniuSvc := qiniuservice.NewQiniuService(config.AppConfig.Qiniu)
token, err := qiniuSvc.CreateUploadToken(0, 0, req.Filename)
if err != nil {
if errors.Is(err, qiniuservice.ErrQiniuNotConfigured) {
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "未配置七牛上传服务"))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取上传凭证失败"))
return
}
c.JSON(http.StatusOK, model.Success(token))
}