diff --git a/cmd/server/wire_gen.go b/cmd/server/wire_gen.go index ab5a6ea..542a4c3 100644 --- a/cmd/server/wire_gen.go +++ b/cmd/server/wire_gen.go @@ -28,10 +28,14 @@ func InitializeApp(configConfig *config.Config, allLogger log.AllLogger) (*serve publishImpl := impl.NewPublishImpl(db) loginRelationImpl := impl.NewLoginRelationImpl(db) publishBiz := biz.NewPublishBiz(configConfig, publishImpl, userImpl, platImpl, tokenImpl, loginRelationImpl) - appService := service.NewAppService(configConfig, tokenImpl, userImpl, platImpl, publishBiz, loginRelationImpl) - loginService := service.NewLoginService(configConfig, publishBiz) - publishService := service.NewPublishService(configConfig, publishBiz, db) - appModule := router.NewAppModule(configConfig, appService, loginService, publishService) + authBiz := biz.NewAuthBiz(configConfig, tokenImpl, userImpl) + appService := service.NewAppService(configConfig, tokenImpl, userImpl, platImpl, publishBiz, authBiz, loginRelationImpl) + loginService := service.NewLoginService(configConfig, publishBiz, authBiz) + publishService := service.NewPublishService(configConfig, publishBiz, authBiz, db) + productImpl := impl.NewProductImpl(db) + productService := service.NewProductService(configConfig, productImpl, authBiz) + productSourceService := service.NewProductSourceService(configConfig, productImpl, authBiz) + appModule := router.NewAppModule(configConfig, appService, loginService, publishService, productService, productSourceService) routerServer := router.NewRouterServer(appModule) app := server.NewHTTPServer(routerServer) servers := server.NewServers(configConfig, app) diff --git a/internal/biz/auth.go b/internal/biz/auth.go index 1bf75ea..425184d 100644 --- a/internal/biz/auth.go +++ b/internal/biz/auth.go @@ -6,6 +6,7 @@ import ( "geo/internal/data/impl" "geo/internal/data/model" "geo/tmpl/errcode" + "xorm.io/builder" ) @@ -62,7 +63,7 @@ func (a *AuthBiz) UserValid(ctx context.Context, userIndex string, tokenId int32 return userInfo, nil } -func (a *AuthBiz) UserAndTokenValid(ctx context.Context, accessToken, userIndex string) (*model.User, *model.Token, error) { +func (a *AuthBiz) UserAndTokenValid(ctx context.Context, userIndex, accessToken string) (*model.User, *model.Token, error) { tokenInfo, err := a.ValidateAccessToken(ctx, accessToken) if err != nil { return nil, nil, err diff --git a/internal/data/impl/artical_type.go b/internal/data/impl/artical_type.go new file mode 100644 index 0000000..0f165cd --- /dev/null +++ b/internal/data/impl/artical_type.go @@ -0,0 +1,27 @@ +package impl + +import ( + "geo/internal/data/model" + "geo/tmpl/dataTemp" + "geo/utils" +) + +type ArticleTypeImpl struct { + dataTemp.DataTemp + db *utils.Db +} + +func NewArticleTypeImpl(db *utils.Db) *ArticleTypeImpl { + return &ArticleTypeImpl{ + DataTemp: *dataTemp.NewDataTemp(db, new(model.ArticleType)), + db: db, + } +} + +func (m *ArticleTypeImpl) PrimaryKey() string { + return "id" +} + +func (m *ArticleTypeImpl) GetTemp() *dataTemp.DataTemp { + return &m.DataTemp +} diff --git a/internal/data/impl/product.go b/internal/data/impl/product.go index ac5f0b2..187a067 100644 --- a/internal/data/impl/product.go +++ b/internal/data/impl/product.go @@ -11,8 +11,8 @@ type ProductImpl struct { db *utils.Db } -func NewProductImpl(db *utils.Db) *UserImpl { - return &UserImpl{ +func NewProductImpl(db *utils.Db) *ProductImpl { + return &ProductImpl{ DataTemp: *dataTemp.NewDataTemp(db, new(model.Product)), db: db, } diff --git a/internal/data/impl/product_source.go b/internal/data/impl/product_source.go new file mode 100644 index 0000000..a678a0f --- /dev/null +++ b/internal/data/impl/product_source.go @@ -0,0 +1,27 @@ +package impl + +import ( + "geo/internal/data/model" + "geo/tmpl/dataTemp" + "geo/utils" +) + +type ProductSourceImpl struct { + dataTemp.DataTemp + db *utils.Db +} + +func NewProductSourceImpl(db *utils.Db) *ProductSourceImpl { + return &ProductSourceImpl{ + DataTemp: *dataTemp.NewDataTemp(db, new(model.ProductSource)), + db: db, + } +} + +func (m *ProductSourceImpl) PrimaryKey() string { + return "id" +} + +func (m *ProductSourceImpl) GetTemp() *dataTemp.DataTemp { + return &m.DataTemp +} diff --git a/internal/data/impl/provider_set.go b/internal/data/impl/provider_set.go index 7935e0e..dfbe556 100644 --- a/internal/data/impl/provider_set.go +++ b/internal/data/impl/provider_set.go @@ -11,4 +11,6 @@ var ProviderImpl = wire.NewSet( NewTokenImpl, NewPublishImpl, NewProductImpl, + NewProductSourceImpl, + NewArticleTypeImpl, ) diff --git a/internal/data/model/artical_type.gen.go b/internal/data/model/artical_type.gen.go new file mode 100644 index 0000000..ce42be5 --- /dev/null +++ b/internal/data/model/artical_type.gen.go @@ -0,0 +1,20 @@ +// Code generated by gorm.io/gen. DO NOT EDIT. +// Code generated by gorm.io/gen. DO NOT EDIT. +// Code generated by gorm.io/gen. DO NOT EDIT. + +package model + +const TableNameArticalType = "article_type" + +// ArticalType mapped from table +type ArticleType struct { + ID int32 `gorm:"column:id;primaryKey" json:"id"` + ArticleName string `gorm:"column:artical_name;not null" json:"artical_name"` + Desc string `gorm:"column:desc;not null" json:"desc"` + Status int32 `gorm:"column:status;not null;default:1" json:"status"` +} + +// TableName ArticalType's table name +func (*ArticleType) TableName() string { + return TableNameArticalType +} diff --git a/internal/data/model/product.gen.go b/internal/data/model/product.gen.go new file mode 100644 index 0000000..db22884 --- /dev/null +++ b/internal/data/model/product.gen.go @@ -0,0 +1,41 @@ +// Code generated by gorm.io/gen. DO NOT EDIT. +// Code generated by gorm.io/gen. DO NOT EDIT. +// Code generated by gorm.io/gen. DO NOT EDIT. + +package model + +import ( + "time" + + "gorm.io/gorm" +) + +const TableNameProduct = "product" + +// Product mapped from table +type Product struct { + ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + UserIndex string `gorm:"column:user_index;not null" json:"user_index"` + Name string `gorm:"column:name;not null" json:"name"` + Industry string `gorm:"column:industry;not null;comment:所属行业" json:"industry"` // 所属行业 + Type string `gorm:"column:type;not null;comment:产品类型" json:"type"` // 产品类型 + ProductOrService string `gorm:"column:product_or_service;comment:主营业务" json:"product_or_service"` // 主营业务 + Advantages string `gorm:"column:advantages;comment:核心优势" json:"advantages"` // 核心优势 + Story string `gorm:"column:story;comment:发展故事" json:"story"` // 发展故事 + Problem string `gorm:"column:problem;comment:解决痛点" json:"problem"` // 解决痛点 + Background string `gorm:"column:background;comment:信任背书" json:"background"` // 信任背书 + Case string `gorm:"column:case;comment:品牌案例" json:"case"` // 品牌案例 + Other string `gorm:"column:other;comment:其他信息" json:"other"` // 其他信息 + ServiceCope string `gorm:"column:service_cope;comment:服务范围" json:"service_cope"` // 服务范围 + TargetAudience string `gorm:"column:target_audience;comment:目标客户群体" json:"target_audience"` // 目标客户群体 + Imgs string `gorm:"column:imgs;comment:图片" json:"imgs"` // 图片 + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` + Status int32 `gorm:"column:status;default:1" json:"status"` +} + +// TableName Product's table name +func (*Product) TableName() string { + return TableNameProduct +} diff --git a/internal/data/model/product.go b/internal/data/model/product.go deleted file mode 100644 index cb6cf20..0000000 --- a/internal/data/model/product.go +++ /dev/null @@ -1,36 +0,0 @@ -// Code generated by gorm.io/gen. DO NOT EDIT. -// Code generated by gorm.io/gen. DO NOT EDIT. -// Code generated by gorm.io/gen. DO NOT EDIT. - -package model - -import ( - "gorm.io/gorm" - "time" -) - -const TableNameProduct = "product" - -type Product struct { - ID int32 `gorm:"column:id;primaryKey;autoIncrement" json:"id"` - UserIndex string `gorm:"column:user_index;not null" json:"user_index"` - Name string `gorm:"column:name;not null;default:''" json:"name"` - Industry string `gorm:"column:industry;not null" json:"industry"` - Type string `gorm:"column:type;not null;default:''" json:"type"` - Advantages string `gorm:"column:advantages;type:text" json:"advantages"` - Problem string `gorm:"column:problem;type:text" json:"problem"` - Background string `gorm:"column:background;type:text" json:"background"` - Case string `gorm:"column:case;type:text" json:"case"` - Other string `gorm:"column:other;type:text" json:"other"` - ServiceCope string `gorm:"column:service_cope;size:100" json:"service_cope"` - TargetAudience string `gorm:"column:target_audience;size:255" json:"target_audience"` - CreateAt *time.Time `gorm:"column:create_at" json:"create_at"` - UpdateAt *time.Time `gorm:"column:update_at" json:"update_at"` - DeleteAt gorm.DeletedAt `gorm:"column:delete_at;index" json:"delete_at"` - Status int8 `gorm:"column:status;default:1" json:"status"` -} - -// TableName LoginRelation's table name -func (*Product) TableName() string { - return TableNameProduct -} diff --git a/internal/data/model/product_source.gen.go b/internal/data/model/product_source.gen.go new file mode 100644 index 0000000..7fb3cd9 --- /dev/null +++ b/internal/data/model/product_source.gen.go @@ -0,0 +1,28 @@ +// Code generated by gorm.io/gen. DO NOT EDIT. +// Code generated by gorm.io/gen. DO NOT EDIT. +// Code generated by gorm.io/gen. DO NOT EDIT. + +package model + +import ( + "time" +) + +const TableNameProductSource = "product_source" + +// ProductSource mapped from table +type ProductSource struct { + ProductSource int64 `gorm:"column:product_source;primaryKey" json:"product_source"` + Title string `gorm:"column:title;not null" json:"title"` + SourceURL string `gorm:"column:source_url;not null" json:"source_url"` + CreateAt time.Time `gorm:"column:create_at" json:"create_at"` + RecommendPlatfrom string `gorm:"column:recommend_platfrom" json:"recommend_platfrom"` + UpdateAt time.Time `gorm:"column:update_at" json:"update_at"` + DeleteAt time.Time `gorm:"column:delete_at" json:"delete_at"` + Status int32 `gorm:"column:status;default:1" json:"status"` +} + +// TableName ProductSource's table name +func (*ProductSource) TableName() string { + return TableNameProductSource +} diff --git a/internal/entitys/request.go b/internal/entitys/request.go index 68a9a82..ac3696b 100644 --- a/internal/entitys/request.go +++ b/internal/entitys/request.go @@ -88,19 +88,20 @@ type ( } CreateProductRequest struct { - AccessToken string `json:"access_token" validate:"required" zh:"access_token"` - UserIndex string `json:"user_index" validate:"required" zh:"用户索引"` - Name string `json:"name" validate:"required" zh:"产品名称"` - Industry string `json:"industry" validate:"required" zh:"所属行业"` - Type string `json:"type" validate:"required" zh:"产品类型"` - Advantages string `json:"advantages" zh:"核心优势"` - Story string `json:"story" zh:"发展故事"` - Problem string `json:"problem" zh:"解决痛点"` - Background string `json:"background" zh:"信任背书"` - Case string `json:"case" zh:"品牌案例"` - Other string `json:"other" zh:"其他信息"` - ServiceCope string `json:"service_cope" zh:"服务范围"` - TargetAudience string `json:"target_audience" zh:"目标客户群体"` + AccessToken string `json:"access_token" validate:"required" zh:"access_token"` + UserIndex string `json:"user_index" validate:"required" zh:"用户索引"` + Name string `json:"name" validate:"required" zh:"产品名称"` + Industry string `json:"industry" validate:"required" zh:"所属行业"` + Type string `json:"type" validate:"required" zh:"产品类型"` + ProductOrService string `json:"product_or_service" validate:"required" zh:"主营业务"` + Advantages string `json:"advantages" zh:"核心优势"` + Story string `json:"story" zh:"发展故事"` + Problem string `json:"problem" zh:"解决痛点"` + Background string `json:"background" zh:"信任背书"` + Case string `json:"case" zh:"品牌案例"` + Other string `json:"other" zh:"其他信息"` + ServiceCope string `json:"service_cope" zh:"服务范围"` + TargetAudience string `json:"target_audience" zh:"目标客户群体"` } ProductListRequest struct { @@ -111,15 +112,25 @@ type ( } ProductUpdateRequest struct { - AccessToken string `json:"access_token" validate:"required" zh:"access_token"` - UserIndex string `json:"user_index" validate:"required" zh:"用户索引"` - PlatIndex string `json:"plat_index" validate:"required" zh:"平台索引"` + AccessToken string `json:"access_token" validate:"required" zh:"access_token"` + Id int32 `json:"id" validate:"required" zh:"id"` + Name string `json:"name" zh:"产品名称"` + Industry string `json:"industry" zh:"所属行业"` + ProductOrService string `json:"product_or_service" zh:"主营业务"` + Type string `json:"type" zh:"产品类型"` + Advantages string `json:"advantages" zh:"核心优势"` + Story string `json:"story" zh:"发展故事"` + Problem string `json:"problem" zh:"解决痛点"` + Background string `json:"background" zh:"信任背书"` + Case string `json:"case" zh:"品牌案例"` + Other string `json:"other" zh:"其他信息"` + ServiceCope string `json:"service_cope" zh:"服务范围"` + TargetAudience string `json:"target_audience" zh:"目标客户群体"` } ProductDelRequest struct { AccessToken string `json:"access_token" validate:"required" zh:"access_token"` - UserIndex string `json:"user_index" validate:"required" zh:"用户索引"` - PlatIndex string `json:"plat_index" validate:"required" zh:"平台索引"` + Id int32 `json:"id" validate:"required" zh:"产品id"` } ProductSourceCreateRequest struct { diff --git a/internal/server/router/app.go b/internal/server/router/app.go index a49d919..dc42f77 100644 --- a/internal/server/router/app.go +++ b/internal/server/router/app.go @@ -4,6 +4,7 @@ import ( "geo/internal/config" "geo/internal/entitys" "geo/internal/service" + "github.com/gofiber/fiber/v2" ) @@ -50,13 +51,13 @@ func (m *AppModule) Register(router fiber.Router) { router.Post("/logout_platform", vali(m.loginService.LogoutPlatform, &entitys.LogoutPlatformRequest{})) router.Get("/logs/:request_id", m.loginService.Log) - router.Get("/product/add", vali(m.productService.Add, &entitys.ProductAddRequest{})) - router.Get("/product/list", vali(m.productService.List, &entitys.ProductListRequest{})) - router.Get("/product/update", vali(m.productService.Update, &entitys.ProductUpdateRequest{})) - router.Get("/product/del", vali(m.productService.Del, &entitys.ProductDelRequest{})) + router.Post("/product/add", vali(m.productService.Add, &entitys.CreateProductRequest{})) + router.Post("/product/list", vali(m.productService.List, &entitys.ProductListRequest{})) + router.Post("/product/update", vali(m.productService.Update, &entitys.ProductUpdateRequest{})) + router.Post("/product/del", vali(m.productService.Del, &entitys.ProductDelRequest{})) - router.Get("/product/word/create", vali(m.productSourceService.Create, &entitys.ProductSourceCreateRequest{})) - router.Get("/product/word/list", vali(m.productSourceService.List, &entitys.ProductSourceListRequest{})) - router.Get("/product/word/upload", vali(m.productSourceService.Upload, &entitys.ProductSourceUploadRequest{})) - router.Get("/product/word/del", vali(m.productSourceService.Del, &entitys.ProductSourceDelRequest{})) + router.Post("/product/word/create", vali(m.productSourceService.Create, &entitys.ProductSourceCreateRequest{})) + router.Post("/product/word/list", vali(m.productSourceService.List, &entitys.ProductSourceListRequest{})) + router.Post("/product/word/upload", vali(m.productSourceService.Upload, &entitys.ProductSourceUploadRequest{})) + router.Post("/product/word/del", vali(m.productSourceService.Del, &entitys.ProductSourceDelRequest{})) } diff --git a/internal/service/product.go b/internal/service/product.go index e4947a4..424ab59 100644 --- a/internal/service/product.go +++ b/internal/service/product.go @@ -8,7 +8,10 @@ import ( "geo/internal/entitys" "geo/pkg" "geo/tmpl/dataTemp" + + "github.com/go-viper/mapstructure/v2" "github.com/gofiber/fiber/v2" + "xorm.io/builder" ) type ProductService struct { @@ -26,24 +29,16 @@ func NewProductService(cfg *config.Config, ProductImpl *impl.ProductImpl, authBi } func (p *ProductService) Add(c *fiber.Ctx, req *entitys.CreateProductRequest) error { - userInfo, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) + _, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) if err != nil { return err } - add := &model.Product{ - UserIndex: userInfo.UserIndex, - Name: req.Name, - Industry: req.Industry, - Type: req.Type, - Advantages: req.Advantages, - Problem: req.Problem, - Background: req.Background, - Case: req.Case, - Other: req.Other, - ServiceCope: req.ServiceCope, - TargetAudience: req.TargetAudience, + var data model.Product + err = mapstructure.Decode(req, &data) + if err != nil { + return err } - err = p.productImpl.Add(c.UserContext(), add) + err = p.productImpl.Add(c.UserContext(), &data) if err != nil { return err } @@ -67,7 +62,10 @@ func (p *ProductService) List(c *fiber.Ctx, req *entitys.ProductListRequest) err pageSize = 100 } var list []*model.Product - total, err := p.productImpl.GetListToStruct(c.UserContext(), nil, &dataTemp.ReqPageBo{Page: page, Limit: pageSize}, list, "") + cond := builder.NewCond(). + And(builder.Eq{"user_index": req.UserIndex}). + And(builder.Eq{"status": 1}) + total, err := p.productImpl.GetListToStruct(c.UserContext(), &cond, &dataTemp.ReqPageBo{Page: page, Limit: pageSize}, &list, "") if err != nil { return err } @@ -76,17 +74,24 @@ func (p *ProductService) List(c *fiber.Ctx, req *entitys.ProductListRequest) err } func (p *ProductService) Update(c *fiber.Ctx, req *entitys.ProductUpdateRequest) error { - userInfo, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) + _, err := p.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken) if err != nil { return err } + var data model.Product + err = mapstructure.Decode(req, &data) + if err != nil { + return err + } + err = p.productImpl.UpdateByKey(c.UserContext(), p.productImpl.PrimaryKey(), req.Id, &data) return nil } func (p *ProductService) Del(c *fiber.Ctx, req *entitys.ProductDelRequest) error { - userInfo, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) + _, err := p.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken) if err != nil { return err } + err = p.productImpl.DeleteByKey(c.UserContext(), p.productImpl.PrimaryKey(), req.Id) return nil } diff --git a/internal/service/product_source.go b/internal/service/product_source.go index 8f2d2a2..5d8f45f 100644 --- a/internal/service/product_source.go +++ b/internal/service/product_source.go @@ -5,6 +5,7 @@ import ( "geo/internal/config" "geo/internal/data/impl" "geo/internal/entitys" + "github.com/gofiber/fiber/v2" ) @@ -24,7 +25,7 @@ func NewProductSourceService(cfg *config.Config, ProductImpl *impl.ProductImpl, func (p *ProductSourceService) Create(c *fiber.Ctx, req *entitys.ProductSourceCreateRequest) error { // 验证token - userInfo, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) + _, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) if err != nil { return err } @@ -34,7 +35,7 @@ func (p *ProductSourceService) Create(c *fiber.Ctx, req *entitys.ProductSourceCr func (p *ProductSourceService) List(c *fiber.Ctx, req *entitys.ProductSourceListRequest) error { // 验证token - userInfo, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) + _, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) if err != nil { return err } @@ -44,7 +45,7 @@ func (p *ProductSourceService) List(c *fiber.Ctx, req *entitys.ProductSourceList func (p *ProductSourceService) Upload(c *fiber.Ctx, req *entitys.ProductSourceUploadRequest) error { // 验证token - userInfo, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) + _, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) if err != nil { return err } @@ -54,7 +55,7 @@ func (p *ProductSourceService) Upload(c *fiber.Ctx, req *entitys.ProductSourceUp func (p *ProductSourceService) Del(c *fiber.Ctx, req *entitys.ProductSourceDelRequest) error { // 验证token - userInfo, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) + _, _, err := p.authBiz.UserAndTokenValid(c.UserContext(), req.UserIndex, req.AccessToken) if err != nil { return err } diff --git a/tmpl/dataTemp/queryTempl.go b/tmpl/dataTemp/queryTempl.go index ced03ca..988952b 100644 --- a/tmpl/dataTemp/queryTempl.go +++ b/tmpl/dataTemp/queryTempl.go @@ -191,9 +191,11 @@ func (k DataTemp) GetListToStruct(ctx context.Context, cond *builder.Cond, pageB if elem.Kind() != reflect.Slice { return nil, fmt.Errorf("result must be a pointer to slice") } - + var query string // 构建基础查询 - query, _ := builder.ToBoundSQL(*cond) + if cond != nil { + query, _ = builder.ToBoundSQL(*cond) + } // 预编译 SQL 以提高性能 // 使用 Table 指定表名,避免 GORM 的反射开销 diff --git a/utils/utils_gorm/gorm.go b/utils/utils_gorm/gorm.go index c241eb5..a9bc042 100644 --- a/utils/utils_gorm/gorm.go +++ b/utils/utils_gorm/gorm.go @@ -4,10 +4,10 @@ import ( "database/sql" "fmt" "geo/internal/config" + "time" + "gorm.io/driver/mysql" "gorm.io/gorm" - "gorm.io/gorm/logger" - "time" ) func DBConn(c *config.DB) (*gorm.DB, func()) { @@ -15,7 +15,7 @@ func DBConn(c *config.DB) (*gorm.DB, func()) { gormDB, err := gorm.Open( mysql.New(mysql.Config{Conn: mysqlConn}), ) - gormDB.Logger = gormDB.Logger.LogMode(logger.Silent) + //gormDB.Logger = gormDB.Logger.LogMode(logger.Silent) if err != nil { panic("failed to connect database") }