From d06241b2678e1bad6825341e180a88328a51ce1e Mon Sep 17 00:00:00 2001 From: Hiven Date: Wed, 2 Aug 2023 18:14:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0logo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/fs_user_material_gen.go | 24 ++++ model/gmodel/fs_user_material_logic.go | 15 ++ model/gmodel/var_gen.go | 2 + server/upload/etc/upload.yaml | 5 +- server/upload/internal/config/config.go | 5 + server/upload/internal/handler/routes.go | 5 + .../internal/handler/uploadlogohandler.go | 35 +++++ .../upload/internal/logic/uploadlogologic.go | 134 ++++++++++++++++++ server/upload/internal/types/types.go | 8 ++ server_api/upload.api | 15 ++ utils/basic/basic.go | 3 +- 11 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 model/gmodel/fs_user_material_gen.go create mode 100644 model/gmodel/fs_user_material_logic.go create mode 100644 server/upload/internal/handler/uploadlogohandler.go create mode 100644 server/upload/internal/logic/uploadlogologic.go diff --git a/model/gmodel/fs_user_material_gen.go b/model/gmodel/fs_user_material_gen.go new file mode 100644 index 00000000..4de4076d --- /dev/null +++ b/model/gmodel/fs_user_material_gen.go @@ -0,0 +1,24 @@ +package gmodel + +import ( + "gorm.io/gorm" +) + +// fs_user_material 用户素材表 +type FsUserMaterial struct { + Id int64 `gorm:"primary_key;default:0;" json:"id"` // 用户 ID + Module *string `gorm:"default:'';" json:"module"` // 所属模块 + UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID + ResourceId *string `gorm:"default:'';" json:"resource_id"` // 资源ID + ResourceUrl *string `gorm:"default:'';" json:"resource_url"` // 资源 URL + Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式,存储图像分率 + CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 上传时间 +} +type FsUserMaterialModel struct { + db *gorm.DB + name string +} + +func NewFsUserMaterialModel(db *gorm.DB) *FsUserMaterialModel { + return &FsUserMaterialModel{db: db, name: "fs_user_material"} +} diff --git a/model/gmodel/fs_user_material_logic.go b/model/gmodel/fs_user_material_logic.go new file mode 100644 index 00000000..89dcc405 --- /dev/null +++ b/model/gmodel/fs_user_material_logic.go @@ -0,0 +1,15 @@ +package gmodel + +import "context" + +// TODO: 使用model的属性做你想做的 + +func (p *FsUserMaterialModel) CreateOrUpdate(ctx context.Context, req *FsUserMaterial) (resp *FsUserMaterial, err error) { + rowBuilder := p.db.Table(p.name).WithContext(ctx) + if req.Id > 0 { + err = rowBuilder.Save(req).Error + } else { + err = rowBuilder.Create(req).Error + } + return req, err +} diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index a90a6f7c..c2139293 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -90,6 +90,7 @@ type AllModelsGen struct { FsTrade *FsTradeModel // fs_trade FsUser *FsUserModel // fs_user 用户表 FsUserDesign *FsUserDesignModel // fs_user_design 废弃表 + FsUserMaterial *FsUserMaterialModel // fs_user_material 用户素材表 FsUserStock *FsUserStockModel // fs_user_stock 用户云仓库存 FsWebSet *FsWebSetModel // fs_web_set 网站配置表 @@ -183,6 +184,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsTrade: NewFsTradeModel(gdb), FsUser: NewFsUserModel(gdb), FsUserDesign: NewFsUserDesignModel(gdb), + FsUserMaterial: NewFsUserMaterialModel(gdb), FsUserStock: NewFsUserStockModel(gdb), FsWebSet: NewFsWebSetModel(gdb), } diff --git a/server/upload/etc/upload.yaml b/server/upload/etc/upload.yaml index 8481514f..e9c285fd 100644 --- a/server/upload/etc/upload.yaml +++ b/server/upload/etc/upload.yaml @@ -12,4 +12,7 @@ AWS: Credentials: AccessKeyID: AKIAZB2JKUXDPNRP4YT2 Secret: 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 - Token: \ No newline at end of file + Token: +BLMService: + ImageProcess: + Url: "http://110.41.19.98:8868/removebg" diff --git a/server/upload/internal/config/config.go b/server/upload/internal/config/config.go index a8d273d7..43cc0577 100644 --- a/server/upload/internal/config/config.go +++ b/server/upload/internal/config/config.go @@ -20,4 +20,9 @@ type Config struct { } } } + BLMService struct { + ImageProcess struct { + Url string + } + } } diff --git a/server/upload/internal/handler/routes.go b/server/upload/internal/handler/routes.go index 1417f043..8d5b0aa0 100644 --- a/server/upload/internal/handler/routes.go +++ b/server/upload/internal/handler/routes.go @@ -47,6 +47,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/upload/upload-callback", Handler: UploadCallbackHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/upload/up-logo", + Handler: UploadLogoHandler(serverCtx), + }, }, ) } diff --git a/server/upload/internal/handler/uploadlogohandler.go b/server/upload/internal/handler/uploadlogohandler.go new file mode 100644 index 00000000..52c58424 --- /dev/null +++ b/server/upload/internal/handler/uploadlogohandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/upload/internal/logic" + "fusenapi/server/upload/internal/svc" + "fusenapi/server/upload/internal/types" +) + +func UploadLogoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UploadLogoReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUploadLogoLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UploadLogo(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/upload/internal/logic/uploadlogologic.go b/server/upload/internal/logic/uploadlogologic.go new file mode 100644 index 00000000..df4283c9 --- /dev/null +++ b/server/upload/internal/logic/uploadlogologic.go @@ -0,0 +1,134 @@ +package logic + +import ( + "fmt" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "time" + + "context" + + "fusenapi/server/upload/internal/svc" + "fusenapi/server/upload/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type UploadLogoLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUploadLogoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogoLogic { + return &UploadLogoLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UploadLogoLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UploadLogoLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + if userinfo.IsOnlooker() { + // 如果是,返回未授权的错误码 + return resp.SetStatus(basic.CodeUnAuth) + } + + // 定义用户ID + var uid int64 + + // 检查用户是否是游客 + if userinfo.IsGuest() { + // 如果是,使用游客ID和游客键名格式 + uid = userinfo.GuestId + } else { + // 否则,使用用户ID和用户键名格式 + uid = userinfo.UserId + } + + fmt.Println(uid) + + var logoWidth int64 + var logoHeight int64 + // 查看sku是否存在 + if req.SkuId > 0 { + // 查询出产品模板信息 + productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn) + productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, req.SkuId) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeFileUploadLogoErr, "logo upload err,no product template") + } + + logoWidth = *productTemplateV2Info.LogoWidth + logoHeight = *productTemplateV2Info.LogoHeight + } + // 设置默认宽高 + if logoWidth == 0 || logoHeight == 0 { + logoWidth = 300 + logoHeight = 200 + } + var resultStr string + + // apiUrl := l.svcCtx.Config.BLMService.ImageProcess.Url + // var onlyScale = true + // data := url.Values{} + // data.Set("imgurl", req.ResourceUrl) + // data.Set("layerwidth", strconv.Itoa(int(logoWidth))) + // data.Set("layerheight", strconv.Itoa(int(logoHeight))) + // data.Set("is_remove_bg", strconv.Itoa(int(req.IsRemoveBg))) + // data.Set("proportion", strconv.Itoa(int(req.Proportion))) + // data.Set("only_scale", fmt.Sprintf("%v", onlyScale)) + + // u, err := url.ParseRequestURI(apiUrl) + // if err != nil { + // logx.Error(err) + // return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail") + // } + // u.RawQuery = data.Encode() // URL encode + // fmt.Println(u.String()) + // result, err := http.Get(u.String()) + // if err != nil { + // logx.Error(err) + // return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail") + // } + // defer result.Body.Close() + // b, err := io.ReadAll(result.Body) + // if err != nil { + // logx.Error(err) + // return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail") + // } + // resultStr = string(b) + + var module = "logo" + var nowTime = time.Now().Unix() + // 新增记录 + userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) + _, err := userMaterialModel.CreateOrUpdate(l.ctx, &gmodel.FsUserMaterial{ + Module: &module, + UserId: &uid, + ResourceId: &req.ResourceId, + ResourceUrl: &req.ResourceUrl, + Metadata: &resultStr, + CreateAt: &nowTime, + }) + + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail") + } + + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/upload/internal/types/types.go b/server/upload/internal/types/types.go index f09a9e44..8373d29c 100644 --- a/server/upload/internal/types/types.go +++ b/server/upload/internal/types/types.go @@ -5,6 +5,14 @@ import ( "fusenapi/utils/basic" ) +type UploadLogoReq struct { + ResourceId string `form:"resource_id"` // 资源ID + ResourceUrl string `form:"resource_url"` // 资源URL + IsRemoveBg int64 `form:"is_remove_bg"` // 是否要去掉背景 + Proportion int64 `form:"proportion,default=60"` // 贴图在模型面板上的比例 + SkuId int64 `form:"sku_id,default=0"` // 模板ID +} + type UploadInfo struct { FileSize int64 `form:"file_size,optional"` // 上传唯一标识信息 FileKeys string `form:"file_keys,optional"` // 上传唯一标识信息 diff --git a/server_api/upload.api b/server_api/upload.api index de42df59..5524fead 100644 --- a/server_api/upload.api +++ b/server_api/upload.api @@ -33,8 +33,23 @@ service upload { // 上传文件回调 @handler UploadCallbackHandler post /api/upload/upload-callback(UploadCallbackReq) returns (response); + + // 上传LOGO + @handler UploadLogoHandler + post /api/upload/up-logo(UploadLogoReq) returns (response); + } +type ( + UploadLogoReq { + ResourceId string `form:"resource_id"` // 资源ID + ResourceUrl string `form:"resource_url"` // 资源URL + IsRemoveBg int64 `form:"is_remove_bg"` // 是否要去掉背景 + Proportion int64 `form:"proportion,default=60"` // 贴图在模型面板上的比例 + SkuId int64 `form:"sku_id,default=0"` // 模板ID + } +) + type ( UploadInfo { FileSize int64 `form:"file_size,optional"` // 上传唯一标识信息 diff --git a/utils/basic/basic.go b/utils/basic/basic.go index a2ae1644..8de345cc 100644 --- a/utils/basic/basic.go +++ b/utils/basic/basic.go @@ -77,7 +77,8 @@ var ( CodeAesCbcEncryptionErr = &StatusResponse{5106, "encryption data err"} // 加密数据失败 CodeAesCbcDecryptionErr = &StatusResponse{5107, "decryption data err"} // 解密数据失败 - CodeFileUploadErr = &StatusResponse{5110, "file upload err"} // 文件上传失败 + CodeFileUploadErr = &StatusResponse{5110, "file upload err"} // 文件上传失败 + CodeFileUploadLogoErr = &StatusResponse{5111, "logo upload err"} // 用户上传LOGO失败 ) type Response struct {