diff --git a/server/resource/internal/handler/resourceinfohandler.go b/server/resource/internal/handler/resourceinfohandler.go new file mode 100644 index 00000000..14b39d98 --- /dev/null +++ b/server/resource/internal/handler/resourceinfohandler.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 ResourceInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.ResourceInfoReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewResourceInfoLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.ResourceInfo(&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 efeb7530..581fdcb1 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.MethodGet, + Path: "/api/resource/info", + Handler: ResourceInfoHandler(serverCtx), + }, }, ) } diff --git a/server/resource/internal/logic/resourceinfologic.go b/server/resource/internal/logic/resourceinfologic.go new file mode 100644 index 00000000..599bede4 --- /dev/null +++ b/server/resource/internal/logic/resourceinfologic.go @@ -0,0 +1,64 @@ +package logic + +import ( + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/hash" + + "context" + + "fusenapi/server/resource/internal/svc" + "fusenapi/server/resource/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ResourceInfoLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewResourceInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResourceInfoLogic { + return &ResourceInfoLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *ResourceInfoLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *ResourceInfoLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *ResourceInfoLogic) ResourceInfo(req *types.ResourceInfoReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + var resourceId = req.ResourceId + if req.ResourceKey != "" { + resourceId = hash.JsonHashKey(req.ResourceKey) + } + + resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) + resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId) + + var resourceUrl string + var metadata string + if err == nil && resourceInfo.ResourceId != "" { + resourceId = resourceInfo.ResourceId + resourceUrl = *resourceInfo.ResourceUrl + metadata = *resourceInfo.Metadata + } + return resp.SetStatus(basic.CodeOK, map[string]interface{}{ + "resource_id": resourceId, + "resource_url": resourceUrl, + "resource_metadata": metadata, + }) +} diff --git a/server/resource/internal/types/types.go b/server/resource/internal/types/types.go index e2d222ed..47e38155 100644 --- a/server/resource/internal/types/types.go +++ b/server/resource/internal/types/types.go @@ -5,6 +5,11 @@ import ( "fusenapi/utils/basic" ) +type ResourceInfoReq struct { + ResourceId string `form:"resource_id,optional"` // 资源ID + ResourceKey string `form:"resource_key,optional"` // 资源唯一标识 +} + type LogoCombineReq struct { ResourceKey string `form:"resource_key"` // 资源唯一标识 CombineParam string `form:"combine_param"` // 合图参数 diff --git a/server_api/resource.api b/server_api/resource.api index d04080ed..0a37f93f 100644 --- a/server_api/resource.api +++ b/server_api/resource.api @@ -12,8 +12,18 @@ import "basic.api" service resource { @handler LogoCombineHandler post /api/resource/logo-combine(LogoCombineReq) returns (response); + + @handler ResourceInfoHandler + get /api/resource/info(ResourceInfoReq) returns (response); } +type ( + ResourceInfoReq { + ResourceId string `form:"resource_id,optional"` // 资源ID + ResourceKey string `form:"resource_key,optional"` // 资源唯一标识 + } +) + type ( LogoCombineReq { ResourceKey string `form:"resource_key"` // 资源唯一标识