2024-10-02 00:57:17 +08:00
|
|
|
|
package svc
|
|
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
|
|
|
"tianyuan-api/apps/sentinel/sentinel"
|
|
|
|
|
"tianyuan-api/apps/user/internal/config"
|
|
|
|
|
"tianyuan-api/apps/user/internal/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ServiceContext struct {
|
|
|
|
|
Config config.Config
|
|
|
|
|
Redis *redis.Redis
|
|
|
|
|
UserModel model.UsersModel // 用户表的模型
|
|
|
|
|
EnterpriseModel model.EnterpriseInfoModel
|
|
|
|
|
EnterpriseAuthModel model.EnterpriseAuthModel
|
2024-10-12 20:41:55 +08:00
|
|
|
|
WalletsModel model.WalletsModel
|
|
|
|
|
DeductionsModel model.DeductionsModel
|
|
|
|
|
ApiRequestsModel model.ApiRequestsModel
|
2024-10-15 17:19:23 +08:00
|
|
|
|
RechargeModel model.RechargeModel
|
2024-10-16 20:46:46 +08:00
|
|
|
|
UserConfigModel model.UserConfigModel
|
2024-10-02 00:57:17 +08:00
|
|
|
|
SecretRpc sentinel.SecretClient
|
2024-10-12 20:41:55 +08:00
|
|
|
|
ProductRpc sentinel.ProductClient
|
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"
|
|
|
|
|
}
|
2024-10-15 17:53:15 +08:00
|
|
|
|
// 初始化 SecretRpc 和 ProductRpc
|
|
|
|
|
var secretRpc sentinel.SecretClient
|
|
|
|
|
var productRpc sentinel.ProductClient
|
|
|
|
|
|
|
|
|
|
// 捕获RPC初始化时的错误,但不影响服务启动
|
|
|
|
|
secretClient, err := zrpc.NewClient(c.SentinelRpc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Errorf("Failed to connect to SecretRpc: %v", err)
|
|
|
|
|
} else {
|
|
|
|
|
secretRpc = sentinel.NewSecretClient(secretClient.Conn())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
productClient, err := zrpc.NewClient(c.SentinelRpc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Errorf("Failed to connect to ProductRpc: %v", err)
|
|
|
|
|
} else {
|
|
|
|
|
productRpc = sentinel.NewProductClient(productClient.Conn())
|
|
|
|
|
}
|
2024-10-02 00:57:17 +08:00
|
|
|
|
|
|
|
|
|
// 使用 MustNewRedis 来初始化 Redis 客户端
|
|
|
|
|
rds := redis.MustNewRedis(redisConf)
|
|
|
|
|
return &ServiceContext{
|
|
|
|
|
Config: c,
|
|
|
|
|
Redis: rds, // 单独使用的 Redis 客户端
|
|
|
|
|
UserModel: model.NewUsersModel(db, c.CacheRedis), // 注入UserModel
|
2024-10-16 20:46:46 +08:00
|
|
|
|
UserConfigModel: model.NewUserConfigModel(db, c.CacheRedis),
|
2024-10-02 00:57:17 +08:00
|
|
|
|
EnterpriseModel: model.NewEnterpriseInfoModel(db, c.CacheRedis),
|
|
|
|
|
EnterpriseAuthModel: model.NewEnterpriseAuthModel(db, c.CacheRedis),
|
2024-10-12 20:41:55 +08:00
|
|
|
|
WalletsModel: model.NewWalletsModel(db, c.CacheRedis),
|
|
|
|
|
DeductionsModel: model.NewDeductionsModel(db, c.CacheRedis),
|
|
|
|
|
ApiRequestsModel: model.NewApiRequestsModel(db, c.CacheRedis),
|
2024-10-15 17:19:23 +08:00
|
|
|
|
RechargeModel: model.NewRechargeModel(db, c.CacheRedis),
|
2024-10-15 17:53:15 +08:00
|
|
|
|
SecretRpc: secretRpc, // 捕获连接错误后,继续运行
|
|
|
|
|
ProductRpc: productRpc, // 捕获连接错误后,继续运行
|
2024-10-02 00:57:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|