Files
wx_service/internal/common/qiniu/handler/upload_handler.go
T
nepiedg 9200600b1c Enhance smoking tracking API with new features and improvements
- Added a new API endpoint `GET /api/v1/smoke/home` to consolidate core modules for the home dashboard, reducing the need for multiple requests.
- Updated the `smoke` routes to include the new home endpoint and improved user profile management with the addition of a `quit_date` field.
- Enhanced the algorithm for calculating daily targets and next smoke suggestions, ensuring accurate future time handling and user-specific recommendations.
- Improved API documentation to reflect new endpoints, response formats, and detailed field descriptions for better clarity and usability.
- Refactored user authentication handling in various handlers to streamline the process and ensure consistent error responses.
2026-01-29 17:16:35 +00:00

47 lines
1.4 KiB
Go

package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
qiniuservice "wx_service/internal/common/qiniu/service"
"wx_service/internal/middleware"
"wx_service/internal/model"
)
type UploadHandler struct {
qiniuService *qiniuservice.QiniuService
}
func NewUploadHandler(qiniuService *qiniuservice.QiniuService) *UploadHandler {
return &UploadHandler{qiniuService: qiniuService}
}
type qiniuTokenRequest struct {
// filename 用于保留文件后缀(可选),例如:"a.png"、"video.mp4"
Filename string `json:"filename"`
}
// QiniuToken 返回七牛直传所需的 token/key/upload_url 等信息。
// 建议放在鉴权后:用当前登录用户生成 key,避免前端写入任意路径。
func (h *UploadHandler) QiniuToken(c *gin.Context) {
user := middleware.MustCurrentUser(c)
var req qiniuTokenRequest
_ = c.ShouldBindJSON(&req) // filename 可选,解析失败也不影响生成 token
token, err := h.qiniuService.CreateUploadToken(user.MiniProgramID, user.ID, 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))
}