回调
CI / changes (push) Successful in 36s
CI / ad (push) Successful in 23s
CI / admin (push) Successful in 19s
CI / bff (push) Failing after 1h33m11s
CI / chore (push) Successful in 5m20s
CI / equipment (push) Successful in 7m24s
CI / express (push) Successful in 29s
CI / product (push) Successful in 21s
CI / record (push) Successful in 18s
CI / sale (push) Successful in 16s
CI / task (push) Successful in 15s
CI / user (push) Successful in 18s
CI / wecom (push) Successful in 16s
CI / changes (push) Successful in 36s
CI / ad (push) Successful in 23s
CI / admin (push) Successful in 19s
CI / bff (push) Failing after 1h33m11s
CI / chore (push) Successful in 5m20s
CI / equipment (push) Successful in 7m24s
CI / express (push) Successful in 29s
CI / product (push) Successful in 21s
CI / record (push) Successful in 18s
CI / sale (push) Successful in 16s
CI / task (push) Successful in 15s
CI / user (push) Successful in 18s
CI / wecom (push) Successful in 16s
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
||||
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
cli *dysmsapi.Client
|
||||
signName string
|
||||
}
|
||||
|
||||
func New(accessKeyId, accessKeySecret, signName, endpoint string) (*Client, error) {
|
||||
config := &openapi.Config{
|
||||
AccessKeyId: tea.String(accessKeyId),
|
||||
AccessKeySecret: tea.String(accessKeySecret),
|
||||
Endpoint: tea.String(endpoint),
|
||||
}
|
||||
cli, err := dysmsapi.NewClient(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Client{cli: cli, signName: signName}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Send(phone, templateCode string, params map[string]string) error {
|
||||
if phone == "" || templateCode == "" {
|
||||
return fmt.Errorf("phone and templateCode are required")
|
||||
}
|
||||
|
||||
var templateParam *string
|
||||
if len(params) > 0 {
|
||||
raw, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal template params: %w", err)
|
||||
}
|
||||
templateParam = tea.String(string(raw))
|
||||
}
|
||||
|
||||
resp, err := c.cli.SendSms(&dysmsapi.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(phone),
|
||||
SignName: tea.String(c.signName),
|
||||
TemplateCode: tea.String(templateCode),
|
||||
TemplateParam: templateParam,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp == nil || resp.Body == nil {
|
||||
return fmt.Errorf("aliyun sms: empty response")
|
||||
}
|
||||
if code := tea.StringValue(resp.Body.Code); code != "OK" {
|
||||
return fmt.Errorf("aliyun sms: %s %s", code, tea.StringValue(resp.Body.Message))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"lone-services/pkg/sms/aliyun"
|
||||
)
|
||||
|
||||
type TemplateKind string
|
||||
|
||||
const (
|
||||
TemplateCode TemplateKind = "code"
|
||||
TemplatePassword TemplateKind = "password"
|
||||
)
|
||||
|
||||
type SMS interface {
|
||||
Send(phone string, templateCode string, params map[string]string) error
|
||||
SendByKind(phone string, kind TemplateKind, params map[string]string) error
|
||||
Template(kind TemplateKind) string
|
||||
Timeout() int
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
AccessKeyId string
|
||||
AccessKeySecret string
|
||||
SignName string
|
||||
Endpoint string
|
||||
TemplateCode string
|
||||
TemplatePwd string
|
||||
Timeout int
|
||||
}
|
||||
|
||||
type client struct {
|
||||
sender *aliyun.Client
|
||||
templates map[TemplateKind]string
|
||||
timeout int
|
||||
}
|
||||
|
||||
func New(cfg Config) (SMS, error) {
|
||||
if cfg.AccessKeyId == "" || cfg.AccessKeySecret == "" || cfg.SignName == "" {
|
||||
return nil, fmt.Errorf("config missing")
|
||||
}
|
||||
|
||||
sender, err := aliyun.New(cfg.AccessKeyId, cfg.AccessKeySecret, cfg.SignName, cfg.Endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.Timeout <= 0 {
|
||||
cfg.Timeout = 600
|
||||
}
|
||||
return &client{
|
||||
sender: sender,
|
||||
templates: map[TemplateKind]string{
|
||||
TemplateCode: cfg.TemplateCode,
|
||||
TemplatePassword: cfg.TemplatePwd,
|
||||
},
|
||||
timeout: cfg.Timeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *client) Send(phone, templateCode string, params map[string]string) error {
|
||||
return c.sender.Send(phone, templateCode, params)
|
||||
}
|
||||
|
||||
func (c *client) SendByKind(phone string, kind TemplateKind, params map[string]string) error {
|
||||
code := c.Template(kind)
|
||||
if code == "" {
|
||||
return fmt.Errorf("sms template not configured for kind %s", kind)
|
||||
}
|
||||
return c.Send(phone, code, params)
|
||||
}
|
||||
|
||||
func (c *client) Template(kind TemplateKind) string { return c.templates[kind] }
|
||||
func (c *client) Timeout() int { return c.timeout }
|
||||
Reference in New Issue
Block a user