package svc import ( "context" "code.30cm.net/digimon/app-cloudep-member-server/internal/config" cfg "code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/config" "code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/usecase" "code.30cm.net/digimon/app-cloudep-member-server/pkg/repository" uc "code.30cm.net/digimon/app-cloudep-member-server/pkg/usecase" "code.30cm.net/digimon/library-go/errs" "code.30cm.net/digimon/library-go/errs/code" mgo "code.30cm.net/digimon/library-go/mongo" vi "code.30cm.net/digimon/library-go/validator" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/mon" "github.com/zeromicro/go-zero/core/stores/redis" ) type ServiceContext struct { Config config.Config Validate vi.Validate AccountUseCase usecase.AccountUseCase } func NewServiceContext(c config.Config) *ServiceContext { // 設置 errs.Scope = code.CloudEPMember return &ServiceContext{ Config: c, Validate: vi.MustValidator(vi.WithAccount("account")), AccountUseCase: NewAccountUC(&c), } } func NewAccountUC(c *config.Config) usecase.AccountUseCase { // 準備Mongo Config conf := &mgo.Conf{ Schema: c.Mongo.Schema, Host: c.Mongo.Host, Database: c.Mongo.Database, MaxStaleness: c.Mongo.MaxStaleness, MaxPoolSize: c.Mongo.MaxPoolSize, MinPoolSize: c.Mongo.MinPoolSize, MaxConnIdleTime: c.Mongo.MaxConnIdleTime, Compressors: c.Mongo.Compressors, EnableStandardReadWriteSplitMode: c.Mongo.EnableStandardReadWriteSplitMode, ConnectTimeoutMs: c.Mongo.ConnectTimeoutMs, } if c.Mongo.User != "" { conf.User = c.Mongo.User conf.Password = c.Mongo.Password } // 快取選項 cacheOpts := []cache.Option{ cache.WithExpiry(c.CacheExpireTime), cache.WithNotFoundExpiry(c.CacheWithNotFoundExpiry), } dbOpts := []mon.Option{ mgo.SetCustomDecimalType(), mgo.InitMongoOptions(*conf), } newRedis, err := redis.NewRedis(c.Redis.RedisConf) if err != nil { panic(err) } ac := repository.NewAccountRepository(repository.AccountRepositoryParam{ Conf: conf, CacheConf: c.Cache, CacheOpts: cacheOpts, DBOpts: dbOpts, }) u := repository.NewUserRepository(repository.UserRepositoryParam{ Conf: conf, CacheConf: c.Cache, CacheOpts: cacheOpts, DBOpts: dbOpts, }) guid := repository.NewAutoIDRepository(repository.AutoIDRepositoryParam{ Conf: conf, DBOpts: dbOpts, }) auid := repository.NewAccountUIDRepository(repository.AccountUIDRepositoryParam{ Conf: conf, CacheConf: c.Cache, CacheOpts: cacheOpts, DBOpts: dbOpts, }) _, _ = ac.Index20241226001UP(context.Background()) _, _ = u.Index20241226001UP(context.Background()) _, _ = guid.Index20241226001UP(context.Background()) _, _ = auid.Index20241226001UP(context.Background()) return uc.MustMemberUseCase(uc.MemberUseCaseParam{ Account: ac, User: u, AccountUID: auid, VerifyCodeModel: repository.NewVerifyCodeRepository(newRedis), GenerateUID: guid, Config: prepareCfg(c), }) } func prepareCfg(c *config.Config) cfg.Config { return cfg.Config{ Bcrypt: struct{ Cost int }{Cost: c.Bcrypt.Cost}, GoogleAuth: struct { ClientID string AuthURL string }{ ClientID: c.GoogleAuth.ClientID, AuthURL: c.GoogleAuth.AuthURL, }, LineAuth: struct { ClientID string ClientSecret string RedirectURI string }{ ClientID: c.LineAuth.ClientID, ClientSecret: c.LineAuth.ClientSecret, RedirectURI: c.LineAuth.RedirectURI, }, } }