fusenapi/server/home-user-auth/internal/logic/userlogodatasetlogic.go
2023-10-26 16:46:14 +08:00

131 lines
3.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
import (
"errors"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/service/repositories"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/s3url_to_s3id"
"time"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/zeromicro/go-zero/core/logc"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type UserLogoDataSetLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserLogoDataSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoDataSetLogic {
return &UserLogoDataSetLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserLogoDataSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *UserLogoDataSetLogic) UserLogoDataSet(req *types.UserLogoDataSetReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if userinfo.IsOnlooker() {
// 如果是,返回未授权的错误码
return resp.SetStatus(basic.CodeUnAuth)
}
var userId int64
var guestId int64
// 检查用户是否是游客
if userinfo.IsGuest() {
// 如果是使用游客ID和游客键名格式
guestId = userinfo.GuestId
} else {
// 否则使用用户ID和用户键名格式
userId = userinfo.UserId
}
var logoData = gmodel.FsPreprocessLogo{}
result := l.svcCtx.MysqlConn.Model(&gmodel.FsPreprocessLogo{}).Where("id = ?", req.LogoDataId).Take(&logoData)
err := result.Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logc.Errorf(l.ctx, "UserLogoDataSet logoData find err%+v", err)
basic.CodeApiErr.Message = "logo data not find"
return resp.SetStatus(basic.CodeApiErr)
}
return resp.SetStatus(basic.CodeApiErr)
}
var userMaterialMetadata []byte
if logoData.Metadata == nil {
var resultStr string
resLogoStandard, err := l.svcCtx.Repositories.ImageHandle.LogoInfoSet(l.ctx, &repositories.LogoInfoSetReq{
LogoUrl: *logoData.ResourceUrl,
Version: l.svcCtx.Config.BLMService.Version,
Debug: userinfo.Debug,
})
if err != nil {
logx.Error(err)
basic.CodeServiceErr.Message = fmt.Sprintf("算法请求--LOGO信息--错误:%+v", err)
return resp.SetStatus(basic.CodeServiceErr, fmt.Sprintf("算法请求--LOGO信息--错误:%+v", err))
}
resultStr = resLogoStandard.Res
userMaterialMetadata = []byte(resultStr)
} else {
userMaterialMetadata = *logoData.Metadata
}
// 新增素材
var module = "logo"
var nowTime = time.Now().UTC()
var resourceId = s3url_to_s3id.GetS3ResourceIdFormUrl(*logoData.ResourceUrl)
// 新增素材记录
materialInfo := gmodel.FsUserMaterial{
Module: &module,
UserId: &userId,
GuestId: &guestId,
ResourceId: &resourceId,
ResourceUrl: logoData.ResourceUrl,
Metadata: &userMaterialMetadata,
Ctime: &nowTime,
}
resCreate := l.svcCtx.MysqlConn.Create(&materialInfo)
err = resCreate.Error
if err != nil {
logc.Errorf(l.ctx, "UserLogoDataSet logoData find err%+v", err)
return resp.SetStatus(basic.CodeApiErr)
}
// 返回成功的响应和上传URL
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"id": materialInfo.Id,
"module": materialInfo.Module,
"user_id": materialInfo.UserId,
"guest_id": materialInfo.GuestId,
"resource_id": materialInfo.ResourceId,
"resource_url": materialInfo.ResourceUrl,
"metadata": materialInfo.Metadata,
"ctime": materialInfo.Ctime,
"resource_info": nil,
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserLogoDataSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }