diff --git a/app/constants/common/common.go b/app/constants/common/common.go index a3a296b..86f5b5c 100644 --- a/app/constants/common/common.go +++ b/app/constants/common/common.go @@ -4,4 +4,6 @@ const ( TOKEN_PRE = "player_token_" TOKEN_Admin = "Admin_token_" ADMIN_V1 = "/admin/api/v1" + + FRONT_API_V1 = "/api/v1" ) diff --git a/app/http/controllers/base.go b/app/http/controllers/base.go index 887be73..6ea947d 100644 --- a/app/http/controllers/base.go +++ b/app/http/controllers/base.go @@ -103,7 +103,7 @@ func GenRequest(c *gin.Context, request interface{}) (msgs []string, err error) if c.Request.Method == "GET" || c.Request.Method == "DELETE" { err = c.ShouldBindQuery(request) } else { - err = c.ShouldBindJSON(request) + err = c.ShouldBind(request) } if err == nil { diff --git a/app/http/controllers/front/UserController.go b/app/http/controllers/front/UserController.go new file mode 100644 index 0000000..9bad7d0 --- /dev/null +++ b/app/http/controllers/front/UserController.go @@ -0,0 +1 @@ +package front diff --git a/app/http/controllers/front/VoucherController.go b/app/http/controllers/front/VoucherController.go new file mode 100644 index 0000000..b1d78ab --- /dev/null +++ b/app/http/controllers/front/VoucherController.go @@ -0,0 +1,12 @@ +package front + +import ( + "github.com/gin-gonic/gin" + "qteam/app/http/controllers" + "qteam/app/services" +) + +func VoucherList(c *gin.Context) { + code, data := services.VoucherList() + controllers.HandCodeRes(c, data, code) +} diff --git a/app/http/entities/Brand.go b/app/http/entities/Brand.go new file mode 100644 index 0000000..1899b98 --- /dev/null +++ b/app/http/entities/Brand.go @@ -0,0 +1 @@ +package entities diff --git a/app/http/entities/Products.go b/app/http/entities/Products.go new file mode 100644 index 0000000..1899b98 --- /dev/null +++ b/app/http/entities/Products.go @@ -0,0 +1 @@ +package entities diff --git a/app/http/entities/Voucher.go b/app/http/entities/Voucher.go new file mode 100644 index 0000000..0ffaaf6 --- /dev/null +++ b/app/http/entities/Voucher.go @@ -0,0 +1,43 @@ +package entities + +import ( + "qteam/app/models/brandmodel" + "qteam/app/models/productsmodel" + "qteam/config" +) + +type VoucherListResponse struct { + MilkUrl string `json:"milkUrl"` + MilkList []MilkList `json:"milkList"` + MilkVoucherList []MilkVoucherList `json:"milkVoucherList"` +} + +type MilkList struct { + BrandLogo string `json:"brandLogo"` + BrandFlag string `json:"brandFlag"` +} + +func (this *MilkList) ResponseFromDb(in brandmodel.Brand) { + this.BrandFlag = in.Flag + this.BrandLogo = in.Logo +} + +type MilkVoucherList struct { + BrandFlag string `json:"brandFlag"` + VoucherTitle string `json:"voucherTitle"` + VoucherUrl string `json:"voucherUrl"` + VoucherIcon string `json:"voucherIcon"` + VoucherAmount string `json:"voucherAmount"` + VoucherOriginalPrice string `json:"voucherOriginalPrice"` + VoucherStatus string `json:"voucherStatus"` +} + +func (this *MilkVoucherList) ResponseFromDb(in productsmodel.MilkProductsList) { + this.BrandFlag = in.Flag + this.VoucherTitle = in.Name + this.VoucherUrl = config.GetConf().YouChu.MilkUrl + "?voucher_id=" + in.Id + this.VoucherIcon = in.MainImage + this.VoucherAmount = in.ShowPrice + this.VoucherOriginalPrice = in.Price + this.VoucherStatus = in.Status +} diff --git a/app/http/requestmapping/front.go b/app/http/requestmapping/front.go index 36ccad7..6d8c33c 100644 --- a/app/http/requestmapping/front.go +++ b/app/http/requestmapping/front.go @@ -1,7 +1,12 @@ package requestmapping +import "qteam/app/constants/common" + var FrontRequestMap = map[string]func() interface{}{ //"/v1/login": func() interface{} { // return new(front.LoginRequest) //}, + + // 奶茶专区代金券列表 + common.FRONT_API_V1 + "/VoucherList": func() interface{} { return new(struct{}) }, } diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 54b5230..7354937 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -4,7 +4,9 @@ package routes * 配置路由 */ import ( + "qteam/app/constants/common" "qteam/app/http/controllers" + "qteam/app/http/controllers/front" "qteam/app/http/middlewares" "qteam/app/http/trace" "qteam/app/utils/metric" @@ -44,10 +46,10 @@ func RegisterRoute(router *gin.Engine) { router.NoRoute(controllers.Error404) //api版本 - //v1 := router.Group("/v1", middlewares.ValidateRequest()) - //{ - // - //} + v1 := router.Group(common.FRONT_API_V1, middlewares.ValidateRequest()) + { + v1.POST("/VoucherList", front.VoucherList) + } router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) diff --git a/app/models/brandmodel/brand.go b/app/models/brandmodel/brand.go new file mode 100644 index 0000000..fc5da29 --- /dev/null +++ b/app/models/brandmodel/brand.go @@ -0,0 +1,43 @@ +package brandmodel + +import ( + "github.com/qit-team/snow-core/db" + "sync" + "time" +) + +var ( + once sync.Once + m *BrandModel +) + +// 实体 +type Brand struct { + Id int `xorm:"'id' int(10) pk autoincr"` + Logo string `xorm:"'logo' varchar(255)"` + Flag string `xorm:"'flag' varchar(50)"` + Name string `xorm:"'name' varchar(50)"` + State int `xorm:"'state' TINYINT"` + CreateTime time.Time `xorm:"'create_time' datetime"` + UpdateTime time.Time `xorm:"'update_time' datetime"` + Deleted time.Time `xorm:"'Deleted' datetime"` +} + +// 表名 +func (m *Brand) TableName() string { + return "Brand" +} + +// 私有化,防止被外部new +type BrandModel struct { + db.Model //组合基础Model,集成基础Model的属性和方法 +} + +// 单例模式 +func GetInstance() *BrandModel { + once.Do(func() { + m = new(BrandModel) + //m.DiName = "" //设置数据库实例连接,默认db.SingletonMain + }) + return m +} diff --git a/app/models/ordersmodel/orders.go b/app/models/ordersmodel/orders.go new file mode 100644 index 0000000..8bc7a84 --- /dev/null +++ b/app/models/ordersmodel/orders.go @@ -0,0 +1,52 @@ +package ordersmodel + +import ( + "github.com/qit-team/snow-core/db" + "sync" + "time" +) + +var ( + once sync.Once + m *OrdersModel +) + +// 实体 +type Orders struct { + Id string `xorm:"'id' UNSIGNED INT pk autoincr"` + OrderNo string `xorm:"'order_no' varchar(50)"` + UserId int `xorm:"'user_id' int(0)"` + YouchuUserId int `xorm:"'YouChu_User_id' int(11)"` + UserName string `xorm:"'user_name' varchar(11)"` + Mobile string `xorm:"'mobile' varchar(13)"` + ProductId int `xorm:"'product_id' int(0)"` + ProductName string `xorm:"'product_name' varchar(50)"` + Price string `xorm:"'price' decimal(10,2)"` + ActivityId string `xorm:"'activity_id' UNSIGNED INT"` + State string `xorm:"'state' UNSIGNED TINYINT"` + VoucherId int `xorm:"'voucher_id' int(0)"` + VoucherLink string `xorm:"'voucher_link' varchar(255)"` + CreateTime time.Time `xorm:"'create_time' datetime"` + UpdateTime time.Time `xorm:"'update_time' datetime"` + ExchangeTime time.Time `xorm:"'exchange_time' datetime"` + Deleted time.Time `xorm:"'Deleted' datetime"` +} + +// 表名 +func (m *Orders) TableName() string { + return "Orders" +} + +// 私有化,防止被外部new +type OrdersModel struct { + db.Model //组合基础Model,集成基础Model的属性和方法 +} + +// 单例模式 +func GetInstance() *OrdersModel { + once.Do(func() { + m = new(OrdersModel) + //m.DiName = "" //设置数据库实例连接,默认db.SingletonMain + }) + return m +} diff --git a/app/models/productsmodel/products.go b/app/models/productsmodel/products.go new file mode 100644 index 0000000..df0adf4 --- /dev/null +++ b/app/models/productsmodel/products.go @@ -0,0 +1,55 @@ +package productsmodel + +import ( + "github.com/qit-team/snow-core/db" + "sync" + "time" +) + +var ( + once sync.Once + m *ProductsModel +) + +// 实体 +type Products struct { + Id string `xorm:"'id' UNSIGNED BIGINT pk autoincr"` + Name string `xorm:"'name' varchar(255)"` + MainImage string `xorm:"'main_image' varchar(255)"` + Brand string `xorm:"'brand' varchar(100)"` + BrandId int `xorm:"'brand_id' int(10)"` + Stock string `xorm:"'stock' UNSIGNED INT"` + ShowPrice string `xorm:"'show_price' decimal(10,2)"` + Price string `xorm:"'price' decimal(10,2)"` + Type string `xorm:"'type' UNSIGNED TINYINT"` + CreateTime time.Time `xorm:"'create_time' datetime"` + UpdateTime time.Time `xorm:"'update_time' datetime"` + Deleted time.Time `xorm:"'Deleted' datetime"` + Status string `xorm:"'status' UNSIGNED TINYINT"` + Description string `xorm:"'description' varchar(500)"` + ThirdProductId int `xorm:"'third_product_Id' int(0)"` +} + +type MilkProductsList struct { + Products `xorm:"extends"` + Flag string `xorm:"'flag' varchar(50)"` +} + +// 表名 +func (m *Products) TableName() string { + return "Products" +} + +// 私有化,防止被外部new +type ProductsModel struct { + db.Model //组合基础Model,集成基础Model的属性和方法 +} + +// 单例模式 +func GetInstance() *ProductsModel { + once.Do(func() { + m = new(ProductsModel) + //m.DiName = "" //设置数据库实例连接,默认db.SingletonMain + }) + return m +} diff --git a/app/models/usersmodel/users.go b/app/models/usersmodel/users.go new file mode 100644 index 0000000..427a5b3 --- /dev/null +++ b/app/models/usersmodel/users.go @@ -0,0 +1,43 @@ +package usersmodel + +import ( + "github.com/qit-team/snow-core/db" + "sync" + "time" +) + +var ( + once sync.Once + m *UsersModel +) + +// 实体 +type Users struct { + Id string `xorm:"'id' UNSIGNED INT pk autoincr"` + YouchuUserFlag int `xorm:"'YouChu_user_flag' int(10)"` + Name string `xorm:"'name' varchar(128)"` + Phone string `xorm:"'phone' char(11)"` + Status string `xorm:"'status' UNSIGNED TINYINT"` + CreateTime time.Time `xorm:"'create_time' datetime"` + UpdateTime time.Time `xorm:"'update_time' datetime"` + Deleted time.Time `xorm:"'Deleted' datetime"` +} + +// 表名 +func (m *Users) TableName() string { + return "Users" +} + +// 私有化,防止被外部new +type UsersModel struct { + db.Model //组合基础Model,集成基础Model的属性和方法 +} + +// 单例模式 +func GetInstance() *UsersModel { + once.Do(func() { + m = new(UsersModel) + //m.DiName = "" //设置数据库实例连接,默认db.SingletonMain + }) + return m +} diff --git a/app/services/BrandService.go b/app/services/BrandService.go new file mode 100644 index 0000000..5965599 --- /dev/null +++ b/app/services/BrandService.go @@ -0,0 +1,10 @@ +package services + +import ( + "qteam/app/models/brandmodel" +) + +func MilkBrandList() (code int, brandData []brandmodel.Brand) { + err := brandmodel.GetInstance().GetDb().Where("state = ?", 1).Find(&brandData) + return handErr(err), brandData +} diff --git a/app/services/ProductService.go b/app/services/ProductService.go new file mode 100644 index 0000000..d5361a8 --- /dev/null +++ b/app/services/ProductService.go @@ -0,0 +1,10 @@ +package services + +import "qteam/app/models/productsmodel" + +func MilkProductList() (code int, productList []productsmodel.MilkProductsList) { + err := productsmodel.GetInstance().GetDb().Alias("a"). + Join("INNER", "brand b", "a.brand_id = b.id"). + Where("a.status = ?", 1).Find(&productList) + return handErr(err), productList +} diff --git a/app/services/VoucherService.go b/app/services/VoucherService.go new file mode 100644 index 0000000..21999ce --- /dev/null +++ b/app/services/VoucherService.go @@ -0,0 +1,34 @@ +package services + +import ( + "github.com/ahmetb/go-linq/v3" + "qteam/app/http/entities" + "qteam/app/models/brandmodel" + "qteam/app/models/productsmodel" + "qteam/config" +) + +func VoucherList() (code int, VoucherListResponse entities.VoucherListResponse) { + VoucherListResponse.MilkUrl = config.GetConf().YouChu.MilkUrl + var BrandData []brandmodel.Brand + var VoucherData []productsmodel.MilkProductsList + code, BrandData = MilkBrandList() + if BrandData != nil { + var MilkList []entities.MilkList + linq.From(BrandData).SelectT(func(in brandmodel.Brand) (d entities.MilkList) { + d.ResponseFromDb(in) + return + }).ToSlice(&MilkList) + VoucherListResponse.MilkList = MilkList + } + code, VoucherData = MilkProductList() + if VoucherData != nil { + var MilkVoucherList []entities.MilkVoucherList + linq.From(VoucherData).SelectT(func(in productsmodel.MilkProductsList) (d entities.MilkVoucherList) { + d.ResponseFromDb(in) + return + }).ToSlice(&MilkVoucherList) + VoucherListResponse.MilkVoucherList = MilkVoucherList + } + return +} diff --git a/app/services/common.go b/app/services/common.go new file mode 100644 index 0000000..a29c600 --- /dev/null +++ b/app/services/common.go @@ -0,0 +1,15 @@ +package services + +import ( + "qteam/app/constants/errorcode" + "qteam/app/utils" +) + +func handErr(err error) int { + if err != nil { + utils.Log(nil, "sys err", err.Error()) + return errorcode.SystemError + } else { + return errorcode.Success + } +} diff --git a/config/config.go b/config/config.go index 9fdff6f..32be229 100644 --- a/config/config.go +++ b/config/config.go @@ -36,6 +36,11 @@ type Config struct { OpenApi OpenApi `toml:"OpenApi"` Jwt Jwt `toml:"Jwt"` AliOss AliOss `toml:"AliOss"` + YouChu YouChu `toml:"YouChu"` +} + +type YouChu struct { + MilkUrl string } type AliOss struct { diff --git a/go.mod b/go.mod index 11ba3a8..3ace40f 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/ahmetb/go-linq/v3 v3.2.0 // indirect github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68 // indirect github.com/alibabacloud-go/tea v1.1.17 // indirect github.com/alibabacloud-go/tea-utils v1.4.4 // indirect diff --git a/go.sum b/go.sum index a3fda71..5020055 100644 --- a/go.sum +++ b/go.sum @@ -61,6 +61,8 @@ github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/ github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/agiledragon/gomonkey/v2 v2.3.1 h1:k+UnUY0EMNYUFUAQVETGY9uUTxjMdnUkP0ARyJS1zzs= github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= +github.com/ahmetb/go-linq/v3 v3.2.0 h1:BEuMfp+b59io8g5wYzNoFe9pWPalRklhlhbiU3hYZDE= +github.com/ahmetb/go-linq/v3 v3.2.0/go.mod h1:haQ3JfOeWK8HpVxMtHHEMPVgBKiYyQ+f1/kLZh/cj9U= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=