diff --git a/server/resource/internal/handler/logoremovebghandler.go b/server/resource/internal/handler/logoremovebghandler.go new file mode 100644 index 00000000..b68ab38b --- /dev/null +++ b/server/resource/internal/handler/logoremovebghandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/resource/internal/logic" + "fusenapi/server/resource/internal/svc" + "fusenapi/server/resource/internal/types" +) + +func LogoRemovebgHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.LogoRemovebgReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewLogoRemovebgLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.LogoRemovebg(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/resource/internal/handler/routes.go b/server/resource/internal/handler/routes.go index 581fdcb1..50884ef8 100644 --- a/server/resource/internal/handler/routes.go +++ b/server/resource/internal/handler/routes.go @@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/resource/logo-combine", Handler: LogoCombineHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/resource/logo-removebg", + Handler: LogoRemovebgHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/api/resource/info", diff --git a/server/resource/internal/logic/logoremovebglogic.go b/server/resource/internal/logic/logoremovebglogic.go new file mode 100644 index 00000000..6088c9e7 --- /dev/null +++ b/server/resource/internal/logic/logoremovebglogic.go @@ -0,0 +1,61 @@ +package logic + +import ( + "fusenapi/service/repositories" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/resource/internal/svc" + "fusenapi/server/resource/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type LogoRemovebgLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewLogoRemovebgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoRemovebgLogic { + return &LogoRemovebgLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *LogoRemovebgLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *LogoRemovebgLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *LogoRemovebgLogic) LogoRemovebg(req *types.LogoRemovebgReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + res, err := l.svcCtx.Repositories.ImageHandle.LogoStandard(l.ctx, &repositories.LogoStandardReq{ + IsRemoveBg: req.IsRemoveBg, + LogoFile: req.LogoFile, + Width: req.Width, + Height: req.Height, + Proportion: req.Proportion, + }) + + if err != nil { + return resp.SetStatus(basic.CodeServiceErr) + } + + // 返回成功的响应和上传URL + return resp.SetStatus(basic.CodeOK, map[string]interface{}{ + "resource_id": res.ResourceId, + "resource_url": res.ResourceUrl, + "ismax_proportion": res.IsmaxProportion, + "img_color": req.Proportion, + }) +} diff --git a/server/resource/internal/types/types.go b/server/resource/internal/types/types.go index f32c5505..ccf8d112 100644 --- a/server/resource/internal/types/types.go +++ b/server/resource/internal/types/types.go @@ -5,6 +5,14 @@ import ( "fusenapi/utils/basic" ) +type LogoRemovebgReq struct { + IsRemoveBg string `form:"is_remove_bg"` + LogoFile string `form:"logo_file"` + Width string `form:"width"` + Height string `form:"height"` + Proportion int64 `form:"proportion"` +} + type ResourceInfoReq struct { ResourceId string `form:"resource_id,optional"` // 资源ID ResourceKey string `form:"resource_key,optional"` // 资源唯一标识 diff --git a/server_api/resource.api b/server_api/resource.api index 89423498..f78dae89 100644 --- a/server_api/resource.api +++ b/server_api/resource.api @@ -13,10 +13,23 @@ service resource { @handler LogoCombineHandler post /api/resource/logo-combine(LogoCombineReq) returns (response); + @handler LogoRemovebgHandler + post /api/resource/logo-removebg(LogoRemovebgReq) returns (response); + @handler ResourceInfoHandler get /api/resource/info(ResourceInfoReq) returns (response); } +type ( + LogoRemovebgReq { + IsRemoveBg string `form:"is_remove_bg"` + LogoFile string `form:"logo_file"` + Width string `form:"width"` + Height string `form:"height"` + Proportion int64 `form:"proportion"` + } +) + type ( ResourceInfoReq { ResourceId string `form:"resource_id,optional"` // 资源ID diff --git a/service/repositories/image_handle.go b/service/repositories/image_handle.go index 09eda85f..700d89a8 100644 --- a/service/repositories/image_handle.go +++ b/service/repositories/image_handle.go @@ -250,7 +250,8 @@ type ( /* 图片裁剪 */ func (l *defaultImageHandle) LogoStandard(ctx context.Context, in *LogoStandardReq) (*LogoStandardRes, error) { - var postMap = make(map[string]interface{}, 2) + var resourceId string = hash.JsonHashKey(in) + var postMap = make(map[string]interface{}, 5) postMap["is_remove_bg"] = in.IsRemoveBg postMap["logo_file"] = in.LogoFile postMap["width"] = in.Width @@ -305,7 +306,37 @@ func (l *defaultImageHandle) LogoStandard(ctx context.Context, in *LogoStandardR } //$removeBg ='{"nobg_url": "/test/dIE10gGfXM_scale.png", "thumbnail_url": "/test/dIE10gGfXM_thumbnail.png", "ismax_proportion": true, "img_color": ["#000000", "#EEF5FB", "#6AAFE6", "#9ECDF1", "#298EDC", "#0C7BD1"]}' - return nil, nil + var fileBase = resultData["nobg_url"].(string) + var ismaxProportion = resultData["ismax_proportion"].(bool) + + var imgColor []string + for _, v := range resultData["img_color"].([]interface{}) { + imgColor = append(imgColor, v.(string)) + } + + // 上传文件 + var upload = file.Upload{ + Ctx: ctx, + MysqlConn: l.MysqlConn, + AwsSession: l.AwsSession, + } + uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{ + Source: "combine-removebg", + FileHash: resourceId, + FileData: fileBase, + UploadBucket: 1, + ApiType: 2, + }) + if err != nil { + logx.Error(err) + return nil, err + } + return &LogoStandardRes{ + ResourceId: uploadRes.ResourceId, + ResourceUrl: uploadRes.ResourceUrl, + IsmaxProportion: ismaxProportion, + ImgColor: imgColor, + }, nil } /* 图片裁剪 */