重构增加模型枚举值定义
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"platform/web/auth"
|
||||
"platform/web/core"
|
||||
client2 "platform/web/domains/client"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"time"
|
||||
@@ -26,13 +27,11 @@ func (s *authService) OauthAuthorizationCode(ctx context.Context, client *m.Clie
|
||||
func (s *authService) OauthClientCredentials(ctx context.Context, client *m.Client, scope ...string) (*TokenDetails, error) {
|
||||
|
||||
var clientType auth.PayloadType
|
||||
switch client.Spec {
|
||||
case 1:
|
||||
clientType = auth.PayloadClientPublic
|
||||
case 2:
|
||||
clientType = auth.PayloadClientPublic
|
||||
case 3:
|
||||
clientType = auth.PayloadClientConfidential
|
||||
switch client2.Spec(client.Spec) {
|
||||
case client2.SpecNative, client2.SpecBrowser:
|
||||
clientType = auth.PayloadPublicServer
|
||||
case client2.SpecWeb, client2.SpecTrusted:
|
||||
clientType = auth.PayloadSecuredServer
|
||||
}
|
||||
|
||||
var permissions = make(map[string]struct{}, len(scope))
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
"platform/pkg/u"
|
||||
"platform/web/auth"
|
||||
"platform/web/core"
|
||||
channel2 "platform/web/domains/channel"
|
||||
proxy2 "platform/web/domains/proxy"
|
||||
g "platform/web/globals"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
@@ -214,7 +216,7 @@ func (s *channelService) CreateChannel(
|
||||
ctx context.Context,
|
||||
authCtx *auth.Context,
|
||||
resourceId int32,
|
||||
protocol ChannelProtocol,
|
||||
protocol channel2.Protocol,
|
||||
authType ChannelAuthType,
|
||||
count int,
|
||||
nodeFilter ...NodeFilterConfig,
|
||||
@@ -340,7 +342,7 @@ func findResource(q *q.Query, resourceId int32, authCtx *auth.Context, count int
|
||||
|
||||
func findProxies(q *q.Query) (proxies []*m.Proxy, err error) {
|
||||
proxies, err = q.Proxy.
|
||||
Where(q.Proxy.Type.Eq(1)).
|
||||
Where(q.Proxy.Type.Eq(int32(proxy2.TypeThirdParty))).
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -394,7 +396,7 @@ func calcChannels(
|
||||
whitelist *[]string,
|
||||
count int,
|
||||
userId int32,
|
||||
protocol ChannelProtocol,
|
||||
protocol channel2.Protocol,
|
||||
authType ChannelAuthType,
|
||||
expiration time.Time,
|
||||
filter NodeFilterConfig,
|
||||
@@ -700,15 +702,6 @@ const (
|
||||
ChannelAuthTypePass
|
||||
)
|
||||
|
||||
type ChannelProtocol int32
|
||||
|
||||
const (
|
||||
ProtocolAll ChannelProtocol = iota
|
||||
ProtocolHTTP
|
||||
ProtocolHttps
|
||||
ProtocolSocks5
|
||||
)
|
||||
|
||||
type ChannelServiceErr string
|
||||
|
||||
func (c ChannelServiceErr) Error() string {
|
||||
|
||||
@@ -7,6 +7,9 @@ import (
|
||||
"fmt"
|
||||
"platform/pkg/rds"
|
||||
"platform/web/core"
|
||||
bill2 "platform/web/domains/bill"
|
||||
resource2 "platform/web/domains/resource"
|
||||
trade2 "platform/web/domains/trade"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"strings"
|
||||
@@ -19,7 +22,7 @@ var Resource = &resourceService{}
|
||||
|
||||
type resourceService struct{}
|
||||
|
||||
func (s *resourceService) PrepareResource(ctx context.Context, data *CreateResourceData, uid int32, method TransactionMethod) (*TransactionPrepareResult, error) {
|
||||
func (s *resourceService) PrepareResource(ctx context.Context, data *CreateResourceData, uid int32, method trade2.Method) (*TransactionPrepareResult, error) {
|
||||
amount := data.GetPrice()
|
||||
|
||||
// 保存到数据库
|
||||
@@ -32,7 +35,7 @@ func (s *resourceService) PrepareResource(ctx context.Context, data *CreateResou
|
||||
Subject: "购买套餐 - " + data.GetName(),
|
||||
Amount: amount,
|
||||
ExpireAt: time.Now().Add(30 * time.Minute),
|
||||
Type: TransactionTypePurchase,
|
||||
Type: trade2.TypeRecharge,
|
||||
Method: method,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -150,7 +153,7 @@ func (s *resourceService) CreateResource(data *CreateResourceData, uid int32) er
|
||||
ResourceID: resource.ID,
|
||||
BillNo: ID.GenReadable("bil"),
|
||||
Info: "购买套餐 - " + data.GetName(),
|
||||
Type: 1,
|
||||
Type: int32(bill2.TypeConsume),
|
||||
Amount: data.GetPrice(),
|
||||
}
|
||||
err = q.Bill.
|
||||
@@ -243,7 +246,7 @@ func createResource(q *q.Query, data *CreateResourceData, uid int32) (*m.Resourc
|
||||
UserID: uid,
|
||||
ResourceNo: ID.GenReadable("res"),
|
||||
Active: true,
|
||||
Type: 1,
|
||||
Type: int32(resource2.TypeDynamic),
|
||||
Pss: &m.ResourcePss{
|
||||
Type: data.Type,
|
||||
Live: data.Live,
|
||||
@@ -260,7 +263,7 @@ func createResource(q *q.Query, data *CreateResourceData, uid int32) (*m.Resourc
|
||||
return &resource, nil
|
||||
}
|
||||
|
||||
func (s *resourceService) CancelResource(ctx context.Context, tradeNo string, at time.Time, method TransactionMethod) error {
|
||||
func (s *resourceService) CancelResource(ctx context.Context, tradeNo string, at time.Time, method trade2.Method) error {
|
||||
// 删除请求缓存
|
||||
_, err := rds.Client.Del(ctx, tradeNo).Result()
|
||||
if err != nil {
|
||||
|
||||
@@ -9,6 +9,9 @@ import (
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/u"
|
||||
"platform/web/core"
|
||||
bill2 "platform/web/domains/bill"
|
||||
coupon2 "platform/web/domains/coupon"
|
||||
trade2 "platform/web/domains/trade"
|
||||
g "platform/web/globals"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
@@ -43,7 +46,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
coupon, err := q.Coupon.WithContext(ctx).
|
||||
Where(
|
||||
q.Coupon.Code.Eq(data.CouponCode),
|
||||
q.Coupon.Status.Eq(0),
|
||||
q.Coupon.Status.Eq(int32(coupon2.StatusUnused)),
|
||||
).
|
||||
Take()
|
||||
if err != nil {
|
||||
@@ -57,7 +60,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
if !expireAt.IsZero() && expireAt.Before(time.Now()) {
|
||||
_, err = q.Coupon.
|
||||
Where(q.Coupon.ID.Eq(coupon.ID)).
|
||||
Update(q.Coupon.Status, 2)
|
||||
Update(q.Coupon.Status, coupon2.StatusExpired)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -83,7 +86,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
if time.Time(coupon.ExpireAt).IsZero() {
|
||||
_, err = q.Coupon.
|
||||
Where(q.Coupon.ID.Eq(coupon.ID)).
|
||||
Update(q.Coupon.Status, 1)
|
||||
Update(q.Coupon.Status, int32(coupon2.StatusUsed))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -102,7 +105,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
switch method {
|
||||
|
||||
// 调用支付宝支付接口
|
||||
case TransactionMethodAlipay:
|
||||
case trade2.MethodAlipay:
|
||||
resp, err := g.Alipay.TradePagePay(alipay.TradePagePay{
|
||||
QRPayMode: "4",
|
||||
Trade: alipay.Trade{
|
||||
@@ -119,7 +122,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
payUrl = resp.String()
|
||||
|
||||
// 调用微信支付接口
|
||||
case TransactionMethodWeChat:
|
||||
case trade2.MethodWeChat:
|
||||
resp, _, err := g.WechatPay.Native.Prepay(ctx, native.PrepayRequest{
|
||||
Appid: &env.WechatPayAppId,
|
||||
Mchid: &env.WechatPayMchId,
|
||||
@@ -142,15 +145,12 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
}
|
||||
|
||||
// 保存交易订单
|
||||
var tradeType int
|
||||
var billType int
|
||||
var billType bill2.Type
|
||||
switch tType {
|
||||
case TransactionTypeRecharge:
|
||||
tradeType = 2
|
||||
billType = 3
|
||||
case TransactionTypePurchase:
|
||||
tradeType = 1
|
||||
billType = 1
|
||||
case trade2.TypeRecharge:
|
||||
billType = bill2.TypeRecharge
|
||||
case trade2.TypePurchase:
|
||||
billType = bill2.TypeConsume
|
||||
}
|
||||
|
||||
var trade = m.Trade{
|
||||
@@ -158,7 +158,7 @@ func (s *transactionService) PrepareTransaction(ctx context.Context, q *q.Query,
|
||||
InnerNo: tradeNo,
|
||||
Subject: subject,
|
||||
Method: int32(method),
|
||||
Type: int32(tradeType),
|
||||
Type: int32(tType),
|
||||
Amount: amount,
|
||||
PayURL: payUrl,
|
||||
}
|
||||
@@ -202,7 +202,7 @@ func (s *transactionService) VerifyTransaction(ctx context.Context, data *Transa
|
||||
switch method {
|
||||
|
||||
// 检查支付宝交易
|
||||
case TransactionMethodAlipay:
|
||||
case trade2.MethodAlipay:
|
||||
resp, err := g.Alipay.TradeQuery(ctx, alipay.TradeQuery{
|
||||
OutTradeNo: tradeNo,
|
||||
})
|
||||
@@ -228,7 +228,7 @@ func (s *transactionService) VerifyTransaction(ctx context.Context, data *Transa
|
||||
}
|
||||
|
||||
// 检查微信交易
|
||||
case TransactionMethodWeChat:
|
||||
case trade2.MethodWeChat:
|
||||
resp, _, err := g.WechatPay.Native.QueryOrderByOutTradeNo(ctx, native.QueryOrderByOutTradeNoRequest{
|
||||
OutTradeNo: &tradeNo,
|
||||
Mchid: &env.WechatPayMchId,
|
||||
@@ -274,12 +274,12 @@ func (s *transactionService) CompleteTransaction(ctx context.Context, q *q.Query
|
||||
}
|
||||
|
||||
// 检查交易状态
|
||||
if trade.Status != 0 {
|
||||
if trade.Status != int32(trade2.StatusPending) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 更新交易状态
|
||||
trade.Status = 1
|
||||
trade.Status = int32(trade2.StatusSuccess)
|
||||
trade.OuterNo = transId
|
||||
trade.Payment = payment
|
||||
trade.PaidAt = core.LocalDateTime(paidAt)
|
||||
@@ -294,11 +294,11 @@ func (s *transactionService) CompleteTransaction(ctx context.Context, q *q.Query
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *transactionService) RevokeTransaction(ctx context.Context, tradeNo string, method TransactionMethod) error {
|
||||
func (s *transactionService) RevokeTransaction(ctx context.Context, tradeNo string, method trade2.Method) error {
|
||||
|
||||
switch method {
|
||||
|
||||
case TransactionMethodAlipay:
|
||||
case trade2.MethodAlipay:
|
||||
resp, err := g.Alipay.TradeCancel(ctx, alipay.TradeCancel{
|
||||
OutTradeNo: tradeNo,
|
||||
})
|
||||
@@ -310,7 +310,7 @@ func (s *transactionService) RevokeTransaction(ctx context.Context, tradeNo stri
|
||||
return errors.New("交易取消失败")
|
||||
}
|
||||
|
||||
case TransactionMethodWeChat:
|
||||
case trade2.MethodWeChat:
|
||||
resp, err := g.WechatPay.Native.CloseOrder(ctx, native.CloseOrderRequest{
|
||||
Mchid: &env.WechatPayMchId,
|
||||
OutTradeNo: &tradeNo,
|
||||
@@ -333,7 +333,7 @@ func (s *transactionService) FinishTransaction(ctx context.Context, q *q.Query,
|
||||
Where(q.Trade.InnerNo.Eq(tradeNo)).
|
||||
Select(q.Trade.Status, q.Trade.CancelAt, q.Trade.PayURL).
|
||||
Updates(m.Trade{
|
||||
Status: 2,
|
||||
Status: int32(trade2.StatusCanceled),
|
||||
CancelAt: core.LocalDateTime(time),
|
||||
PayURL: "",
|
||||
})
|
||||
@@ -344,26 +344,12 @@ func (s *transactionService) FinishTransaction(ctx context.Context, q *q.Query,
|
||||
return nil
|
||||
}
|
||||
|
||||
type TransactionType int32
|
||||
|
||||
const (
|
||||
TransactionTypePurchase TransactionType = iota + 1
|
||||
TransactionTypeRecharge
|
||||
)
|
||||
|
||||
type TransactionMethod int32
|
||||
|
||||
const (
|
||||
TransactionMethodAlipay TransactionMethod = iota
|
||||
TransactionMethodWeChat
|
||||
)
|
||||
|
||||
type TransactionPrepareData struct {
|
||||
Subject string
|
||||
Amount float64
|
||||
ExpireAt time.Time
|
||||
Type TransactionType
|
||||
Method TransactionMethod
|
||||
Type trade2.Type
|
||||
Method trade2.Method
|
||||
CouponCode string
|
||||
}
|
||||
|
||||
@@ -376,7 +362,7 @@ type TransactionPrepareResult struct {
|
||||
|
||||
type TransactionVerifyData struct {
|
||||
TradeNo string
|
||||
Method TransactionMethod
|
||||
Method trade2.Method
|
||||
}
|
||||
|
||||
type TransactionVerifyResult struct {
|
||||
|
||||
Reference in New Issue
Block a user