diff --git a/model/gmodel/fs_merchant_category_gen.go b/model/gmodel/fs_merchant_category_gen.go new file mode 100644 index 00000000..37944dce --- /dev/null +++ b/model/gmodel/fs_merchant_category_gen.go @@ -0,0 +1,25 @@ +package gmodel + +import ( + "gorm.io/gorm" +) + +// fs_merchant_category 商户类型表 +type FsMerchantCategory struct { + Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // Id + ZnName *string `gorm:"default:'';" json:"zn_name"` // 中文名 + EnName *string `gorm:"default:'';" json:"en_name"` // 英文名 + Icon *string `gorm:"default:'';" json:"icon"` // 图标 + RecommendProduct *string `gorm:"default:'';" json:"recommend_product"` // 推荐商品 + Sort *int64 `gorm:"default:128;" json:"sort"` // 排序 + Status *int64 `gorm:"default:0;" json:"status"` // 状态 + Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 +} +type FsMerchantCategoryModel struct { + db *gorm.DB + name string +} + +func NewFsMerchantCategoryModel(db *gorm.DB) *FsMerchantCategoryModel { + return &FsMerchantCategoryModel{db: db, name: "fs_merchant_category"} +} diff --git a/model/gmodel/fs_merchant_category_logic.go b/model/gmodel/fs_merchant_category_logic.go new file mode 100644 index 00000000..bb6ef130 --- /dev/null +++ b/model/gmodel/fs_merchant_category_logic.go @@ -0,0 +1,45 @@ +package gmodel + +import ( + "context" + "fusenapi/utils/handlers" + "reflect" + + "gorm.io/gorm" +) + +// TODO: 使用model的属性做你想做的 + +func (m *FsMerchantCategoryModel) BuilderDB(ctx context.Context, selectData []string) *gorm.DB { + if selectData != nil { + return m.db.WithContext(ctx).Select(selectData) + } else { + return m.db.WithContext(ctx).Select("*") + } +} + +func (m *FsMerchantCategoryModel) FindAll(gormDB *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsMerchantCategory, error) { + var resp []*FsMerchantCategory + + // 过滤 + if filterMap != nil { + gormDB = gormDB.Scopes(handlers.FilterData(filterMap)) + } + + // 排序 + if orderBy != "" { + var fieldsMap = make(map[string]struct{}) + s := reflect.TypeOf(&FsOrder{}).Elem() //通过反射获取type定义 + for i := 0; i < s.NumField(); i++ { + fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{} + } + gormDB = gormDB.Scopes(handlers.OrderCheck(orderBy, fieldsMap)) + } + + result := gormDB.Find(&resp) + if result.Error != nil { + return nil, result.Error + } else { + return resp, nil + } +} diff --git a/model/gmodel/fs_product_template_v2_gen.go b/model/gmodel/fs_product_template_v2_gen.go index 5e14427f..07ad37fc 100644 --- a/model/gmodel/fs_product_template_v2_gen.go +++ b/model/gmodel/fs_product_template_v2_gen.go @@ -9,20 +9,21 @@ type FsProductTemplateV2 struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ProductId *int64 `gorm:"index;default:0;" json:"product_id"` // 产品ID ModelId *int64 `gorm:"default:0;" json:"model_id"` // 模型ID - Title *string `gorm:"default:'';" json:"title"` // 模板(sku),预留字段 - Name *string `gorm:"default:'';" json:"name"` // 名称 - CoverImg *string `gorm:"default:'';" json:"cover_img"` // 模板背景图 - TemplateInfo *string `gorm:"default:'';" json:"template_info"` // 模板详情 - MaterialImg *string `gorm:"default:'';" json:"material_img"` // 合成好的贴图 + Title *string `gorm:"default:'';" json:"title"` // + Name *string `gorm:"default:'';" json:"name"` // + CoverImg *string `gorm:"default:'';" json:"cover_img"` // + TemplateInfo *string `gorm:"default:'';" json:"template_info"` // + MaterialImg *string `gorm:"default:'';" json:"material_img"` // Sort *int64 `gorm:"default:0;" json:"sort"` // 排序 LogoWidth *int64 `gorm:"default:0;" json:"logo_width"` // logo图最大宽度 LogoHeight *int64 `gorm:"default:0;" json:"logo_height"` // logo图最大高度 IsPublic *int64 `gorm:"default:0;" json:"is_public"` // 是否可公用(1:可以,0:不可以) Status *int64 `gorm:"default:0;" json:"status"` // 状态1正常 2异常 Ctime *int64 `gorm:"default:0;" json:"ctime"` // 添加时间 - Tag *string `gorm:"default:'';" json:"tag"` // 标签(用户自填) + Tag *string `gorm:"default:'';" json:"tag"` // IsDel *int64 `gorm:"default:0;" json:"is_del"` // 是否删除 1删除 - GroupOptions *string `gorm:"default:'';" json:"group_options"` // 颜色分组 + SwitchInfo *string `gorm:"default:'';" json:"switch_info"` // + GroupOptions *string `gorm:"default:'';" json:"group_options"` // Version *int64 `gorm:"default:0;" json:"version"` // } type FsProductTemplateV2Model struct { diff --git a/model/gmodel/fs_user_info_gen.go b/model/gmodel/fs_user_info_gen.go new file mode 100644 index 00000000..71b1bd7a --- /dev/null +++ b/model/gmodel/fs_user_info_gen.go @@ -0,0 +1,25 @@ +package gmodel + +import ( + "gorm.io/gorm" +) + +// fs_user_info 用户信息表 +type FsUserInfo struct { + Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID + Module *string `gorm:"default:'';" json:"module"` // 所属模块 + UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID + GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID + Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式 + MetaKey1 *string `gorm:"default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加 + Ctime *int64 `gorm:"default:0;" json:"ctime"` // + Utime *int64 `gorm:"default:0;" json:"utime"` // +} +type FsUserInfoModel struct { + db *gorm.DB + name string +} + +func NewFsUserInfoModel(db *gorm.DB) *FsUserInfoModel { + return &FsUserInfoModel{db: db, name: "fs_user_info"} +} diff --git a/model/gmodel/fs_user_info_logic.go b/model/gmodel/fs_user_info_logic.go new file mode 100644 index 00000000..3ca2b8d0 --- /dev/null +++ b/model/gmodel/fs_user_info_logic.go @@ -0,0 +1,42 @@ +package gmodel + +import ( + "context" + "fusenapi/utils/handlers" + + "gorm.io/gorm" +) + +// TODO: 使用model的属性做你想做的 + +func (m *FsUserInfoModel) BuilderDB(ctx context.Context, selectData []string) *gorm.DB { + if selectData != nil { + return m.db.WithContext(ctx).Select(selectData) + } else { + return m.db.WithContext(ctx).Select("*") + } +} + +func (m *FsUserInfoModel) FindOne(gormDB *gorm.DB, filterMap map[string]string) (*FsUserInfo, error) { + var resp FsUserInfo + + if filterMap != nil { + gormDB = gormDB.Scopes(handlers.FilterData(filterMap)) + } + + result := gormDB.Limit(1).Find(&resp) + if result.Error != nil { + return nil, result.Error + } else { + return &resp, nil + } +} + +func (p *FsUserInfoModel) CreateOrUpdate(gormDB *gorm.DB, req *FsUserInfo) (resp *FsUserInfo, err error) { + if req.Id > 0 { + err = gormDB.Save(req).Error + } else { + err = gormDB.Create(req).Error + } + return req, err +} diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index 46818f31..d8c7fa82 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -45,6 +45,7 @@ type AllModelsGen struct { FsLog *FsLogModel // fs_log 日志表 FsMapLibrary *FsMapLibraryModel // fs_map_library 贴图库 FsMenu *FsMenuModel // fs_menu 后台菜单 + FsMerchantCategory *FsMerchantCategoryModel // fs_merchant_category 商户类型表 FsMigration *FsMigrationModel // fs_migration 版本库 FsOrder *FsOrderModel // fs_order FsOrderAffiliate *FsOrderAffiliateModel // fs_order_affiliate 订单附属表-流程控制时间等 @@ -91,6 +92,7 @@ type AllModelsGen struct { FsTrade *FsTradeModel // fs_trade FsUser *FsUserModel // fs_user 用户表 FsUserDesign *FsUserDesignModel // fs_user_design 废弃表 + FsUserInfo *FsUserInfoModel // fs_user_info 用户信息表 FsUserMaterial *FsUserMaterialModel // fs_user_material 用户素材表 FsUserStock *FsUserStockModel // fs_user_stock 用户云仓库存 FsWebSet *FsWebSetModel // fs_web_set 网站配置表 @@ -140,6 +142,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsLog: NewFsLogModel(gdb), FsMapLibrary: NewFsMapLibraryModel(gdb), FsMenu: NewFsMenuModel(gdb), + FsMerchantCategory: NewFsMerchantCategoryModel(gdb), FsMigration: NewFsMigrationModel(gdb), FsOrder: NewFsOrderModel(gdb), FsOrderAffiliate: NewFsOrderAffiliateModel(gdb), @@ -186,6 +189,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsTrade: NewFsTradeModel(gdb), FsUser: NewFsUserModel(gdb), FsUserDesign: NewFsUserDesignModel(gdb), + FsUserInfo: NewFsUserInfoModel(gdb), FsUserMaterial: NewFsUserMaterialModel(gdb), FsUserStock: NewFsUserStockModel(gdb), FsWebSet: NewFsWebSetModel(gdb), diff --git a/server/base/base.go b/server/base/base.go new file mode 100644 index 00000000..df2962f3 --- /dev/null +++ b/server/base/base.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "time" + + "fusenapi/utils/auth" + + "fusenapi/server/base/internal/config" + "fusenapi/server/base/internal/handler" + "fusenapi/server/base/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/base.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + c.Timeout = int64(time.Second * 15) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + })) + defer server.Stop() + + ctx := svc.NewServiceContext(c) + handler.RegisterHandlers(server, ctx) + + fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + server.Start() +} diff --git a/server/base/base_test.go b/server/base/base_test.go new file mode 100644 index 00000000..80820ef2 --- /dev/null +++ b/server/base/base_test.go @@ -0,0 +1,9 @@ +package main + +import ( + "testing" +) + +func TestMain(t *testing.T) { + main() +} diff --git a/server/base/etc/base.yaml b/server/base/etc/base.yaml new file mode 100644 index 00000000..e201a85b --- /dev/null +++ b/server/base/etc/base.yaml @@ -0,0 +1,10 @@ +Name: base +Host: 0.0.0.0 +Port: 9920 +Timeout: 15000 #服务超时时间(毫秒) +SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest +Auth: + AccessSecret: fusen2023 + AccessExpire: 2592000 + RefreshAfter: 1592000 +SourceRabbitMq: amqp://rabbit001:rabbit001129@110.41.19.98:5672 \ No newline at end of file diff --git a/server/base/internal/config/config.go b/server/base/internal/config/config.go new file mode 100644 index 00000000..be42afd5 --- /dev/null +++ b/server/base/internal/config/config.go @@ -0,0 +1,14 @@ +package config + +import ( + "fusenapi/server/base/internal/types" + + "github.com/zeromicro/go-zero/rest" +) + +type Config struct { + rest.RestConf + SourceMysql string + Auth types.Auth + SourceRabbitMq string +} diff --git a/server/base/internal/handler/merchantcategorylisthandler.go b/server/base/internal/handler/merchantcategorylisthandler.go new file mode 100644 index 00000000..c7b4484a --- /dev/null +++ b/server/base/internal/handler/merchantcategorylisthandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/base/internal/logic" + "fusenapi/server/base/internal/svc" + "fusenapi/server/base/internal/types" +) + +func MerchantCategoryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.MerchantCategoryListReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewMerchantCategoryListLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.MerchantCategoryList(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/base/internal/handler/routes.go b/server/base/internal/handler/routes.go new file mode 100644 index 00000000..4f4764c0 --- /dev/null +++ b/server/base/internal/handler/routes.go @@ -0,0 +1,22 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/server/base/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodGet, + Path: "/api/base/merchant_category_list", + Handler: MerchantCategoryListHandler(serverCtx), + }, + }, + ) +} diff --git a/server/base/internal/logic/merchantcategorylistlogic.go b/server/base/internal/logic/merchantcategorylistlogic.go new file mode 100644 index 00000000..eb9dd3ae --- /dev/null +++ b/server/base/internal/logic/merchantcategorylistlogic.go @@ -0,0 +1,53 @@ +package logic + +import ( + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/base/internal/svc" + "fusenapi/server/base/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type MerchantCategoryListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewMerchantCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MerchantCategoryListLogic { + return &MerchantCategoryListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *MerchantCategoryListLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *MerchantCategoryListLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *MerchantCategoryListLogic) MerchantCategoryList(req *types.MerchantCategoryListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + fsMerchantCategoryModel := gmodel.NewFsMerchantCategoryModel(l.svcCtx.MysqlConn) + BuilderDB := fsMerchantCategoryModel.BuilderDB(l.ctx, nil).Model(&gmodel.FsMerchantCategory{}) + resourceInfo, err := fsMerchantCategoryModel.FindAll(BuilderDB, nil, "sort desc") + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "MerchantCategoryList error system failed") + } + // 返回成功 + return resp.SetStatus(basic.CodeOK, map[string]interface{}{ + "list": resourceInfo, + }) +} diff --git a/server/base/internal/svc/servicecontext.go b/server/base/internal/svc/servicecontext.go new file mode 100644 index 00000000..335cab29 --- /dev/null +++ b/server/base/internal/svc/servicecontext.go @@ -0,0 +1,62 @@ +package svc + +import ( + "errors" + "fmt" + "fusenapi/server/base/internal/config" + "net/http" + + "fusenapi/initalize" + "fusenapi/model/gmodel" + + "github.com/golang-jwt/jwt" + "gorm.io/gorm" +) + +type ServiceContext struct { + Config config.Config + + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + MysqlConn: initalize.InitMysql(c.SourceMysql), + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + } +} + +func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { + AuthKey := r.Header.Get("Authorization") + if AuthKey == "" { + return nil, nil + } + AuthKey = AuthKey[7:] + + if len(AuthKey) <= 50 { + return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) + } + + token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) { + // 检查签名方法是否为 HS256 + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) + } + // 返回用于验证签名的密钥 + return []byte(svcCtx.Config.Auth.AccessSecret), nil + }) + if err != nil { + return nil, errors.New(fmt.Sprint("Error parsing token:", err)) + } + + // 验证成功返回 + if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { + return claims, nil + } + + return nil, errors.New(fmt.Sprint("Invalid token", err)) +} diff --git a/server/base/internal/types/types.go b/server/base/internal/types/types.go new file mode 100644 index 00000000..afa558e9 --- /dev/null +++ b/server/base/internal/types/types.go @@ -0,0 +1,78 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +import ( + "fusenapi/utils/basic" +) + +type MerchantCategoryListReq struct { +} + +type Request struct { +} + +type Response struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` +} + +type Auth struct { + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` + RefreshAfter int64 `json:"refreshAfter"` +} + +type File struct { + Filename string `fsfile:"filename"` + Header map[string][]string `fsfile:"header"` + Size int64 `fsfile:"size"` + Data []byte `fsfile:"data"` +} + +type Meta struct { + TotalCount int64 `json:"totalCount"` + PageCount int64 `json:"pageCount"` + CurrentPage int `json:"currentPage"` + PerPage int `json:"perPage"` +} + +// Set 设置Response的Code和Message值 +func (resp *Response) Set(Code int, Message string) *Response { + return &Response{ + Code: Code, + Message: Message, + } +} + +// Set 设置整个Response +func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { + return &Response{ + Code: Code, + Message: Message, + Data: Data, + } +} + +// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} + +// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + Message: msg, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 607e6278..c6957627 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -102,6 +102,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/one-more-order", Handler: UserAgainOrderHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/user/set_user_info", + Handler: UserInfoSetHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/userinfosethandler.go b/server/home-user-auth/internal/handler/userinfosethandler.go new file mode 100644 index 00000000..8593b501 --- /dev/null +++ b/server/home-user-auth/internal/handler/userinfosethandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/home-user-auth/internal/logic" + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" +) + +func UserInfoSetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserInfoSetReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserInfoSetLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserInfoSet(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/home-user-auth/internal/logic/userinfosetlogic.go b/server/home-user-auth/internal/logic/userinfosetlogic.go new file mode 100644 index 00000000..c27836b8 --- /dev/null +++ b/server/home-user-auth/internal/logic/userinfosetlogic.go @@ -0,0 +1,95 @@ +package logic + +import ( + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/validate" + "time" + + "context" + + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type UserInfoSetLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserInfoSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoSetLogic { + return &UserInfoSetLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserInfoSetLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserInfoSetLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *UserInfoSetLogic) UserInfoSet(req *types.UserInfoSetReq, 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 + } + + _, err := validate.Validate(&req.Module, &req.Metadata) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeRequestParamsErr, "UserInfoSet error Validate failed") + } + + // 根据模块类型检查数据 + 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) + userInfo, err = fsUserInfoModel.FindOne(BuilderDB1, nil) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed") + } + var nowTime = time.Now().Unix() + if userInfo.Id != 0 { + userInfo.Metadata = &req.Metadata + userInfo.Utime = &nowTime + } else { + userInfo.GuestId = &guestId + userInfo.UserId = &userId + userInfo.Module = &req.Module + userInfo.Metadata = &req.Metadata + userInfo.Ctime = &nowTime + } + _, err = fsUserInfoModel.CreateOrUpdate(BuilderDB, userInfo) + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeDbSqlErr, "UserInfoSet error system failed") + } + + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index b183ef86..6bbf9f49 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,11 @@ import ( "fusenapi/utils/basic" ) +type UserInfoSetReq struct { + Module string `form:"module,options=[merchant_category]"` // json格式字符串 + Metadata string `form:"metadata"` // json格式字符串 +} + type UserAgainOrderReq struct { Sn string `form:"sn"` // 订单编号 } diff --git a/server_api/base.api b/server_api/base.api new file mode 100644 index 00000000..3a174324 --- /dev/null +++ b/server_api/base.api @@ -0,0 +1,20 @@ +syntax = "v1" + +info ( + title: // TODO: add title + desc: // TODO: add description + author: "" + email: "" +) + +import "basic.api" + +service base { + @handler MerchantCategoryListHandler + get /api/base/merchant_category_list(MerchantCategoryListReq) returns (response); +} + +type ( + MerchantCategoryListReq { + } +) \ No newline at end of file diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index 3b686163..62ae1f35 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -75,8 +75,19 @@ service home-user-auth { // 再来一单 @handler UserAgainOrderHandler get /api/user/one-more-order (UserAgainOrderReq) returns (response); + + // 保存商户信息 + @handler UserInfoSetHandler + post /api/user/set_user_info (UserInfoSetReq) returns (response); } +type ( + UserInfoSetReq { + Module string `form:"module,options=[merchant_category]"` // json格式字符串 + Metadata string `form:"metadata"` // json格式字符串 + } +) + type ( UserAgainOrderReq { Sn string `form:"sn"` // 订单编号 diff --git a/utils/validate/user_info.go b/utils/validate/user_info.go new file mode 100644 index 00000000..9e0342ec --- /dev/null +++ b/utils/validate/user_info.go @@ -0,0 +1,26 @@ +package validate + +import ( + "encoding/json" + "errors" +) + +type MerchantCategory struct { + CategoryId int64 `json:"category_id"` +} + +func Validate(module *string, metadata *string) (interface{}, error) { + if *module == "merchant_category" { + var merchantCategory MerchantCategory + err := json.Unmarshal([]byte(*metadata), &merchantCategory) + if err != nil { + return nil, err + } else { + if merchantCategory.CategoryId == 0 { + return nil, errors.New("merchant_category.category_id is required") + } + return merchantCategory, nil + } + } + return nil, nil +}