package svc import ( "tyc-server/app/main/api/internal/config" "tyc-server/app/main/api/internal/middleware" "tyc-server/app/main/api/internal/service" "tyc-server/app/main/model" "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" ) 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 GlobalNotificationsModel model.GlobalNotificationsModel ExampleModel model.ExampleModel ExampleParamsModel model.ExampleParamsModel AlipayService *service.AliPayService WechatPayService *service.WechatPayService ApplePayService *service.ApplePayService WestDexService *service.WestDexService YushanService *service.YushanService TianjuService *service.TianjuService 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) tianjuService := service.NewTianjuService(c) productFeatureModel := model.NewProductFeatureModel(db, c.CacheRedis) featureModel := model.NewFeatureModel(db, c.CacheRedis) userAuthModel := model.NewUserAuthModel(db, c.CacheRedis) apiRequestService := service.NewApiRequestService(c, westDexService, yushanService, tianjuService, featureModel, productFeatureModel) 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, TianjuService: tianjuService, VerificationService: service.NewVerificationService(c, westDexService, apiRequestService), AsynqServer: asynqServer, ApiRequestService: apiRequestService, 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), ExampleModel: model.NewExampleModel(db, c.CacheRedis), ExampleParamsModel: model.NewExampleParamsModel(db, c.CacheRedis), GlobalNotificationsModel: model.NewGlobalNotificationsModel(db, c.CacheRedis), FeatureModel: featureModel, ProductFeatureModel: productFeatureModel, } } func (s *ServiceContext) Close() { if s.AsynqService != nil { s.AsynqService.Close() } }