修改工具的调用方式
This commit is contained in:
45
factory/factory.go
Normal file
45
factory/factory.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package factory
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.toowon.com/jimmy/go-commom/config"
|
||||
"git.toowon.com/jimmy/go-commom/email"
|
||||
"git.toowon.com/jimmy/go-commom/sms"
|
||||
)
|
||||
|
||||
// Factory 工厂类,用于从配置创建各种客户端对象
|
||||
type Factory struct {
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
// NewFactory 创建工厂实例
|
||||
func NewFactory(cfg *config.Config) *Factory {
|
||||
return &Factory{
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
// GetEmailClient 获取邮件客户端(已初始化)
|
||||
// 返回已初始化的邮件客户端对象,可直接使用
|
||||
func (f *Factory) GetEmailClient() (*email.Email, error) {
|
||||
if f.cfg.Email == nil {
|
||||
return nil, fmt.Errorf("email config is nil")
|
||||
}
|
||||
return email.NewEmail(f.cfg.Email)
|
||||
}
|
||||
|
||||
// GetSMSClient 获取短信客户端(已初始化)
|
||||
// 返回已初始化的短信客户端对象,可直接使用
|
||||
func (f *Factory) GetSMSClient() (*sms.SMS, error) {
|
||||
if f.cfg.SMS == nil {
|
||||
return nil, fmt.Errorf("SMS config is nil")
|
||||
}
|
||||
return sms.NewSMS(f.cfg.SMS)
|
||||
}
|
||||
|
||||
// GetConfig 获取配置对象
|
||||
func (f *Factory) GetConfig() *config.Config {
|
||||
return f.cfg
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user