fusenapi/server/resource/internal/logic/resourceinfologic.go
2023-09-05 14:26:31 +08:00

66 lines
1.8 KiB
Go

package logic
import (
"encoding/json"
"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 resourceMetadata map[string]interface{}
if err == nil && resourceInfo.ResourceId != "" {
resourceId = resourceInfo.ResourceId
resourceUrl = *resourceInfo.ResourceUrl
json.Unmarshal([]byte(*resourceInfo.Metadata), &resourceMetadata)
}
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"resource_id": resourceId,
"resource_url": resourceUrl,
"resource_metadata": resourceMetadata,
})
}