Skip to content

Commit

Permalink
♻️ Refactor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
maiqingqiang committed May 14, 2023
1 parent ab59e0f commit 93e0c9c
Show file tree
Hide file tree
Showing 30 changed files with 981 additions and 525 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
example
example
gsms_mock.go
81 changes: 56 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,32 @@ package main

import (
"github.com/maiqingqiang/gsms"
"github.com/maiqingqiang/gsms/core"
"github.com/maiqingqiang/gsms/gateways/aliyun"
"github.com/maiqingqiang/gsms/gateways/yunpian"
"github.com/maiqingqiang/gsms/message"
"log"
)

func main() {
g := gsms.New(
[]core.Gateway{
client := gsms.New(
[]gsms.Gateway{
&yunpian.Gateway{
ApiKey: "f4c1c41f48120eb311111111111097",
Signature: "【默认签名】",
ApiKey: "ApiKey",
Signature: "Signature",
},
&aliyun.Gateway{
AccessKeyId: "AccessKeyId",
AccessKeySecret: "AccessKeySecret",
SignName: "SignName",
},
},
gsms.WithDefaultGateway([]string{
yunpian.NAME,
gsms.WithGateways([]string{
yunpian.NAME, aliyun.NAME,
}),
)

results, err := g.Send(18888888888, &core.Message{
Template: "SMS_00000001",
results, err := client.Send(18888888888, &message.Message{
Template: "5532044",
Data: map[string]string{
"code": "521410",
},
Expand Down Expand Up @@ -135,48 +141,73 @@ client.Send(18888888888, &core.Message{

## 自定义网关

只需要实现 `core.GatewayInterface` 接口即可,例如:
只需要实现 `gsms.Gateway` 接口即可,例如:

## 场景发送

```go

var _ core.MessageInterface = (*OrderPaidMessage)(nil)
var _ gsms.Message = (*OrderPaidMessage)(nil)

type OrderPaidMessage struct {
OrderNo string
OrderNo string
}

func (o *OrderPaidMessage) Gateways() ([]string, error) {
return []string{yunpian.NAME}, nil
return []string{yunpian.NAME}, nil
}

func (o *OrderPaidMessage) Strategy() (core.StrategyInterface, error) {
return nil, nil
func (o *OrderPaidMessage) Strategy() (gsms.Strategy, error) {
return nil, nil
}

func (o *OrderPaidMessage) GetContent(gateway core.GatewayInterface) (string, error) {
return fmt.Sprintf("您的订单:%s, 已经完成付款", o.OrderNo), nil
func (o *OrderPaidMessage) GetContent(gateway gsms.Gateway) (string, error) {
return fmt.Sprintf("您的订单:%s, 已经完成付款", o.OrderNo), nil
}

func (o *OrderPaidMessage) GetTemplate(gateway core.GatewayInterface) (string, error) {
return "5532044", nil
func (o *OrderPaidMessage) GetTemplate(gateway gsms.Gateway) (string, error) {
return "5532044", nil
}

func (o *OrderPaidMessage) GetData(gateway core.GatewayInterface) (map[string]string, error) {
return map[string]string{
"code": "6379",
}, nil
func (o *OrderPaidMessage) GetData(gateway gsms.Gateway) (map[string]string, error) {
return map[string]string{
"code": "6379",
}, nil
}

func (o *OrderPaidMessage) GetType(gateway core.GatewayInterface) (string, error) {
return core.TextMessage, nil
func (o *OrderPaidMessage) GetType(gateway gsms.Gateway) (string, error) {
return message.TextMessage, nil
}

client.Send(18888888888, &OrderPaidMessage{OrderNo: "1234"})

```

## 各平台配置说明

### [阿里云](https://www.aliyun.com/)

短信内容使用 `Template` + `Data`

```go
&aliyun.Gateway{
AccessKeyId: "AccessKeyId",
AccessKeySecret: "AccessKeySecret",
SignName: "【默认签名】",
}
```

### [云片](https://www.yunpian.com)

短信内容使用 `Content`

```go
&yunpian.Gateway{
ApiKey: "ApiKey",
Signature: "【默认签名】", // 内容中无签名时使用
}
```

## 版权说明

该项目签署了 MIT 授权许可,详情请参阅 [LICENSE](LICENSE)
Expand Down
135 changes: 0 additions & 135 deletions core/client.go

This file was deleted.

14 changes: 0 additions & 14 deletions core/gateway.go

This file was deleted.

19 changes: 0 additions & 19 deletions core/result.go

This file was deleted.

6 changes: 0 additions & 6 deletions core/strategy.go

This file was deleted.

2 changes: 1 addition & 1 deletion core/errors.go → errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package gsms

import (
"errors"
Expand Down
27 changes: 17 additions & 10 deletions gateways/aliyun/aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/maiqingqiang/gsms/core"
"github.com/maiqingqiang/gsms"
"github.com/maiqingqiang/gsms/utils/dove"
"net/http"
"net/url"
"strings"
"time"
)

var _ core.GatewayInterface = (*Gateway)(nil)
var _ gsms.Gateway = (*Gateway)(nil)

type Gateway struct {
AccessKeyId string
Expand All @@ -26,7 +27,7 @@ func (g *Gateway) Name() string {
return NAME
}

func (g *Gateway) Send(to core.PhoneNumberInterface, message core.MessageInterface, client core.ClientInterface) (string, error) {
func (g *Gateway) Send(to *gsms.PhoneNumber, message gsms.Message, config *gsms.Config) error {
query := url.Values{}

query.Add("RegionId", EndpointRegionId)
Expand All @@ -43,30 +44,36 @@ func (g *Gateway) Send(to core.PhoneNumberInterface, message core.MessageInterfa

template, err := message.GetTemplate(g)
if err != nil {
return "", err
return err
}
query.Add("TemplateCode", template)

data, err := message.GetData(g)
if err != nil {
return "", err
return err
}
marshal, err := json.Marshal(data)
if err != nil {
return "", err
return err
}
query.Add("TemplateParam", string(marshal))

query.Add("Signature", generateSign(http.MethodGet, g.AccessKeySecret, query))

response := &Response{}
var response Response

body, err := client.GetWithUnmarshal(EndpointUrl, query, response)
d := dove.New(dove.WithTimeout(config.Timeout), dove.WithLogger(config.Logger))

err = d.Get(EndpointUrl, strings.NewReader(query.Encode()), &response)
if err != nil {
return "", err
return err
}

if response.Code != OK {
return fmt.Errorf("send failed: %+v", response)
}

return body, nil
return nil
}

// generateSign Generate sign.
Expand Down
Loading

0 comments on commit 93e0c9c

Please sign in to comment.