优化与代理服务的密钥存储与传递方式;更新套餐,账单查询对长效套餐的支持,新增长效套餐分页查询接口
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/shopspring/decimal"
|
||||
bill2 "platform/web/domains/bill"
|
||||
resource2 "platform/web/domains/resource"
|
||||
@@ -44,7 +43,7 @@ func (s *resourceService) CreateResource(uid int32, now time.Time, ser *CreateRe
|
||||
|
||||
// 检查余额
|
||||
if user.Balance.Cmp(amount) < 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "余额不足")
|
||||
return ErrBalanceNotEnough
|
||||
}
|
||||
|
||||
// 保存套餐
|
||||
@@ -70,7 +69,9 @@ func (s *resourceService) CreateResource(uid int32, now time.Time, ser *CreateRe
|
||||
}
|
||||
|
||||
// 更新用户余额
|
||||
_, err = q.User.UpdateSimple(q.User.Balance.Value(user.Balance.Sub(amount)))
|
||||
_, err = q.User.
|
||||
Where(q.User.ID.Eq(uid)).
|
||||
UpdateSimple(q.User.Balance.Value(user.Balance.Sub(amount)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -115,7 +116,7 @@ func (s *resourceService) PrepareResource(uid int32, now time.Time, method trade
|
||||
}
|
||||
|
||||
// 保存请求缓存
|
||||
resourceSerializer := CreateResourceSerializer{}
|
||||
resourceSerializer := new(CreateResourceSerializer)
|
||||
if err := resourceSerializer.ByData(data); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -154,7 +155,7 @@ func (s *resourceService) CompleteResource(tradeNo string, now time.Time, opResu
|
||||
|
||||
// 检查交易结果
|
||||
var rs *TransactionVerifyResult
|
||||
if len(opResult) > 0 {
|
||||
if len(opResult) > 0 && opResult[0] != nil {
|
||||
rs = opResult[0]
|
||||
} else {
|
||||
var err error
|
||||
@@ -296,7 +297,7 @@ type CreateResourceData interface {
|
||||
}
|
||||
|
||||
type CreateShortResourceData struct {
|
||||
Live int32 `json:"live" validate:"required min=180"`
|
||||
Live int32 `json:"live" validate:"required,min=180"`
|
||||
Mode int32 `json:"mode" validate:"required"`
|
||||
Expire int32 `json:"expire"`
|
||||
DailyLimit int32 `json:"daily_limit" validate:"min=2000"`
|
||||
@@ -315,14 +316,13 @@ func (data *CreateShortResourceData) GetName() string {
|
||||
case 2:
|
||||
mode = "包量"
|
||||
}
|
||||
data.name = fmt.Sprintf("短效动态%s %d 天", mode, data.Live)
|
||||
data.name = fmt.Sprintf("短效动态%s %v 分钟", mode, data.Live/60)
|
||||
}
|
||||
return data.name
|
||||
}
|
||||
|
||||
func (data *CreateShortResourceData) GetPrice() decimal.Decimal {
|
||||
if data.price == nil {
|
||||
|
||||
var factor int32
|
||||
switch data.Mode {
|
||||
case 1:
|
||||
@@ -345,8 +345,8 @@ func (data *CreateShortResourceData) GetPrice() decimal.Decimal {
|
||||
}
|
||||
|
||||
type CreateLongResourceData struct {
|
||||
Live int32 `json:"live" validate:"required oneof=1,4,8,12,24"`
|
||||
Mode int32 `json:"mode" validate:"required oneof=1,2"`
|
||||
Live int32 `json:"live" validate:"required,oneof=1 4 8 12 24"`
|
||||
Mode int32 `json:"mode" validate:"required,oneof=1 2"`
|
||||
Expire int32 `json:"expire"`
|
||||
DailyLimit int32 `json:"daily_limit" validate:"min=100"`
|
||||
Quota int32 `json:"quota" validate:"min=500"`
|
||||
@@ -364,14 +364,13 @@ func (data *CreateLongResourceData) GetName() string {
|
||||
case 2:
|
||||
mode = "包量"
|
||||
}
|
||||
data.name = fmt.Sprintf("长效动态%s %d 天", mode, data.Live)
|
||||
data.name = fmt.Sprintf("长效动态%s %d 小时", mode, data.Live)
|
||||
}
|
||||
return data.name
|
||||
}
|
||||
|
||||
func (data *CreateLongResourceData) GetPrice() decimal.Decimal {
|
||||
if data.price == nil {
|
||||
|
||||
var factor int32 = 0
|
||||
switch resource2.Mode(data.Mode) {
|
||||
|
||||
@@ -404,14 +403,6 @@ func (data *CreateLongResourceData) GetPrice() decimal.Decimal {
|
||||
return *data.price
|
||||
}
|
||||
|
||||
type CreateResourceCache struct {
|
||||
Uid int32 `json:"uid"`
|
||||
TradeId int32 `json:"trade_id"`
|
||||
BillId int32 `json:"bill_id"`
|
||||
Method trade2.Method `json:"method"`
|
||||
CreateResourceSerializer
|
||||
}
|
||||
|
||||
type CreateResourceSerializer struct {
|
||||
Type resource2.Type `json:"type" validate:"required"`
|
||||
Short *CreateShortResourceData `json:"short,omitempty"`
|
||||
@@ -441,3 +432,36 @@ func (s *CreateResourceSerializer) ByData(data CreateResourceData) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type CreateResourceCache struct {
|
||||
Uid int32 `json:"uid"`
|
||||
TradeId int32 `json:"trade_id"`
|
||||
BillId int32 `json:"bill_id"`
|
||||
Method trade2.Method `json:"method"`
|
||||
*CreateResourceSerializer
|
||||
}
|
||||
|
||||
func (c CreateResourceCache) MarshalBinary() (data []byte, err error) {
|
||||
data, err = json.Marshal(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (c CreateResourceCache) UnmarshalBinary(data []byte) error {
|
||||
if err := json.Unmarshal(data, &c); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ResourceServiceErr string
|
||||
|
||||
func (e ResourceServiceErr) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
const (
|
||||
ErrBalanceNotEnough = ResourceServiceErr("余额不足")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user