feat(utils): 新增工具类:邮箱、请求
This commit is contained in:
@@ -36,11 +36,19 @@ type JWT struct {
|
|||||||
ExpireDurationHour time.Duration `mapstructure:"expire_duration_hour"`
|
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 {
|
type Config struct {
|
||||||
Server Server `mapstructure:"server"`
|
Server Server `mapstructure:"server"`
|
||||||
Log Log `mapstructure:"log"`
|
Log Log `mapstructure:"log"`
|
||||||
Database Database `mapstructure:"database"`
|
Database Database `mapstructure:"database"`
|
||||||
JWT JWT `mapstructure:"jwt"`
|
JWT JWT `mapstructure:"jwt"`
|
||||||
|
Email Email `mapstructure:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
@@ -72,5 +80,11 @@ func DefaultConfig() *Config {
|
|||||||
SecretKey: common.Rand(16),
|
SecretKey: common.Rand(16),
|
||||||
ExpireDurationHour: 24,
|
ExpireDurationHour: 24,
|
||||||
},
|
},
|
||||||
|
Email: Email{
|
||||||
|
SMTPServer: "smtp.qq.com",
|
||||||
|
Port: 465,
|
||||||
|
Username: "xxx@qq.com",
|
||||||
|
Password: "xxx",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
54
pkg/email/email.go
Normal file
54
pkg/email/email.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
59
pkg/request/request.go
Normal file
59
pkg/request/request.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user