| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | package svc | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  |  | 	"fmt" | 
					
						
							|  |  |  |  | 	"github.com/smartwalle/alipay/v3" | 
					
						
							| 
									
										
										
										
											2024-10-15 17:53:15 +08:00
										 |  |  |  | 	"github.com/zeromicro/go-zero/core/logx" | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | 	"github.com/zeromicro/go-zero/core/stores/redis" | 
					
						
							|  |  |  |  | 	"github.com/zeromicro/go-zero/core/stores/sqlx" | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  |  | 	"github.com/zeromicro/go-zero/zrpc" | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | 	"tianyuan-api/apps/sentinel/internal/config" | 
					
						
							|  |  |  |  | 	"tianyuan-api/apps/sentinel/internal/model" | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  |  | 	"tianyuan-api/apps/user/user" | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | type ServiceContext struct { | 
					
						
							|  |  |  |  | 	Config            config.Config | 
					
						
							|  |  |  |  | 	Redis             *redis.Redis | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  |  | 	AlipayClient      *alipay.Client | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | 	WhitelistModel    model.WhitelistModel | 
					
						
							|  |  |  |  | 	SecretsModel      model.SecretsModel | 
					
						
							|  |  |  |  | 	ProductsModel     model.ProductsModel | 
					
						
							|  |  |  |  | 	UserProductsModel model.UserProductsModel | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  |  | 	PayOrderModel     model.PayOrderModel | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  |  | 	WalletRpc         user.WalletServiceClient | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | func NewServiceContext(c config.Config) *ServiceContext { | 
					
						
							|  |  |  |  | 	db := sqlx.NewMysql(c.DataSource) // 创建数据库连接 | 
					
						
							|  |  |  |  | 	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) | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 创建支付宝客户端 | 
					
						
							|  |  |  |  | 	client, err := alipay.New(c.Alipay.AppID, c.Alipay.PrivateKey, c.Alipay.IsProduction) | 
					
						
							|  |  |  |  | 	if err != nil { | 
					
						
							|  |  |  |  | 		panic(fmt.Sprintf("创建支付宝客户端失败: %v", err)) | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 加载支付宝公钥 | 
					
						
							|  |  |  |  | 	err = client.LoadAliPayPublicKey(c.Alipay.AlipayPublicKey) | 
					
						
							|  |  |  |  | 	if err != nil { | 
					
						
							|  |  |  |  | 		panic(fmt.Sprintf("加载支付宝公钥失败: %v", err)) | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-10-15 17:53:15 +08:00
										 |  |  |  | 	var walletRpc user.WalletServiceClient | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-15 17:53:15 +08:00
										 |  |  |  | 	// 捕获RPC初始化时的错误,但不影响服务启动 | 
					
						
							|  |  |  |  | 	userClient, err := zrpc.NewClient(c.UserRpc) | 
					
						
							|  |  |  |  | 	if err != nil { | 
					
						
							|  |  |  |  | 		logx.Errorf("Failed to connect to SecretRpc: %v", err) | 
					
						
							|  |  |  |  | 	} else { | 
					
						
							|  |  |  |  | 		walletRpc = user.NewWalletServiceClient(userClient.Conn()) | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | 	return &ServiceContext{ | 
					
						
							|  |  |  |  | 		Config:            c, | 
					
						
							|  |  |  |  | 		Redis:             rds, | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  |  | 		AlipayClient:      client, | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | 		WhitelistModel:    model.NewWhitelistModel(rds, db, c.CacheRedis), | 
					
						
							|  |  |  |  | 		SecretsModel:      model.NewSecretsModel(db, c.CacheRedis), | 
					
						
							|  |  |  |  | 		ProductsModel:     model.NewProductsModel(db, c.CacheRedis), | 
					
						
							|  |  |  |  | 		UserProductsModel: model.NewUserProductsModel(rds, db, c.CacheRedis), | 
					
						
							| 
									
										
										
										
											2024-10-15 00:23:07 +08:00
										 |  |  |  | 		PayOrderModel:     model.NewPayOrderModel(db, c.CacheRedis), | 
					
						
							| 
									
										
										
										
											2024-10-15 17:19:23 +08:00
										 |  |  |  | 		WalletRpc:         walletRpc, | 
					
						
							| 
									
										
										
										
											2024-10-02 00:57:17 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } |