package logic import ( "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) } var userId int64 var guestId int64 // 检查用户是否是游客 if userinfo.IsGuest() { // 如果是,使用游客ID和游客键名格式 guestId = userinfo.GuestId } else { // 否则,使用用户ID和用户键名格式 userId = userinfo.UserId } 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 var err error // 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 reqs types.UploadFileBaseReq // reqs.FileType = // 创建一个业务逻辑层实例 NewUploadFileBaseLogic(l.ctx, l.svcCtx).UploadFileBase(&reqs, userinfo) var module = "logo" var nowTime = time.Now().Unix() // 新增记录 userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) _, err = userMaterialModel.CreateOrUpdate(l.ctx, &gmodel.FsUserMaterial{ Module: &module, UserId: &userId, GuestId: &guestId, 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) }