diff --git a/api/admin/wechat/officialaccount/media.api b/api/admin/wechat/officialaccount/media.api index 8c0b5b1d..3c425a82 100644 --- a/api/admin/wechat/officialaccount/media.api +++ b/api/admin/wechat/officialaccount/media.api @@ -27,6 +27,10 @@ service PowerX { @handler GetOAMedia get /medias/:mediaId (GetOAMediaRequest) returns (GetOAMediaReply) + @doc "根据媒体key获取媒体" + @handler GetOAMediaByVideo + get /medias/video/:mediaId (GetOAMediaRequest) returns (GetOAMediaByVideoReply) + @doc "创建菜单" @handler UploadOAMedia @@ -79,6 +83,12 @@ type ( GetOAMediaReply struct { OAMedia interface{} `json:"media"` } + + GetOAMediaByVideoReply struct { + Title string `json:"title"` + Description string `json:"description"` + DownUrl string `json:"down_url"` + } ) diff --git a/internal/handler/admin/crm/market/media/getmediabyvideohandler.go b/internal/handler/admin/crm/market/media/getmediabyvideohandler.go new file mode 100644 index 00000000..7296e2da --- /dev/null +++ b/internal/handler/admin/crm/market/media/getmediabyvideohandler.go @@ -0,0 +1,28 @@ +package media + +import ( + "net/http" + + "PowerX/internal/logic/admin/crm/market/media" + "PowerX/internal/svc" + "PowerX/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func GetMediaByVideoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.GetMediaRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := media.NewGetMediaByVideoLogic(r.Context(), svcCtx) + resp, err := l.GetMediaByVideo(&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/getoamediabyvideohandler.go b/internal/handler/admin/wechat/officialaccount/media/getoamediabyvideohandler.go new file mode 100644 index 00000000..0c2bb368 --- /dev/null +++ b/internal/handler/admin/wechat/officialaccount/media/getoamediabyvideohandler.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 GetOAMediaByVideoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.GetOAMediaRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := media.NewGetOAMediaByVideoLogic(r.Context(), svcCtx) + resp, err := l.GetOAMediaByVideo(&req) + 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 ae54fda3..b83fe09d 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -1590,6 +1590,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/medias/:mediaId", Handler: adminwechatofficialaccountmedia.GetOAMediaHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/medias/video/:mediaId", + Handler: adminwechatofficialaccountmedia.GetOAMediaByVideoHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/medias/upload", diff --git a/internal/logic/admin/crm/market/media/getmediabyvideologic.go b/internal/logic/admin/crm/market/media/getmediabyvideologic.go new file mode 100644 index 00000000..906a43f5 --- /dev/null +++ b/internal/logic/admin/crm/market/media/getmediabyvideologic.go @@ -0,0 +1,30 @@ +package media + +import ( + "context" + + "PowerX/internal/svc" + "PowerX/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetMediaByVideoLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetMediaByVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMediaByVideoLogic { + return &GetMediaByVideoLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetMediaByVideoLogic) GetMediaByVideo(req *types.GetMediaRequest) (resp *types.GetMediaReply, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/logic/admin/wechat/officialaccount/media/getoamediabyvideologic.go b/internal/logic/admin/wechat/officialaccount/media/getoamediabyvideologic.go new file mode 100644 index 00000000..049d9987 --- /dev/null +++ b/internal/logic/admin/wechat/officialaccount/media/getoamediabyvideologic.go @@ -0,0 +1,34 @@ +package media + +import ( + "context" + + "PowerX/internal/svc" + "PowerX/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetOAMediaByVideoLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetOAMediaByVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOAMediaByVideoLogic { + return &GetOAMediaByVideoLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetOAMediaByVideoLogic) GetOAMediaByVideo(req *types.GetOAMediaRequest) (resp *types.GetOAMediaByVideoReply, err error) { + res, err := l.svcCtx.PowerX.WechatOA.App.Material.GetVideo(l.ctx, req.MediaId) + + return &types.GetOAMediaByVideoReply{ + Title: res.Title, + Description: res.Description, + DownUrl: res.DownUrl, + }, nil +} diff --git a/internal/logic/admin/wechat/officialaccount/media/getoamedialogic.go b/internal/logic/admin/wechat/officialaccount/media/getoamedialogic.go index d0b7259e..2db4baef 100644 --- a/internal/logic/admin/wechat/officialaccount/media/getoamedialogic.go +++ b/internal/logic/admin/wechat/officialaccount/media/getoamedialogic.go @@ -4,6 +4,7 @@ import ( "PowerX/internal/svc" "PowerX/internal/types" "context" + "io" "github.com/zeromicro/go-zero/core/logx" ) @@ -25,8 +26,15 @@ func NewGetOAMediaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOAM func (l *GetOAMediaLogic) GetOAMedia(req *types.GetOAMediaRequest) (resp *types.GetOAMediaReply, err error) { res, err := l.svcCtx.PowerX.WechatOA.App.Material.Get(l.ctx, req.MediaId) + defer res.Body.Close() + + // 读取响应体 + body, err := io.ReadAll(res.Body) + if err != nil { + return nil, err + } return &types.GetOAMediaReply{ - OAMedia: res, + OAMedia: body, }, nil } diff --git a/internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go b/internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go index 0aa90008..dd684750 100644 --- a/internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go +++ b/internal/logic/admin/wechat/officialaccount/media/uploadoamedialogic.go @@ -2,21 +2,17 @@ package media import ( "PowerX/internal/logic/admin/mediaresource" + "PowerX/internal/svc" + "PowerX/internal/types" "PowerX/internal/types/errorx" - fmt "PowerX/pkg/printx" + "PowerX/pkg/filex" "context" - fmt2 "fmt" "github.com/ArtisanCloud/PowerLibs/v3/object" "github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/material/response" + "github.com/zeromicro/go-zero/core/logx" "io" "net/http" "os" - "time" - - "PowerX/internal/svc" - "PowerX/internal/types" - - "github.com/zeromicro/go-zero/core/logx" ) type UploadOAMediaLogic struct { @@ -43,7 +39,7 @@ func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateO paramValues := r.Form mediaType := paramValues.Get("type") - fmt.Dump(mediaType) + //fmt.Dump(mediaType) query := &object.StringMap{} res := &response.ResponseMaterialAddMaterial{} if mediaType == "video" { @@ -60,7 +56,7 @@ func (l *UploadOAMediaLogic) UploadOAMedia(r *http.Request) (resp *types.CreateO } } - file, _, err := r.FormFile("file") + file, handler, err := r.FormFile("file") //fmt.Dump(handler.Filename) if err != nil { return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) @@ -75,15 +71,12 @@ 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()) - } + //extension := filex.GetFileExtension(handler.Filename) + //tempFileName := fmt2.Sprintf("%d_*.%s", time.Now().Unix(), extension) + tempFileName := handler.Filename // 创建临时文件 - tempFile, err := os.CreateTemp(tempDir, tempFileName) + tempFile, err := filex.CreateTempWithoutRandom(tempDir, tempFileName) if err != nil { return nil, errorx.WithCause(errorx.ErrBadRequest, "failed to create temporary file") } diff --git a/internal/types/types.go b/internal/types/types.go index 0c14a22b..9587f343 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -3028,6 +3028,12 @@ type GetOAMediaReply struct { OAMedia interface{} `json:"media"` } +type GetOAMediaByVideoReply struct { + Title string `json:"title"` + Description string `json:"description"` + DownUrl string `json:"down_url"` +} + type DeleteOAMediaRequest struct { MediaId string `path:"mediaId"` } diff --git a/pkg/filex/filex.go b/pkg/filex/filex.go index a44cbf92..7ed1c79b 100644 --- a/pkg/filex/filex.go +++ b/pkg/filex/filex.go @@ -48,3 +48,36 @@ func GetTempFilePath(fileName string) (string, error) { filePath := filepath.Join(os.TempDir(), fileName) return filePath, nil } + +func GetFileExtension(filename string) string { + ext := filepath.Ext(filename) + // 去掉后缀中的点号 + return ext[1:] +} + +// CreateTempWithoutRandom creates a new temporary file in the directory dir +// with the specified filename. +// If dir is the empty string, CreateTempWithoutRandom uses the default directory for temporary files, as returned by TempDir. +// The caller can use the file's Name method to find the pathname of the file. +// It is the caller's responsibility to remove the file when it is no longer needed. +func CreateTempWithoutRandom(dir, filename string) (*os.File, error) { + if dir == "" { + dir = os.TempDir() + } + + path := filepath.Join(dir, filename) + + // 先检查文件是否存在,存在则删除 + if _, err := os.Stat(path); err == nil { + if err := os.Remove(path); err != nil { + return nil, err + } + } + + f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) + if err != nil { + return nil, &os.PathError{Op: "createtemp", Path: path, Err: err} + } + + return f, nil +} diff --git a/wechat/info-2023-11-26T17-32-18.551.log.gz b/wechat/info-2023-11-26T17-32-18.551.log.gz new file mode 100644 index 00000000..2c57f552 Binary files /dev/null and b/wechat/info-2023-11-26T17-32-18.551.log.gz differ diff --git a/wechat/info-2023-11-26T17-44-07.965.log.gz b/wechat/info-2023-11-26T17-44-07.965.log.gz new file mode 100644 index 00000000..abfcffe0 Binary files /dev/null and b/wechat/info-2023-11-26T17-44-07.965.log.gz differ diff --git a/wechat/info-2023-11-26T17-55-09.552.log.gz b/wechat/info-2023-11-26T17-55-09.552.log.gz new file mode 100644 index 00000000..caf9cf40 Binary files /dev/null and b/wechat/info-2023-11-26T17-55-09.552.log.gz differ