Refactor: Remove deprecated gRPC service files and implement new API structure
- Deleted old gRPC service definitions in `game_grpc.pb.go` and `public.go`. - Added new API server implementations for objectstory, player, and shop services. - Introduced configuration files for new APIs in `etc/*.yaml`. - Created main entry points for each service in `objectstory.go`, `player.go`, and `shop.go`. - Removed unused user update handler and user API files. - Added utility functions for context management and HTTP header parsing. - Introduced PostgreSQL backup configuration in `backup/postgreSql.yaml`.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package wallet
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"juwan-backend/app/wallet/api/internal/svc"
|
||||
"juwan-backend/app/wallet/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetBalanceLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取余额
|
||||
func NewGetBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBalanceLogic {
|
||||
return &GetBalanceLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetBalanceLogic) GetBalance(req *types.EmptyResp) (resp *types.WalletBalance, err error) {
|
||||
userID, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out, err := l.svcCtx.WalletRpc.GetWalletsById(l.ctx, &pb.GetWalletsByIdReq{Id: userID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if out.GetWallets() == nil {
|
||||
return &types.WalletBalance{Balance: "0", FrozenBalance: "0"}, nil
|
||||
}
|
||||
|
||||
return &types.WalletBalance{
|
||||
Balance: out.Wallets.Balance,
|
||||
FrozenBalance: out.Wallets.FrozenBalance,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package wallet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"juwan-backend/app/wallet/api/internal/svc"
|
||||
"juwan-backend/app/wallet/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ListTransactionsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取流水
|
||||
func NewListTransactionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListTransactionsLogic {
|
||||
return &ListTransactionsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ListTransactionsLogic) ListTransactions(req *types.PageReq) (resp *types.TransactionListResp, err error) {
|
||||
userID, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Limit <= 0 {
|
||||
req.Limit = 20
|
||||
}
|
||||
|
||||
out, err := l.svcCtx.WalletRpc.SearchWalletTransactions(l.ctx, &pb.SearchWalletTransactionsReq{
|
||||
Page: req.Offset / req.Limit,
|
||||
Limit: req.Limit,
|
||||
UserId: &userID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items := make([]types.Transaction, 0, len(out.GetWalletTransactions()))
|
||||
for _, item := range out.GetWalletTransactions() {
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
tx := types.Transaction{
|
||||
Id: item.Id,
|
||||
Type: item.Type,
|
||||
Amount: item.Amount,
|
||||
Description: item.Description,
|
||||
CreatedAt: time.Unix(item.CreatedAt, 0).Format(time.RFC3339),
|
||||
}
|
||||
if item.OrderId > 0 {
|
||||
tx.OrderId = strconv.FormatInt(item.OrderId, 10)
|
||||
}
|
||||
items = append(items, tx)
|
||||
}
|
||||
|
||||
return &types.TransactionListResp{
|
||||
Items: items,
|
||||
Meta: types.PageMeta{
|
||||
Total: int64(len(items)),
|
||||
Offset: req.Offset,
|
||||
Limit: req.Limit,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package wallet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"juwan-backend/app/wallet/api/internal/svc"
|
||||
"juwan-backend/app/wallet/api/internal/types"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type TopupLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 充值
|
||||
func NewTopupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TopupLogic {
|
||||
return &TopupLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *TopupLogic) Topup(req *types.TopupReq) (resp *types.EmptyResp, err error) {
|
||||
amount, err := decimal.NewFromString(req.Amount)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid amount")
|
||||
}
|
||||
if !amount.GreaterThan(decimal.Zero) {
|
||||
return nil, errors.New("amount must be greater than 0")
|
||||
}
|
||||
|
||||
userID, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
walletResp, err := l.svcCtx.WalletRpc.GetWalletsById(l.ctx, &pb.GetWalletsByIdReq{Id: userID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
currentBalance := decimal.Zero
|
||||
frozenBalance := decimal.Zero
|
||||
expectedVersion := int64(0)
|
||||
if walletResp.GetWallets() == nil {
|
||||
zeroAmount := "0"
|
||||
_, err = l.svcCtx.WalletRpc.AddWallets(l.ctx, &pb.AddWalletsReq{
|
||||
UserId: userID,
|
||||
Balance: zeroAmount,
|
||||
FrozenBalance: zeroAmount,
|
||||
UpdatedAt: time.Now().Unix(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
walletResp, err = l.svcCtx.WalletRpc.GetWalletsById(l.ctx, &pb.GetWalletsByIdReq{Id: userID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if walletResp.GetWallets() == nil {
|
||||
return nil, errors.New("wallet not found")
|
||||
}
|
||||
currentBalance, err = decimal.NewFromString(walletResp.Wallets.Balance)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid wallet balance")
|
||||
}
|
||||
frozenBalance, err = decimal.NewFromString(walletResp.Wallets.FrozenBalance)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid wallet frozen balance")
|
||||
}
|
||||
expectedVersion = walletResp.Wallets.Version
|
||||
} else {
|
||||
currentBalance, err = decimal.NewFromString(walletResp.Wallets.Balance)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid wallet balance")
|
||||
}
|
||||
frozenBalance, err = decimal.NewFromString(walletResp.Wallets.FrozenBalance)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid wallet frozen balance")
|
||||
}
|
||||
expectedVersion = walletResp.Wallets.Version
|
||||
}
|
||||
|
||||
newBalance := currentBalance.Add(amount)
|
||||
newBalanceStr := newBalance.String()
|
||||
frozenBalanceStr := frozenBalance.String()
|
||||
updatedAt := time.Now().Unix()
|
||||
_, err = l.svcCtx.WalletRpc.UpdateWallets(l.ctx, &pb.UpdateWalletsReq{
|
||||
UserId: userID,
|
||||
Balance: &newBalanceStr,
|
||||
FrozenBalance: &frozenBalanceStr,
|
||||
UpdatedAt: &updatedAt,
|
||||
Version: &expectedVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
desc := fmt.Sprintf("topup via %s", req.Method)
|
||||
_, err = l.svcCtx.WalletRpc.AddWalletTransactions(l.ctx, &pb.AddWalletTransactionsReq{
|
||||
UserId: userID,
|
||||
Type: "topup",
|
||||
Amount: amount.String(),
|
||||
BalanceAfter: newBalanceStr,
|
||||
Description: desc,
|
||||
CreatedAt: updatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.EmptyResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package wallet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
|
||||
"juwan-backend/app/wallet/api/internal/svc"
|
||||
"juwan-backend/app/wallet/api/internal/types"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type WithdrawLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 提现
|
||||
func NewWithdrawLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawLogic {
|
||||
return &WithdrawLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *WithdrawLogic) Withdraw(req *types.TopupReq) (resp *types.EmptyResp, err error) {
|
||||
amount, err := decimal.NewFromString(req.Amount)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid amount")
|
||||
}
|
||||
if !amount.GreaterThan(decimal.Zero) {
|
||||
return nil, errors.New("amount must be greater than 0")
|
||||
}
|
||||
|
||||
userID, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
walletResp, err := l.svcCtx.WalletRpc.GetWalletsById(l.ctx, &pb.GetWalletsByIdReq{Id: userID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if walletResp.GetWallets() == nil {
|
||||
return nil, errors.New("wallet not found")
|
||||
}
|
||||
|
||||
currentBalance, err := decimal.NewFromString(walletResp.Wallets.Balance)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid wallet balance")
|
||||
}
|
||||
frozenBalance, err := decimal.NewFromString(walletResp.Wallets.FrozenBalance)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid wallet frozen balance")
|
||||
}
|
||||
expectedVersion := walletResp.Wallets.Version
|
||||
if currentBalance.LessThan(amount) {
|
||||
return nil, errors.New("insufficient balance")
|
||||
}
|
||||
|
||||
newBalance := currentBalance.Sub(amount)
|
||||
newBalanceStr := newBalance.String()
|
||||
frozenBalanceStr := frozenBalance.String()
|
||||
updatedAt := time.Now().Unix()
|
||||
_, err = l.svcCtx.WalletRpc.UpdateWallets(l.ctx, &pb.UpdateWalletsReq{
|
||||
UserId: userID,
|
||||
Balance: &newBalanceStr,
|
||||
FrozenBalance: &frozenBalanceStr,
|
||||
UpdatedAt: &updatedAt,
|
||||
Version: &expectedVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
desc := fmt.Sprintf("withdraw via %s", req.Method)
|
||||
_, err = l.svcCtx.WalletRpc.AddWalletTransactions(l.ctx, &pb.AddWalletTransactionsReq{
|
||||
UserId: userID,
|
||||
Type: "withdrawal",
|
||||
Amount: amount.String(),
|
||||
BalanceAfter: newBalanceStr,
|
||||
Description: desc,
|
||||
CreatedAt: updatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.EmptyResp{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user