Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
laodaming 2023-09-07 18:28:27 +08:00
commit 172ec53785
11 changed files with 322 additions and 39 deletions

View File

@ -8,6 +8,10 @@ import (
)
// TODO: 使用model的属性做你想做的
type RelaFsResource struct {
FsResource
MetaDataMap map[string]interface{} `json:"meta_data_map" gorm:"-"`
}
func (p *FsResourceModel) FindOneById(ctx context.Context, resourceId string) (resp *FsResource, err error) {
err = p.db.Table(p.name).WithContext(ctx).Where("resource_id =?", resourceId).Take(&resp).Error

View File

@ -11,6 +11,13 @@ import (
// TODO: 使用model的属性做你想做的
type RelaFsUserMaterial struct {
FsUserMaterial
// 关联字段填充
ResourceInfo *RelaFsResource `json:"resource_info" gorm:"foreignkey:resource_id;references:resource_id"`
MetaDataMap map[string]interface{} `json:"metadata_map" gorm:"-"`
}
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 {
@ -46,6 +53,31 @@ func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB,
}
}
func (m *FsUserMaterialModel) FindList(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string, orderBy string) ([]*RelaFsUserMaterial, error) {
var resp []*RelaFsUserMaterial
// 过滤
if filterMap != nil {
rowBuilder = rowBuilder.Scopes(handlers.FilterData(filterMap))
}
// 排序
if orderBy != "" {
var fieldsMap = make(map[string]struct{})
s := reflect.TypeOf(&FsUserMaterial{}).Elem() //通过反射获取type定义
for i := 0; i < s.NumField(); i++ {
fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{}
}
rowBuilder = rowBuilder.Scopes(handlers.OrderCheck(orderBy, fieldsMap))
}
result := rowBuilder.WithContext(ctx).Find(&resp)
if result.Error != nil {
return nil, result.Error
} else {
return resp, nil
}
}
func (m *FsUserMaterialModel) RowSelectBuilder(selectData []string) *gorm.DB {
var rowBuilder = m.db.Table(m.name)

View File

@ -22,3 +22,15 @@ PayConfig:
EndpointSecret: "whsec_f5f9a121d43af3789db7459352f08cf523eb9e0fbf3381f91ba6c97c324c174d"
SuccessURL: "http://www.baidu.com"
CancelURL: "http://www.baidu.com"
AWS:
S3:
Credentials:
AccessKeyID: AKIAZB2JKUXDPNRP4YT2
Secret: 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
Token:
BLMService:
Url: "http://18.119.109.254:8999"
# Url: "http://192.168.1.9:8999"
LogoCombine:
#Url: "http://192.168.1.7:8999/LogoCombine"
Url: "http://18.119.109.254:8999/LogoCombine"

View File

@ -26,4 +26,19 @@ type Config struct {
SuccessURL string
}
}
AWS struct {
S3 struct {
Credentials struct {
AccessKeyID string
Secret string
Token string
}
}
}
BLMService struct {
Url string
LogoCombine struct {
Url string
}
}
}

View File

@ -1,9 +1,12 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/metadata"
"fusenapi/utils/validate"
"time"
@ -12,7 +15,9 @@ import (
"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 UserInfoSetLogic struct {
@ -68,17 +73,112 @@ func (l *UserInfoSetLogic) UserInfoSet(req *types.UserInfoSetReq, userinfo *auth
var userInfo = &gmodel.FsUserInfo{}
fsUserInfoModel := gmodel.NewFsUserInfoModel(l.svcCtx.MysqlConn)
BuilderDB := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{})
BuilderDB1 := BuilderDB.Where("module = ?", req.Module).Where("user_id=?", userId).Where("guest_id=?", guestId)
BuilderDB1 := fsUserInfoModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsUserInfo{})
if req.Module == "merchant_category" {
BuilderDB1.Where("module = ?", "profile")
} else {
BuilderDB1.Where("module = ?", req.Module)
}
//.Where("user_id=?", userId).Where("guest_id=?", guestId)
if userId > 0 {
BuilderDB1.Where("user_id=?", userId)
} else {
BuilderDB1.Where("guest_id=?", guestId)
}
userInfo, err = fsUserInfoModel.FindOne(BuilderDB1, nil)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
if errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed")
}
}
var nowTime = time.Now().UTC()
if userInfo.Id != 0 {
v := []byte(req.Metadata)
userInfo.Metadata = &v
userInfo.Utime = &nowTime
var reqMetadata map[string]interface{}
json.Unmarshal([]byte(req.Metadata), &reqMetadata)
var metadataMapOld map[string]interface{}
json.Unmarshal(*userInfo.Metadata, &metadataMapOld)
var metadataMap map[string]interface{}
err = l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error {
// 更新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)
} else {
userInfo.GuestId = &guestId
userInfo.UserId = &userId

View File

@ -1,6 +1,7 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
@ -55,9 +56,15 @@ func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *a
}
userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn)
userMaterialRSB := userMaterialModel.RowSelectBuilder(nil).
Where("module = ?", "logo").Where("user_id = ?", userId).Where("guest_id = ?", guestId).Order("id desc")
list, err := userMaterialModel.FindAll(l.ctx, userMaterialRSB, nil, "")
userMaterialRSB := userMaterialModel.RowSelectBuilder(nil).Preload("ResourceInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(gmodel.NewFsResourceModel(l.svcCtx.MysqlConn).TableName())
}).Where("module = ?", "logo").Order("id desc").Limit(10)
if userId != 0 {
userMaterialRSB.Where("user_id = ?", userId)
} else {
userMaterialRSB.Where("guest_id = ?", guestId)
}
list, err := userMaterialModel.FindList(l.ctx, userMaterialRSB, nil, "")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "data not found")
@ -65,6 +72,58 @@ func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *a
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get data list")
}
if len(list) > 0 {
var merchantCategoryIds []int64
for _, v := range list {
var metadataMap map[string]interface{}
if v.Metadata != nil {
json.Unmarshal(*v.Metadata, &metadataMap)
merchantCategory, merchantCategoryEix := metadataMap["merchant_category"]
if merchantCategoryEix {
merchantCategoryId := int64(merchantCategory.(float64))
merchantCategoryIds = append(merchantCategoryIds, merchantCategoryId)
}
metadataMap["merchant_category_info"] = nil
v.MetaDataMap = metadataMap
}
if v.ResourceInfo != nil {
if v.ResourceInfo.Metadata != nil {
var resourceMetadata map[string]interface{}
json.Unmarshal([]byte(*v.ResourceInfo.Metadata), &resourceMetadata)
v.ResourceInfo.MetaDataMap = resourceMetadata
}
}
}
if len(merchantCategoryIds) > 0 {
newFsMerchantCategoryModel := gmodel.NewFsMerchantCategoryModel(l.svcCtx.MysqlConn)
fsMerchantCategoryDb := newFsMerchantCategoryModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsMerchantCategory{}).Where("id in ?", merchantCategoryIds)
resfsMerchantCategoryDb, err := newFsMerchantCategoryModel.FindAll(fsMerchantCategoryDb, nil, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "newFsMerchantCategoryModel FindAll db")
}
merchantCategoryDataLen := len(resfsMerchantCategoryDb)
var merchantCategoryData = make(map[int64]*gmodel.FsMerchantCategory, merchantCategoryDataLen)
if merchantCategoryDataLen > 0 {
for _, v := range resfsMerchantCategoryDb {
merchantCategoryData[v.Id] = v
}
}
for _, v := range list {
if v.MetaDataMap != nil {
merchantCategory1, merchantCategoryEix1 := v.MetaDataMap["merchant_category"]
if merchantCategoryEix1 {
merchantCategoryId1 := int64(merchantCategory1.(float64))
v.MetaDataMap["merchant_category_info"] = merchantCategoryData[merchantCategoryId1]
} else {
v.MetaDataMap["merchant_category_info"] = nil
}
}
}
}
}
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"list": list,
})

View File

@ -5,6 +5,9 @@ import (
"fusenapi/model/gmodel"
"fusenapi/shared"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
@ -18,19 +21,27 @@ type ServiceContext struct {
Config config.Config
SharedState *shared.SharedState
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen
Repositories *initalize.Repositories
}
func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql)
// StateServer := shared.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn)
config := aws.Config{
Credentials: credentials.NewStaticCredentials(c.AWS.S3.Credentials.AccessKeyID, c.AWS.S3.Credentials.Secret, c.AWS.S3.Credentials.Token),
}
return &ServiceContext{
Config: c,
MysqlConn: conn,
SharedState: nil,
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{
GormDB: initalize.InitMysql(c.SourceMysql),
BLMServiceUrl: &c.BLMService.Url,
AwsSession: session.Must(session.NewSession(&config)),
}),
}
}

View File

@ -6,8 +6,8 @@ import (
)
type UserInfoSetReq struct {
Module string `form:"module,options=[merchant_category,logo_merchant_category]"` // json格式字符串
Metadata string `form:"metadata"` // json格式字符串
Module string `form:"module,options=[merchant_category,logo_merchant_category,profile]"` // json格式字符串
Metadata string `form:"metadata"` // json格式字符串
}
type UserAgainOrderReq struct {

View File

@ -55,7 +55,9 @@ func (l *ResourceInfoLogic) ResourceInfo(req *types.ResourceInfoReq, userinfo *a
if err == nil && resourceInfo.ResourceId != "" {
resourceId = resourceInfo.ResourceId
resourceUrl = *resourceInfo.ResourceUrl
json.Unmarshal([]byte(*resourceInfo.Metadata), &resourceMetadata)
if resourceInfo.Metadata != nil {
json.Unmarshal([]byte(*resourceInfo.Metadata), &resourceMetadata)
}
}
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"resource_id": resourceId,

View File

@ -10,7 +10,7 @@ info (
import "basic.api"
service home-user-auth {
// @handler UserRegisterHandler
// post /api/user/register(RequestUserRegister) returns (response);
//用户字体
@ -37,33 +37,33 @@ service home-user-auth {
//联系服务
@handler UserContactServiceHandler
post /api/user/contact-service (RequestContactService) returns (response);
// @handler UserOderListHandler
// get /api/user/order-list(RequestOrderId) returns (response);
//删除订单
@handler UserOderDeleteHandler
post /api/user/order-delete(RequestOrderId) returns (response);
//订单列表
@handler UserOrderListHandler
get /api/user/order-list (UserOrderListReq) returns (response);
//删除订单
@handler UserOrderDeleteHandler
get /api/user/order-delete (UserOrderDeleteReq) returns (response);
//取消订单
@handler UserOrderCancelHandler
get /api/user/order-cancel (UserOrderCancelReq) returns (response);
// 用户logo列表
@handler UserLogoListHandler
get /api/user/logo-list (UserLogoListReq) returns (response);
// 再来一单
@handler UserAgainOrderHandler
get /api/user/one-more-order (UserAgainOrderReq) returns (response);
// 保存商户信息
@handler UserInfoSetHandler
post /api/user/set_user_info (UserInfoSetReq) returns (response);
@ -71,8 +71,8 @@ service home-user-auth {
type (
UserInfoSetReq {
Module string `form:"module,options=[merchant_category,logo_merchant_category]"` // json格式字符串
Metadata string `form:"metadata"` // json格式字符串
Module string `form:"module,options=[merchant_category,logo_merchant_category,profile]"` // json格式字符串
Metadata string `form:"metadata"` // json格式字符串
}
)

View File

@ -63,23 +63,71 @@ func (l *defaultImageHandle) LogoInfo(ctx context.Context, in *LogoInfoReq) (*Lo
var metadata *string
var logoUrl *string
userMaterialModel := gmodel.NewFsUserMaterialModel(l.MysqlConn)
userMaterialInfo, err := userMaterialModel.FindLatestOne(ctx, in.UserId, in.GuestId)
// 更新用户信息
var module = "profile"
userInfoGorm := l.MysqlConn.Where("module = ?", module)
userInfo := gmodel.FsUserInfo{}
if in.UserId > 0 {
userInfoGorm.Where("user_id = ?", in.UserId)
userInfo.UserId = &in.UserId
} else {
userInfoGorm.Where("guest_id = ?", in.GuestId)
userInfo.GuestId = &in.GuestId
}
resFirst := userInfoGorm.First(&userInfo)
err := resFirst.Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
userMaterialInfoDefault, err := userMaterialModel.FindOneById(ctx, 0)
if err != nil {
logx.Error(err)
return nil, err
}
metadataB := string(*userMaterialInfoDefault.Metadata)
metadata = &metadataB
logoUrl = userMaterialInfoDefault.ResourceUrl
} else {
logx.Error(err)
if err != gorm.ErrRecordNotFound {
logc.Errorf(ctx, "FsUserInfo First err%+v", err)
return nil, err
}
}
var userMaterialInfo gmodel.FsUserMaterial
userMaterialModel := gmodel.NewFsUserMaterialModel(l.MysqlConn)
if userInfo.Id == 0 {
userMaterialInfo, err = userMaterialModel.FindLatestOne(ctx, in.UserId, in.GuestId)
if err != gorm.ErrRecordNotFound {
logc.Errorf(ctx, "FsUserMaterial FindLatestOne err%+v", err)
return nil, err
}
} else {
if userInfo.Metadata != nil {
var metadata map[string]interface{}
err = json.Unmarshal([]byte(*userInfo.Metadata), &metadata)
if err != nil {
logc.Errorf(ctx, "userInfo.Metadata Unmarshal err%+v", err)
return nil, err
}
logoSelectedId, isEx := metadata["logo_selected_id"]
if isEx {
var materialId int64 = int64(logoSelectedId.(float64))
userMaterialInfos, err := userMaterialModel.FindOneById(ctx, materialId)
if err != nil {
if err != gorm.ErrRecordNotFound {
logc.Errorf(ctx, "FsUserInfo First err%+v", err)
return nil, err
}
}
if userMaterialInfos.Id != 0 {
userMaterialInfo = *userMaterialInfos
}
}
}
}
if userMaterialInfo.Id == 0 {
userMaterialInfoDefault, err := userMaterialModel.FindOneById(ctx, 0)
if err != nil {
if err != gorm.ErrRecordNotFound {
logc.Errorf(ctx, "userMaterialModel FindOneById defaul err%+v", err)
return nil, err
}
}
metadataB := string(*userMaterialInfoDefault.Metadata)
metadata = &metadataB
logoUrl = userMaterialInfoDefault.ResourceUrl
} else {
metadataB := string(*userMaterialInfo.Metadata)
metadata = &metadataB