service_context.go 5.5 KB
package svc

import (
	"github.com/silenceper/wechat/v2"
	minicache "github.com/silenceper/wechat/v2/cache"
	"github.com/silenceper/wechat/v2/miniprogram"
	miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/rest"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/config"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/middleware"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/repository"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/openlib"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/cache"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/database"
	"gorm.io/gorm"
	"time"
)

type ServiceContext struct {
	Config config.Config
	DB     *gorm.DB
	Redis  *redis.Redis

	ArticleBackupRepository  domain.ArticleBackupRepository
	ArticleCommentRepository domain.ArticleCommentRepository
	ArticleDraftRepository   domain.ArticleDraftRepository
	ArticleRepository        domain.ArticleRepository
	ArticleSectionRepository domain.ArticleSectionRepository
	ArticleTagRepository     domain.ArticleTagRepository
	ArticleAndTagRepository  domain.ArticleAndTagRepository

	CompanyRepository         domain.CompanyRepository
	DepartmentRepository      domain.DepartmentRepository
	MessageBusinessRepository domain.MessageBusinessRepository
	MessageSystemRepository   domain.MessageSystemRepository
	RoleRepository            domain.RoleRepository
	UserFollowRepository      domain.UserFollowRepository
	UserLoveFlagRepository    domain.UserLoveFlagRepository
	UserReadArticleRepository domain.UserReadArticleRepository
	UserRepository            domain.UserRepository
	UserRoleRepository        domain.UserRoleRepository

	ApiAuthService authlib.ApiAuthService
	SmsService     smslib.SMSService
	OpenApiService openlib.OpenApiService

	MiniProgram *miniprogram.MiniProgram

	LoginStatusCheck rest.Middleware
	LogRequest       rest.Middleware
}

func NewServiceContext(c config.Config) *ServiceContext {

	db := database.OpenGormPGDB(c.DB.DataSource, c.Log.Mode)

	mlCache := cache.NewMultiLevelCache([]string{c.Redis.Host}, c.Redis.Pass)
	redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"})
	apiAuth := authlib.ApiAuthService{
		Service: gateway.NewService(c.ApiAuth.Name, c.ApiAuth.Host, c.ApiAuth.Timeout),
	}
	miniProgram := wechat.NewWechat().GetMiniProgram(&miniConfig.Config{
		AppID:     c.Wechat.AppID,
		AppSecret: c.Wechat.AppSecret,
		Cache:     minicache.NewMemory(),
	})
	return &ServiceContext{
		Config:           c,
		DB:               db,
		Redis:            redis,
		ApiAuthService:   apiAuth,
		SmsService:       smslib.SMSService{Service: gateway.NewService("短信服务", "https://sms.fjmaimaimai.com:9897", time.Second*5)},
		OpenApiService:   openlib.OpenApiService{Service: gateway.NewService("开发接口服务", "https://mmm-open-api-test.fjmaimaimai.com", time.Second*5)},
		LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
		LogRequest:       middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
		MiniProgram:      miniProgram,

		ArticleBackupRepository:  repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
		ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),
		ArticleDraftRepository:   repository.NewArticleDraftRepository(cache.NewCachedRepository(mlCache)),
		ArticleRepository:        repository.NewArticleRepository(cache.NewCachedRepository(mlCache)),
		ArticleSectionRepository: repository.NewArticleSectionRepository(cache.NewCachedRepository(mlCache)),
		ArticleAndTagRepository:  repository.NewArticleAndTagRepository(cache.NewCachedRepository(mlCache)),

		CompanyRepository:         repository.NewCompanyRepository(cache.NewCachedRepository(mlCache)),
		DepartmentRepository:      repository.NewDepartmentRepository(cache.NewCachedRepository(mlCache)),
		MessageBusinessRepository: repository.NewMessageBusinessRepository(cache.NewCachedRepository(mlCache)),
		MessageSystemRepository:   repository.NewMessageSystemRepository(cache.NewCachedRepository(mlCache)),
		RoleRepository:            repository.NewRoleRepository(cache.NewCachedRepository(mlCache)),
		UserFollowRepository:      repository.NewUserFollowRepository(cache.NewCachedRepository(mlCache)),
		UserLoveFlagRepository:    repository.NewUserLoveFlagRepository(cache.NewCachedRepository(mlCache)),
		UserRepository:            repository.NewUserRepository(cache.NewCachedRepository(mlCache)),
		UserReadArticleRepository: repository.NewUserReadArticleRepository(cache.NewCachedRepository(mlCache)),
		ArticleTagRepository:      repository.NewArticleTagRepository(cache.NewCachedRepository(mlCache)),
		UserRoleRepository:        repository.NewUserRoleRepository(cache.NewCachedRepository(mlCache)),
	}
}

func (svc *ServiceContext) DefaultDBConn() transaction.Conn {
	return transaction.NewTransactionContext(svc.DB)
}