diff --git a/pkg/config/types.go b/pkg/config/types.go index b2cced0..9d87db5 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -36,11 +36,19 @@ type JWT struct { ExpireDurationHour time.Duration `mapstructure:"expire_duration_hour"` } +type Email struct { + SMTPServer string `mapstructure:"smtp_server"` + Port int `mapstructure:"port"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` +} + type Config struct { Server Server `mapstructure:"server"` Log Log `mapstructure:"log"` Database Database `mapstructure:"database"` JWT JWT `mapstructure:"jwt"` + Email Email `mapstructure:"email"` } func DefaultConfig() *Config { @@ -72,5 +80,11 @@ func DefaultConfig() *Config { SecretKey: common.Rand(16), ExpireDurationHour: 24, }, + Email: Email{ + SMTPServer: "smtp.qq.com", + Port: 465, + Username: "xxx@qq.com", + Password: "xxx", + }, } } diff --git a/pkg/email/email.go b/pkg/email/email.go new file mode 100644 index 0000000..addfaa6 --- /dev/null +++ b/pkg/email/email.go @@ -0,0 +1,54 @@ +package email + +import ( + "crypto/tls" + "fmt" + "gxy/config" + + "gopkg.in/gomail.v2" +) + +var ( + host = config.GlobalConfig.Email.SMTPServer + port = config.GlobalConfig.Email.Port + username = config.GlobalConfig.Email.Username + password = config.GlobalConfig.Email.Password +) + +// Email 发送器结构 +type Email struct { + dialer *gomail.Dialer + from string +} + +// NewEmail 创建一个邮件发送器实例 +func NewEmail() *Email { + // 获取配置信息 + host := config.GlobalConfig.Email.SMTPServer + port := config.GlobalConfig.Email.Port + username := config.GlobalConfig.Email.Username + password := config.GlobalConfig.Email.Password + + // fmt.Printf("%v %v %v %v\n", host, port, username, password) + dialer := gomail.NewDialer(host, port, username, password) + dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true} + return &Email{ + dialer: dialer, + from: username, + } +} + +// Send 发送邮件,支持 HTML 内容 +func (e *Email) Send(to string, subject string, body string) error { + msg := gomail.NewMessage() + msg.SetHeader("From", e.from) + msg.SetHeader("To", to) + // msg.SetHeader("Cc", "benben@5700.cf") + msg.SetHeader("Subject", subject) + msg.SetBody("text/html", body) + + if err := e.dialer.DialAndSend(msg); err != nil { + return fmt.Errorf("发送邮件失败: %w", err) + } + return nil +} diff --git a/pkg/request/request.go b/pkg/request/request.go new file mode 100644 index 0000000..e69eb3c --- /dev/null +++ b/pkg/request/request.go @@ -0,0 +1,59 @@ +package request + +import ( + "net/http/cookiejar" + "time" + + "github.com/go-resty/resty/v2" +) + +type RequestClient struct { + client *resty.Client +} + +func NewRequestClient(baseURL string, headers map[string]string) *RequestClient { + client := resty.New() + jar, _ := cookiejar.New(nil) + client. + SetBaseURL(baseURL). + SetTimeout(60 * time.Second). + SetCookieJar(jar). + SetRetryCount(3) + + if headers != nil { + client.SetHeaders(headers) + } + + return &RequestClient{client} +} + +func (r *RequestClient) Get(endpoint string, queryParams map[string]string) (*resty.Response, error) { + resp, err := r.client.R(). + SetQueryParams(queryParams). + Get(endpoint) + return resp, err +} + +func (r *RequestClient) Post(endpoint string, body interface{}, result interface{}) (*resty.Response, error) { + resp, err := r.client.R(). + SetBody(body). + SetResult(result). + Post(endpoint) + return resp, err +} + +func (r *RequestClient) Put(endpoint string, body interface{}) (*resty.Response, error) { + resp, err := r.client.R(). + SetBody(body). + Put(endpoint) + return resp, err +} + +func (r *RequestClient) Delete(endpoint string) (*resty.Response, error) { + resp, err := r.client.R().Delete(endpoint) + return resp, err +} + +func (r *RequestClient) SetHeaders(headers map[string]string) { + r.client.SetHeaders(headers) +}