first commit
This commit is contained in:
208
app/main/api/internal/svc/servicecontext.go
Normal file
208
app/main/api/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,208 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"aedata-server/app/main/api/internal/config"
|
||||
"aedata-server/app/main/api/internal/middleware"
|
||||
"aedata-server/app/main/api/internal/service"
|
||||
tianyuanapi "aedata-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"aedata-server/app/main/model"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// ServiceContext 服务上下文
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
Redis *redis.Redis
|
||||
|
||||
// 中间件
|
||||
AuthInterceptor rest.Middleware
|
||||
UserAuthInterceptor rest.Middleware
|
||||
|
||||
// 用户相关模型
|
||||
UserModel model.UserModel
|
||||
UserAuthModel model.UserAuthModel
|
||||
UserTempModel model.UserTempModel
|
||||
|
||||
// 产品相关模型
|
||||
ProductModel model.ProductModel
|
||||
FeatureModel model.FeatureModel
|
||||
ProductFeatureModel model.ProductFeatureModel
|
||||
|
||||
// 订单相关模型
|
||||
OrderModel model.OrderModel
|
||||
OrderRefundModel model.OrderRefundModel
|
||||
QueryModel model.QueryModel
|
||||
QueryCleanupLogModel model.QueryCleanupLogModel
|
||||
QueryCleanupDetailModel model.QueryCleanupDetailModel
|
||||
QueryCleanupConfigModel model.QueryCleanupConfigModel
|
||||
|
||||
// 管理后台相关模型
|
||||
AdminApiModel model.AdminApiModel
|
||||
AdminMenuModel model.AdminMenuModel
|
||||
AdminRoleModel model.AdminRoleModel
|
||||
AdminRoleApiModel model.AdminRoleApiModel
|
||||
AdminRoleMenuModel model.AdminRoleMenuModel
|
||||
AdminUserModel model.AdminUserModel
|
||||
AdminUserRoleModel model.AdminUserRoleModel
|
||||
AdminDictDataModel model.AdminDictDataModel
|
||||
AdminDictTypeModel model.AdminDictTypeModel
|
||||
|
||||
// 其他模型
|
||||
ExampleModel model.ExampleModel
|
||||
GlobalNotificationsModel model.GlobalNotificationsModel
|
||||
|
||||
// 服务
|
||||
AlipayService *service.AliPayService
|
||||
WechatPayService *service.WechatPayService
|
||||
ApiRequestService *service.ApiRequestService
|
||||
AsynqServer *asynq.Server
|
||||
AsynqService *service.AsynqService
|
||||
VerificationService *service.VerificationService
|
||||
UserService *service.UserService
|
||||
DictService *service.DictService
|
||||
ImageService *service.ImageService
|
||||
}
|
||||
|
||||
// NewServiceContext 创建服务上下文
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
// ============================== 基础设施初始化 ==============================
|
||||
db := sqlx.NewMysql(c.DataSource)
|
||||
cacheConf := c.CacheRedis
|
||||
|
||||
// 初始化Redis客户端
|
||||
redisConf := redis.RedisConf{
|
||||
Host: cacheConf[0].Host,
|
||||
Pass: cacheConf[0].Pass,
|
||||
Type: cacheConf[0].Type,
|
||||
}
|
||||
redisClient := redis.MustNewRedis(redisConf)
|
||||
|
||||
// ============================== 用户相关模型 ==============================
|
||||
userModel := model.NewUserModel(db, cacheConf)
|
||||
userAuthModel := model.NewUserAuthModel(db, cacheConf)
|
||||
userTempModel := model.NewUserTempModel(db, cacheConf)
|
||||
|
||||
// ============================== 产品相关模型 ==============================
|
||||
productModel := model.NewProductModel(db, cacheConf)
|
||||
featureModel := model.NewFeatureModel(db, cacheConf)
|
||||
productFeatureModel := model.NewProductFeatureModel(db, cacheConf)
|
||||
|
||||
// ============================== 订单相关模型 ==============================
|
||||
orderModel := model.NewOrderModel(db, cacheConf)
|
||||
queryModel := model.NewQueryModel(db, cacheConf)
|
||||
orderRefundModel := model.NewOrderRefundModel(db, cacheConf)
|
||||
queryCleanupLogModel := model.NewQueryCleanupLogModel(db, cacheConf)
|
||||
queryCleanupDetailModel := model.NewQueryCleanupDetailModel(db, cacheConf)
|
||||
queryCleanupConfigModel := model.NewQueryCleanupConfigModel(db, cacheConf)
|
||||
|
||||
// ============================== 管理后台相关模型 ==============================
|
||||
adminApiModel := model.NewAdminApiModel(db, cacheConf)
|
||||
adminMenuModel := model.NewAdminMenuModel(db, cacheConf)
|
||||
adminRoleModel := model.NewAdminRoleModel(db, cacheConf)
|
||||
adminRoleApiModel := model.NewAdminRoleApiModel(db, cacheConf)
|
||||
adminRoleMenuModel := model.NewAdminRoleMenuModel(db, cacheConf)
|
||||
adminUserModel := model.NewAdminUserModel(db, cacheConf)
|
||||
adminUserRoleModel := model.NewAdminUserRoleModel(db, cacheConf)
|
||||
adminDictDataModel := model.NewAdminDictDataModel(db, cacheConf)
|
||||
adminDictTypeModel := model.NewAdminDictTypeModel(db, cacheConf)
|
||||
|
||||
|
||||
// ============================== 其他模型 ==============================
|
||||
exampleModel := model.NewExampleModel(db, cacheConf)
|
||||
globalNotificationsModel := model.NewGlobalNotificationsModel(db, cacheConf)
|
||||
|
||||
// ============================== 第三方服务初始化 ==============================
|
||||
tianyuanapi, err := tianyuanapi.NewClient(tianyuanapi.Config{
|
||||
AccessID: c.Tianyuanapi.AccessID,
|
||||
Key: c.Tianyuanapi.Key,
|
||||
BaseURL: c.Tianyuanapi.BaseURL,
|
||||
Timeout: time.Duration(c.Tianyuanapi.Timeout) * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("初始化天远API失败: %+v", err)
|
||||
}
|
||||
// ============================== 业务服务初始化 ==============================
|
||||
alipayService := service.NewAliPayService(c)
|
||||
wechatPayService := service.NewWechatPayService(c, userAuthModel, service.InitTypeWxPayPubKey)
|
||||
apiRequestService := service.NewApiRequestService(c, featureModel, productFeatureModel, tianyuanapi)
|
||||
verificationService := service.NewVerificationService(c, tianyuanapi, apiRequestService)
|
||||
asynqService := service.NewAsynqService(c)
|
||||
userService := service.NewUserService(&c, userModel, userAuthModel, userTempModel)
|
||||
dictService := service.NewDictService(adminDictTypeModel, adminDictDataModel)
|
||||
imageService := service.NewImageService()
|
||||
// ============================== 异步任务服务 ==============================
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
||||
// ============================== 返回服务上下文 ==============================
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
Redis: redisClient,
|
||||
AuthInterceptor: middleware.NewAuthInterceptorMiddleware(c).Handle,
|
||||
UserAuthInterceptor: middleware.NewUserAuthInterceptorMiddleware().Handle,
|
||||
|
||||
// 用户相关模型
|
||||
UserModel: userModel,
|
||||
UserAuthModel: userAuthModel,
|
||||
UserTempModel: userTempModel,
|
||||
|
||||
// 产品相关模型
|
||||
ProductModel: productModel,
|
||||
FeatureModel: featureModel,
|
||||
ProductFeatureModel: productFeatureModel,
|
||||
|
||||
// 订单相关模型
|
||||
OrderModel: orderModel,
|
||||
QueryModel: queryModel,
|
||||
OrderRefundModel: orderRefundModel,
|
||||
QueryCleanupLogModel: queryCleanupLogModel,
|
||||
QueryCleanupDetailModel: queryCleanupDetailModel,
|
||||
QueryCleanupConfigModel: queryCleanupConfigModel,
|
||||
|
||||
|
||||
// 管理后台相关模型
|
||||
AdminApiModel: adminApiModel,
|
||||
AdminMenuModel: adminMenuModel,
|
||||
AdminRoleModel: adminRoleModel,
|
||||
AdminRoleApiModel: adminRoleApiModel,
|
||||
AdminRoleMenuModel: adminRoleMenuModel,
|
||||
AdminUserModel: adminUserModel,
|
||||
AdminUserRoleModel: adminUserRoleModel,
|
||||
AdminDictDataModel: adminDictDataModel,
|
||||
AdminDictTypeModel: adminDictTypeModel,
|
||||
// 其他模型
|
||||
ExampleModel: exampleModel,
|
||||
GlobalNotificationsModel: globalNotificationsModel,
|
||||
|
||||
// 服务
|
||||
AlipayService: alipayService,
|
||||
WechatPayService: wechatPayService,
|
||||
ApiRequestService: apiRequestService,
|
||||
AsynqServer: asynqServer,
|
||||
AsynqService: asynqService,
|
||||
VerificationService: verificationService,
|
||||
UserService: userService,
|
||||
DictService: dictService,
|
||||
ImageService: imageService,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServiceContext) Close() {
|
||||
if s.AsynqService != nil {
|
||||
s.AsynqService.Close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user