167 lines
4.9 KiB
Go
167 lines
4.9 KiB
Go
package logic
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fusenapi/model/gmodel"
|
||
"fusenapi/utils/auth"
|
||
"fusenapi/utils/basic"
|
||
"fusenapi/utils/metadata"
|
||
"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 UserLogoSetLogic struct {
|
||
logx.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
func NewUserLogoSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoSetLogic {
|
||
return &UserLogoSetLogic{
|
||
Logger: logx.WithContext(ctx),
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
// 处理进入前逻辑w,r
|
||
// func (l *UserLogoSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||
// }
|
||
|
||
func (l *UserLogoSetLogic) UserLogoSet(req *types.UserLogoSetReq, 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 userInfo = &gmodel.FsUserInfo{}
|
||
fsUserInfoModel := gmodel.NewFsUserInfoModel(l.svcCtx.MysqlConn)
|
||
BuilderDB := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{}).Where("module = ?", "profile")
|
||
if userId > 0 {
|
||
BuilderDB.Where("user_id=?", userId)
|
||
} else {
|
||
BuilderDB.Where("guest_id=?", guestId)
|
||
}
|
||
userInfo, err := fsUserInfoModel.FindOne(BuilderDB, nil)
|
||
if err != nil {
|
||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
logx.Error(err)
|
||
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
|
||
}
|
||
}
|
||
|
||
var nowTime = time.Now().UTC()
|
||
|
||
var metadataMap map[string]interface{}
|
||
err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
|
||
if userInfo.Id != 0 {
|
||
var metadataMapOld map[string]interface{}
|
||
json.Unmarshal(*userInfo.Metadata, &metadataMapOld)
|
||
}
|
||
// 更新merchant_category
|
||
if req.Module == "merchant_category" {
|
||
logoSelectedId, isEx := metadataMapOld["logo_selected_id"]
|
||
if isEx {
|
||
var materialId int64 = int64(logoSelectedId.(float64))
|
||
|
||
var userMaterialInfo gmodel.FsUserMaterial
|
||
userMaterialGorm := tx.Where("id = ?", materialId)
|
||
resUserMaterialInfo := userMaterialGorm.First(&userMaterialInfo)
|
||
err = resUserMaterialInfo.Error
|
||
if err != nil {
|
||
logc.Errorf(l.ctx, "FsUserMaterial First err:%+v", err)
|
||
return err
|
||
}
|
||
var metadataMapOld map[string]interface{}
|
||
if userMaterialInfo.Id > 0 {
|
||
err = json.Unmarshal(*userMaterialInfo.Metadata, &metadataMapOld)
|
||
if err != nil {
|
||
logc.Errorf(l.ctx, "userMaterialInfo Metadata Unmarshal err:%+v", err)
|
||
return err
|
||
}
|
||
}
|
||
var metadataChild = make(map[string]interface{}, 1)
|
||
metadataChild["merchant_category"] = reqMetadata["category_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
|
||
}
|
||
userMaterialInfo.Metadata = &metadataB
|
||
resUpdates := tx.Select("metadata").Where("id = ?", materialId).Updates(&userMaterialInfo)
|
||
err = resUpdates.Error
|
||
if err != nil {
|
||
if err != gorm.ErrRecordNotFound {
|
||
logc.Errorf(l.ctx, "userMaterialInfo Updates err:%+v", err)
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
var metadataChild = make(map[string]interface{}, 1)
|
||
metadataChild["logo_selected_id"] = reqMetadata["logo_selected_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
|
||
userInfo.Utime = &nowTime
|
||
resUpdates := tx.Model(&userInfo).Select("metadata").Where("id = ?", userInfo.Id).Updates(&userInfo)
|
||
err = resUpdates.Error
|
||
if err != nil {
|
||
if err != gorm.ErrRecordNotFound {
|
||
logc.Errorf(l.ctx, "FsUserInfo Updates err:%+v", err)
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return resp.SetStatus(basic.CodeDbSqlErr, "service fail")
|
||
}
|
||
return resp.SetStatus(basic.CodeOK)
|
||
}
|
||
|
||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||
// func (l *UserLogoSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||
// }
|