Skip to content

Commit

Permalink
fix(wechat): official account media upload with type
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix-X committed Nov 22, 2023
1 parent 4be82f6 commit 398da65
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.21
require (
github.com/ArtisanCloud/PowerLibs/v3 v3.0.15
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.7
github.com/ArtisanCloud/PowerWeChat/v3 v3.1.4
github.com/ArtisanCloud/PowerWeChat/v3 v3.1.12
github.com/Blank-Xu/sql-adapter v0.0.0-20210714092925-0bdd0d548758
github.com/brianvoe/gofakeit/v6 v6.21.0
github.com/casbin/casbin/v2 v2.60.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ github.com/ArtisanCloud/PowerLibs/v3 v3.0.15 h1:EKWDvgu3y6S7OjT3ku7LEF6l5KpIKygB
github.com/ArtisanCloud/PowerLibs/v3 v3.0.15/go.mod h1:KBO7ZQGTBjvYTveKY5M4QQpwxuoGBI0V2fZ4xP+TIuM=
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.7 h1:P+erNlErr+X2v7Et+yTWaTfIRhw+HfpAPdvNIEwk9Gw=
github.com/ArtisanCloud/PowerSocialite/v3 v3.0.7/go.mod h1:VZQNCvcK/rldF3QaExiSl1gJEAkyc5/I8RLOd3WFZq4=
github.com/ArtisanCloud/PowerWeChat/v3 v3.1.4 h1:2xrbaBcNvnEUjq5IC0T5j+Q+9HFM8ub6KtBNeVbgVP0=
github.com/ArtisanCloud/PowerWeChat/v3 v3.1.4/go.mod h1:N6ATGWtdjvnts0dzbe9AYxQ1aRuA1yuTTabcUTQPFZo=
github.com/ArtisanCloud/PowerWeChat/v3 v3.1.12 h1:XlQXLOUpdGluhK6hYs1Pv+8ds314iPTB59ABcNBkAoE=
github.com/ArtisanCloud/PowerWeChat/v3 v3.1.12/go.mod h1:N6ATGWtdjvnts0dzbe9AYxQ1aRuA1yuTTabcUTQPFZo=
github.com/Blank-Xu/sql-adapter v0.0.0-20210714092925-0bdd0d548758 h1:L4iwZiJYTNirz4Er7Bw2+AVzW8VCpCGYc8luHc3y5lU=
github.com/Blank-Xu/sql-adapter v0.0.0-20210714092925-0bdd0d548758/go.mod h1:GlBcPpgzETyqz83HIUFEMobVY38h1fOFXgwpy7T64xE=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (l *GetOAMediaListLogic) GetOAMediaList(req *types.GetOAMediaListRequest) (
if req.Count <= 0 {
req.Count = 10
}
req.Offset -= 1
res, err := l.svcCtx.PowerX.WechatOA.App.Material.List(l.ctx, &request.RequestMaterialBatchGetMaterial{
Type: req.MediaType,
Offset: req.Offset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package media
import (
"PowerX/internal/logic/admin/mediaresource"
"PowerX/internal/types/errorx"
fmt "PowerX/pkg/printx"
"context"
fmt2 "fmt"
"github.com/ArtisanCloud/PowerLibs/v3/object"
Expand Down Expand Up @@ -34,11 +35,31 @@ func NewUploadOAMediaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upl

func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateOAMediaReply, err error) {

// 解析表单数据
err = r.ParseMultipartForm(mediaresource.MaxFileSize)
if err != nil {
return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error())
}

paramValues := r.Form
mediaType := paramValues.Get("type")
fmt.Dump(mediaType)
query := &object.StringMap{}
res := &response.ResponseMaterialAddMaterial{}
if mediaType == "video" {
jsonDescription, err := object.JsonEncode(&object.HashMap{
"title": paramValues.Get("title"),
"introduction": paramValues.Get("description"),
})
if err != nil {
return nil, err
}

query = &object.StringMap{
"Description": jsonDescription,
}
}

file, _, err := r.FormFile("file")
//fmt.Dump(handler.Filename)
if err != nil {
Expand All @@ -55,6 +76,11 @@ func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateO
// 获取文件的临时目录和文件名
tempDir := os.TempDir()
tempFileName := fmt2.Sprintf("%d_*.jpg", time.Now().Unix())
if mediaType == "video" {
tempFileName = fmt2.Sprintf("%d_*.mp4", time.Now().Unix())
} else if mediaType == "voice" {
tempFileName = fmt2.Sprintf("%d_*.mp3", time.Now().Unix())
}

// 创建临时文件
tempFile, err := os.CreateTemp(tempDir, tempFileName)
Expand All @@ -68,9 +94,8 @@ func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateO
return nil, errorx.WithCause(errorx.ErrBadRequest, "failed to save file")
}
//fmt.Dump("temp", tempFile.Name(), "end")
paramValues := r.Form
res := &response.ResponseMaterialAddMaterial{}
_, err = l.svcCtx.PowerX.WechatOA.App.Material.Upload(l.ctx, paramValues.Get("type"), tempFile.Name(), &object.StringMap{}, res)

_, err = l.svcCtx.PowerX.WechatOA.App.Material.Upload(l.ctx, mediaType, tempFile.Name(), query, res)
if err != nil {
return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error())
}
Expand Down

0 comments on commit 398da65

Please sign in to comment.