| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | package svc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/core/stores/redis" | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/rest" | 
					
						
							|  |  |  | 	"github.com/zeromicro/go-zero/zrpc" | 
					
						
							|  |  |  | 	"tianyuan-api/apps/gateway/internal/config" | 
					
						
							|  |  |  | 	"tianyuan-api/apps/gateway/internal/middleware" | 
					
						
							|  |  |  | 	"tianyuan-api/apps/sentinel/sentinel" | 
					
						
							|  |  |  | 	"tianyuan-api/apps/user/user" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ServiceContext struct { | 
					
						
							|  |  |  | 	Config             config.Config | 
					
						
							|  |  |  | 	AuthInterceptor    rest.Middleware | 
					
						
							|  |  |  | 	EntAuthInterceptor rest.Middleware | 
					
						
							|  |  |  | 	Redis              *redis.Redis | 
					
						
							|  |  |  | 	AuthRpc            user.AuthClient | 
					
						
							|  |  |  | 	EntRpc             user.EnterpriseClient | 
					
						
							|  |  |  | 	UserRpc            user.UserClient | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  | 	Wallets            user.WalletServiceClient | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | 	ProductRpc         sentinel.ProductClient | 
					
						
							|  |  |  | 	UserProductRpc     sentinel.UserProductClient | 
					
						
							|  |  |  | 	WhitelistRpc       sentinel.WhitelistClient | 
					
						
							|  |  |  | 	SecretRpc          sentinel.SecretClient | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  | 	TopUpRpc           sentinel.TopUpClient | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | 	authRpc := user.NewAuthClient(zrpc.MustNewClient(c.UserRpc).Conn()) | 
					
						
							|  |  |  | 	entRpc := user.NewEnterpriseClient(zrpc.MustNewClient(c.UserRpc).Conn()) | 
					
						
							|  |  |  | 	userRpc := user.NewUserClient(zrpc.MustNewClient(c.UserRpc).Conn()) | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  | 	wallets := user.NewWalletServiceClient(zrpc.MustNewClient(c.UserRpc).Conn()) | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | 	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()) | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  | 	topUpRpc := sentinel.NewTopUpClient(zrpc.MustNewClient(c.SentinelRpc).Conn()) | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | 	return &ServiceContext{ | 
					
						
							|  |  |  | 		Config:             c, | 
					
						
							|  |  |  | 		AuthInterceptor:    middleware.NewAuthInterceptorMiddleware(c).Handle, | 
					
						
							|  |  |  | 		EntAuthInterceptor: middleware.NewEntAuthInterceptorMiddleware(userRpc).Handle, | 
					
						
							|  |  |  | 		Redis:              rds, // 单独使用的 Redis 客户端 | 
					
						
							|  |  |  | 		AuthRpc:            authRpc, | 
					
						
							|  |  |  | 		EntRpc:             entRpc, | 
					
						
							|  |  |  | 		UserRpc:            userRpc, | 
					
						
							|  |  |  | 		ProductRpc:         productRpc, | 
					
						
							|  |  |  | 		UserProductRpc:     userProductRpc, | 
					
						
							|  |  |  | 		WhitelistRpc:       whitelistRpc, | 
					
						
							|  |  |  | 		SecretRpc:          secretRpc, | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  | 		TopUpRpc:           topUpRpc, | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  | 		Wallets:            wallets, | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |