diff --git a/models/migration.go b/models/migration.go index 4150182..e9bb4d4 100644 --- a/models/migration.go +++ b/models/migration.go @@ -160,6 +160,7 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti {Name: "temp_path", Value: "temp", Type: "path"}, {Name: "score_enabled", Value: "1", Type: "score"}, {Name: "share_score_rate", Value: "80", Type: "score"}, + {Name: "score_price", Value: "1", Type: "score"}, {Name: "home_view_method", Value: "icon", Type: "view"}, {Name: "share_view_method", Value: "list", Type: "view"}, {Name: "cron_garbage_collect", Value: "@hourly", Type: "cron"}, diff --git a/models/order.go b/models/order.go index 48af776..22fd055 100644 --- a/models/order.go +++ b/models/order.go @@ -10,6 +10,8 @@ const ( PackOrderType = iota // GroupOrderType 用户组订单 GroupOrderType + // ScoreOrderType 积分充值订单 + ScoreOrderType ) const ( diff --git a/pkg/payment/order.go b/pkg/payment/order.go index 20d9408..9b82ff5 100644 --- a/pkg/payment/order.go +++ b/pkg/payment/order.go @@ -58,16 +58,21 @@ func NewOrder(pack *serializer.PackProduct, group *serializer.GroupProducts, num title string price int ) - if pack == nil { + if pack != nil { + orderType = model.PackOrderType + productID = pack.ID + title = pack.Name + price = pack.Price + } else if group != nil { orderType = model.GroupOrderType productID = group.ID title = group.Name price = group.Price } else { - orderType = model.PackOrderType - productID = pack.ID - title = pack.Name - price = pack.Price + orderType = model.ScoreOrderType + productID = 0 + title = fmt.Sprintf("%d 积分", num) + price = model.GetIntSetting("score_price", 1) } // 创建订单记录 diff --git a/pkg/payment/purchase.go b/pkg/payment/purchase.go index 249d36c..a1bd574 100644 --- a/pkg/payment/purchase.go +++ b/pkg/payment/purchase.go @@ -53,12 +53,19 @@ func GiveGroup(user *model.User, groupInfo *serializer.GroupProducts, num int) e return nil } +// GiveScore 积分充值 +func GiveScore(user *model.User, num int) error { + user.AddScore(num) + return nil +} + // GiveProduct “发货” func GiveProduct(user *model.User, pack *serializer.PackProduct, group *serializer.GroupProducts, num int) error { if pack != nil { return GivePack(user, pack, num) } else if group != nil { return GiveGroup(user, group, num) + } else { + return GiveScore(user, num) } - return nil } diff --git a/pkg/payment/score.go b/pkg/payment/score.go index 8e49a26..a404bf6 100644 --- a/pkg/payment/score.go +++ b/pkg/payment/score.go @@ -34,6 +34,7 @@ func (pay *ScorePayment) Create(order *model.Order, pack *serializer.PackProduct } // 创建订单记录 + order.Status = model.OrderPaid if _, err := order.Create(); err != nil { return nil, ErrInsertOrder.WithError(err) } diff --git a/pkg/serializer/vas.go b/pkg/serializer/vas.go index adc08f9..f38a0ec 100644 --- a/pkg/serializer/vas.go +++ b/pkg/serializer/vas.go @@ -68,17 +68,18 @@ type GroupProducts struct { } // BuildProductResponse 构建增值服务商品响应 -func BuildProductResponse(groups []GroupProducts, packs []PackProduct, alipay, payjs bool) Response { +func BuildProductResponse(groups []GroupProducts, packs []PackProduct, alipay, payjs bool, scorePrice int) Response { // 隐藏响应中的用户组ID for i := 0; i < len(groups); i++ { groups[i].GroupID = 0 } return Response{ Data: map[string]interface{}{ - "packs": packs, - "groups": groups, - "alipay": alipay, - "payjs": payjs, + "packs": packs, + "groups": groups, + "alipay": alipay, + "payjs": payjs, + "score_price": scorePrice, }, } } diff --git a/service/vas/quota.go b/service/vas/quota.go index 1e7c086..25cc1c8 100644 --- a/service/vas/quota.go +++ b/service/vas/quota.go @@ -14,10 +14,10 @@ type GeneralVASService struct { // CreateOrderService 创建订单服务 type CreateOrderService struct { - Action string `json:"action" binding:"required,eq=group|eq=pack"` + Action string `json:"action" binding:"required,eq=group|eq=pack|eq=score"` Method string `json:"method" binding:"required,eq=alipay|eq=score|eq=payjs"` ID int64 `json:"id" binding:"required"` - Num int `json:"num" binding:"required,min=1,max=99"` + Num int `json:"num" binding:"required,min=1"` } // Create 创建新订单 @@ -40,7 +40,7 @@ func (service *CreateOrderService) Create(c *gin.Context, user *model.User) seri break } } - } else { + } else if service.Action == "pack" { for _, v := range packs { if v.ID == service.ID { pack = &v @@ -48,8 +48,12 @@ func (service *CreateOrderService) Create(c *gin.Context, user *model.User) seri } } } + + // 购买积分 if pack == nil && group == nil { - return serializer.Err(serializer.CodeNotFound, "商品不存在", nil) + if service.Method == "score" { + return serializer.Err(serializer.CodeNotFound, "不支持此支付方式", nil) + } } // 创建订单 @@ -64,13 +68,14 @@ func (service *CreateOrderService) Create(c *gin.Context, user *model.User) seri // Products 获取商品信息 func (service *GeneralVASService) Products(c *gin.Context, user *model.User) serializer.Response { - options := model.GetSettingByNames("alipay_enabled", "payjs_enabled") + options := model.GetSettingByNames("alipay_enabled", "payjs_enabled", "score_price") + scorePrice := model.GetIntSetting("score_price", 0) packs, groups, err := decodeProductInfo() if err != nil { return serializer.Err(serializer.CodeInternalSetting, "无法解析商品设置", err) } - return serializer.BuildProductResponse(groups, packs, options["alipay_enabled"] == "1", options["payjs_enabled"] == "1") + return serializer.BuildProductResponse(groups, packs, options["alipay_enabled"] == "1", options["payjs_enabled"] == "1", scorePrice) } func decodeProductInfo() ([]serializer.PackProduct, []serializer.GroupProducts, error) {