fix:上传logo

This commit is contained in:
momo 2023-09-07 12:23:19 +08:00
parent a1f170b1ff
commit 70bda7e02c
2 changed files with 101 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package logic
import (
"encoding/json"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/service/repositories"
@ -8,6 +9,7 @@ import (
"fusenapi/utils/basic"
"fusenapi/utils/file"
"fusenapi/utils/hash"
"fusenapi/utils/metadata"
"io"
"net/http"
"time"
@ -17,7 +19,9 @@ import (
"fusenapi/server/upload/internal/svc"
"fusenapi/server/upload/internal/types"
"github.com/zeromicro/go-zero/core/logc"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type UploadLogoLogic struct {
@ -186,16 +190,69 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us
var module = "logo"
var nowTime = time.Now().UTC()
// 新增记录
userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn)
_, err = userMaterialModel.CreateOrUpdate(l.ctx, &gmodel.FsUserMaterial{
Module: &module,
UserId: &userId,
GuestId: &guestId,
ResourceId: &uploadRes.ResourceId,
ResourceUrl: &uploadRes.ResourceUrl,
Metadata: &resultStrB,
Ctime: &nowTime,
err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
// 新增素材记录
materialInfo := gmodel.FsUserMaterial{
Module: &module,
UserId: &userId,
GuestId: &guestId,
ResourceId: &uploadRes.ResourceId,
ResourceUrl: &uploadRes.ResourceUrl,
Metadata: &resultStrB,
Ctime: &nowTime,
}
resCreate := tx.Create(&materialInfo)
err = resCreate.Error
if err != nil {
logc.Errorf(l.ctx, "FsUserInfo First err%+v", err)
return err
}
// 更新用户信息
userInfoGorm := tx.Where("module = ?", "profile")
userInfo := gmodel.FsUserInfo{}
if userId > 0 {
userInfoGorm.Where("user_id = ?", userId)
userInfo.UserId = &userId
} else {
userInfoGorm.Where("guest_id = ?", guestId)
userInfo.GuestId = &guestId
}
resFirst := userInfoGorm.First(&userInfo)
err = resFirst.Error
if err != nil {
if err != gorm.ErrRecordNotFound {
logc.Errorf(l.ctx, "FsUserInfo First err%+v", err)
return err
}
}
var metadataMap map[string]interface{}
var metadataMapOld map[string]interface{}
if userInfo.Id > 0 {
err = json.Unmarshal(*userInfo.Metadata, &metadataMapOld)
if err != nil {
logc.Errorf(l.ctx, "userInfo.Metadata Unmarshal err%+v", err)
return err
}
}
var metadataChild = make(map[string]interface{}, 1)
metadataChild["logo_selected_id"] = materialInfo.Id
metadataMap, err = metadata.SetMetadata(metadataChild, metadataMapOld)
if err != nil {
logc.Errorf(l.ctx, "metadata SetMetadata err%+v", err)
return err
}
metadataB, err := json.Marshal(metadataMap)
if err != nil {
logc.Errorf(l.ctx, "metadata marshal err%+v", err)
return err
}
userInfo.Metadata = &metadataB
if userInfo.Id > 0 {
tx.Select("metadata").Where("id = ?", userInfo.Id).Updates(userInfo)
} else {
tx.Create(userInfo)
}
return nil
})
if err != nil {

View File

@ -0,0 +1,34 @@
package metadata
func SetMetadata(metadataChild map[string]interface{}, oldMetadata map[string]interface{}) (map[string]interface{}, error) {
if oldMetadata != nil {
for k, v := range metadataChild {
switch val := v.(type) {
case map[string]interface{}:
oldResourceMetadataData := oldMetadata[k].(map[string]interface{})
for key, val1 := range val {
oldResourceMetadataData[key] = val1
}
oldMetadata[k] = oldResourceMetadataData
default:
oldMetadata[k] = val
}
}
return oldMetadata, nil
} else {
var oldMetadata = make(map[string]interface{}, len(metadataChild))
for k, v := range metadataChild {
switch val := v.(type) {
case map[string]interface{}:
var resourceMetadataData = make(map[string]interface{}, len(val))
for key, val1 := range val {
resourceMetadataData[key] = val1
}
oldMetadata[k] = resourceMetadataData
default:
oldMetadata[k] = val
}
}
return oldMetadata, nil
}
}