qnc-server/app/user/cmd/api/internal/svc/servicecontext.go
2025-02-13 19:39:16 +08:00

100 lines
4.2 KiB
Go

package svc
import (
"github.com/hibiken/asynq"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/rest"
"qnc-server/app/user/cmd/api/internal/config"
"qnc-server/app/user/cmd/api/internal/middleware"
"qnc-server/app/user/cmd/api/internal/service"
"qnc-server/app/user/model"
)
type ServiceContext struct {
Config config.Config
Redis *redis.Redis
SourceInterceptor rest.Middleware
UserModel model.UserModel
UserAuthModel model.UserAuthModel
ProductModel model.ProductModel
FeatureModel model.FeatureModel
ProductFeatureModel model.ProductFeatureModel
ProductRenderModel model.ProductRenderModel
OrderModel model.OrderModel
QueryModel model.QueryModel
AgentModel model.AgentModel
AgentAuditModel model.AgentAuditModel
AgentClosureModel model.AgentClosureModel
AgentCommissionModel model.AgentCommissionModel
AgentWalletModel model.AgentWalletModel
GlobalNotificationsModel model.GlobalNotificationsModel
AlipayService *service.AliPayService
WechatPayService *service.WechatPayService
ApplePayService *service.ApplePayService
WestDexService *service.WestDexService
YushanService *service.YushanService
ApiRequestService *service.ApiRequestService
AsynqServer *asynq.Server // 服务端
AsynqService *service.AsynqService // 客户端
VerificationService *service.VerificationService
}
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,
}
asynqServer := asynq.NewServer(
asynq.RedisClientOpt{Addr: c.CacheRedis[0].Host, Password: c.CacheRedis[0].Pass},
asynq.Config{
IsFailure: func(err error) bool {
logx.Errorf("异步任务失败: %+v \n", err)
return true
},
Concurrency: 10,
},
)
westDexService := service.NewWestDexService(c)
yushanService := service.NewYushanService(c)
productFeatureModel := model.NewProductFeatureModel(db, c.CacheRedis)
featureModel := model.NewFeatureModel(db, c.CacheRedis)
userAuthModel := model.NewUserAuthModel(db, c.CacheRedis)
return &ServiceContext{
Config: c,
Redis: redis.MustNewRedis(redisConf),
SourceInterceptor: middleware.NewSourceInterceptorMiddleware().Handle,
AlipayService: service.NewAliPayService(c),
WechatPayService: service.NewWechatPayService(c, userAuthModel),
ApplePayService: service.NewApplePayService(c),
WestDexService: westDexService,
YushanService: yushanService,
VerificationService: service.NewVerificationService(c, westDexService),
AsynqServer: asynqServer,
ApiRequestService: service.NewApiRequestService(c, westDexService, yushanService, featureModel, productFeatureModel),
AsynqService: service.NewAsynqService(c),
UserModel: model.NewUserModel(db, c.CacheRedis),
UserAuthModel: userAuthModel,
ProductModel: model.NewProductModel(db, c.CacheRedis),
ProductRenderModel: model.NewProductRenderModel(db, c.CacheRedis),
OrderModel: model.NewOrderModel(db, c.CacheRedis),
QueryModel: model.NewQueryModel(db, c.CacheRedis),
GlobalNotificationsModel: model.NewGlobalNotificationsModel(db, c.CacheRedis),
AgentModel: model.NewAgentModel(db, c.CacheRedis),
AgentAuditModel: model.NewAgentAuditModel(db, c.CacheRedis),
AgentCommissionModel: model.NewAgentCommissionModel(db, c.CacheRedis),
AgentWalletModel: model.NewAgentWalletModel(db, c.CacheRedis),
AgentClosureModel: model.NewAgentClosureModel(db, c.CacheRedis),
FeatureModel: featureModel,
ProductFeatureModel: productFeatureModel,
}
}
func (s *ServiceContext) Close() {
if s.AsynqService != nil {
s.AsynqService.Close()
}
}