✨ 查寝完善,图片上传

This commit is contained in:
sugarBecomer
2025-10-31 22:34:05 +08:00
parent c8beca447e
commit 5135f278ab
8 changed files with 112 additions and 6 deletions
+1
View File
@@ -49,6 +49,7 @@ func StartServer(ctx context.Context) {
cpdailyGroup := group.Group("/cpdaily").Middleware(middleware.AuthHandler, middleware.PermissionHandler)
cpdailyGroup.Bind(cpdaily.NewCpUserCtrlV1())
cpdailyGroup.Bind(cpdaily.NewCpTaskCtrlV1())
cpdailyGroup.Bind(cpdaily.NewCpImageCtrlV1())
})
s.SetPort(port.Int())
+16
View File
@@ -0,0 +1,16 @@
package v1
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
type UploadImageReq struct {
g.Meta `path:"/image" method:"post" tags:"Image" summary:"上传图片"`
File *ghttp.UploadFile `json:"file" v:"required#请上传图片"`
}
type PreviewImageReq struct {
g.Meta `path:"/image/preview" method:"get" tags:"Image" summary:"预览图片"`
Key string `json:"key" v:"required#请输入图片key"`
}
+6
View File
@@ -0,0 +1,6 @@
package v1
type UploadImageRes struct {
FileKey string `json:"key"`
FileSize int64 `json:"size"`
}
@@ -191,8 +191,8 @@ func (c *CpdailySleepCheckTask) QueryFormByTitle(ctx context.Context, titleGlob
findMatchTitle := func(jsonArr []any) map[string]*gvar.Var {
for _, rawJson := range jsonArr {
item, ok := rawJson.(map[string]*gvar.Var)
if !ok {
item := gvar.New(rawJson).MapStrVar()
if len(item) == 0 {
continue
}
taskName, exist := item["taskName"]
@@ -449,7 +449,7 @@ func (c *CpdailySleepCheckTask) ParseFormJson(ctx context.Context, rawJson map[s
}
list = append(list, dto)
var current uint
var current uint = 1
if isPhoto := rows["isPhoto"]; isPhoto != nil && isPhoto.Int() == 1 {
dto = &formtype.CpTaskFormItem{
@@ -0,0 +1,12 @@
package cpdaily
import (
"context"
v1 "likedream-goframe/internal/api/v1"
"likedream-goframe/internal/utils"
)
type ICpImageCtrlV1 interface {
Upload(ctx context.Context, req *v1.UploadImageReq) (res *utils.Response[any], err error)
Preview(ctx context.Context, req *v1.PreviewImageReq) (res *utils.Response[any], err error)
}
@@ -0,0 +1,43 @@
package cpdaily
import (
"context"
v1 "likedream-goframe/internal/api/v1"
"likedream-goframe/internal/application/filestorage"
"likedream-goframe/internal/utils"
"github.com/gogf/gf/v2/frame/g"
"github.com/samber/oops"
)
type CpImageCtrlV1 struct {
}
func (c CpImageCtrlV1) Upload(ctx context.Context, req *v1.UploadImageReq) (res *utils.Response[any], err error) {
defer utils.Pass(&err)
file := req.File
open, err := file.Open()
utils.OkOrPanic(err,
func(err error) error {
return oops.WithContext(ctx).Wrap(err)
})
key := utils.GenerateFileName("cpdaily/")
size := filestorage.GlobalS3.UploadFile(ctx, req.File.Filename, open)
return utils.SuccessWithData[any](v1.UploadImageRes{
FileKey: key,
FileSize: *size,
}), nil
}
func (c CpImageCtrlV1) Preview(ctx context.Context, req *v1.PreviewImageReq) (res *utils.Response[any], err error) {
defer utils.Pass(&err)
key := req.Key
file := filestorage.GlobalS3.GetFile(ctx, key)
g.RequestFromCtx(ctx).Response.Header().Set("Content-Type", "image/png")
g.RequestFromCtx(ctx).Response.WriteExit(file)
return nil, nil
}
func NewCpImageCtrlV1() ICpImageCtrlV1 {
return &CpImageCtrlV1{}
}
+3 -3
View File
@@ -103,9 +103,9 @@ func (v CpTaskServiceV1) TaskFormData(ctx context.Context, req *v1.GetCpTaskForm
if !login.CookieEnabled(ctx, schoolDetail.WecloudUrl, client) {
loginExecutor := login.GetCpdailyLoginBySchoolInfo(ctx, &schoolDetail)
loginExecutor.Login(ctx, &schoolDetail, cpUser.Username, cpUser.Password)
cookie := loginExecutor.GetCookieString()
db.Global.CpUser.UpdateOne(cpUser).SetCookie(cookie).ExecX(ctx)
client = login.GetClient(ctx, cookie)
cpUser.Cookie = loginExecutor.GetCookieString()
db.Global.CpUser.UpdateOne(cpUser).SetCookie(cpUser.Cookie).ExecX(ctx)
client = login.GetClient(ctx, cpUser.Cookie)
}
taskService := task.GetCpdailyTaskService(ctx, string(cpTask.TaskType), cpUser.Cookie, cpUser.SchoolID)
formMap := taskService.QueryFormByTitle(ctx, cpTask.Title)
+28
View File
@@ -0,0 +1,28 @@
package utils
import (
"crypto/rand"
"math/big"
"time"
)
func GenerateFileName(prefix string) string {
return prefix + getNowTimeString() + getRandomString(6)
}
func getNowTimeString() string {
return time.Now().Format("20060102150405")
}
func getRandomString(length int) string {
allStr := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := make([]byte, length)
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(allStr))))
if err != nil {
panic(err)
}
bytes[i] = allStr[num.Int64()]
}
return string(bytes)
}