From 4be82f6543f00e20e0ff4b6247d768f60318300b Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 21 Nov 2023 01:26:58 +0800 Subject: [PATCH] feat(wechat): upload image to oa --- api/admin/wechat/officialaccount/media.api | 7 +- .../media/getmedialisthandler.go | 4 +- .../media/getoamedialisthandler.go | 28 +++++++ .../media/uploadoamediahandler.go | 21 +++++ internal/handler/routes.go | 7 +- .../media/deleteoamedialogic.go | 15 +++- ...dialistlogic.go => getoamedialistlogic.go} | 13 +-- .../media/uploadoamedialogic.go | 84 +++++++++++++++++++ pkg/filex/filex.go | 7 ++ 9 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 internal/handler/admin/wechat/officialaccount/media/getoamedialisthandler.go create mode 100644 internal/handler/admin/wechat/officialaccount/media/uploadoamediahandler.go rename internal/logic/admin/wechat/officialaccount/media/{getmedialistlogic.go => getoamedialistlogic.go} (74%) create mode 100644 internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go diff --git a/api/admin/wechat/officialaccount/media.api b/api/admin/wechat/officialaccount/media.api index 93df269c..9f4dd0eb 100644 --- a/api/admin/wechat/officialaccount/media.api +++ b/api/admin/wechat/officialaccount/media.api @@ -16,7 +16,7 @@ info( service PowerX { @doc "查询菜单列表" - @handler GetMediaList + @handler GetOAMediaList post /medias/page-list (GetOAMediaListRequest) returns (GetOAMediaListReply) @doc "查询菜单列表" @@ -28,6 +28,11 @@ service PowerX { post /medias/:mediaId (GetOAMediaRequest) returns (GetOAMediaReply) + @doc "创建菜单" + @handler UploadOAMedia + post /medias/upload returns (CreateOAMediaReply) + + @doc "创建菜单" @handler CreateOAMedia post /medias (CreateOAMediaRequest) returns (CreateOAMediaReply) diff --git a/internal/handler/admin/wechat/officialaccount/media/getmedialisthandler.go b/internal/handler/admin/wechat/officialaccount/media/getmedialisthandler.go index 7abaee2f..cabea964 100644 --- a/internal/handler/admin/wechat/officialaccount/media/getmedialisthandler.go +++ b/internal/handler/admin/wechat/officialaccount/media/getmedialisthandler.go @@ -17,8 +17,8 @@ func GetMediaListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return } - l := media.NewGetMediaListLogic(r.Context(), svcCtx) - resp, err := l.GetMediaList(&req) + l := media.NewGetOAMediaListLogic(r.Context(), svcCtx) + resp, err := l.GetOAMediaList(&req) if err != nil { httpx.ErrorCtx(r.Context(), w, err) } else { diff --git a/internal/handler/admin/wechat/officialaccount/media/getoamedialisthandler.go b/internal/handler/admin/wechat/officialaccount/media/getoamedialisthandler.go new file mode 100644 index 00000000..ba52193d --- /dev/null +++ b/internal/handler/admin/wechat/officialaccount/media/getoamedialisthandler.go @@ -0,0 +1,28 @@ +package media + +import ( + "net/http" + + "PowerX/internal/logic/admin/wechat/officialaccount/media" + "PowerX/internal/svc" + "PowerX/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func GetOAMediaListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.GetOAMediaListRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := media.NewGetOAMediaListLogic(r.Context(), svcCtx) + resp, err := l.GetOAMediaList(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/internal/handler/admin/wechat/officialaccount/media/uploadoamediahandler.go b/internal/handler/admin/wechat/officialaccount/media/uploadoamediahandler.go new file mode 100644 index 00000000..bfef0f31 --- /dev/null +++ b/internal/handler/admin/wechat/officialaccount/media/uploadoamediahandler.go @@ -0,0 +1,21 @@ +package media + +import ( + "net/http" + + "PowerX/internal/logic/admin/wechat/officialaccount/media" + "PowerX/internal/svc" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func UploadOAMediaHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + l := media.NewUploadOAMediaLogic(r.Context(), svcCtx) + resp, err := l.UploadOAMedia(r) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 7eca11e9..b3ee9de7 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -1578,7 +1578,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { { Method: http.MethodPost, Path: "/medias/page-list", - Handler: adminwechatofficialaccountmedia.GetMediaListHandler(serverCtx), + Handler: adminwechatofficialaccountmedia.GetOAMediaListHandler(serverCtx), }, { Method: http.MethodGet, @@ -1590,6 +1590,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/medias/:mediaId", Handler: adminwechatofficialaccountmedia.GetOAMediaHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/medias/upload", + Handler: adminwechatofficialaccountmedia.UploadOAMediaHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/medias", diff --git a/internal/logic/admin/wechat/officialaccount/media/deleteoamedialogic.go b/internal/logic/admin/wechat/officialaccount/media/deleteoamedialogic.go index 7e817c94..f7460a0d 100644 --- a/internal/logic/admin/wechat/officialaccount/media/deleteoamedialogic.go +++ b/internal/logic/admin/wechat/officialaccount/media/deleteoamedialogic.go @@ -1,6 +1,7 @@ package media import ( + "PowerX/internal/types/errorx" "context" "PowerX/internal/svc" @@ -24,7 +25,17 @@ func NewDeleteOAMediaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Del } func (l *DeleteOAMediaLogic) DeleteOAMedia(req *types.DeleteOAMediaRequest) (resp *types.DeleteOAMediaReply, err error) { - // todo: add your logic here and delete this line + res, err := l.svcCtx.PowerX.WechatOA.App.Material.Delete(l.ctx, req.MediaId) + if err != nil { + return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) + } + + if res.ErrCode != 0 { + return nil, errorx.WithCause(errorx.ErrBadRequest, res.ErrMsg) + } - return + return &types.DeleteOAMediaReply{ + Success: true, + Data: nil, + }, nil } diff --git a/internal/logic/admin/wechat/officialaccount/media/getmedialistlogic.go b/internal/logic/admin/wechat/officialaccount/media/getoamedialistlogic.go similarity index 74% rename from internal/logic/admin/wechat/officialaccount/media/getmedialistlogic.go rename to internal/logic/admin/wechat/officialaccount/media/getoamedialistlogic.go index 6767a68b..617edc4b 100644 --- a/internal/logic/admin/wechat/officialaccount/media/getmedialistlogic.go +++ b/internal/logic/admin/wechat/officialaccount/media/getoamedialistlogic.go @@ -1,30 +1,31 @@ package media import ( - "PowerX/internal/svc" - "PowerX/internal/types" "PowerX/internal/types/errorx" "context" "github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/material/request" + "PowerX/internal/svc" + "PowerX/internal/types" + "github.com/zeromicro/go-zero/core/logx" ) -type GetMediaListLogic struct { +type GetOAMediaListLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } -func NewGetMediaListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMediaListLogic { - return &GetMediaListLogic{ +func NewGetOAMediaListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOAMediaListLogic { + return &GetOAMediaListLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } -func (l *GetMediaListLogic) GetMediaList(req *types.GetOAMediaListRequest) (resp *types.GetOAMediaListReply, err error) { +func (l *GetOAMediaListLogic) GetOAMediaList(req *types.GetOAMediaListRequest) (resp *types.GetOAMediaListReply, err error) { if req.Count <= 0 { req.Count = 10 diff --git a/internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go b/internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go new file mode 100644 index 00000000..ca17d888 --- /dev/null +++ b/internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go @@ -0,0 +1,84 @@ +package media + +import ( + "PowerX/internal/logic/admin/mediaresource" + "PowerX/internal/types/errorx" + "context" + fmt2 "fmt" + "github.com/ArtisanCloud/PowerLibs/v3/object" + "github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/material/response" + "io" + "net/http" + "os" + "time" + + "PowerX/internal/svc" + "PowerX/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type UploadOAMediaLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUploadOAMediaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadOAMediaLogic { + return &UploadOAMediaLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +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()) + } + + file, _, err := r.FormFile("file") + //fmt.Dump(handler.Filename) + if err != nil { + return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) + } + defer file.Close() + + // 读取文件内容 + fileContents, err := io.ReadAll(file) + if err != nil { + return nil, errorx.WithCause(errorx.ErrBadRequest, "failed to read file content") + } + + // 获取文件的临时目录和文件名 + tempDir := os.TempDir() + tempFileName := fmt2.Sprintf("%d_*.jpg", time.Now().Unix()) + + // 创建临时文件 + tempFile, err := os.CreateTemp(tempDir, tempFileName) + if err != nil { + return nil, errorx.WithCause(errorx.ErrBadRequest, "failed to create temporary file") + } + defer os.Remove(tempFile.Name()) // 删除临时文件,确保在函数退出时清理 + + // 将文件内容保存到临时文件 + if err := os.WriteFile(tempFile.Name(), fileContents, 0644); err != nil { + 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) + if err != nil { + return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) + } + if res.ErrCode != 0 { + return nil, errorx.WithCause(errorx.ErrBadRequest, res.ErrMsg) + } + + return &types.CreateOAMediaReply{ + Success: true, + }, nil +} diff --git a/pkg/filex/filex.go b/pkg/filex/filex.go index abde4761..a44cbf92 100644 --- a/pkg/filex/filex.go +++ b/pkg/filex/filex.go @@ -4,6 +4,7 @@ import ( "io" "mime/multipart" "os" + "path/filepath" "strings" ) @@ -41,3 +42,9 @@ func SaveFileToLocal(fileHeader *multipart.FileHeader, uploadPath string) error return nil } + +func GetTempFilePath(fileName string) (string, error) { + + filePath := filepath.Join(os.TempDir(), fileName) + return filePath, nil +}