69 lines
2.8 KiB
Go
69 lines
2.8 KiB
Go
package svc
|
|
|
|
import (
|
|
"tianyuan-api/apps/api/internal/config"
|
|
"tianyuan-api/apps/api/internal/middleware"
|
|
"tianyuan-api/apps/api/internal/service"
|
|
"tianyuan-api/apps/sentinel/sentinel"
|
|
"tianyuan-api/apps/user/user"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-queue/kq"
|
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
|
"github.com/zeromicro/go-zero/rest"
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
ApiAuthInterceptor rest.Middleware
|
|
ApiMqsInterceptor rest.Middleware
|
|
Redis *redis.Redis
|
|
WhitelistRpc sentinel.WhitelistClient
|
|
SecretRpc sentinel.SecretClient
|
|
ProductRpc sentinel.ProductClient
|
|
UserProductRpc sentinel.UserProductClient
|
|
WestDexService *service.WestDexService
|
|
YushanService *service.YushanService
|
|
ApiRequestMqsService *service.ApiRequestMqsService
|
|
}
|
|
type ApiRequestMessage struct {
|
|
TransactionID string `json:"transactionID"`
|
|
UserId string `json:"userId"`
|
|
ProductCode string `json:"productCode"`
|
|
Status string `json:"status"` // 1. success 2. error
|
|
Charges bool `json:"charges"` // 是否扣费
|
|
Timestamp time.Time `json:"timestamp"` // 添加时间戳
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
redisConf := redis.RedisConf{
|
|
Host: c.CacheRedis[0].Host,
|
|
Pass: c.CacheRedis[0].Pass,
|
|
Type: c.CacheRedis[0].Type, // Redis 节点类型,如 "node"
|
|
}
|
|
// 使用 MustNewRedis 来初始化 Redis 客户端
|
|
rds := redis.MustNewRedis(redisConf)
|
|
|
|
productRpc := sentinel.NewProductClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
|
|
userProductRpc := sentinel.NewUserProductClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
|
|
whitelistRpc := sentinel.NewWhitelistClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
|
|
secretRpc := sentinel.NewSecretClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
|
|
userRpc := user.NewUserClient(zrpc.MustNewClient(c.UserRpc).Conn())
|
|
KqPusherClient := kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic)
|
|
apiRequestMqsService := service.NewApiRequestMqsService(KqPusherClient)
|
|
return &ServiceContext{
|
|
Config: c,
|
|
Redis: rds,
|
|
WhitelistRpc: whitelistRpc,
|
|
SecretRpc: secretRpc,
|
|
ProductRpc: productRpc,
|
|
UserProductRpc: userProductRpc,
|
|
ApiAuthInterceptor: middleware.NewApiAuthInterceptorMiddleware(whitelistRpc, secretRpc, userProductRpc, userRpc, rds).Handle,
|
|
ApiMqsInterceptor: middleware.NewApiMqsInterceptorMiddleware(apiRequestMqsService).Handle,
|
|
ApiRequestMqsService: apiRequestMqsService,
|
|
WestDexService: service.NewWestDexService(c.WestConfig), // 假设你将密钥和 ID 配置在 config 中
|
|
YushanService: service.NewYushanService(c.YushanConfig),
|
|
}
|
|
}
|