Skip to content

Commit

Permalink
Merge pull request #278 from ArtisanCloud/dev/michaelhu
Browse files Browse the repository at this point in the history
feat(wechat): upload image to oa
  • Loading branch information
Matrix-X authored Nov 20, 2023
2 parents db6abff + 4be82f6 commit 6d8dfe0
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 12 deletions.
7 changes: 6 additions & 1 deletion api/admin/wechat/officialaccount/media.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ info(

service PowerX {
@doc "查询菜单列表"
@handler GetMediaList
@handler GetOAMediaList
post /medias/page-list (GetOAMediaListRequest) returns (GetOAMediaListReply)

@doc "查询菜单列表"
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
7 changes: 6 additions & 1 deletion internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package media

import (
"PowerX/internal/types/errorx"
"context"

"PowerX/internal/svc"
Expand All @@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions pkg/filex/filex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"mime/multipart"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 6d8dfe0

Please sign in to comment.