-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from ArtisanCloud/dev/michaelhu
feat(wechat): upload image to oa
- Loading branch information
Showing
9 changed files
with
174 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
internal/handler/admin/wechat/officialaccount/media/getoamedialisthandler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
internal/handler/admin/wechat/officialaccount/media/uploadoamediahandler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletions
13
...fficialaccount/media/getmedialistlogic.go → ...icialaccount/media/getoamedialistlogic.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters