feat:获取资源详情
This commit is contained in:
parent
2ed06a6df0
commit
d5e5cdc8f0
35
server/resource/internal/handler/resourceinfohandler.go
Normal file
35
server/resource/internal/handler/resourceinfohandler.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
64
server/resource/internal/logic/resourceinfologic.go
Normal file
64
server/resource/internal/logic/resourceinfologic.go
Normal file
|
@ -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,
|
||||
})
|
||||
}
|
|
@ -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"` // 合图参数
|
||||
|
|
|
@ -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"` // 资源唯一标识
|
||||
|
|
Loading…
Reference in New Issue
Block a user